AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

StripeMerchantAccounts.php 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace App\Commerce\Payments;
  3. use App\Models\Store;
  4. use Stripe\Exception\ApiErrorException;
  5. use Stripe\StripeClient;
  6. /**
  7. * Merchants' own Stripe accounts, through Stripe Connect.
  8. *
  9. * Stripe only accepts its newer accounts API (v2) for new platforms. Each account gets the full Stripe Dashboard,
  10. * pays Stripe's fees itself, and Stripe covers losses, so the merchant is the seller in every way.
  11. */
  12. final class StripeMerchantAccounts implements MerchantAccounts
  13. {
  14. public function create(Store $store, string $email): string
  15. {
  16. try {
  17. return $this->client()->v2->core->accounts->create([
  18. 'contact_email' => $email,
  19. 'display_name' => $store->name,
  20. 'dashboard' => 'full',
  21. 'identity' => ['country' => 'my'],
  22. // FPX cannot be asked for yet: Stripe first needs the business type from the sign-up form.
  23. 'configuration' => ['merchant' => ['capabilities' => ['card_payments' => ['requested' => true]]]],
  24. 'defaults' => [
  25. 'currency' => 'myr',
  26. 'responsibilities' => ['fees_collector' => 'stripe', 'losses_collector' => 'stripe'],
  27. ],
  28. 'metadata' => ['store_id' => (string) $store->id],
  29. ])->id;
  30. } catch (ApiErrorException $e) {
  31. throw new PaymentException('Stripe could not open an account: '.$e->getMessage(), 0, $e);
  32. }
  33. }
  34. public function setupLink(string $accountId, string $returnUrl, string $refreshUrl): string
  35. {
  36. try {
  37. return $this->client()->v2->core->accountLinks->create([
  38. 'account' => $accountId,
  39. 'use_case' => [
  40. 'type' => 'account_onboarding',
  41. 'account_onboarding' => [
  42. 'return_url' => $returnUrl,
  43. 'refresh_url' => $refreshUrl,
  44. ],
  45. ],
  46. ])->url;
  47. } catch (ApiErrorException $e) {
  48. throw new PaymentException('Stripe could not open its sign-up form: '.$e->getMessage(), 0, $e);
  49. }
  50. }
  51. public function status(string $accountId): MerchantAccountStatus
  52. {
  53. try {
  54. // The older accounts API still reads v2 accounts, and says plainly what we need.
  55. $account = $this->client()->accounts->retrieve($accountId);
  56. } catch (ApiErrorException $e) {
  57. throw new PaymentException('Stripe could not find the account: '.$e->getMessage(), 0, $e);
  58. }
  59. return new MerchantAccountStatus(
  60. chargesEnabled: (bool) $account->charges_enabled,
  61. payoutsEnabled: (bool) $account->payouts_enabled,
  62. detailsSubmitted: (bool) $account->details_submitted,
  63. );
  64. }
  65. public function requestFpx(string $accountId): void
  66. {
  67. try {
  68. // The older capability call. The v2 update refuses Malaysian sole proprietors
  69. // ("sole_proprietorship is not supported for entity type company"), found on 2026-11-30.
  70. $client = $this->client();
  71. $in = ['stripe_account' => $accountId];
  72. $client->accounts->updateCapability($accountId, 'fpx_payments', ['requested' => true]);
  73. // Approved is not enough: FPX also starts switched off in the account's payment method
  74. // settings, so shoppers would only see cards. Switch it on there too.
  75. foreach ($client->paymentMethodConfigurations->all(['limit' => 10], $in)->data as $configuration) {
  76. $client->paymentMethodConfigurations->update($configuration->id, ['fpx' => ['display_preference' => ['preference' => 'on']]], $in);
  77. }
  78. } catch (ApiErrorException $e) {
  79. throw new PaymentException('Stripe could not switch on FPX: '.$e->getMessage(), 0, $e);
  80. }
  81. }
  82. private function client(): StripeClient
  83. {
  84. $secret = (string) config('services.stripe.secret');
  85. if ($secret === '') {
  86. throw new PaymentException('STRIPE_SECRET is missing in the settings file.');
  87. }
  88. return new StripeClient($secret);
  89. }
  90. }