Reputation: 909
Using Stripe Checkout, I have a button, which directs the user to the pre-made Stripe checkout page. The user fills out their payment info, and submits. Once the payment has succeeded, they are redirected to a URL on the originating site. (Something like www.example.com/success).
On the success page that they are redirected to, I would then like to write a custom PHP script that creates a user in a database, which would give them access to a members only area of the site. In order to create that user however, I need the email address and name that they entered during the checkout process.
Is it possible to gather this information, from the success page they are redirected to? Or to put it another way, does Stripe send any information to the success page, that you can grab with JS or PHP?
Upvotes: 1
Views: 2478
Reputation: 983
Yes, You can access the name and email of the customer after the customer gets redirected to a success page.
All you need to do is to make a POST
request to an API endpoint, as shown below, when the success page renders on the screen.
app.post('/order/success', async (req, res) => {
const session = await stripe.checkout.sessions.retrieve(req.query.session_id);
const customer = await stripe.customers.retrieve(session.customer);
res.send(customer);
});
The route handler function sends a customer object to the client, from which you can access the email and name.
You can check for more details in the following link:
https://stripe.com/docs/payments/checkout/custom-success-page
Upvotes: 1
Reputation: 574
The Stripe Checkout modal is all javascript and will only create a token, however to actually capture the payment your backend needs to run the Charge command. For example:
$charge = \Stripe\Charge::create([
'amount' => 2000,
'currency' => 'usd',
'source' => $token, // token from stripe checkout
'description' => 'Example charge',
]);
Then if you want to capture the email and name you can do the following:
$name = $charge->billing_details->name
$email = $charge->billing_details->email
Here is what the charge object looks like:
{
"id": "ch_1FoEV02eZvKYlo2CrhIIb3hh",
"object": "charge",
"amount": 100,
"amount_refunded": 0,
"application": null,
"application_fee": null,
"application_fee_amount": null,
"balance_transaction": "txn_19XJJ02eZvKYlo2ClwuJ1rbA",
"billing_details": {
"address": {
"city": null,
"country": null,
"line1": null,
"line2": null,
"postal_code": null,
"state": null
},
"email": null,
"name": null,
"phone": null
},
"captured": false,
"created": 1576006558,
"currency": "usd",
"customer": null,
"description": "My First Test Charge (created for API docs)",
"dispute": null,
"disputed": false,
"failure_code": null,
"failure_message": null,
"fraud_details": {},
"invoice": null,
"livemode": false,
"metadata": {},
"on_behalf_of": null,
"order": null,
"outcome": null,
"paid": true,
"payment_intent": null,
"payment_method": "card_1FoEUk2eZvKYlo2CH4xOI0A6",
"payment_method_details": {
"card": {
"brand": "visa",
"checks": {
"address_line1_check": null,
"address_postal_code_check": null,
"cvc_check": null
},
"country": "US",
"exp_month": 8,
"exp_year": 2020,
"fingerprint": "Xt5EWLLDS7FJjR1c",
"funding": "credit",
"installments": null,
"last4": "4242",
"network": "visa",
"three_d_secure": null,
"wallet": null
},
"type": "card"
},
"receipt_email": null,
"receipt_number": null,
"receipt_url": "https://pay.stripe.com/receipts/acct_1032D82eZvKYlo2C/ch_1FoEV02eZvKYlo2CrhIIb3hh/rcpt_GKuJm4SIx4vClE2bI43Nq8r6ZvxTtT3",
"refunded": false,
"refunds": {
"object": "list",
"data": [],
"has_more": false,
"url": "/v1/charges/ch_1FoEV02eZvKYlo2CrhIIb3hh/refunds"
},
"review": null,
"shipping": null,
"source_transfer": null,
"statement_descriptor": null,
"statement_descriptor_suffix": null,
"status": "succeeded",
"transfer_data": null,
"transfer_group": null
}
Learn more here: https://stripe.com/docs/api/charges/object
Upvotes: 1