1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00
invoiceninja/app/PaymentDrivers/Stripe/ImportCustomers.php

123 lines
3.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
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
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;
use App\Utils\Traits\MakesHash;
use Stripe\Customer;
use Stripe\PaymentMethod;
class ImportCustomers
{
use MakesHash;
/** @var StripePaymentDriver */
public $stripe;
public function __construct(StripePaymentDriver $stripe)
{
$this->stripe = $stripe;
}
public function run()
{
$this->stripe->init();
$customers = Customer::all();
foreach($customers as $customer)
{
$this->addCustomer($customer);
}
/* Now call the update payment methods handler*/
$this->stripe->updateAllPaymentMethods();
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
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
nlog("search Stripe for {$custom->id}");
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();
if($existing_customer)
2021-05-17 14:24:00 +02:00
return;
$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-17 14:24:00 +02:00
$client->name = property_exists($customer, 'name') ? $customer->name : '';
2021-05-17 08:01:32 +02:00
2021-05-17 14:24:00 +02:00
$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 = $client->email ?: '';
$contact->save();
2021-05-17 08:01:32 +02:00
}
}