From 8c764a1716eab55c19497f99a0166d46d37143e2 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 19 May 2021 14:22:22 +1000 Subject: [PATCH 1/2] Fixes for importing customers into Stripe. --- app/PaymentDrivers/Stripe/ImportCustomers.php | 26 +++++++++++-------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/app/PaymentDrivers/Stripe/ImportCustomers.php b/app/PaymentDrivers/Stripe/ImportCustomers.php index 05a3e9f59ae1..641bd582a34d 100644 --- a/app/PaymentDrivers/Stripe/ImportCustomers.php +++ b/app/PaymentDrivers/Stripe/ImportCustomers.php @@ -42,7 +42,7 @@ class ImportCustomers $this->stripe->init(); - $customers = Customer::all(); + $customers = Customer::all([], $this->stripe->stripe_connect_auth); foreach($customers as $customer) { @@ -62,7 +62,7 @@ class ImportCustomers if(!$account->isPaidHostedClient() && Client::where('company_id', $this->stripe->company_gateway->company_id)->count() > config('ninja.quotas.free.clients')) return; - nlog("search Stripe for {$custom->id}"); + nlog("search Stripe for {$customer->id}"); $existing_customer = $this->stripe ->company_gateway @@ -70,9 +70,13 @@ class ImportCustomers ->where('gateway_customer_reference', $customer->id) ->exists(); - if($existing_customer) + if($existing_customer){ + nlog("Skipping - Customer exists: {$customer->email}"); return; + } + nlog("inserting a customer"); + nlog($customer); $client = ClientFactory::create($this->stripe->company_gateway->company_id, $this->stripe->company_gateway->user_id); if(property_exists($customer, 'address')) @@ -107,16 +111,16 @@ class ImportCustomers } - $client->name = property_exists($customer, 'name') ? $customer->name : ''; + $client->name = property_exists($customer, 'name') ? $customer->name : $customer->email; - $client->save(); + $client->save(); - $contact = ClientContactFactory::create($client->company_id, $client->user_id); - $contact->client_id = $client->id; - $contact->first_name = $client->name ?: ''; - $contact->phone = $client->phone ?: ''; - $contact->email = $client->email ?: ''; - $contact->save(); + $contact = ClientContactFactory::create($client->company_id, $client->user_id); + $contact->client_id = $client->id; + $contact->first_name = $client->name ?: ''; + $contact->phone = $client->phone ?: ''; + $contact->email = $customer->email ?: ''; + $contact->save(); } } From 2e6896dd58b360ef68befe5ff3dbaaedafaab291 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 19 May 2021 14:27:47 +1000 Subject: [PATCH 2/2] Fixes for importing customers into Stripe. --- app/Http/Controllers/StripeController.php | 6 +++--- app/Models/User.php | 8 +++++--- app/PaymentDrivers/Stripe/ImportCustomers.php | 1 + 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/app/Http/Controllers/StripeController.php b/app/Http/Controllers/StripeController.php index be825544d402..1f4c901d4895 100644 --- a/app/Http/Controllers/StripeController.php +++ b/app/Http/Controllers/StripeController.php @@ -22,8 +22,8 @@ class StripeController extends BaseController { if(auth()->user()->isAdmin()) { - - StripeUpdatePaymentMethods::dispatch(auth()->user()->getCompany()); + + StripeUpdatePaymentMethods::dispatch(auth()->user()->company()); return response()->json(['message' => 'Processing'], 200); @@ -39,7 +39,7 @@ class StripeController extends BaseController if(auth()->user()->isAdmin()) { - ImportStripeCustomers::dispatch(auth()->user()->getCompany()); + ImportStripeCustomers::dispatch(auth()->user()->company()); return response()->json(['message' => 'Processing'], 200); diff --git a/app/Models/User.php b/app/Models/User.php index 9c785ea117b1..aabe8564d06d 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -169,15 +169,17 @@ class User extends Authenticatable implements MustVerifyEmail */ public function getCompany() { - if ($this->company) { - return $this->company; - } if (request()->header('X-API-TOKEN')) { $company_token = CompanyToken::with(['company'])->whereRaw('BINARY `token`= ?', [request()->header('X-API-TOKEN')])->first(); return $company_token->company; } + elseif ($this->company){ + + return $this->company; + + } // return false; throw new \Exception('No Company Found'); diff --git a/app/PaymentDrivers/Stripe/ImportCustomers.php b/app/PaymentDrivers/Stripe/ImportCustomers.php index 641bd582a34d..0875dac36ec6 100644 --- a/app/PaymentDrivers/Stripe/ImportCustomers.php +++ b/app/PaymentDrivers/Stripe/ImportCustomers.php @@ -77,6 +77,7 @@ class ImportCustomers nlog("inserting a customer"); nlog($customer); + $client = ClientFactory::create($this->stripe->company_gateway->company_id, $this->stripe->company_gateway->user_id); if(property_exists($customer, 'address'))