问题
I am trying to retrieve various pieces of data from a Stripe Checkout form submission, where I am just using the Stripe Checkout code provided in my Stripe Dashboard.
In my checkout_submission_completed
event I have webhook where I am trying to retrieve email
, name
so that after successful purchase I can take other actions.
It is surprisingly difficult.
Here is how I can retrieve email (where payload
is the response my webhook receives):
$cust = $payload['data']['object']['customer'];
$custdata = \Stripe\Customer::retrieve($cust);
$email=$custdata->email;
Ok, not a big deal.
How about Name? Well, here is where things get really fun. After hitting the form payment submit button Stripe creates a customer, completes a successful charge. But in the Customer object there is no name. Yes, no name. During a chat today with Stripe they had no explanation and said they would look into it more.
Turns out the only place where the name entered on the form shows up in a Stripe object is the Payment Details Object within the Payment Intent object.
I am serious. So here is how I am getting the name (using cust
from previous code:
$piid = $cust = $payload['data']['object']['payment_intent'];
$pi = \Stripe\PaymentIntent::retrieve($piid);
$name = $pi['charges']['data'][0]['billing_details']['name'];
Is there a better way for me to do this?
thanks, Brian
回答1:
I think the idea is that the name collected is a cardholder name and is associated with the card [0] , not the Customer. A Customer might end up with multiple cards or other payment methods, and they might reasonably all have different cardholder names. So that information isn't transposed up to the Customer by default.
Your approach looks generally good — I would personally use the expand feature [1] of the API so you can skip a bunch of API calls by retrieving the full context of the Checkout Session and its payment and customer in one call from the webhook handler.
$session = \Stripe\Checkout\Session::retrieve(
$payload['data']['object']['id'],
["expand" => ["payment_intent", "customer"]]);
$cardholderName = $session['payment_intent']['charges']['data'][0]['billing_details']['name'];
\Stripe\Customer::update($session['customer'].id,
["name" => $cardholderName]);
[0] - https://stripe.com/docs/api/payment_methods/object?lang=php#payment_method_object-billing_details-name
[1] - https://stripe.com/docs/api/expanding_objects?lang=php
来源:https://stackoverflow.com/questions/57810851/why-doesnt-stripe-checkout-add-customer-name-to-customer-record