Customer data not being passed over to stripe
iamtompickering opened this issue · 5 comments
Description
Customer's name, billing address, and shipping address are not being transmitted to Stripe.
I cannot see any of this information in the transaction logs, so it's like the data just isn't being considered for passing over to Stripe. Is there any way of resolving this, so I can see all of the customers data in Stripe as will as in the CMS.
Additional info
- Craft CMS version: 3.8.13
- Stripe for Craft Commerce version: 2.4.4.1
- PHP version: 8.0.28
I update the Stripe Customer when a new user registers or when the billing address has been updated, using the following code:
use craft\commerce\models\Customer;
use craft\commerce\stripe\Plugin as StripePlugin;
use craft\helpers\App;
use Stripe\Customer as StripeCustomer;
use Stripe\Stripe;
...
public static function updateCustomerAddressInStripe(Customer $customer)
{
$address = $customer->getPrimaryBillingAddress();
if (!$address) {
return;
}
$user = $customer->getUser();
$stripeCustomer = StripePlugin::getInstance()->getCustomers()->getCustomer(1, $user);
$customerData = [
'name' => $user->getFullName(),
'email' => $user->email,
'description' => $address->businessName ?: '',
'address' => [
'city' => $address->city,
'country' => $address->getCountryIso(),
'line1' => $address->address1,
'line2' => $address->address2,
'postal_code' => $address->zipCode
]
];
Stripe::setApiKey(App::parseEnv('$STRIPE_SK'));
StripeCustomer::update($stripeCustomer->reference, $customerData);
}
Something similar should work for you, too.
@Saboteur777 thanks for this, really helpful!
My only question is how would guest customers be handled with the above code? I have run some tests and when checking out as a guest, the $user
returns null
.
I've had a look in the service getCustomer
and it looks like it should handle creating new users, no? But it just doesn't seem to be firing with the user variable is null
. Any ideas why?
In my case there are just subscriptions and registered users only, that's why $customer->getUser()
works.
In case of guest orders, a User
is not created for an Order (but a Customer
is), so I would listen to some other event, e.g. craft\commerce\elements\Order::EVENT_AFTER_SAVE
, so anytime the Order is being updated (because the address associated to it is updated), Stripe Customer could be updated, too.
In this case you could access Customer with $event->sender->customer
.
Any update on this with a guest customer? Much appreciated.
@eshopsoftware Have you tried mapping the Customer to the Stripe Customer?