From 76130b8de974a036bd0db65eb6188603844e4632 Mon Sep 17 00:00:00 2001 From: Philip Date: Sun, 26 Feb 2023 20:52:38 +0000 Subject: [PATCH] Support auth only and capture --- payments/StripeCheckout.php | 52 +++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/payments/StripeCheckout.php b/payments/StripeCheckout.php index 6533353..89e556b 100644 --- a/payments/StripeCheckout.php +++ b/payments/StripeCheckout.php @@ -51,6 +51,55 @@ public function isApplicable($total, $host) { return $host->order_total <= $total; } + + public function capturePaymentIntent($paymentIntentId, $order, $data = []) + { + if ($order->payment !== $this->model->code) + return; + + try { + $response = $this->createGateway()->paymentIntents->capture( + $paymentIntentId, + $data, + ); + + if ($response->status == 'succeeded') { + $order->logPaymentAttempt('Payment captured successfully', 1, $data, $response); + } + else { + $order->logPaymentAttempt('Payment captured failed', 0, $data, $response); + } + + return $response; + } + catch (Exception $ex) { + $order->logPaymentAttempt('Payment capture failed -> '.$ex->getMessage(), 0, $data, $response); + } + } + + public function cancelPaymentIntent($paymentIntentId, $order, $data = []) + { + if ($order->payment !== $this->model->code) + return; + + try { + $response = $this->createGateway()->paymentIntents->cancel( + $paymentIntentId, $data + ); + + if ($response->status == 'canceled') { + $order->logPaymentAttempt('Payment canceled successfully', 1, $data, $response); + } + else { + $order->logPaymentAttempt('Payment canceled failed', 0, $data, $response); + } + + return $response; + } + catch (Exception $ex) { + $order->logPaymentAttempt('Payment canceled failed -> '.$ex->getMessage(), 0, $data, $response); + } + } /** * @param array $data @@ -230,6 +279,9 @@ protected function getPaymentFormFields($order, $data = []) 'metadata' => [ 'order_id' => $order->order_id, ], + 'payment_intent_data' => [ + 'capture_method' => $this->shouldAuthorizePayment() ? 'manual' : 'automatic', + ], ]; // Share the email field in our form to Stripe checkout session,