From 47bf332ad7a3592decda4aa36b7c1865e74c5a14 Mon Sep 17 00:00:00 2001 From: Alec Ritson Date: Tue, 3 Oct 2023 14:23:58 +0100 Subject: [PATCH] Stripe updates --- payment/stripe/config/stripe.php | 2 +- payment/stripe/routes/webhooks.php | 3 ++- payment/stripe/src/Actions/StoreCharges.php | 4 ++-- .../Middleware/StripeWebhookMiddleware.php | 8 ++++---- payment/stripe/src/StripePaymentType.php | 20 +++---------------- 5 files changed, 12 insertions(+), 25 deletions(-) diff --git a/payment/stripe/config/stripe.php b/payment/stripe/config/stripe.php index e42b7b6..343d1d1 100644 --- a/payment/stripe/config/stripe.php +++ b/payment/stripe/config/stripe.php @@ -40,7 +40,7 @@ \Stripe\PaymentIntent::STATUS_REQUIRES_CAPTURE => 'awaiting-payment', \Stripe\PaymentIntent::STATUS_CANCELED => 'cancelled', \Stripe\PaymentIntent::STATUS_PROCESSING => 'processing', - \Stripe\PaymentIntent::STATUS_REQUIRES_ACTION => 'failed', + \Stripe\PaymentIntent::STATUS_REQUIRES_ACTION => 'awaiting-payment', \Stripe\PaymentIntent::STATUS_REQUIRES_CONFIRMATION => 'auth-pending', \Stripe\PaymentIntent::STATUS_REQUIRES_PAYMENT_METHOD => 'failed', \Stripe\PaymentIntent::STATUS_SUCCEEDED => 'payment-received', diff --git a/payment/stripe/routes/webhooks.php b/payment/stripe/routes/webhooks.php index b58c1cb..4cf1901 100644 --- a/payment/stripe/routes/webhooks.php +++ b/payment/stripe/routes/webhooks.php @@ -3,6 +3,7 @@ use Illuminate\Support\Facades\Route; Route::post(config('lunar.stripe.webhook_path', 'stripe/webhook'), \Lunar\Stripe\Http\Controllers\WebhookController::class) + ->middleware([\Lunar\Stripe\Http\Middleware\StripeWebhookMiddleware::class, 'api']) ->withoutMiddleware([\App\Http\Middleware\VerifyCsrfToken::class]) - ->middleware(\Lunar\Stripe\Http\Middleware\StripeWebhookMiddleware::class) + ->name('lunar.stripe.webhook'); diff --git a/payment/stripe/src/Actions/StoreCharges.php b/payment/stripe/src/Actions/StoreCharges.php index fb01872..7020584 100644 --- a/payment/stripe/src/Actions/StoreCharges.php +++ b/payment/stripe/src/Actions/StoreCharges.php @@ -51,10 +51,10 @@ public function store(Order $order, Collection $charges) } if (! empty($paymentDetails['last4'])) { - $cardType = $paymentDetails['last4']; + $lastFour = $paymentDetails['last4']; } - if (! empty($paymentDetails['checked'])) { + if (! empty($paymentDetails['checks'])) { $meta = array_merge($meta, (array) $paymentDetails['checks']); } diff --git a/payment/stripe/src/Http/Middleware/StripeWebhookMiddleware.php b/payment/stripe/src/Http/Middleware/StripeWebhookMiddleware.php index 23613f8..941676a 100644 --- a/payment/stripe/src/Http/Middleware/StripeWebhookMiddleware.php +++ b/payment/stripe/src/Http/Middleware/StripeWebhookMiddleware.php @@ -2,15 +2,15 @@ namespace Lunar\Stripe\Http\Middleware; +use Closure; use Illuminate\Http\Request; -use Illuminate\Routing\Controllers\Middleware; use Lunar\Stripe\Concerns\ConstructsWebhookEvent; use Stripe\Exception\SignatureVerificationException; use Stripe\Exception\UnexpectedValueException; -class StripeWebhookMiddleware extends Middleware +class StripeWebhookMiddleware { - public function handle(Request $request, \Closure $next) + public function handle(Request $request, Closure $next = null) { $secret = config('services.stripe.webhooks.payment_intent'); $stripeSig = $request->header('Stripe-Signature'); @@ -26,7 +26,7 @@ public function handle(Request $request, \Closure $next) } if (! in_array($event->type, ['payment_intent.succeeded', 'payment_intent.payment_failed', 'payment_intent.payment_failed'])) { - return response(status: 200); + abort(200); } return $next($request); diff --git a/payment/stripe/src/StripePaymentType.php b/payment/stripe/src/StripePaymentType.php index 69bcde6..24e2179 100644 --- a/payment/stripe/src/StripePaymentType.php +++ b/payment/stripe/src/StripePaymentType.php @@ -50,7 +50,9 @@ public function __construct() */ final public function authorize(): PaymentAuthorize { - if (! $this->order || ! $this->order = $this->cart->draftOrder) { + $this->order = $this->cart->draftOrder ?: $this->cart->completedOrder; + + if (! $this->order) { try { $this->order = $this->cart->createOrder(); } catch (DisallowMultipleCartOrdersException $e) { @@ -61,14 +63,6 @@ final public function authorize(): PaymentAuthorize } } - if ($this->order->placed_at) { - return new PaymentAuthorize( - success: false, - message: 'This order has already been placed', - orderId: $this->order->id, - ); - } - $this->paymentIntent = $this->stripe->paymentIntents->retrieve( $this->data['payment_intent'] ); @@ -81,14 +75,6 @@ final public function authorize(): PaymentAuthorize ); } - if ($this->paymentIntent->status == PaymentIntent::STATUS_REQUIRES_PAYMENT_METHOD) { - return new PaymentAuthorize( - success: false, - message: 'A payment method is required for this intent.', - orderId: $this->order->id, - ); - } - if ($this->paymentIntent->status == PaymentIntent::STATUS_REQUIRES_CAPTURE && $this->policy == 'automatic') { $this->paymentIntent = $this->stripe->paymentIntents->capture( $this->data['payment_intent']