AI store generator for ecommerce, Malaysia first. Laravel 13 + React + TypeScript.
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

StripeWebhookController.php 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Billing\Billing;
  4. use App\Billing\SubscriptionGateway;
  5. use App\Commerce\Checkout;
  6. use App\Commerce\Payments\PaymentException;
  7. use App\Commerce\Payments\PaymentGateway;
  8. use App\Models\Order;
  9. use Illuminate\Http\Request;
  10. use Illuminate\Http\Response;
  11. /**
  12. * Stripe calls this address to tell us about payments.
  13. *
  14. * It is the safety net for shoppers who pay but close the browser before coming back to the store,
  15. * and it is how we hear that a merchant's monthly payment went through, failed or was cancelled.
  16. * Every message is checked with the webhook signing secret before anything is done.
  17. */
  18. class StripeWebhookController extends Controller
  19. {
  20. public function __invoke(Request $request, PaymentGateway $gateway, Checkout $checkout, SubscriptionGateway $subscriptions, Billing $billing): Response
  21. {
  22. $payload = $request->getContent();
  23. $signature = (string) $request->header('Stripe-Signature');
  24. try {
  25. $payment = $gateway->readWebhook($payload, $signature);
  26. // The same address also receives news about merchants' own plans.
  27. $subscription = $subscriptions->readWebhook($payload, $signature);
  28. } catch (PaymentException) {
  29. return response('Invalid webhook.', 400);
  30. }
  31. if ($subscription !== null) {
  32. $billing->applyFromWebhook($subscription);
  33. }
  34. if ($payment !== null) {
  35. $order = Order::query()->where('payment_checkout_id', $payment->checkoutId)->first();
  36. if ($order !== null) {
  37. $checkout->markPaid($order, $payment);
  38. }
  39. }
  40. return response('OK');
  41. }
  42. }