| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- <?php
-
- namespace App\Commerce\Payments;
-
- use App\Models\Store;
- use App\Models\StripeAccount;
-
- /**
- * Getting a store ready to take payments into the merchant's own Stripe account.
- *
- * We keep a copy of what Stripe says about the account. While the account is not fully set up, the copy is
- * refreshed when the merchant comes back from Stripe, and at most once a minute when Settings opens or a
- * shopper reaches checkout.
- */
- class StripeConnect
- {
- /** How long a "not ready" answer is trusted before we ask Stripe again. */
- private const int RECHECK_SECONDS = 60;
-
- public function __construct(private readonly MerchantAccounts $accounts) {}
-
- public function isConfigured(): bool
- {
- return (string) config('services.stripe.secret') !== '';
- }
-
- /**
- * Where to send the merchant to fill in Stripe's sign-up form. Opens the account the first time.
- *
- * @throws PaymentException
- */
- public function setupUrl(Store $store, string $email, string $returnUrl, string $refreshUrl): string
- {
- $account = $store->stripeAccount;
-
- if ($account === null) {
- $account = $store->stripeAccount()->create(['account_id' => $this->accounts->create($store, $email)]);
- $store->setRelation('stripeAccount', $account);
- }
-
- return $this->accounts->setupLink($account->account_id, $returnUrl, $refreshUrl);
- }
-
- /**
- * Ask Stripe about the store's account and save the answer.
- * The first time the sign-up form is finished, FPX is asked for too.
- *
- * @throws PaymentException
- */
- public function refresh(Store $store): ?StripeAccount
- {
- $account = $store->stripeAccount;
-
- if ($account === null) {
- return null;
- }
-
- $status = $this->accounts->status($account->account_id);
-
- $account->fill([
- 'charges_enabled' => $status->chargesEnabled,
- 'payouts_enabled' => $status->payoutsEnabled,
- 'details_submitted' => $status->detailsSubmitted,
- 'checked_at' => now(),
- ]);
-
- if ($status->detailsSubmitted && ! $account->fpx_requested) {
- try {
- $this->accounts->requestFpx($account->account_id);
- $account->fpx_requested = true;
- } catch (PaymentException $e) {
- // Cards still work. We try again next time.
- report($e);
- }
- }
-
- $account->save();
-
- return $account;
- }
-
- /**
- * The store's account, refreshed from Stripe when it is not fully set up and was not checked in the last minute.
- * A store that is fully set up is not asked about again here.
- */
- public function current(Store $store): ?StripeAccount
- {
- $account = $store->stripeAccount;
-
- if ($account === null || ($account->charges_enabled && $account->payouts_enabled)) {
- return $account;
- }
-
- if ($account->checked_at !== null && $account->checked_at->diffInSeconds(now()) < self::RECHECK_SECONDS) {
- return $account;
- }
-
- try {
- return $this->refresh($store);
- } catch (PaymentException $e) {
- report($e);
-
- return $account;
- }
- }
-
- /**
- * Whether shoppers can pay this store now.
- */
- public function canTakePayments(Store $store): bool
- {
- return (bool) $this->current($store)?->charges_enabled;
- }
- }
|