Polish Stripe best practices examples for consistency

- Remove payment_method_types=['card'] from Quick Start (dynamic payment methods)
- Remove unused appearance variable from Pattern 2 JS example
- Fix actions access pattern: destructure before use for consistency
- Add inline comments clarifying sync/async distinction and amount format
- Add ui_mode='embedded' to Embedded checkout bullet for completeness
- Replace payment_method_types with automatic_payment_methods in test example
This commit is contained in:
Seth Hobson
2026-02-19 13:42:36 -05:00
parent 2b8e3166a1
commit 204e8129aa

View File

@@ -26,7 +26,7 @@ Master Stripe payment processing integration for robust, PCI-compliant payment f
- Recommended for most integrations - Recommended for most integrations
- Supports all UI paths: - Supports all UI paths:
- Stripe-hosted checkout page - Stripe-hosted checkout page
- Embedded checkout form - Embedded checkout form (`ui_mode='embedded'`)
- Custom UI with Elements (Payment Element, Express Checkout Element) using `ui_mode='custom'` - Custom UI with Elements (Payment Element, Express Checkout Element) using `ui_mode='custom'`
- Provides built-in checkout capabilities (line items, discounts, tax, shipping, address collection, saved payment methods, and checkout lifecycle events) - Provides built-in checkout capabilities (line items, discounts, tax, shipping, address collection, saved payment methods, and checkout lifecycle events)
- Lower integration and maintenance burden than Payment Intents - Lower integration and maintenance burden than Payment Intents
@@ -79,7 +79,6 @@ stripe.api_key = "sk_test_..."
# Create a checkout session # Create a checkout session
session = stripe.checkout.Session.create( session = stripe.checkout.Session.create(
payment_method_types=['card'],
line_items=[{ line_items=[{
'price_data': { 'price_data': {
'currency': 'usd', 'currency': 'usd',
@@ -155,19 +154,19 @@ def create_checkout_session_for_elements(amount, currency='usd'):
}], }],
return_url='https://yourdomain.com/complete?session_id={CHECKOUT_SESSION_ID}' return_url='https://yourdomain.com/complete?session_id={CHECKOUT_SESSION_ID}'
) )
return session.client_secret # Send to frontend return session.client_secret # Send to frontend for stripe.initCheckout()
# Frontend (JavaScript) # Frontend (JavaScript)
""" """
const stripe = Stripe('pk_test_...'); const stripe = Stripe('pk_test_...');
const appearance = { theme: 'stripe' };
// initCheckout() is synchronous; loadActions() is async
const checkout = stripe.initCheckout({clientSecret}); const checkout = stripe.initCheckout({clientSecret});
const loadActionsResult = await checkout.loadActions(); const loadActionsResult = await checkout.loadActions();
if (loadActionsResult.type === 'success') { if (loadActionsResult.type === 'success') {
const session = loadActionsResult.actions.getSession();
const {actions} = loadActionsResult; const {actions} = loadActionsResult;
const session = actions.getSession();
const button = document.getElementById('pay-button'); const button = document.getElementById('pay-button');
const checkoutContainer = document.getElementById('checkout-container'); const checkoutContainer = document.getElementById('checkout-container');
@@ -175,7 +174,7 @@ if (loadActionsResult.type === 'success') {
const emailErrors = document.getElementById('email-errors'); const emailErrors = document.getElementById('email-errors');
const errors = document.getElementById('confirm-errors'); const errors = document.getElementById('confirm-errors');
// Display total to user // Display grand total (amount in smallest currency unit, e.g. cents)
checkoutContainer.append(`Total: ${session.total.total.amount}`); checkoutContainer.append(`Total: ${session.total.total.amount}`);
// Mount Payment Element // Mount Payment Element
@@ -432,7 +431,7 @@ def test_payment_flow():
amount=1000, amount=1000,
currency='usd', currency='usd',
customer=customer.id, customer=customer.id,
payment_method_types=['card'] automatic_payment_methods={'enabled': True},
) )
# Confirm with test card # Confirm with test card