craftcms / commerce-stripe

Stripe payment gateway for Craft Commerce
https://plugins.craftcms.com/commerce-stripe
MIT License
30 stars 50 forks source link

Customer data not being passed over to stripe #244

Open iamtompickering opened 1 year ago

iamtompickering commented 1 year ago

Description

Customer's name, billing address, and shipping address are not being transmitted to Stripe.

image

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

Saboteur777 commented 1 year ago

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.

iamtompickering commented 1 year ago

@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?

Saboteur777 commented 1 year ago

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.

eshopsoftware commented 9 months ago

Any update on this with a guest customer? Much appreciated.

Saboteur777 commented 9 months ago

@eshopsoftware Have you tried mapping the Customer to the Stripe Customer?