| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- <?php
-
- namespace App\Http\Controllers;
-
- use App\Billing\Billing;
- use App\Billing\SubscriptionGateway;
- 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,
- * and it is how we hear that a merchant's monthly payment went through, failed or was cancelled.
- * 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, SubscriptionGateway $subscriptions, Billing $billing): Response
- {
- $payload = $request->getContent();
- $signature = (string) $request->header('Stripe-Signature');
-
- try {
- $payment = $gateway->readWebhook($payload, $signature);
- // The same address also receives news about merchants' own plans.
- $subscription = $subscriptions->readWebhook($payload, $signature);
- } catch (PaymentException) {
- return response('Invalid webhook.', 400);
- }
-
- if ($subscription !== null) {
- $billing->applyFromWebhook($subscription);
- }
-
- if ($payment !== null) {
- $order = Order::query()->where('payment_checkout_id', $payment->checkoutId)->first();
-
- if ($order !== null) {
- $checkout->markPaid($order, $payment);
- }
- }
-
- return response('OK');
- }
- }
|