mirror of
https://github.com/wshobson/agents.git
synced 2026-03-18 09:37:15 +00:00
Add modernized Payment Intents pattern with Payment Element
- Restore Payment Intents flow removed by PR, updated for modern best practices - Use Payment Element instead of legacy Card Element - Use stripe.confirmPayment() instead of deprecated confirmCardPayment() - Use automatic_payment_methods instead of hardcoded payment_method_types - Split Python/JS into separate fenced code blocks for clarity - Add guidance on when to use Payment Intents vs Checkout Sessions - Renumber subsequent patterns (Subscription → 4, Customer Portal → 5)
This commit is contained in:
@@ -198,7 +198,48 @@ if (loadActionsResult.type === 'success') {
|
||||
"""
|
||||
```
|
||||
|
||||
### Pattern 3: Subscription Creation
|
||||
### Pattern 3: Payment Intents with Payment Element (Bespoke Control)
|
||||
|
||||
Use this when you need full control over the payment flow and cannot use Checkout Sessions
|
||||
(e.g., you have your own tax, discount, or subscription calculation engine).
|
||||
|
||||
```python
|
||||
def create_payment_intent(amount, currency='usd', customer_id=None):
|
||||
"""Create a payment intent for bespoke checkout UI with Payment Element."""
|
||||
intent = stripe.PaymentIntent.create(
|
||||
amount=amount,
|
||||
currency=currency,
|
||||
customer=customer_id,
|
||||
automatic_payment_methods={
|
||||
'enabled': True,
|
||||
},
|
||||
)
|
||||
return intent.client_secret # Send to frontend
|
||||
```
|
||||
|
||||
```javascript
|
||||
// Frontend: Mount Payment Element and confirm via Payment Intents
|
||||
const stripe = Stripe('pk_test_...');
|
||||
const elements = stripe.elements({clientSecret});
|
||||
|
||||
const paymentElement = elements.create('payment');
|
||||
paymentElement.mount('#payment-element');
|
||||
|
||||
document.getElementById('pay-button').addEventListener('click', async () => {
|
||||
const {error} = await stripe.confirmPayment({
|
||||
elements,
|
||||
confirmParams: {
|
||||
return_url: 'https://yourdomain.com/complete',
|
||||
},
|
||||
});
|
||||
|
||||
if (error) {
|
||||
document.getElementById('errors').textContent = error.message;
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
### Pattern 4: Subscription Creation
|
||||
|
||||
```python
|
||||
def create_subscription(customer_id, price_id):
|
||||
@@ -221,7 +262,7 @@ def create_subscription(customer_id, price_id):
|
||||
raise
|
||||
```
|
||||
|
||||
### Pattern 4: Customer Portal
|
||||
### Pattern 5: Customer Portal
|
||||
|
||||
```python
|
||||
def create_customer_portal_session(customer_id):
|
||||
|
||||
Reference in New Issue
Block a user