Lesson 38 โข Advanced
Payment Gateways ๐ณ
Accept payments with Stripe Checkout, handle webhooks for reliable payment confirmation, and process refunds and disputes.
What You'll Learn in This Lesson
- โข Understand the Stripe Checkout payment flow
- โข Create checkout sessions with line items
- โข Handle webhooks with signature verification
- โข Process refunds and chargeback disputes
- โข Implement idempotent webhook processing
Stripe Checkout Integration
Stripe Checkout handles the entire payment UI โ card forms, validation, 3D Secure, Apple Pay, and Google Pay. Your PHP backend creates a session with line items and redirect URLs, then sends the customer to Stripe. You never touch card numbers, keeping you PCI compliant.
Try It: Stripe Checkout
Create checkout sessions, verify webhooks, and process payments
// Stripe Payment Integration in PHP
console.log("=== Stripe Checkout Flow ===");
console.log();
console.log(" 1. Customer clicks 'Pay' โ Your PHP backend creates a Checkout Session");
console.log(" 2. Customer is redirected to Stripe's hosted payment page");
console.log(" 3. Customer enters card details (on Stripe's secure page)");
console.log(" 4. Stripe processes payment โ redirects to your success/cancel URL");
console.log(" 5. Stripe sends a webhook to confirm payment (async, reliable)
...Webhooks & Refunds
Never rely on the success URL redirect alone โ customers close browsers, networks fail. Webhooks are server-to-server notifications that Stripe sends to your endpoint. Always verify the webhook signature, process events idempotently (handle duplicates gracefully), and return a 200 response quickly.
Try It: Webhook Handler
Process webhooks idempotently, handle refunds and disputes
// Webhook Handling & Refund Processing
console.log("=== Why Webhooks Matter ===");
console.log();
console.log(" Without webhooks:");
console.log(" โ You don't know if payment actually succeeded");
console.log(" โ Customer closes browser before redirect โ lost sale");
console.log(" โ Network issues prevent success URL redirect");
console.log();
console.log(" With webhooks:");
console.log(" โ
Stripe sends server-to-server notification");
console.log(" โ
Reliable even if customer disconnect
...โ ๏ธ Common Mistakes
Stripe-Signature header to ensure it's really from Stripe.checkout.session.completed webhook as the authoritative payment confirmation.stripe listen --forward-to localhost:8000/webhook โ it forwards real-time events to your dev server.๐ Quick Reference โ Payments
| Concept | Description |
|---|---|
| Checkout Session | Server-created payment page with line items |
| Webhook | Server-to-server payment confirmation |
| Idempotency | Process each event exactly once |
| Signature Verification | Prove webhook is genuinely from Stripe |
| PCI Compliance | Stripe handles card data so you don't have to |
๐ Lesson Complete!
You can now accept payments securely! Next, learn to build e-commerce logic with carts, orders, and invoices.
Sign up for free to track which lessons you've completed and get learning reminders.