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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. * The first time the sign-up form is finished, FPX is asked for too.
  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 && ! $account->fpx_requested) {
  55. try {
  56. $this->accounts->requestFpx($account->account_id);
  57. $account->fpx_requested = true;
  58. } catch (PaymentException $e) {
  59. // Cards still work. We try again next time.
  60. report($e);
  61. }
  62. }
  63. $account->save();
  64. return $account;
  65. }
  66. /**
  67. * The store's account, refreshed from Stripe when it is not fully set up and was not checked in the last minute.
  68. * A store that is fully set up is not asked about again here.
  69. */
  70. public function current(Store $store): ?StripeAccount
  71. {
  72. $account = $store->stripeAccount;
  73. if ($account === null || ($account->charges_enabled && $account->payouts_enabled)) {
  74. return $account;
  75. }
  76. if ($account->checked_at !== null && $account->checked_at->diffInSeconds(now()) < self::RECHECK_SECONDS) {
  77. return $account;
  78. }
  79. try {
  80. return $this->refresh($store);
  81. } catch (PaymentException $e) {
  82. report($e);
  83. return $account;
  84. }
  85. }
  86. /**
  87. * Whether shoppers can pay this store now.
  88. */
  89. public function canTakePayments(Store $store): bool
  90. {
  91. return (bool) $this->current($store)?->charges_enabled;
  92. }
  93. }