我正在使用 Square 网络支付 SDK 并初始化 Cash App Pay。首先(在初始化阶段),我确定总金额并初始化 Cash App Pay,这将呈现 Cash App Pay 按钮。这就是它的初始化方式:
cashAppInitialize: function(payments) {
var $this = this;
new Promise(function (resolve, reject) {
var amount = $this.getTotalAmount().toString();
var currency = $this.getCurrency();
var paymentRequest = payments.paymentRequest({
countryCode: 'US',
currencyCode: currency,
total: {
amount: amount,
label: 'Total',
pending: false
}
});
var cashAppPay = payments.cashAppPay(paymentRequest, {
redirectURL: window.location.href,
referenceId: ((Math.random() + 1).toString(36).substring(7))+Math.random(),
});
if (cashAppPay) {
resolve(cashAppPay);
} else {
reject(cashAppPay);
}
}).then(function (cashAppPay) {
$this.cashAppPayInstance = cashAppPay;
var buttonOptions = {
shape: 'semiround',
width: 'full',
size: 'medium'
};
if ($$('#cash-app-pay') && $$('#cash-app-pay').length > 0) {
cashAppPay.attach('#cash-app-pay', buttonOptions);
}
try {
if (cashAppPay) {
cashAppPay.addEventListener('ontokenization', (event) => {
var { tokenResult } = event.detail;
var tokenStatus = tokenResult.status;
if (tokenStatus === 'OK') {
$this.attachPaymentMethodToForm(tokenResult.details.method);
$this.attachTokenToForm(tokenResult.token);
$this.resubmitForm();
}
});
}
} catch (e) {
var errorMessage = $this.getErrorMessage('cashApp', e);
$this.handlePaymentMethodAlerts('cashApp', errorMessage);
console.log(e);
}
}).catch(function (error) {
var errorMessage = $this.getErrorMessage('cashApp', error);
$this.handlePaymentMethodAlerts('cashApp', errorMessage);
console.log(error);
});
},
运行此方法后,页面上会呈现Cash App Pay按钮,点击后可以看到支付二维码。与此同时,我正在监听上面 ontokenization 事件的响应,该事件不断发送一个包含总金额信息的请求
我想做的是更新这个“金额”值。但是,当 Cash App Pay 初始化时, paymentRequest 实例中的“update”方法始终返回 false。因此,我无法更新总金额。
我尝试过的另一个解决方法是调用 Square. payments().cashAppPay 中的“destroy”方法。然而,它似乎也没有按预期工作。尽管我从 DOM 中销毁了相关的 Cash App 按钮,但我无法重新初始化它。您可以在下面找到我收到的控制台错误:
这就是我销毁和召回cashAppInitialize的方式。注释掉的代码应该更新金额值:
updatePaymentRequest: function() {
var $this = this;
var amount = paymentTotal.toString();
if ($this.cashAppPayInstance) {
$this.cashAppPayInstance.destroy().then(function() {
$this.cashAppInitialize($this.payments);
});
return;
}
// var params = {
// countryCode: 'US',
// currencyCode: $this.getCurrency(),
// total: {
// amount: amount,
// label: 'Total',
// pending: false
// }
// };
// var updateSuccessful = $this.cashAppPaymentRequest.update(params);
// console.log('sd', updateSuccessful);
},
是否可以以某种方式更新发送到 Cash App Pay 的总金额,并使用更新后的金额更改 QR 代码?如有任何帮助,我们将不胜感激。
这对我有用,尽管更新您的代码以类似于docs
try{
cashAppPay.attach('#cash-app-pay', buttonOptions);
} catch (e) {
console.log("Initialize error",e)
//get the exact message and match it to the if statement
//the exact error i got when trying to update the amount before destroying was as per my .match() in the if statement below
console.log(e.message)
const error_msg = e.message
if(error_msg.match('already rendered')){
await cashAppPay.destroy();
await cashAppPay.attach('#cash-app-pay', buttonOptions);
}
}
基本上,您将在更新金额或其任何所需选项时销毁以前的二维码并附加一个新的二维码。如果您按照文档使用异步等待,我只放置了正确的代码来尝试适合您的代码。 Thenables 往往有太多的书写和嵌套
祝你好运。希望您找到比我更好的解决方案。
购买经过验证的现金应用程序帐户:立即访问 从值得信赖的卖家处购买经过验证的 Cash App 帐户,以确保交易安全可靠。 Cash App 是一种广泛流行的移动支付服务,允许用户以数字方式发送和接收资金。 在此输入图片描述