| 1234567891011121314151617181920212223242526272829303132333435363738 |
- <?php
-
- namespace App\Http\Controllers;
-
- use App\Commerce\Checkout;
- use App\Commerce\Payments\PaymentException;
- use App\Commerce\Payments\PaymentGateway;
- use App\Models\Order;
- use Illuminate\Http\Request;
- use Illuminate\Http\Response;
-
- /**
- * Stripe calls this address to tell us about payments.
- *
- * It is the safety net for shoppers who pay but close the browser before coming back to the store.
- * Every message is checked with the webhook signing secret before anything is done.
- */
- class StripeWebhookController extends Controller
- {
- public function __invoke(Request $request, PaymentGateway $gateway, Checkout $checkout): Response
- {
- try {
- $payment = $gateway->readWebhook($request->getContent(), (string) $request->header('Stripe-Signature'));
- } catch (PaymentException) {
- return response('Invalid webhook.', 400);
- }
-
- if ($payment !== null) {
- $order = Order::query()->where('payment_checkout_id', $payment->checkoutId)->first();
-
- if ($order !== null) {
- $checkout->markPaid($order, $payment);
- }
- }
-
- return response('OK');
- }
- }
|