🚀 快速安装
复制以下命令并运行,立即安装此 Skill:
npx @anthropic-ai/skills install wshobson/agents/stripe-integration
💡 提示:需要 Node.js 和 NPM
Stripe 集成
掌握 Stripe 支付处理集成,构建健壮、符合 PCI 标准的支付流程,包括结账、订阅、Webhook 和退款。
何时使用此技能
- 在 Web/移动应用中实现支付处理
- 设置订阅计费系统
- 处理一次性支付和周期性扣款
- 处理退款和争议
- 管理客户支付方式
- 为欧洲支付实现 SCA(强客户认证)
- 使用 Stripe Connect 构建市场支付流程
核心概念
1. 支付流程
结账会话 (Checkout Sessions)
- 推荐用于大多数集成场景
- 支持所有 UI 路径:
- Stripe 托管的结账页面
- 嵌入式结账表单
- 使用
ui_mode='custom'的自定义 UI(支付元素、快速结账元素)
- 提供内置结账功能(订单项、折扣、税费、运费、地址收集、保存的支付方式以及结账生命周期事件)
- 相比 Payment Intents,集成和维护负担更小
支付意向 (Payment Intents) – 定制化控制
- 您需要自行计算包含税费、折扣、订阅和货币转换的最终金额。
- 实现和长期维护负担更复杂
- 需要 Stripe.js 以保证 PCI 合规
设置意向 (Setup Intents) – 保存支付方式
- 收集支付方式但不扣款
- 用于订阅和未来支付
- 需要客户确认
2. Webhooks
关键事件:
payment_intent.succeeded: 支付完成payment_intent.payment_failed: 支付失败customer.subscription.updated: 订阅变更customer.subscription.deleted: 订阅取消charge.refunded: 退款处理完成invoice.payment_succeeded: 订阅支付成功
3. 订阅
组成部分:
- 产品 (Product): 您销售的商品或服务
- 价格 (Price): 定价和计费周期
- 订阅 (Subscription): 客户的周期性支付
- 发票 (Invoice): 每个计费周期生成
4. 客户管理
- 创建和管理客户记录
- 存储多种支付方式
- 追踪客户元数据
- 管理账单详情
快速开始
import stripe
stripe.api_key = "sk_test_..."
# 创建一个结账会话
session = stripe.checkout.Session.create(
line_items=[{
'price_data': {
'currency': 'usd',
'product_data': {
'name': 'Premium Subscription',
},
'unit_amount': 2000, # $20.00
'recurring': {
'interval': 'month',
},
},
'quantity': 1,
}],
mode='subscription',
success_url='https://yourdomain.com/success?session_id={CHECKOUT_SESSION_ID}',
cancel_url='https://yourdomain.com/cancel'
)
# 将用户重定向到 session.url
print(session.url)
支付实现模式
模式 1: 一次性支付 (托管结账)
def create_checkout_session(amount, currency='usd'):
"""创建一个一次性支付的结账会话。"""
try:
session = stripe.checkout.Session.create(
line_items=[{
'price_data': {
'currency': currency,
'product_data': {
'name': 'Blue T-shirt',
'images': ['https://example.com/product.jpg'],
},
'unit_amount': amount, # 金额单位是分
},
'quantity': 1,
}],
mode='payment',
success_url='https://yourdomain.com/success?session_id={CHECKOUT_SESSION_ID}',
cancel_url='https://yourdomain.com/cancel',
metadata={
'order_id': 'order_123',
'user_id': 'user_456'
}
)
return session
except stripe.error.StripeError as e:
# 处理错误
print(f"Stripe 错误: {e.user_message}")
raise
模式 2: 带结账会话的 Elements
def create_checkout_session_for_elements(amount, currency='usd'):
"""创建一个为支付元素配置的结账会话。"""
session = stripe.checkout.Session.create(
mode='payment',
ui_mode='custom',
line_items=[{
'price_data': {
'currency': currency,
'product_data': {'name': 'Blue T-shirt'},
'unit_amount': amount,
},
'quantity': 1,
}],
return_url='https://yourdomain.com/complete?session_id={CHECKOUT_SESSION_ID}'
)
return session.client_secret # 发送到前端
const stripe = Stripe("pk_test_...");
const appearance = { theme: "stripe" };
const checkout = stripe.initCheckout({
clientSecret,
elementsOptions: { appearance },
});
const loadActionsResult = await checkout.loadActions();
if (loadActionsResult.type === "success") {
const { actions } = loadActionsResult;
const session = actions.getSession();
const button = document.getElementById("pay-button");
const checkoutContainer = document.getElementById("checkout-container");
const emailInput = document.getElementById("email");
const emailErrors = document.getElementById("email-errors");
const errors = document.getElementById("confirm-errors");
// 显示代表总金额的格式化字符串
checkoutContainer.append(`总金额: ${session.total.total.amount}`);
// 挂载支付元素
const paymentElement = checkout.createPaymentElement();
paymentElement.mount("#payment-element");
// 存储邮箱以便提交
emailInput.addEventListener("blur", () => {
actions.updateEmail(emailInput.value).then((result) => {
if (result.error) emailErrors.textContent = result.error.message;
});
});
// 处理表单提交
button.addEventListener("click", () => {
actions.confirm().then((result) => {
if (result.type === "error") errors.textContent = result.error.message;
});
});
}
模式 3: 带 Payment Intents 的 Elements
模式 2(带结账会话的 Elements)是 Stripe 推荐的方法,但您也可以使用 Payment Intents 作为替代方案。
def create_payment_intent(amount, currency='usd', customer_id=None):
"""为定制化结账 UI(使用支付元素)创建一个支付意向。"""
intent = stripe.PaymentIntent.create(
amount=amount,
currency=currency,
customer=customer_id,
automatic_payment_methods={
'enabled': True,
},
metadata={
'integration_check': 'accept_a_payment'
}
)
return intent.client_secret # 发送到前端
// 挂载支付元素并通过 Payment Intents 确认
const stripe = Stripe("pk_test_...");
const appearance = { theme: "stripe" };
const elements = stripe.elements({ appearance, 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;
}
});
模式 4: 创建订阅
def create_subscription(customer_id, price_id):
"""为客户创建订阅。"""
try:
subscription = stripe.Subscription.create(
customer=customer_id,
items=[{'price': price_id}],
payment_behavior='default_incomplete',
payment_settings={'save_default_payment_method': 'on_subscription'},
expand=['latest_invoice.payment_intent'],
)
return {
'subscription_id': subscription.id,
'client_secret': subscription.latest_invoice.payment_intent.client_secret
}
except stripe.error.StripeError as e:
print(f"订阅创建失败: {e}")
raise
模式 5: 客户门户
def create_customer_portal_session(customer_id):
"""创建一个门户会话,供客户管理订阅。"""
session = stripe.billing_portal.Session.create(
customer=customer_id,
return_url='https://yourdomain.com/account',
)
return session.url # 将客户重定向到此 URL
Webhook 处理
安全的 Webhook 端点
from flask import Flask, request
import stripe
app = Flask(__name__)
endpoint_secret = 'whsec_...'
@app.route('/webhook', methods=['POST'])
def webhook():
payload = request.data
sig_header = request.headers.get('Stripe-Signature')
try:
event = stripe.Webhook.construct_event(
payload, sig_header, endpoint_secret
)
except ValueError:
# 无效的 payload
return '无效的 payload', 400
except stripe.error.SignatureVerificationError:
# 无效的签名
return '无效的签名', 400
# 处理事件
if event['type'] == 'payment_intent.succeeded':
payment_intent = event['data']['object']
handle_successful_payment(payment_intent)
elif event['type'] == 'payment_intent.payment_failed':
payment_intent = event['data']['object']
handle_failed_payment(payment_intent)
elif event['type'] == 'customer.subscription.deleted':
subscription = event['data']['object']
handle_subscription_canceled(subscription)
return '成功', 200
def handle_successful_payment(payment_intent):
"""处理支付成功。"""
customer_id = payment_intent.get('customer')
amount = payment_intent['amount']
metadata = payment_intent.get('metadata', {})
# 更新您的数据库
# 发送确认邮件
# 完成订单
print(f"支付成功: {payment_intent['id']}")
def handle_failed_payment(payment_intent):
"""处理支付失败。"""
error = payment_intent.get('last_payment_error', {})
print(f"支付失败: {error.get('message')}")
# 通知客户
# 更新订单状态
def handle_subscription_canceled(subscription):
"""处理订阅取消。"""
customer_id = subscription['customer']
# 更新用户访问权限
# 发送取消邮件
print(f"订阅已取消: {subscription['id']}")
Webhook 最佳实践
import hashlib
import hmac
def verify_webhook_signature(payload, signature, secret):
"""手动验证 webhook 签名。"""
expected_sig = hmac.new(
secret.encode('utf-8'),
payload,
hashlib.sha256
).hexdigest()
return hmac.compare_digest(signature, expected_sig)
def handle_webhook_idempotently(event_id, handler):
"""确保 webhook 仅被处理一次(幂等性)。"""
# 检查事件是否已处理
if is_event_processed(event_id):
return
# 处理事件
try:
handler()
mark_event_processed(event_id)
except Exception as e:
log_error(e)
# Stripe 会重试失败的 webhook
raise
客户管理
def create_customer(email, name, payment_method_id=None):
"""创建一个 Stripe 客户。"""
customer = stripe.Customer.create(
email=email,
name=name,
payment_method=payment_method_id,
invoice_settings={
'default_payment_method': payment_method_id
} if payment_method_id else None,
metadata={
'user_id': '12345'
}
)
return customer
def attach_payment_method(customer_id, payment_method_id):
"""将支付方式附加到客户。"""
stripe.PaymentMethod.attach(
payment_method_id,
customer=customer_id
)
# 设置为默认支付方式
stripe.Customer.modify(
customer_id,
invoice_settings={
'default_payment_method': payment_method_id
}
)
def list_customer_payment_methods(customer_id):
"""列出客户的所有支付方式。"""
payment_methods = stripe.PaymentMethod.list(
customer=customer_id,
type='card'
)
return payment_methods.data
退款处理
def create_refund(payment_intent_id, amount=None, reason=None):
"""创建退款。"""
refund_params = {
'payment_intent': payment_intent_id
}
if amount:
refund_params['amount'] = amount # 部分退款
if reason:
refund_params['reason'] = reason # 原因:'duplicate', 'fraudulent', 'requested_by_customer'
refund = stripe.Refund.create(**refund_params)
return refund
def handle_dispute(charge_id, evidence):
"""使用证据更新争议。"""
stripe.Dispute.modify(
charge_id,
evidence={
'customer_name': evidence.get('customer_name'),
'customer_email_address': evidence.get('customer_email'),
'shipping_documentation': evidence.get('shipping_proof'),
'customer_communication': evidence.get('communication'),
}
)
测试
# 使用测试模式密钥
stripe.api_key = "sk_test_..."
# 测试卡号
TEST_CARDS = {
'success': '4242424242424242',
'declined': '4000000000000002',
'3d_secure': '4000002500003155',
'insufficient_funds': '4000000000009995'
}
def test_payment_flow():
"""测试完整的支付流程。"""
# 创建测试客户
customer = stripe.Customer.create(
email="test@example.com"
)
# 创建支付意向
intent = stripe.PaymentIntent.create(
amount=1000,
automatic_payment_methods={
'enabled': True
},
currency='usd',
customer=customer.id
)
# 使用测试卡确认
confirmed = stripe.PaymentIntent.confirm(
intent.id,
payment_method='pm_card_visa' # 测试支付方式
)
assert confirmed.status == 'succeeded'
📄 原始文档
完整文档(英文):
https://skills.sh/wshobson/agents/stripe-integration
💡 提示:点击上方链接查看 skills.sh 原始英文文档,方便对照翻译。
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

评论(0)