AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

StripeConnect.php 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace App\Commerce\Payments;
  3. use App\Models\Store;
  4. use App\Models\StripeAccount;
  5. /**
  6. * Getting a store ready to take payments into the merchant's own Stripe account.
  7. *
  8. * We keep a copy of what Stripe says about the account. While the account is not fully set up, the copy is
  9. * refreshed when the merchant comes back from Stripe, and at most once a minute when Settings opens or a
  10. * shopper reaches checkout.
  11. */
  12. class StripeConnect
  13. {
  14. /** How long a "not ready" answer is trusted before we ask Stripe again. */
  15. private const int RECHECK_SECONDS = 60;
  16. public function __construct(private readonly MerchantAccounts $accounts) {}
  17. public function isConfigured(): bool
  18. {
  19. return (string) config('services.stripe.secret') !== '';
  20. }
  21. /**
  22. * Where to send the merchant to fill in Stripe's sign-up form. Opens the account the first time.
  23. *
  24. * @throws PaymentException
  25. */
  26. public function setupUrl(Store $store, string $email, string $returnUrl, string $refreshUrl): string
  27. {
  28. $account = $store->stripeAccount;
  29. if ($account === null) {
  30. $account = $store->stripeAccount()->create(['account_id' => $this->accounts->create($store, $email)]);
  31. $store->setRelation('stripeAccount', $account);
  32. }
  33. return $this->accounts->setupLink($account->account_id, $returnUrl, $refreshUrl);
  34. }
  35. /**
  36. * Ask Stripe about the store's account and save the answer.
  37. * Once the sign-up form is finished, FPX and GrabPay are asked for too, each once.
  38. *
  39. * @throws PaymentException
  40. */
  41. public function refresh(Store $store): ?StripeAccount
  42. {
  43. $account = $store->stripeAccount;
  44. if ($account === null) {
  45. return null;
  46. }
  47. $status = $this->accounts->status($account->account_id);
  48. $account->fill([
  49. 'charges_enabled' => $status->chargesEnabled,
  50. 'payouts_enabled' => $status->payoutsEnabled,
  51. 'details_submitted' => $status->detailsSubmitted,
  52. 'checked_at' => now(),
  53. ]);
  54. if ($status->detailsSubmitted) {
  55. foreach ($account->methodsToRequest() as $method) {
  56. try {
  57. $this->accounts->requestMethod($account->account_id, $method);
  58. $account->{StripeAccount::METHODS[$method]} = true;
  59. } catch (PaymentException $e) {
  60. // Cards still work. We try again next time.
  61. report($e);
  62. }
  63. }
  64. }
  65. $account->save();
  66. return $account;
  67. }
  68. /**
  69. * The store's account, refreshed from Stripe when it is not fully set up and was not checked in the last minute.
  70. * A store that is fully set up is not asked about again here.
  71. */
  72. public function current(Store $store): ?StripeAccount
  73. {
  74. $account = $store->stripeAccount;
  75. // A fully set up store is not asked about again, unless a payment method we added later
  76. // (such as GrabPay, added 2026-12-01) still has to be switched on for it.
  77. if ($account === null || ($account->charges_enabled && $account->payouts_enabled && $account->methodsToRequest() === [])) {
  78. return $account;
  79. }
  80. if ($account->checked_at !== null && $account->checked_at->diffInSeconds(now()) < self::RECHECK_SECONDS) {
  81. return $account;
  82. }
  83. try {
  84. return $this->refresh($store);
  85. } catch (PaymentException $e) {
  86. report($e);
  87. return $account;
  88. }
  89. }
  90. /**
  91. * Whether shoppers can pay this store now.
  92. */
  93. public function canTakePayments(Store $store): bool
  94. {
  95. return (bool) $this->current($store)?->charges_enabled;
  96. }
  97. }