1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-21 00:41:34 +02:00
invoiceninja/app/PaymentDrivers/Stripe/ImportCustomers.php

223 lines
7.6 KiB
PHP
Raw Normal View History

2021-05-17 08:01:32 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2023-01-28 23:21:40 +01:00
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
2021-05-17 08:01:32 +02:00
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
*/
2021-05-17 08:01:32 +02:00
namespace App\PaymentDrivers\Stripe;
use App\Exceptions\StripeConnectFailure;
2021-05-17 08:01:32 +02:00
use App\Factory\ClientContactFactory;
2021-05-17 14:24:00 +02:00
use App\Factory\ClientFactory;
2021-05-17 08:01:32 +02:00
use App\Models\Client;
use App\Models\ClientGatewayToken;
use App\Models\Country;
use App\Models\Currency;
use App\PaymentDrivers\StripePaymentDriver;
2021-09-02 08:17:46 +02:00
use App\Utils\Ninja;
2021-08-15 07:49:36 +02:00
use App\Utils\Traits\GeneratesCounter;
2021-05-17 08:01:32 +02:00
use App\Utils\Traits\MakesHash;
use Illuminate\Database\QueryException;
2021-05-17 08:01:32 +02:00
use Stripe\Customer;
class ImportCustomers
{
use MakesHash;
2021-08-15 07:49:36 +02:00
use GeneratesCounter;
2021-05-17 08:01:32 +02:00
/** @var StripePaymentDriver */
public $stripe;
private bool $completed = true;
2021-05-21 12:08:48 +02:00
public $update_payment_methods;
2021-05-17 08:01:32 +02:00
public function __construct(StripePaymentDriver $stripe)
{
$this->stripe = $stripe;
}
public function run()
{
$this->stripe->init();
2021-05-21 12:08:48 +02:00
$this->update_payment_methods = new UpdatePaymentMethods($this->stripe);
if (Ninja::isHosted() && strlen($this->stripe->company_gateway->getConfigField('account_id')) < 1) {
2021-09-04 04:09:49 +02:00
throw new StripeConnectFailure('Stripe Connect has not been configured');
}
2022-02-19 11:41:34 +01:00
$starting_after = null;
2021-05-17 08:01:32 +02:00
2022-02-19 11:41:34 +01:00
do {
$customers = Customer::all(['limit' => 100, 'starting_after' => $starting_after], $this->stripe->stripe_connect_auth);
foreach ($customers as $customer) {
2022-02-19 11:41:34 +01:00
$this->addCustomer($customer);
}
2022-02-19 11:41:34 +01:00
2023-02-16 02:36:09 +01:00
//handle
2022-11-27 07:39:10 +01:00
// if(is_array($customers->data) && end($customers->data) && array_key_exists('id', end($customers->data)))
// $starting_after = end($customers->data)['id'];
// else
// break;
$starting_after = isset(end($customers->data)['id']) ? end($customers->data)['id'] : false;
2022-11-27 07:39:10 +01:00
2023-02-16 02:36:09 +01:00
if (!$starting_after) {
break;
2023-02-16 02:36:09 +01:00
}
} while ($customers->has_more);
2021-05-17 08:01:32 +02:00
}
private function addCustomer(Customer $customer)
{
$account = $this->stripe->company_gateway->company->account;
2021-05-19 05:32:20 +02:00
2023-08-11 09:58:43 +02:00
if (Ninja::isHosted() && ! $account->isPaidHostedClient() && Client::query()->where('company_id', $this->stripe->company_gateway->company_id)->count() > config('ninja.quotas.free.clients')) {
return;
}
2021-05-17 08:01:32 +02:00
2021-09-02 08:17:46 +02:00
$existing_customer_token = $this->stripe
2021-05-17 14:24:00 +02:00
->company_gateway
2021-05-17 08:01:32 +02:00
->client_gateway_tokens()
->where('gateway_customer_reference', $customer->id)
2021-09-02 08:17:46 +02:00
->first();
2021-05-17 08:01:32 +02:00
if ($existing_customer_token) {
2021-09-02 04:08:16 +02:00
nlog("Skipping - Customer exists: {$customer->email} just updating payment methods");
2021-09-02 08:17:46 +02:00
$this->update_payment_methods->updateMethods($customer, $existing_customer_token->client);
2021-09-02 04:08:16 +02:00
}
if ($customer->email && $this->stripe->company_gateway->company->client_contacts()->where('email', $customer->email)->exists()) {
2021-09-02 04:08:16 +02:00
nlog("Customer exists: {$customer->email} just updating payment methods");
2021-09-04 04:08:53 +02:00
$this->stripe->company_gateway->company->client_contacts()->where('email', $customer->email)->each(function ($contact) use ($customer) {
2021-09-04 04:08:53 +02:00
$this->update_payment_methods->updateMethods($customer, $contact->client);
});
2021-05-17 14:24:00 +02:00
return;
}
2021-05-17 14:24:00 +02:00
$client = ClientFactory::create($this->stripe->company_gateway->company_id, $this->stripe->company_gateway->user_id);
2021-05-17 08:01:32 +02:00
if ($customer->address) {
2021-09-02 04:08:16 +02:00
$client->address1 = $customer->address->line1 ? $customer->address->line1 : '';
$client->address2 = $customer->address->line2 ? $customer->address->line2 : '';
$client->city = $customer->address->city ? $customer->address->city : '';
$client->state = $customer->address->state ? $customer->address->state : '';
2023-09-24 05:07:32 +02:00
$client->phone = $customer->phone ?? '';
2021-05-17 08:01:32 +02:00
if ($customer->address->country) {
2023-08-11 09:58:43 +02:00
$country = Country::query()->where('iso_3166_2', $customer->address->country)->first();
2021-05-17 08:01:32 +02:00
if ($country) {
2021-05-17 14:24:00 +02:00
$client->country_id = $country->id;
}
2021-05-17 14:24:00 +02:00
}
2021-05-17 08:01:32 +02:00
}
if ($customer->currency) {
2023-08-11 09:58:43 +02:00
$currency = Currency::query()->where('code', $customer->currency)->first();
2021-05-17 08:01:32 +02:00
if ($currency) {
2021-05-17 08:01:32 +02:00
$settings = $client->settings;
$settings->currency_id = (string) $currency->id;
2021-05-17 08:01:32 +02:00
$client->settings = $settings;
}
}
2021-09-02 04:08:16 +02:00
$client->name = $customer->name ? $customer->name : $customer->email;
2021-05-17 08:01:32 +02:00
if (! isset($client->number) || empty($client->number)) {
$x = 1;
do {
try {
$client->number = $this->getNextClientNumber($client);
$client->saveQuietly();
$this->completed = false;
} catch (QueryException $e) {
$x++;
if ($x > 10) {
$this->completed = false;
}
}
} while ($this->completed);
2023-02-16 02:36:09 +01:00
} else {
$client->save();
}
2021-05-17 08:01:32 +02:00
$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();
2021-05-21 12:08:48 +02:00
$this->update_payment_methods->updateMethods($customer, $client);
2021-05-17 08:01:32 +02:00
}
2021-08-18 02:15:11 +02:00
public function importCustomer($customer_id)
{
$this->stripe->init();
$this->update_payment_methods = new UpdatePaymentMethods($this->stripe);
if (strlen($this->stripe->company_gateway->getConfigField('account_id')) < 1) {
throw new StripeConnectFailure('Stripe Connect has not been configured');
}
$customer = Customer::retrieve($customer_id, $this->stripe->stripe_connect_auth);
if (! $customer) {
return;
}
foreach ($this->stripe->company_gateway->company->clients as $client) {
if ($client->present()->email() == $customer->email) {
$this->update_payment_methods->updateMethods($customer, $client);
}
}
}
2021-08-18 02:15:11 +02:00
public function match()
{
2021-08-18 04:02:07 +02:00
$this->stripe->init();
$this->update_payment_methods = new UpdatePaymentMethods($this->stripe);
if (strlen($this->stripe->company_gateway->getConfigField('account_id')) < 1) {
throw new StripeConnectFailure('Stripe Connect has not been configured');
}
2021-08-18 02:15:11 +02:00
foreach ($this->stripe->company_gateway->company->clients as $client) {
2021-08-18 02:15:11 +02:00
$searchResults = \Stripe\Customer::all([
'email' => $client->present()->email(),
'limit' => 2,
'starting_after' => null,
], $this->stripe->stripe_connect_auth);
2021-08-18 02:15:11 +02:00
// nlog(count($searchResults));
if (count($searchResults) == 1) {
2023-08-11 09:58:43 +02:00
$cgt = ClientGatewayToken::query()->where('gateway_customer_reference', $searchResults->data[0]->id)->where('company_id', $this->stripe->company_gateway->company->id)->exists();
2021-08-18 02:15:11 +02:00
if (! $cgt) {
nlog('customer '.$searchResults->data[0]->id.' does not exist.');
2021-08-18 02:15:11 +02:00
$this->update_payment_methods->updateMethods($searchResults->data[0], $client);
}
}
}
}
2021-05-17 08:01:32 +02:00
}