Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Customer data not being passed over to stripe #244

Open
iamtompickering opened this issue Aug 16, 2023 · 5 comments
Open

Customer data not being passed over to stripe #244

iamtompickering opened this issue Aug 16, 2023 · 5 comments
Labels

Comments

@iamtompickering
Copy link

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

  • Craft CMS version: 3.8.13
  • Stripe for Craft Commerce version: 2.4.4.1
  • PHP version: 8.0.28
@Saboteur777
Copy link

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
Copy link
Author

@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
Copy link

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
Copy link

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

@Saboteur777
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants