From 841ad2764feeffa3f371246c5b7c3a83689e95a4 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Mon, 17 May 2021 16:01:32 +1000 Subject: [PATCH] Import Stripe customers --- app/PaymentDrivers/Stripe/ImportCustomers.php | 112 ++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 app/PaymentDrivers/Stripe/ImportCustomers.php diff --git a/app/PaymentDrivers/Stripe/ImportCustomers.php b/app/PaymentDrivers/Stripe/ImportCustomers.php new file mode 100644 index 000000000000..48e6fac72ea4 --- /dev/null +++ b/app/PaymentDrivers/Stripe/ImportCustomers.php @@ -0,0 +1,112 @@ +stripe = $stripe; + } + + public function run() + { + + $this->stripe->init(); + + $customers = Customer::all(); + + foreach($customers as $customer) + { + $this->addCustomer($customer); + } + + } + + private function addCustomer(Customer $customer) + { + + $account = $this->company_gateway->company->account; + + $existing_customer = $this->company_gateway + ->client_gateway_tokens() + ->where('gateway_customer_reference', $customer->id) + ->exists(); + + + if($existing_customer) + return + + $client = ClientFactory::create($this->company_gateway->company_id, $this->company_gateway->user_id); + $client->address1 = $customer->address->line1 ?: ''; + $client->address2 = $customer->address->line2 ?: ''; + $client->city = $customer->address->city ?: ''; + $client->state = $customer->address->state ?: ''; + + if($customer->address->country){ + + $country = Country::where('iso_3166_2', $customer->address->country)->first() + + if($country) + $client->country_id = $country->id; + + } + + if($customer->currency) { + + $currency = Currency::where('code', $customer->currency)->first(); + + if($currency){ + + $settings = $client->settings; + $settings->currency_id = (string)$currency->id; + $client->settings = $settings; + + } + + } + + $client->phone = $customer->phone ?: ''; + $client->name = $customer->name ?: ''; + + if(!$account->isPaidHostedClient() && Client::where('company_id', $this->company_gateway->company_id)->count() <= config('ninja.quotas.free.clients')){ + + $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(); + + } + } +}