Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have the button working using test data, but there is a form that collects the amount and sets the label with a dropdown. I need to update the payment request button with the form data just before submitting.

I have the button initialized and it appears on my Android device. I call initPaymentRequest when the document is ready.

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");
}
like image 337
dawoodman71 Avatar asked Nov 30 '17 16:11

dawoodman71


People also ask

Can you request money through stripe?

Use the Payment Request Button with Stripe Connect You can use the Stripe API for this, using your platform's secret key to authenticate the request, and setting the Stripe-Account header to your connected account's Stripe ID, as described in Making API calls for connected accounts.

What is a stripe button?

With the new Stripe Button widget you can integrate your Elementor website with your Stripe account, enabling you to sell a single item or several items, and accept payments seamlessly, without creating an entire online store.

How does Stripe JS work?

Well, Stripe. js is a JavaScript library which you can wire into your checkout form to handle the credit card information. When a user signs up using the checkout form, it sends the credit card information directly from the user's browser to Stripe's servers.


1 Answers

Just try this instead of function updatePaymentRequest():

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

Stripe docs:

  • paymentRequest.update(options) - Stripe.js Reference | Stripe
like image 98
Vidhya Avatar answered Oct 09 '22 07:10

Vidhya