2021-05-17 08:01:32 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2022-04-27 05:20:41 +02:00
|
|
|
* @copyright Copyright (c) 2022. 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-19 10:38:51 +02:00
|
|
|
*/
|
2021-05-17 08:01:32 +02:00
|
|
|
|
|
|
|
namespace App\PaymentDrivers\Stripe;
|
|
|
|
|
2021-08-15 08:21:18 +02:00
|
|
|
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\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-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 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-09-04 04:09:49 +02:00
|
|
|
if(Ninja::isHosted() && strlen($this->stripe->company_gateway->getConfigField('account_id')) < 1)
|
|
|
|
throw new StripeConnectFailure('Stripe Connect has not been configured');
|
2021-08-14 23:51:37 +02:00
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
$this->addCustomer($customer);
|
|
|
|
}
|
|
|
|
|
|
|
|
$starting_after = end($customers->data)['id'];
|
2021-05-17 08:01:32 +02:00
|
|
|
|
2022-02-19 11:41:34 +01:00
|
|
|
} while($customers->has_more);
|
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
|
|
|
|
2022-03-11 00:19:22 +01:00
|
|
|
if(Ninja::isHosted() && !$account->isPaidHostedClient() && Client::where('company_id', $this->stripe->company_gateway->company_id)->count() > config('ninja.quotas.free.clients'))
|
2021-05-19 05:24:18 +02:00
|
|
|
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
|
|
|
|
2021-09-02 08:17:46 +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
|
|
|
}
|
|
|
|
|
2021-09-04 04:08:53 +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){
|
|
|
|
|
|
|
|
$this->update_payment_methods->updateMethods($customer, $contact->client);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
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
|
|
|
|
|
|
|
$client = ClientFactory::create($this->stripe->company_gateway->company_id, $this->stripe->company_gateway->user_id);
|
2021-05-17 08:01:32 +02:00
|
|
|
|
2021-09-02 04:08:16 +02:00
|
|
|
if($customer->address)
|
2021-05-17 14:24:00 +02:00
|
|
|
{
|
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 : '';
|
|
|
|
$client->phone = $customer->address->phone ? $customer->phone : '';
|
2021-05-17 08:01:32 +02:00
|
|
|
|
2021-09-02 04:08:16 +02:00
|
|
|
if($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-09-02 04:08:16 +02:00
|
|
|
$client->name = $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
|
|
|
}
|
2021-08-18 02:15:11 +02:00
|
|
|
|
2021-08-20 14:44:39 +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');
|
|
|
|
|
2021-10-13 03:52:44 +02:00
|
|
|
$customer = Customer::retrieve($customer_id, $this->stripe->stripe_connect_auth);
|
2021-08-20 14:44:39 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
|
|
|
|
$searchResults = \Stripe\Customer::all([
|
|
|
|
"email" => $client->present()->email(),
|
|
|
|
"limit" => 2,
|
|
|
|
"starting_after" => null
|
|
|
|
],$this->stripe->stripe_connect_auth);
|
|
|
|
|
|
|
|
// nlog(count($searchResults));
|
|
|
|
|
|
|
|
if(count($searchResults) == 1)
|
|
|
|
{
|
|
|
|
|
2021-08-18 02:32:09 +02:00
|
|
|
$cgt = ClientGatewayToken::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.");
|
|
|
|
|
|
|
|
$this->update_payment_methods->updateMethods($searchResults->data[0], $client);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2021-05-17 08:01:32 +02:00
|
|
|
}
|