使用 Stripe 的 PaymentRequestButton 时,如何在提交令牌请求之前更改标签和总数?

When using Stripe's PaymentRequestButton how can I change the label and total before submitting the token request?

我有一个使用测试数据的按钮,但有一个表单可以收集金额并使用下拉菜单设置标签。我需要在提交之前用表单数据更新付款请求按钮。

我已经初始化了按钮,它出现在我的 Android 设备上。我在文件准备好后调用 initPaymentRequest。

function initPaymentRequest(){
    paymentRequest = stripe.paymentRequest({
        country: 'US',
        currency: 'usd',
        total: {
            label: 'Demo total',
            amount: 1000,
        },
    });
    prButton = elements.create('paymentRequestButton', {
        paymentRequest: paymentRequest,
    });

    // Check the availability of the Payment Request API first.
    paymentRequest.canMakePayment().then(function(result) {
        if (result) {
            log("Payment Request Available");
            $(".ux-submit, #payment-request-button").addClass("col-xs-6");
            prButton.mount('#payment-request-button');
        } else {
            log("Payment Request NOT Available");
            $(".ux-submit, #payment-request-button").addClass("col-xs-6");
        }
    });

    paymentRequest.on('click', updatePaymentRequest);
    paymentRequest.on('token', function(ev) {
        // Send the token to your server to charge it!
        fetch('/charges', {
            method: 'POST',
            body: JSON.stringify({token: ev.token.id}),
        })
            .then(function(response) {
                if (response.ok) {
                    // Report to the browser that the payment was successful, prompting
                    // it to close the browser payment interface.
                    ev.complete('success');
                    process_form(ev);
                } else {
                    // Report to the browser that the payment failed, prompting it to
                    // re-show the payment interface, or show an error message and close
                    // the payment interface.
                    ev.complete('fail');
                }
            });
    });
}

function updatePaymentRequest(){;

    paymentRequest = stripe.paymentRequest({
        country: 'US',
        currency: 'usd',
        total: {
            label: $("select[name='charge_label'] option:selected").text(),
            amount: $("#charge-amount").val()*100,
        },
    });

    prButton = elements.create('paymentRequestButton', {
        paymentRequest: paymentRequest,
    });

    $("#payment-request-button").append("<br>update");
}

试试这个而不是函数 updatePaymentRequest():

paymentRequestElement.on('click', function(ev) {
  paymentRequest.update({
    total: {
      label: $("select[name='charge_label'] option:selected").text(),
      amount: $("#charge-amount").val()*100,
    },
  })
})

条纹文档: