| 123456789101112131415161718192021222324252627282930313233343536373839 |
- <?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 about payments made in merchants' own Stripe accounts ("Connected accounts" events).
- *
- * It is the safety net for shoppers who pay but close the browser before coming back to the store.
- * Messages are signed with their own secret (STRIPE_CONNECT_WEBHOOK_SECRET) and checked before anything is done.
- */
- class StripeConnectWebhookController extends Controller
- {
- public function __invoke(Request $request, PaymentGateway $gateway, Checkout $checkout): Response
- {
- try {
- $payment = $gateway->readWebhook($request->getContent(), (string) $request->header('Stripe-Signature'), fromMerchants: true);
- } catch (PaymentException) {
- return response('Invalid webhook.', 400);
- }
-
- if ($payment !== null) {
- $order = Order::query()->where('payment_checkout_id', $payment->checkoutId)->first();
-
- // The payment must be in the same merchant account the order's payment page was made in.
- if ($order !== null && $payment->account !== null && $payment->account === $order->payment_account) {
- $checkout->markPaid($order, $payment);
- }
- }
-
- return response('OK');
- }
- }
|