2021-05-17 08:01:32 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2021-05-19 10:38:51 +02:00
|
|
|
*/
|
2021-05-17 08:01:32 +02:00
|
|
|
|
|
|
|
namespace App\PaymentDrivers\Stripe;
|
|
|
|
|
|
|
|
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\Factory\ClientGatewayTokenFactory;
|
|
|
|
use App\Models\Client;
|
|
|
|
use App\Models\ClientGatewayToken;
|
|
|
|
use App\Models\Country;
|
|
|
|
use App\Models\Currency;
|
|
|
|
use App\Models\GatewayType;
|
|
|
|
use App\PaymentDrivers\StripePaymentDriver;
|
2021-05-21 12:08:48 +02:00
|
|
|
use App\PaymentDrivers\Stripe\UpdatePaymentMethods;
|
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 Stripe\Customer;
|
|
|
|
use Stripe\PaymentMethod;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
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;
|
2021-05-21 12:08:48 +02:00
|
|
|
|
2021-05-17 08:01:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
|
|
|
|
$this->stripe->init();
|
|
|
|
|
2021-05-21 12:08:48 +02:00
|
|
|
$this->update_payment_methods = new UpdatePaymentMethods($this->stripe);
|
|
|
|
|
2021-08-14 23:51:37 +02:00
|
|
|
if(strlen($this->stripe->company_gateway->getConfigField('account_id')) < 1)
|
|
|
|
throw new \Exception('Stripe Connect has not been configured');
|
|
|
|
|
2021-05-19 06:22:22 +02:00
|
|
|
$customers = Customer::all([], $this->stripe->stripe_connect_auth);
|
2021-05-17 08:01:32 +02:00
|
|
|
|
|
|
|
foreach($customers as $customer)
|
|
|
|
{
|
|
|
|
$this->addCustomer($customer);
|
|
|
|
}
|
|
|
|
|
2021-05-17 10:56:14 +02:00
|
|
|
/* Now call the update payment methods handler*/
|
2021-06-15 03:09:12 +02:00
|
|
|
// $this->stripe->updateAllPaymentMethods();
|
2021-05-17 10:56:14 +02:00
|
|
|
|
2021-05-17 08:01:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private function addCustomer(Customer $customer)
|
|
|
|
{
|
2021-05-19 05:32:20 +02:00
|
|
|
|
|
|
|
$account = $this->stripe->company_gateway->company->account;
|
2021-05-17 14:24:00 +02:00
|
|
|
|
2021-05-19 05:24:18 +02:00
|
|
|
if(!$account->isPaidHostedClient() && Client::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-05-19 06:22:22 +02:00
|
|
|
nlog("search Stripe for {$customer->id}");
|
2021-05-19 05:24:18 +02:00
|
|
|
|
2021-05-17 14:24:00 +02:00
|
|
|
$existing_customer = $this->stripe
|
|
|
|
->company_gateway
|
2021-05-17 08:01:32 +02:00
|
|
|
->client_gateway_tokens()
|
|
|
|
->where('gateway_customer_reference', $customer->id)
|
|
|
|
->exists();
|
|
|
|
|
2021-05-19 06:22:22 +02:00
|
|
|
if($existing_customer){
|
|
|
|
nlog("Skipping - Customer exists: {$customer->email}");
|
2021-05-17 14:24:00 +02:00
|
|
|
return;
|
2021-05-19 06:22:22 +02:00
|
|
|
}
|
2021-05-17 14:24:00 +02:00
|
|
|
|
2021-05-19 06:22:22 +02:00
|
|
|
nlog("inserting a customer");
|
2021-05-23 23:05:26 +02:00
|
|
|
//nlog($customer);
|
2021-05-19 06:27:47 +02:00
|
|
|
|
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
|
|
|
|
2021-05-17 14:24:00 +02:00
|
|
|
if(property_exists($customer, 'address'))
|
|
|
|
{
|
|
|
|
$client->address1 = property_exists($customer->address, 'line1') ? $customer->address->line1 : '';
|
|
|
|
$client->address2 = property_exists($customer->address, 'line2') ? $customer->address->line2 : '';
|
|
|
|
$client->city = property_exists($customer->address, 'city') ? $customer->address->city : '';
|
|
|
|
$client->state = property_exists($customer->address, 'state') ? $customer->address->state : '';
|
|
|
|
$client->phone = property_exists($customer->address, 'phone') ? $customer->phone : '';
|
2021-05-17 08:01:32 +02:00
|
|
|
|
2021-05-17 14:24:00 +02:00
|
|
|
if(property_exists($customer->address, 'country')){
|
2021-05-17 08:01:32 +02:00
|
|
|
|
2021-05-17 14:24:00 +02:00
|
|
|
$country = Country::where('iso_3166_2', $customer->address->country)->first();
|
2021-05-17 08:01:32 +02:00
|
|
|
|
2021-05-17 14:24:00 +02:00
|
|
|
if($country)
|
|
|
|
$client->country_id = $country->id;
|
2021-05-17 08:01:32 +02:00
|
|
|
|
2021-05-17 14:24:00 +02:00
|
|
|
}
|
2021-05-17 08:01:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if($customer->currency) {
|
|
|
|
|
|
|
|
$currency = Currency::where('code', $customer->currency)->first();
|
|
|
|
|
|
|
|
if($currency){
|
|
|
|
|
|
|
|
$settings = $client->settings;
|
|
|
|
$settings->currency_id = (string)$currency->id;
|
|
|
|
$client->settings = $settings;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-05-19 06:22:22 +02:00
|
|
|
$client->name = property_exists($customer, 'name') ? $customer->name : $customer->email;
|
2021-05-17 08:01:32 +02:00
|
|
|
|
2021-08-15 07:49:36 +02:00
|
|
|
if (!isset($client->number) || empty($client->number)) {
|
|
|
|
$client->number = $this->getNextClientNumber($client);
|
|
|
|
}
|
|
|
|
|
2021-05-19 06:22:22 +02:00
|
|
|
$client->save();
|
2021-05-17 08:01:32 +02:00
|
|
|
|
2021-05-19 06:22:22 +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-19 05:24:18 +02:00
|
|
|
|
2021-05-21 12:08:48 +02:00
|
|
|
$this->update_payment_methods->updateMethods($customer, $client);
|
2021-05-17 08:01:32 +02:00
|
|
|
}
|
|
|
|
}
|