9

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

我已经初始化了按钮,它出现在我的 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");
}
4

1 回答 1

17

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

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

条纹文档:

于 2018-03-29T04:13:50.483 回答