Pre-populating an email address in Stripe
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

This is a follow-up to my previous question (https://bountify.co/stripe-integration-using-javascript-and-php).

Stripe allows its email field to be pre-populated. Is is explained here: http://stackoverflow.com/questions/38079040/pre-populating-an-email-address-in-stripe-checkout?rq=1

I have the email field set up already:

function openCheckout(description, amount)
{
handler.open({
name: 'Dr. Muscle',
description: description,
amount: amount,
allowRememberMe: false,
email: '',
});
}

But I don't know how to grab the email address from the URL of the page and insert it. Stack Overflow mentions to "use jQuery to dynamically populate the variable". I have no clue how to do that.

So let's say I have http://drmuscleapp.com/subscribe_stripedebug_custom_test_2016-12-09.html?email=myemail@gmail.com. How do I get "myemail@gmail.com" in the email field?

Please provide code.

awarded to iamkdev
Tags
stripe

Crowdsource coding tasks.

1 Solution

Winning solution

function getParameterByName(name, url) {
    if (!url) {
      url = window.location.href;
    }
    name = name.replace(/[\[\]]/g, "\\$&");
    var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
        results = regex.exec(url);
    if (!results) return null;
    if (!results[2]) return '';
    return decodeURIComponent(results[2].replace(/\+/g, " "));
}

var email = getParameterByName('email');

function openCheckout(description, amount)
{
 handler.open({
 name: 'Dr. Muscle',
 description: description,
 amount: amount,
 allowRememberMe: false,
 email: email,
});

}

Thank you!
EtienneJ 11 months ago