Adyen UPI客户端集成:如何获取支付成功/失败状态
如何在Adyen UPI支付后判断状态并仅在成功时跳转?
针对你的问题,核心问题是**onAdditionalDetails并非用来获取最终支付状态的回调**,它的作用是处理支付流程中需要额外验证的步骤(比如UPI支付后的跳转回调)。要判断最终支付状态,你需要通过Adyen的/payments/details API提交额外信息,拿到结果后再决定是否跳转。
具体操作步骤:
- 不要在
onAdditionalDetails里直接调用createFromAction跳转,先从state中提取需要的details数据 - 调用你的后端接口,由后端将这些数据提交给Adyen的
/payments/detailsAPI - 根据Adyen返回的
resultCode判断支付状态:- UPI支付成功的状态通常是
Authorised或Received - 仅当状态为成功时,再执行跳转操作
- UPI支付成功的状态通常是
修改后的代码示例:
async initAdyen_newurl() { let config = null; config = { ...this.config.adyenConfig, onPaymentCompleted: (result, component) => { console.info("onPaymentCompleted"); console.info(result, component); }, onError: (error, component) => { console.error("onError"); console.error(error.name, error.message, error.stack, component); }, onAdditionalDetails: async (state, component) => { // 从state中提取需提交给Adyen的核心数据 const { details, paymentData } = state.data; try { // 调用后端接口提交支付详情 const response = await fetch('/your-backend/adyen-payment-details', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ details, paymentData }) }); const adyenResult = await response.json(); // 判断支付状态,仅成功时执行跳转 if (adyenResult.resultCode === 'Authorised' || adyenResult.resultCode === 'Received') { const actionUrl = "your-success-page-url"; const obj = { paymentMethodType: component.props.paymentMethodType, url: actionUrl, method: "post", type: "redirect", paymentData: component.props.paymentData }; component.props.createFromAction(obj, {}).mount("#id"); } else { // 支付失败,执行失败逻辑(如提示用户) console.error('支付失败,状态码:', adyenResult.resultCode); } } catch (error) { console.error('提交支付详情失败:', error); } }, }; AdyenCheckout(config) .then((checkout) => { // init stuff }) .catch((error) => { console.error(`url failure ${error.message}`); }); },
关键说明:
state.data包含Adyen所需的details(如UPI支付后的跳转结果)和paymentData,必须提交给/payments/detailsAPI才能获取最终支付状态- 必须通过后端调用Adyen的API,禁止前端直接调用(避免泄露API密钥)
- UPI支付的成功状态以Adyen返回的
resultCode为准,核心成功状态为Authorised(已授权)和Received(已收到支付请求)
内容的提问来源于stack exchange,提问作者maname
相关产品推荐
相关产品推荐

