Few days back, I encountered this error “Export transactions require a customer name and address” while integrating Stripe payment gateway with my Laravel application.
I have used the Laravel Cashier(stripe) package to integrate the stripe payment gateway.
In our case, User is the billable model and we have ShippingAddress
model to store the user address. After this, we have SubscriptionController
to create a new subscription for the user and SingleChargeController
to make a direct one time charge for the user.
Now, while making a single charge and while creating a new subscription, I got this error “Export transactions require a customer name and address”.
In the SubscriptionController
while creating a new subscription, we can sync stripe customer details.
protected function subscribe(Request $request, User $user, Plan $planItem) { $user->syncStripeCustomerDetails(); $user->newSubscription('standard', $planItem->price_id) ->create($request->payment_method); }
It basically uses syncStripeCustomerDetails
method from the Laravel\Cashier\Billable trait.
/** * Sync the customer's information to Stripe. * * @return \Stripe\Customer */ public function syncStripeCustomerDetails() { return $this->updateStripeCustomer([ 'name' => $this->stripeName(), 'email' => $this->stripeEmail(), 'phone' => $this->stripePhone(), 'address' => $this->stripeAddress(), ]); }
As we can see in the above code, address is fetched from stripeAddress
method of the Billable trait. I have overridden this method in my Billable model User as below.
public function stripeAddress() { $shippingAddress = $this->shippingAddress; return [ 'line1' => $shippingAddress->line1 ?? ' ', 'postal_code' => $shippingAddress->zip_code ?? ' ', 'city' => $shippingAddress->city ?? ' ', 'state' => $shippingAddress->state ?? ' ', 'country' => $shippingAddress->country ?? ' ', ]; }
Finally our billable model will look like below, although I have a lot of other stuff in this model, but these are useful for this stripe issue.
class User extends Authenticatable implements MustVerifyEmail { public function shippingAddress() { return $this->morphOne(ShippingAddress::class, 'user'); } public function stripeAddress() { $shippingAddress = $this->shippingAddress; return [ 'line1' => $shippingAddress->line1 ?? ' ', 'postal_code' => $shippingAddress->zip_code ?? ' ', 'city' => $shippingAddress->city ?? ' ', 'state' => $shippingAddress->state ?? ' ', 'country' => $shippingAddress->country ?? ' ', ]; } /** * Sync the customer's information to Stripe. * * @return \Stripe\Customer */ public function syncStripeCustomerDetails() { return $this->updateStripeCustomer([ 'name' => $this->stripeName(), 'email' => $this->stripeEmail(), 'phone' => $this->stripePhone(), 'address' => $this->stripeAddress(), ]); } }
While making a single charge with stripe will also throw this error “Export transactions require a customer name and address”. So, below is the solution for this stripe error.
In the SingleChargeController
, I was using the below method to make a charge with stripe. I have passed the customer details and address as a meta in the charge method of Laravel\Cashier\Concerns\PerformsCharges
.
protected function createStripeCharge(User $user, float $amount, string $paymentMethod, string $meta_description) { $meta = $this->stripChargeMeta($user, $meta_description); return $user->charge(($amount*100), $paymentMethod, $meta); } protected function stripChargeMeta(User $user, string $description) { return [ 'description' => $description, 'shipping' => [ 'name' => $user->name, 'address' => $user->stripeAddress(), ] ]; }
Hope you found this tutorial useful, if yes then please share with your colleagues and comment below for more such tutorials.
Today we are going to learn about managing multiple PHP versions on ubuntu with xampp.…
Let's understand about how to use coding to improve your website's SEO. In today’s computerized…
Let's understand the most important linux commands for web developers. Linux, as an open-source and…
Today we are going to discuss top 75+ Laravel interview questions asked by top MNCs.Laravel,…
Today we will discuss about the Mailtrap integration with laravel 10 .Sending and receiving emails…
Today we are going to integrate FCM (Firebase Cloud Messaging) push notifications with ionic application.Firebase…