From e718b9c6fb3c3374896873130a43817ff12333df Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Mon, 6 Sep 2021 08:56:00 +0100 Subject: [PATCH] Track stripe changes in core --- Extension.php | 53 +++++++++++++++++++++++++++++--------------------- composer.json | 8 ++++++++ extension.json | 2 +- 3 files changed, 40 insertions(+), 23 deletions(-) create mode 100644 composer.json diff --git a/Extension.php b/Extension.php index e88a004..21698d5 100644 --- a/Extension.php +++ b/Extension.php @@ -7,7 +7,7 @@ use Admin\Models\Payments_model; use Event; use Igniter\Flame\Exception\ApplicationException; -use Omnipay\Omnipay; +use Stripe\StripeClient; use System\Classes\BaseExtension; use Thoughtco\OrderApprover\Events\OrderCreated; @@ -56,17 +56,21 @@ public function boot() $intentId = $this->getIntentFromOrder($order); $gateway = $this->createGateway($order->payment_method); + + try { - $response = $gateway->capture([ - 'paymentIntentReference' => $intentId, - ])->send(); + $response = $gateway->paymentIntents->capture($intentId, []); - if ($response->isSuccessful()) { - $order->logPaymentAttempt('Payment captured successfully', 1, [], $response->getData()); - return; + if ($response->status == 'succeeded') { + $order->logPaymentAttempt('Payment captured successfully', 1, [], $response); + return; + } + + throw new Exception('Status '.$response->status); + + } catch (Exception $e) { + $order->logPaymentAttempt('Payment capture failed -> '.$e->getMessage(), 0, [], $response); } - - $order->logPaymentAttempt('Payment capture failed -> '.$response->getMessage(), 0, [], $response->getData()); }); // order rejected through orderApprover extension - cancel payment @@ -80,18 +84,21 @@ public function boot() $intentId = $this->getIntentFromOrder($order); $gateway = $this->createGateway($order->payment_method); + + try { - $response = $gateway->cancel([ - 'paymentIntentReference' => $intentId, - ])->send(); - - $data = $response->getData(); - if (array_get($data, 'status') === 'canceled') { - $order->logPaymentAttempt('Payment cancelled successfully', 1, [], $data); - return; - } - - $order->logPaymentAttempt('Payment cancellation failed -> '.$response->getMessage(), 0, [], $data); + $response = $gateway->paymentIntents->cancel($intentId, []); + + if ($response->status == 'canceled') { + $order->logPaymentAttempt('Payment cancelled successfully', 1, [], $response); + return; + } + + throw new Exception('Status '.$response->status); + + } catch (Exception $e) { + $order->logPaymentAttempt('Payment cancellation failed -> '.$e->getMessage(), 0, [], $response); + } }); } @@ -116,8 +123,10 @@ protected function getIntentFromOrder($order) protected function createGateway($paymentMethod) { - $gateway = Omnipay::create('Stripe\PaymentIntents'); - $gateway->setApiKey($paymentMethod->transaction_mode != 'live' ? $paymentMethod->test_secret_key : $paymentMethod->live_secret_key); + $gateway = new StripeClient([ + 'api_key' => $paymentMethod->transaction_mode != 'live' ? $paymentMethod->test_secret_key : $paymentMethod->live_secret_key, + ]); + return $gateway; } diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..42b61a9 --- /dev/null +++ b/composer.json @@ -0,0 +1,8 @@ +{ + "name": "thoughtco/ti-ext-stripeauthorize", + "type": "tastyigniter-extension", + "keywords": ["stripe"], + "require": { + "stripe/stripe-php": "~7.93.0", + } +} diff --git a/extension.json b/extension.json index c260df9..14f3279 100644 --- a/extension.json +++ b/extension.json @@ -2,7 +2,7 @@ "code": "thoughtco.stripeauthorize", "name": "Stripe Authorize", "description": "Listen for Order Approver events to capture or cancel Stripe payments", - "version": "v1.0.7", + "version": "v1.1.0", "author": "ryanmitchell", "icon": { "class": "fa fa-funnel-dollar",