Joel
Joel

Reputation: 1639

Pre-populating an email address in Stripe / Checkout

I am using Stripe / Checkout on a site in order to accept payments. Stripe is fairly simple to use this way, as they give the following script to embed in web pages:

<form id="monthly" action="/subscribe" method="POST">
  <script
    src="https://checkout.stripe.com/checkout.js" class="stripe-button"
    data-key="pk_test_xxxxxxxxx"
    data-image="http://mywebsite.com/assets/img/logo_raw.png"
    data-name="My Website"
    data-description="Monthly Subscription"
    data-amount="6900"
    data-email="[email protected]"
    data-allow-remember-me=false
    data-label="Subscribe" >
  </script>
</form> 

What I would like to be able to do is pre-populate the email address value (data-email="[email protected]") with a variable that I have in my custom.js jQuery script. I am proficient with jQuery, but can I modify this embedded script with jQuery?

Upvotes: 3

Views: 4739

Answers (2)

Haris ur Rehman
Haris ur Rehman

Reputation: 2663

data-email="[email protected]"

as mentioned in the question, works nowadays.

enter image description here

Upvotes: 1

Ywain
Ywain

Reputation: 17503

You need to use the custom integration, and do something like this:

<script src="https://checkout.stripe.com/checkout.js"></script>

<button id="customButton">Purchase</button>

<script>
  var email = "[email protected]"; // or use jQuery to dynamically populate the variable

  var handler = StripeCheckout.configure({
    key: 'pk_test_...',
    image: 'https://stripe.com/img/documentation/checkout/marketplace.png',
    locale: 'auto',
    token: function(token) {
      // You can access the token ID with `token.id`.
      // Get the token ID to your server-side code for use.
    }
  });

  $('#customButton').on('click', function(e) {
    // Open Checkout with further options:
    handler.open({
      name: 'Stripe.com',
      description: '2 widgets',
      amount: 2000,
      email: email
    });
    e.preventDefault();
  });

  // Close Checkout on page navigation:
  $(window).on('popstate', function() {
    handler.close();
  });
</script>

Upvotes: 7

Related Questions