From 1d72bac7fa82ddc325b4da6a8f35d47e81669ce4 Mon Sep 17 00:00:00 2001 From: IceToast Date: Wed, 15 Dec 2021 10:56:58 +0100 Subject: [PATCH] =?UTF-8?q?feat:=20=E2=9C=A8=20Added=20StripeClient=20gene?= =?UTF-8?q?ration?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/Admin/PaymentController.php | 31 ++++++++++++++----- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/app/Http/Controllers/Admin/PaymentController.php b/app/Http/Controllers/Admin/PaymentController.php index af3c8077..e6c549b1 100644 --- a/app/Http/Controllers/Admin/PaymentController.php +++ b/app/Http/Controllers/Admin/PaymentController.php @@ -233,9 +233,10 @@ class PaymentController extends Controller */ public function StripePay(Request $request, CreditProduct $creditProduct) { - \Stripe\Stripe::setApiKey('sk_test_51Js6U8J2KSABgZztx8QWiohacnzGyIlpOk48DfSoUWPW8mhqLzxcQ5B9a1Wiz8jCC4Xfp3QeBDTsuSU7hkXEUksW00JyN08hoU'); - $request = \Stripe\Checkout\Session::create([ + $stripeClient = $this->getStripeClient(); + + $request = $stripeClient->sessions->create([ 'line_items' => [ [ 'price_data' => [ @@ -262,8 +263,11 @@ class PaymentController extends Controller ], 'mode' => 'payment', - 'success_url' => route('payment.StripeSuccess').'?session_id={CHECKOUT_SESSION_ID}', - 'cancel_url' => route('payment.Cancel'), + 'payment_intent_data' => [ + 'capture_method' => 'manual', + ], + 'success_url' => route('payment.StripeSuccess', ['product' => $creditProduct->id]).'&session_id={CHECKOUT_SESSION_ID}', + 'cancel_url' => route('payment.Cancel'), ]); @@ -277,17 +281,18 @@ class PaymentController extends Controller public function StripeSuccess(Request $request) { echo $request->input('product'); - \Stripe\Stripe::setApiKey('sk_test_51Js6U8J2KSABgZztx8QWiohacnzGyIlpOk48DfSoUWPW8mhqLzxcQ5B9a1Wiz8jCC4Xfp3QeBDTsuSU7hkXEUksW00JyN08hoU'); /** @var CreditProduct $creditProduct */ $creditProduct = CreditProduct::findOrFail($request->input('product')); /** @var User $user */ $user = Auth::user(); + $stripeClient = $this->getStripeClient(); try{ - $response = \Stripe\Checkout\Session::retrieve($request->input('session_id')); + $paymentSession = $stripeClient->sessions->retrieve($request->input('session_id')); + $capturedPaymentIntent = $stripeClient->paymentIntents->capture($paymentSession.payment_intent); if ($response->payment_status == "paid") { @@ -340,6 +345,18 @@ class PaymentController extends Controller } } + /** + * @return StripeClient + */ + protected function getStripeClient() + { + $environment = env('APP_ENV') == 'local' + ? $this->getStripeSecret() + : $this->getStripeSecret(); + + return new \Stripe\StripeClient($environment); + } + /** * @return string */ @@ -351,7 +368,7 @@ class PaymentController extends Controller /** * @return string */ - protected function getStripeClientSecret() + protected function getStripeSecret() { return env('STRIPE_SECRET'); }