diff --git a/src/Stripe/Api/Stripe.php b/src/Stripe/Api/Stripe.php index 14160bc..d0d7a4b 100644 --- a/src/Stripe/Api/Stripe.php +++ b/src/Stripe/Api/Stripe.php @@ -94,12 +94,11 @@ public function getPaymentIntent(string $id): PaymentIntent return $this->stripe->paymentIntents->retrieve($id); } - public function createPaymentSession(StripeUser $user, $items, array $urls, Transaction $transaction): Session + public function createPaymentSession(StripeUser $user, $items, array $urls, Transaction $transaction) { try { $session = $this->stripe->checkout->sessions->create([ - //'customer_email' => $user->getEmail(), 'cancel_url' => $urls['cancel'], 'success_url' => $urls['return'], 'mode' => 'payment', @@ -115,6 +114,10 @@ public function createPaymentSession(StripeUser $user, $items, array $urls, Tran return $session; } catch (Exception $e){ + if ("No such customer: '$stripeId'" == $e->getMessage()){ + $this->createCustomer($user); + return $user; + } dd($e->getMessage()); } } diff --git a/src/Stripe/StripePaymentManager.php b/src/Stripe/StripePaymentManager.php index 54df8f0..9268ee4 100644 --- a/src/Stripe/StripePaymentManager.php +++ b/src/Stripe/StripePaymentManager.php @@ -15,6 +15,7 @@ use ClientX\Payment\PaymentManagerInterface; use ClientX\Renderer\RendererInterface; use ClientX\Router; +use Stripe\Checkout\Session; use Psr\Http\Message\ServerRequestInterface as Request; class StripePaymentManager extends AbstractPaymentManager implements PaymentManagerInterface @@ -49,12 +50,12 @@ public function process(Transaction $transaction, Request $request, User $user) $items = collect($transaction->getItems())->filter(function($item) { return $item->price() > 0;})->map(function (TransactionItem $item, $i) use ($transaction) { $discount = 0; - $next = $transaction->getItems()[$i+1] ?? null; - if ($next != null) { - if ($next->price() < 0) { - $discount = $next->price(); - } - } + $next = $transaction->getItems()[$i+1] ?? null; + if ($next != null) { + if ($next->price() < 0) { + $discount = $next->price(); + } + } return [ 'price_data' => @@ -69,8 +70,16 @@ public function process(Transaction $transaction, Request $request, User $user) $user = $this->createStripeUser($this->auth->getUser()); $session = $this->stripe->createPaymentSession($user, $items, $this->getRedirectsLinks($request, $transaction), $transaction); - $params = ['session' => $session, 'key' => $this->stripe->getPublicKey()]; - return $this->renderer->render("@stripe_admin/autoredirect", $params); + if ($session instanceof Session){ + $params = ['session' => $session, 'key' => $this->stripe->getPublicKey()]; + return $this->renderer->render("@stripe_admin/autoredirect", $params); + } else { + $this->table->update($session->getId(), [ + 'stripe_id' => json_encode($session->getStripeId(true)) + ]); + $params = ['session' => $session, 'key' => $this->stripe->getPublicKey()]; + return $this->renderer->render("@stripe_admin/autoredirect", $params); + } } public function refund(array $items): bool