Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stripe get customer email address

I'm trying to yet up a subscription using Stripe checkout.js and then using PHP to Subscribe customers. My problem is getting their email address. Can't find anything useful in the docs.

Docs here: https://stripe.com/docs/guides/subscriptions#step-2-subscribe-customers

What I have:

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

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

<script>
var handler = StripeCheckout.configure({
key: 'pk_test_keyhere',
image: '/square-image.png',
token: function(token) {
  // Use the token to create the charge with a server-side script.
  // You can access the token ID with `token.id`
}
});

document.getElementById('customButton').addEventListener('click', function(e) {
// Open Checkout with further options
handler.open({
  name: 'test2',
  description: 'Test Des',
  amount: 0001,
  currency:'GBP'
});
e.preventDefault();
});
</script>

<?php
// Set your API key
 Stripe::setApiKey("sk_test_keyhere");


 $token = $_POST['stripeToken'];

 $customer = Stripe_Customer::create(array(
 "card" => $token,
 "plan" => "test2",
 "email" => $email
 )
);
?>
like image 423
sarah3585 Avatar asked Oct 06 '14 20:10

sarah3585


2 Answers

Adding to David's answer, you can retrieve the email address via PHP like so:

$stripeinfo =Stripe_Token::retrieve($_POST["token"]);
echo '<pre>',print_r($stripeinfo),'</pre>'; //show stripeObject info
$email = $stripeinfo->email;

Adding a small update to this answer for people who are coming across this now.

 $token  = $_POST['stripeToken'];

 $stripeinfo = \Stripe\Token::retrieve($token);
 $email = $stripeinfo->email;

 $customer = \Stripe\Customer::create(array(
   'email' => $email,
   'source'  => $token,
   'description' => 'New Customer'
 ));

Just some small changes I had to make to get my code to work.

like image 175
user875139 Avatar answered Sep 22 '22 07:09

user875139


If you are using Checkout you will be passed a token, using that token you can obtain the email address through an API call: https://stripe.com/docs/api#retrieve_token

Not sure why email isn't listed under there but I do have an email attribute available to me. You can also see the email under the Stripe's logs.

like image 43
David Nguyen Avatar answered Sep 23 '22 07:09

David Nguyen