| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- <?php
-
- namespace App\Commerce\Payments;
-
- use App\Models\Store;
- use Stripe\Exception\ApiErrorException;
- use Stripe\StripeClient;
-
- /**
- * Merchants' own Stripe accounts, through Stripe Connect.
- *
- * Stripe only accepts its newer accounts API (v2) for new platforms. Each account gets the full Stripe Dashboard,
- * pays Stripe's fees itself, and Stripe covers losses, so the merchant is the seller in every way.
- */
- final class StripeMerchantAccounts implements MerchantAccounts
- {
- public function create(Store $store, string $email): string
- {
- try {
- return $this->client()->v2->core->accounts->create([
- 'contact_email' => $email,
- 'display_name' => $store->name,
- 'dashboard' => 'full',
- 'identity' => ['country' => 'my'],
- // FPX cannot be asked for yet: Stripe first needs the business type from the sign-up form.
- 'configuration' => ['merchant' => ['capabilities' => ['card_payments' => ['requested' => true]]]],
- 'defaults' => [
- 'currency' => 'myr',
- 'responsibilities' => ['fees_collector' => 'stripe', 'losses_collector' => 'stripe'],
- ],
- 'metadata' => ['store_id' => (string) $store->id],
- ])->id;
- } catch (ApiErrorException $e) {
- throw new PaymentException('Stripe could not open an account: '.$e->getMessage(), 0, $e);
- }
- }
-
- public function setupLink(string $accountId, string $returnUrl, string $refreshUrl): string
- {
- try {
- return $this->client()->v2->core->accountLinks->create([
- 'account' => $accountId,
- 'use_case' => [
- 'type' => 'account_onboarding',
- 'account_onboarding' => [
- 'return_url' => $returnUrl,
- 'refresh_url' => $refreshUrl,
- ],
- ],
- ])->url;
- } catch (ApiErrorException $e) {
- throw new PaymentException('Stripe could not open its sign-up form: '.$e->getMessage(), 0, $e);
- }
- }
-
- public function status(string $accountId): MerchantAccountStatus
- {
- try {
- // The older accounts API still reads v2 accounts, and says plainly what we need.
- $account = $this->client()->accounts->retrieve($accountId);
- } catch (ApiErrorException $e) {
- throw new PaymentException('Stripe could not find the account: '.$e->getMessage(), 0, $e);
- }
-
- return new MerchantAccountStatus(
- chargesEnabled: (bool) $account->charges_enabled,
- payoutsEnabled: (bool) $account->payouts_enabled,
- detailsSubmitted: (bool) $account->details_submitted,
- );
- }
-
- public function requestFpx(string $accountId): void
- {
- try {
- // The older capability call. The v2 update refuses Malaysian sole proprietors
- // ("sole_proprietorship is not supported for entity type company"), found on 2026-11-30.
- $client = $this->client();
- $in = ['stripe_account' => $accountId];
- $client->accounts->updateCapability($accountId, 'fpx_payments', ['requested' => true]);
-
- // Approved is not enough: FPX also starts switched off in the account's payment method
- // settings, so shoppers would only see cards. Switch it on there too.
- foreach ($client->paymentMethodConfigurations->all(['limit' => 10], $in)->data as $configuration) {
- $client->paymentMethodConfigurations->update($configuration->id, ['fpx' => ['display_preference' => ['preference' => 'on']]], $in);
- }
- } catch (ApiErrorException $e) {
- throw new PaymentException('Stripe could not switch on FPX: '.$e->getMessage(), 0, $e);
- }
- }
-
- private function client(): StripeClient
- {
- $secret = (string) config('services.stripe.secret');
-
- if ($secret === '') {
- throw new PaymentException('STRIPE_SECRET is missing in the settings file.');
- }
-
- return new StripeClient($secret);
- }
- }
|