1
---
2
name: stripe-integration
3
description: Implement 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.
4
---
5
6
# Stripe Integration
7
8
Master Stripe payment processing integration for robust, PCI-compliant payment flows including checkout, subscriptions, webhooks, and refunds.
9
10
## When to Use This Skill
11
12
- Implementing payment processing in web/mobile applications
13
- Setting up subscription billing systems
14
- Handling one-time payments and recurring charges
15
- Processing refunds and disputes
16
- Managing customer payment methods
17
- Implementing SCA (Strong Customer Authentication) for European payments
18
- Building marketplace payment flows with Stripe Connect
19
20
## Core Concepts
21
22
### 1. Payment Flows
23
24
**Checkout Sessions**
25
26
- Recommended for most integrations
27
- Supports all UI paths:
28
- Stripe-hosted checkout page
29
- Embedded checkout form
30
- Custom UI with Elements (Payment Element, Express Checkout Element) using `ui_mode='custom'`
31
- Provides built-in checkout capabilities (line items, discounts, tax, shipping, address collection, saved payment methods, and checkout lifecycle events)
32
- Lower integration and maintenance burden than Payment Intents
33
34
**Payment Intents (Bespoke control)**
35
36
- You calculate the final amount with taxes, discounts, subscriptions, and currency conversion yourself.
37
- More complex implementation and long-term maintenance burden
38
- Requires Stripe.js for PCI compliance
39
40
**Setup Intents (Save Payment Methods)**
41
42
- Collect payment method without charging
43
- Used for subscriptions and future payments
44
- Requires customer confirmation
45
46
### 2. Webhooks
47
48
**Critical Events:**
49
50
- `payment_intent.succeeded`: Payment completed
51
- `payment_intent.payment_failed`: Payment failed
52
- `customer.subscription.updated`: Subscription changed
53
- `customer.subscription.deleted`: Subscription canceled
54
- `charge.refunded`: Refund processed
55
- `invoice.payment_succeeded`: Subscription payment successful
56
57
### 3. Subscriptions
58
59
**Components:**
60
61
- **Product**: What you're selling
62
- **Price**: How much and how often
63
- **Subscription**: Customer's recurring payment
64
- **Invoice**: Generated for each billing cycle
65
66
### 4. Customer Management
67
68
- Create and manage customer records
69
- Store multiple payment methods
70
- Track customer metadata
71
- Manage billing details
72
73
## Quick Start
74
75
```python
76
import stripe
77
78
stripe.api_key = "sk_test_..."
79
80
# Create a checkout session
81
session = stripe.checkout.Session.create(
82
line_items=[{
83
'price_data': {
84
'currency': 'usd',
85
'product_data': {
86
'name': 'Premium Subscription',
87
},
88
'unit_amount': 2000, # $20.00
89
'recurring': {
90
'interval': 'month',
91
},
92
},
93
'quantity': 1,
94
}],
95
mode='subscription',
96
success_url='https://yourdomain.com/success?session_id={CHECKOUT_SESSION_ID}',
97
cancel_url='https://yourdomain.com/cancel'
98
)
99
100
# Redirect user to session.url
101
print(session.url)
102
```
103
104
## Detailed patterns and worked examples
105
106
Detailed pattern documentation lives in `references/details.md`. Read that file when the navigation tier above is insufficient.
107
108
## Testing
109
110
```python
111
# Use test mode keys
112
stripe.api_key = "sk_test_..."
113
114
# Test card numbers
115
TEST_CARDS = {
116
'success': '4242424242424242',
117
'declined': '4000000000000002',
118
'3d_secure': '4000002500003155',
119
'insufficient_funds': '4000000000009995'
120
}
121
122
def test_payment_flow():
123
"""Test complete payment flow."""
124
# Create test customer
125
customer = stripe.Customer.create(
126
email="test@example.com"
127
)
128
129
# Create payment intent
130
intent = stripe.PaymentIntent.create(
131
amount=1000,
132
automatic_payment_methods={
133
'enabled': True
134
},
135
currency='usd',
136
customer=customer.id
137
)
138
139
# Confirm with test card
140
confirmed = stripe.PaymentIntent.confirm(
141
intent.id,
142
payment_method='pm_card_visa' # Test payment method
143
)
144
145
assert confirmed.status == 'succeeded'
146
```
147