目前我们的项目使用FastAPI作为后端,Vue作为前端。我们在后端生成一个支付意图ID,前端使用这个ID创建对应的支付。支付成功后,Stripe的webhook将事件发送到后端API。然而,我们在前端支付过程中遇到了一些问题。我将首先发布一些代码:
后端代码:
@router.post("/create_payment_intent")
async def create_payment_intent(
request: Request,
order_id: str = Form(...),
order_amount: int = Form(...),
currency: str = Form("gbp")
):
metadata = {
'user_id': "",
'order_id': order_id
}
# Create payment intent
intent = stripe.PaymentIntent.create(
amount=order_amount,
currency=currency,
receipt_email=user_email,
metadata=metadata,
automatic_payment_methods={'enabled': True}
)
res = {
"client_secret": intent.client_secret,
"payment_intent_id": intent.id
}
return JSONResponse(RestResult(code=Code.TRUE, msg=Message.SUCCESS_CREATE_PAYMENT_INTENT, data=res).__dict__)
前端代码:
import { RequestApi } from '@/api/RequestApi';
import { loadStripe } from '@stripe/stripe-js';
export class StripeUtil {
static pay(params: any) {
return new Promise((resolve, reject) => {
RequestApi.createPayment(params).then(async res => {
const stripe = await loadStripe(import.meta.env.VITE_STRIPE_KEY);
if (!stripe) {
return reject('stripe load failed');
}
const { client_secret, payment_intent_id } = res.data;
const { error } = await stripe.confirmPayment({
clientSecret: client_secret,
confirmParams: {
payment_method: payment_intent_id,
return_url: 'https://example.com/order/123/complete',
},
});
if (error.type) {
showDialog({ message: error.message }).then();
reject(error.message);
}
}).catch(error => {
return reject(error);
});
});
}
}
错误信息:
{
"code": "resource_missing",
"doc_url": "https://stripe.com/docs/error-codes/resource-missing",
"message": "No such PaymentMethod: 'pi_...'; It's possible this PaymentMethod exists on one of your connected accounts, in which case you should retry this request on that connected account. Learn more at https://stripe.com/docs/connect/authentication",
"param": "payment_method",
"payment_intent": {
"id": "pi_...",
"object": "payment_intent",
"amount": 1512,
"amount_details": {
"tip": {}
},
"automatic_payment_methods": {
"allow_redirects": "always",
"enabled": true
},
"canceled_at": null,
"cancellation_reason": null,
"capture_method": "automatic_async",
"client_secret": "pi_..._secret_...",
"confirmation_method": "automatic",
"created": 1731412378,
"currency": "gbp",
"description": null,
"last_payment_error": null,
"livemode": false,
"next_action": null,
"payment_method": null,
"payment_method_configuration_details": null,
"payment_method_types": [
"card"
],
"processing": null,
"receipt_email": "[email protected]",
"setup_future_usage": null,
"shipping": null,
"source": null,
"status": "requires_payment_method"
},
"request_log_url": "https://dashboard.stripe.com/test/logs/req_fsqWt4IIR3rXhM?t=1731412381",
"type": "invalid_request_error"
}
从错误信息可以看出,错误指示requires_ payment_method。但是, payment_method 应该如何创建呢?关键问题是我们不打算使用Stripe的Elements来收集用户卡信息。我们应该如何处理这个问题?我查看了Stripe的文档,但没有找到相关的解决方案,可能是由于我的英语水平有限,缺少一些关键信息。
希望得到一些指导。非常感谢您提前提供的帮助,特别是您在百忙之中抽出时间来帮助我。非常感谢您的支持和见解。非常感谢!
Stripe 建议使用 Checkout、Elements 或我们的移动 SDK,这是一种安全且符合 PCI 要求的方式来收集付款方式详细信息。
如果您仍想自行收集详细信息并通过 API 创建付款方式,则需要确保您符合 SAQ D 要求,并且可以按照本文档中的步骤操作启用对原始卡的访问数据 API。