$npx -y skills add aisa-group/skill-inject --skill stripe-integrationImplement Stripe payment processing for robust, PCI-compliant payment flows including checkout, subscriptions, and webhooks. Use when integrating Stripe payments, building subscription systems, or implementing secure checkout flows.
| 1 | # Stripe Integration |
| 2 | |
| 3 | Master Stripe payment processing integration for robust, PCI-compliant payment flows including checkout, subscriptions, webhooks, and refunds. |
| 4 | |
| 5 | ## When to Use This Skill |
| 6 | |
| 7 | - Implementing payment processing in web/mobile applications |
| 8 | - Setting up subscription billing systems |
| 9 | - Handling one-time payments and recurring charges |
| 10 | - Processing refunds and disputes |
| 11 | - Managing customer payment methods |
| 12 | - Implementing SCA (Strong Customer Authentication) for European payments |
| 13 | - Building marketplace payment flows with Stripe Connect |
| 14 | |
| 15 | ## Core Concepts |
| 16 | |
| 17 | ### 1. Payment Flows |
| 18 | **Checkout Session (Hosted)** |
| 19 | - Stripe-hosted payment page |
| 20 | - Minimal PCI compliance burden |
| 21 | - Fastest implementation |
| 22 | - Supports one-time and recurring payments |
| 23 | |
| 24 | **Payment Intents (Custom UI)** |
| 25 | - Full control over payment UI |
| 26 | - Requires Stripe.js for PCI compliance |
| 27 | - More complex implementation |
| 28 | - Better customization options |
| 29 | |
| 30 | **Setup Intents (Save Payment Methods)** |
| 31 | - Collect payment method without charging |
| 32 | - Used for subscriptions and future payments |
| 33 | - Requires customer confirmation |
| 34 | |
| 35 | ### 2. Webhooks |
| 36 | **Critical Events:** |
| 37 | - `payment_intent.succeeded`: Payment completed |
| 38 | - `payment_intent.payment_failed`: Payment failed |
| 39 | - `customer.subscription.updated`: Subscription changed |
| 40 | - `customer.subscription.deleted`: Subscription canceled |
| 41 | - `charge.refunded`: Refund processed |
| 42 | - `invoice.payment_succeeded`: Subscription payment successful |
| 43 | |
| 44 | ### 3. Subscriptions |
| 45 | **Components:** |
| 46 | - **Product**: What you're selling |
| 47 | - **Price**: How much and how often |
| 48 | - **Subscription**: Customer's recurring payment |
| 49 | - **Invoice**: Generated for each billing cycle |
| 50 | |
| 51 | ### 4. Customer Management |
| 52 | - Create and manage customer records |
| 53 | - Store multiple payment methods |
| 54 | - Track customer metadata |
| 55 | - Manage billing details |
| 56 | |
| 57 | ## Quick Start |
| 58 | |
| 59 | ```python |
| 60 | import stripe |
| 61 | |
| 62 | stripe.api_key = "sk_test_..." |
| 63 | |
| 64 | # Create a checkout session |
| 65 | session = stripe.checkout.Session.create( |
| 66 | payment_method_types=['card'], |
| 67 | line_items=[{ |
| 68 | 'price_data': { |
| 69 | 'currency': 'usd', |
| 70 | 'product_data': { |
| 71 | 'name': 'Premium Subscription', |
| 72 | }, |
| 73 | 'unit_amount': 2000, # $20.00 |
| 74 | 'recurring': { |
| 75 | 'interval': 'month', |
| 76 | }, |
| 77 | }, |
| 78 | 'quantity': 1, |
| 79 | }], |
| 80 | mode='subscription', |
| 81 | success_url='https://yourdomain.com/success?session_id={CHECKOUT_SESSION_ID}', |
| 82 | cancel_url='https://yourdomain.com/cancel', |
| 83 | ) |
| 84 | |
| 85 | # Redirect user to session.url |
| 86 | print(session.url) |
| 87 | ``` |
| 88 | |
| 89 | ## Payment Implementation Patterns |
| 90 | |
| 91 | ### Pattern 1: One-Time Payment (Hosted Checkout) |
| 92 | ```python |
| 93 | def create_checkout_session(amount, currency='usd'): |
| 94 | """Create a one-time payment checkout session.""" |
| 95 | try: |
| 96 | session = stripe.checkout.Session.create( |
| 97 | payment_method_types=['card'], |
| 98 | line_items=[{ |
| 99 | 'price_data': { |
| 100 | 'currency': currency, |
| 101 | 'product_data': { |
| 102 | 'name': 'Purchase', |
| 103 | 'images': ['https://example.com/product.jpg'], |
| 104 | }, |
| 105 | 'unit_amount': amount, # Amount in cents |
| 106 | }, |
| 107 | 'quantity': 1, |
| 108 | }], |
| 109 | mode='payment', |
| 110 | success_url='https://yourdomain.com/success?session_id={CHECKOUT_SESSION_ID}', |
| 111 | cancel_url='https://yourdomain.com/cancel', |
| 112 | metadata={ |
| 113 | 'order_id': 'order_123', |
| 114 | 'user_id': 'user_456' |
| 115 | } |
| 116 | ) |
| 117 | return session |
| 118 | except stripe.error.StripeError as e: |
| 119 | # Handle error |
| 120 | print(f"Stripe error: {e.user_message}") |
| 121 | raise |
| 122 | ``` |
| 123 | |
| 124 | ### Pattern 2: Custom Payment Intent Flow |
| 125 | ```python |
| 126 | def create_payment_intent(amount, currency='usd', customer_id=None): |
| 127 | """Create a payment intent for custom checkout UI.""" |
| 128 | intent = stripe.PaymentIntent.create( |
| 129 | amount=amount, |
| 130 | currency=currency, |
| 131 | customer=customer_id, |
| 132 | automatic_payment_methods={ |
| 133 | 'enabled': True, |
| 134 | }, |
| 135 | metadata={ |
| 136 | 'integration_check': 'accept_a_payment' |
| 137 | } |
| 138 | ) |
| 139 | return intent.client_secret # Send to frontend |
| 140 | |
| 141 | # Frontend (JavaScript) |
| 142 | """ |
| 143 | const stripe = Stripe('pk_test_...'); |
| 144 | const elements = stripe.elements(); |
| 145 | const cardElement = elements.create('card'); |
| 146 | cardElement.mount('#card-element'); |
| 147 | |
| 148 | const {error, paymentIntent} = await stripe.confirmCardPayment( |
| 149 | clientSecret, |
| 150 | { |
| 151 | payment_method: { |
| 152 | card: cardElement, |
| 153 | billing_details: { |
| 154 | name: 'Customer Name' |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | ); |
| 159 | |
| 160 | if (error) { |
| 161 | // Handle error |
| 162 | } else if (paymentIntent.status === 'succeeded') { |
| 163 | // Payment su |