请求协助在现有支付代码中集成Stripe Sofort即时支付
实现Stripe Sofort即时支付功能的代码修改方案
参考你已有的Stripe银行卡支付(getPaymentMethod === "4")逻辑,以下是为getPaymentMethod === "8"添加Sofort支付流程的具体代码实现,直接插入到你现有代码第100行附近的支付方式判断区块即可:
// 假设这是你现有代码第100行附近的支付判断逻辑 if (getPaymentMethod === "1") { // PayPal支付逻辑 } else if (getPaymentMethod === "4") { // 已有的Stripe银行卡支付逻辑 const stripe = Stripe('你的Stripe公钥'); const elements = stripe.elements(); const cardElement = elements.create('card'); cardElement.mount('#card-element'); const { error } = await stripe.confirmCardPayment(clientSecret, { payment_method: { card: cardElement, billing_details: { name: '用户姓名', email: '用户邮箱' } } }); if (error) { console.error(error.message); } else { if (paymentIntent.status === 'succeeded') { console.log('银行卡支付成功'); } } } // 新增:Stripe Sofort支付逻辑 else if (getPaymentMethod === "8") { const stripe = Stripe('你的Stripe公钥'); try { // 创建Sofort支付方式 const { paymentMethod } = await stripe.createPaymentMethod({ type: 'sofort', sofort: { country: 'DE', // 按需调整为Sofort支持的国家(如DE、AT、BE等) }, billing_details: { name: '用户姓名', email: '用户邮箱' } }); // 确认支付Intent,触发Sofort跳转授权 const { error, paymentIntent } = await stripe.confirmPaymentIntent(clientSecret, { payment_method: paymentMethod.id, return_url: 'https://your-domain.com/payment-complete' // 需在Stripe后台配置合法域名 }); if (error) { console.error('Sofort支付错误:', error.message); } else { // 处理支付状态 if (paymentIntent.status === 'requires_action') { // 跳转至Sofort页面完成用户授权 window.location.href = paymentIntent.next_action.redirect_to_url.url; } else if (paymentIntent.status === 'succeeded') { console.log('Sofort支付成功'); // 跳转至支付成功页面或处理后续业务逻辑 } } } catch (err) { console.error('Sofort支付流程异常:', err); } }
关键注意事项
- 替换
你的Stripe公钥为实际的Stripe发布密钥(可在Stripe后台获取) country参数必须设置为Sofort支持的国家代码,可根据你的业务目标地区调整return_url必须是Stripe后台已添加的合法域名下的地址,否则会触发域名不允许的错误- 务必在后端监听Stripe的
payment_intent.succeeded等webhook事件,以此作为支付成功的最终判定依据,避免仅依赖前端状态判断导致的业务风险
内容的提问来源于stack exchange,提问作者Vlado Pranjic
相关产品推荐
相关产品推荐

