How can I send an email with Stripe Checkout?
Using Stripe, I want to save the customer's email address from the email they put in Checkout. Sorry, placing stripeEmail
in my file charge.php
returns null
.
How can I get an email back from verification so that I can use it to send a receipt?
Here's my form code:
<script src="https://checkout.stripe.com/v2/checkout.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.js"></script>
<form action="charge.php" method="post">
<input type="hidden" id="amount" name="chargeAmount"/>
<button data-charge-amount="300000" data-charge-name="Name" data-charge-description="Description">Select Pledge Level</button>
<button data-charge-amount="123123" data-charge-name="Name2" data-charge-description="Description2">Donate</button>
</form>
<script>
$('button').click(function(){
var token = function(res){
var $theToken = $('<input type=hidden name=stripeToken />').val(res.id);
$('form').append($theToken).submit();
};
var amount = $(this).data("chargeAmount");
var name = $(this).data("chargeName");
var description = $(this).data("chargeDescription");
$('input#amount').val(amount);
StripeCheckout.open({
key: 'pk_test_xxxxxxxxxxxxxxxxxxxxxxxx',
address: true,
amount: amount,
currency: 'usd',
name: name,
description: description,
panelLabel: 'Pledge',
token: token,
});
return false;
});
</script>
Here is my code charge.php
:
<?php
require_once('./config.php');
$token = $_POST['stripeToken'];
$amount = $_POST['chargeAmount'];
$customer = \Stripe\Customer::create(array(
'email' => $email,
'card' => $token,
));
$charge = \Stripe\Charge::create(array(
'customer' => $customer->id,
'amount' => $amount,
'currency' => 'usd',
));
?>
Here is my code config.php
:
<?php
require_once('./stripe-php-2.1.2/init.php');
$stripe = array(
"secret_key" => "sk_test_xxxxxxxxxxxxxxxxxxxxxxxx",
"publishable_key" => "pk_test_xxxxxxxxxxxxxxxxxxxxxxxx"
);
\Stripe\Stripe::setApiKey($stripe['secret_key']);
?>
Any help would be much appreciated.
THANK!
source to share
The problem is that you are using Custom Checkout , which means that Checkout will not post data to your server automatically, but instead give it in a callback token
. In your case, you only get the token ID, so you don't see it.
Update your code so the callback will token
also retrieve the email and send it to a parameter stripeEmail
:
var token = function(res){
var $theToken = $('<input type="hidden" name="stripeToken" />').val(res.id);
var $theEmail = $('<input type="hidden" name="stripeEmail" />').val(res.email);
$('form').append($theToken).append($theEmail).submit();
};
source to share
I've been trying this problem all night! @Koopajah helped a ton, so here's my whole solution in case anyone else runs into this.
Here's the form:
<form action="/charge.php" method="post">
<input
type="submit"
id="payMe"
class="btn btn-default btn-lg btn-success"
value=" Pay "
data-key="xxxxxxx"
data-amount="199"
data-currency="usd"
data-name="Stuff"
data-description="20 Whozits ($19.99)"
data-image="images/image.jpg"
data-bitcoin="true"
/>
<script src="https://checkout.stripe.com/v2/checkout.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script>
<script>
$(document).ready(function() {
$('#payMe').on('click', function(event) {
event.preventDefault();
var $button = $(this),
$form = $button.parents('form');
var opts = $.extend({}, $button.data(), {
token: function(result) {
var $theToken = $('<input>').attr({ type: 'hidden', name: 'stripeToken', value: result.id })
var $theEmail = $('<input>').attr({ type: 'hidden', name: 'stripeEmail', value: result.email })
$form.append($theToken).append($theEmail).submit();
}
});
StripeCheckout.open(opts);
});
});
</script>
</form>
And here's charge.php:
<?php
require_once('vendor/autoload.php');
$stripe = array(
"secret_key" => "xxxx",
"publishable_key" => "xxxx"
);
\Stripe\Stripe::setApiKey($stripe['secret_key']);
$token = $_POST['stripeToken'];
$email = $_POST['stripeEmail'];
\Stripe\Customer::create(array(
"source" => $token,
"email" => $email,
"description" => "It Worked!"
));
try {
$charge = \Stripe\Charge::create(array(
"amount" => 199, // amount in cents, again
"currency" => "usd",
"source" => $_POST['stripeToken'],
"description" => "Cat Facts"));
} catch(\Stripe\Error\Card $e) {
$error = $e->getMessage();
}
?>
source to share
I had a very similar problem, but I'm using node.js. I modified koopajah's answer and put it in charge.js file
const token = req.body.stripeToken;<br>
const email = req.body.stripeEmail;
and then i used this email variable like so ...
return stripe.charges.create({
// ensures we send a number, and not a string
amount: parseInt(process.env.STRIPE_COST, 10),
currency: process.env.STRIPE_CCY,
source: token,
description: 'My product', // 👈 remember to change this!
receipt_email: email, // that line sends a receipt email to the customer, you can customise that email inside stripe
metadata: {},
});
Hope this is a helpful answer to someone.
source to share