Reputation: 181
I am trying to pass the email from Stripe Checkout to the charge.php page that will then send it to the Dashboard so users will get a confirmation email.
Here is my working code:
<input class="form-control" type="number" id="custom-donation-amount"
placeholder="50.00" min="0" value="50" step="10.00"/>
<script src="https://checkout.stripe.com/checkout.js"></script>
<button id="customButton" class="pay-button">
<h4 class="donate-text button">Donate by <img src="http://#.com/testing/credit-card.png" ></h4>
</button>
<script>
var handler = StripeCheckout.configure({
key: 'pk_test_*************',
image: 'assets/img/#.png',
locale: 'auto',
token: function(token) {
//this is where I am sending the custom amount, works great. Thought I could do the same with email.
$.post( "charge.php", { stripeToken: token.id, amount:$("#custom-donation-amount").val()})
// log when it works
.done(function( data ) {
console.log( "Card charged: " + data );
});
}
});
$('#customButton').on('click', function(e) {
// Open Checkout with further options
var amount = $("#custom-donation-amount").val() * 100;
handler.open({
name: '*********',
description: '*********',
amount: amount
});
e.preventDefault();
});
// Close Checkout on page navigation
$(window).on('popstate', function() {
handler.close();
});
</script>
Charge.php
<?php
require_once('init.php');
\Stripe\Stripe::setApiKey("sk_test_***********");
$token = $_POST['stripeToken'];
$email = $_POST['stripeEmail'];
$amount = $_POST['amount'];
$finalamount = $amount * 100;
// Create a Customer
$customer = \Stripe\Customer::create(array(
"source" => $token,
"description" => "Here's the description",
"email" => $email)
);
// Charge the Customer instead of the card
\Stripe\Charge::create(array(
"amount" => $finalamount, // amount in cents, again
"currency" => "usd",
"customer" => $customer->id)
);
// YOUR CODE: Save the customer ID and other info in a database for later!
// YOUR CODE: When it's time to charge the customer again, retrieve the customer ID!
\Stripe\Charge::create(array(
"amount" => 1500, // $15.00 this time
"currency" => "usd",
"customer" => $customerId
));
?>
How do I go about grabbing the email address from the Stripe Checkout page? I thought it would work nicely using "stripeEmail" but it turns out that isn't the case...
Upvotes: 1
Views: 997
Reputation: 333
The 'token' contains the email, replace this:
$.post( "charge.php", { stripeToken: token.id, amount:$("#custom-donation-amount").val()})
by this
$.post( "charge.php", { stripeToken: token.id, amount:$("#custom-donation-amount").val(),stripeEmail:token.email})
Upvotes: 2