mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-07-09 03:14:30 -04:00
Import customers with GoCardless
This commit is contained in:
parent
76198fa05d
commit
36c2e54412
@ -11,20 +11,27 @@
|
|||||||
|
|
||||||
namespace App\PaymentDrivers;
|
namespace App\PaymentDrivers;
|
||||||
|
|
||||||
|
use App\Factory\ClientContactFactory;
|
||||||
|
use App\Factory\ClientFactory;
|
||||||
use App\Http\Requests\Payments\PaymentWebhookRequest;
|
use App\Http\Requests\Payments\PaymentWebhookRequest;
|
||||||
use App\Jobs\Util\SystemLogger;
|
use App\Jobs\Util\SystemLogger;
|
||||||
|
use App\Models\Client;
|
||||||
use App\Models\ClientGatewayToken;
|
use App\Models\ClientGatewayToken;
|
||||||
|
use App\Models\Country;
|
||||||
use App\Models\GatewayType;
|
use App\Models\GatewayType;
|
||||||
use App\Models\Invoice;
|
use App\Models\Invoice;
|
||||||
use App\Models\Payment;
|
use App\Models\Payment;
|
||||||
use App\Models\PaymentHash;
|
use App\Models\PaymentHash;
|
||||||
use App\Models\PaymentType;
|
use App\Models\PaymentType;
|
||||||
use App\Models\SystemLog;
|
use App\Models\SystemLog;
|
||||||
|
use App\Utils\Traits\GeneratesCounter;
|
||||||
use App\Utils\Traits\MakesHash;
|
use App\Utils\Traits\MakesHash;
|
||||||
|
use Illuminate\Database\QueryException;
|
||||||
|
|
||||||
class GoCardlessPaymentDriver extends BaseDriver
|
class GoCardlessPaymentDriver extends BaseDriver
|
||||||
{
|
{
|
||||||
use MakesHash;
|
use MakesHash;
|
||||||
|
use GeneratesCounter;
|
||||||
|
|
||||||
public $refundable = true;
|
public $refundable = true;
|
||||||
|
|
||||||
@ -36,6 +43,8 @@ class GoCardlessPaymentDriver extends BaseDriver
|
|||||||
|
|
||||||
public $payment_method;
|
public $payment_method;
|
||||||
|
|
||||||
|
private bool $completed = true;
|
||||||
|
|
||||||
public static $methods = [
|
public static $methods = [
|
||||||
GatewayType::BANK_TRANSFER => \App\PaymentDrivers\GoCardless\ACH::class,
|
GatewayType::BANK_TRANSFER => \App\PaymentDrivers\GoCardless\ACH::class,
|
||||||
GatewayType::DIRECT_DEBIT => \App\PaymentDrivers\GoCardless\DirectDebit::class,
|
GatewayType::DIRECT_DEBIT => \App\PaymentDrivers\GoCardless\DirectDebit::class,
|
||||||
@ -374,4 +383,113 @@ class GoCardlessPaymentDriver extends BaseDriver
|
|||||||
throw new \App\Exceptions\PaymentFailed($exception->getMessage());
|
throw new \App\Exceptions\PaymentFailed($exception->getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function importCustomers()
|
||||||
|
{
|
||||||
|
$this->init();
|
||||||
|
|
||||||
|
$customers = $this->gateway->customers()->list();
|
||||||
|
|
||||||
|
foreach ($customers->records as $customer) {
|
||||||
|
$existing_customer_token = $this->company_gateway
|
||||||
|
->client_gateway_tokens()
|
||||||
|
->where('gateway_customer_reference', $customer->id)
|
||||||
|
->first();
|
||||||
|
|
||||||
|
if ($existing_customer_token) {
|
||||||
|
nlog("Skipping - Customer exists: {$customer->email} just updating payment methods");
|
||||||
|
$this->updatePaymentMethods($customer, $existing_customer_token->client);
|
||||||
|
} elseif ($customer->email && $this->company_gateway->company->client_contacts()->where('email', $customer->email)->exists()) {
|
||||||
|
nlog("Customer exists: {$customer->email} just updating payment methods");
|
||||||
|
|
||||||
|
$this->company_gateway->company->client_contacts()->where('email', $customer->email)->each(function ($contact) use ($customer) {
|
||||||
|
$this->updatePaymentMethods($customer, $contact->client);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
nlog("Creating new customer: {$customer->email}");
|
||||||
|
$client = $this->createNinjaClient($customer);
|
||||||
|
$this->updatePaymentMethods($customer, $client);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function updatePaymentMethods($customer, Client $client): void
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
[id] => CU0021PHBG5J6G
|
||||||
|
[created_at] => 2022-12-02T11:24:15.739Z
|
||||||
|
[email] => test@test.com
|
||||||
|
[given_name] => Test
|
||||||
|
[family_name] => Red
|
||||||
|
[company_name] =>
|
||||||
|
[address_line1] =>
|
||||||
|
[address_line2] =>
|
||||||
|
[address_line3] =>
|
||||||
|
[city] =>
|
||||||
|
[region] =>
|
||||||
|
[postal_code] =>
|
||||||
|
[country_code] =>
|
||||||
|
[language] => en
|
||||||
|
[swedish_identity_number] =>
|
||||||
|
[danish_identity_number] =>
|
||||||
|
[phone_number] =>
|
||||||
|
*/
|
||||||
|
private function createNinjaClient($customer): Client
|
||||||
|
{
|
||||||
|
$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->city ?: '';
|
||||||
|
$client->state = $customer->region ?: '';
|
||||||
|
$client->postal_code = $customer->postal_city ?: '';
|
||||||
|
$client->phone = $customer->address->phone ? $customer->phone : '';
|
||||||
|
$client->name = $customer->company_name;
|
||||||
|
|
||||||
|
if ($customer->address->country) {
|
||||||
|
$country = Country::where('iso_3166_2', $customer->country_code)->first();
|
||||||
|
|
||||||
|
if ($country) {
|
||||||
|
$client->country_id = $country->id;
|
||||||
|
} else {
|
||||||
|
$client->country_id = $this->company_gateway->company->settings->country_id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$settings = $client->settings;
|
||||||
|
$settings->currency_id = (string) $this->company_gateway->company->settings->currency_id;
|
||||||
|
$client->settings = $settings;
|
||||||
|
$client->save();
|
||||||
|
|
||||||
|
$contact = ClientContactFactory::create($this->company_gateway->company_id, $this->company_gateway->user_id);
|
||||||
|
$contact->first_name = $customer->first_name ?: '';
|
||||||
|
$contact->last_name = $customer->last_name ?: '';
|
||||||
|
$contact->email = $customer->email ?: '';
|
||||||
|
$contact->client_id = $client->id;
|
||||||
|
|
||||||
|
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);
|
||||||
|
} else {
|
||||||
|
$client->saveQuietly();
|
||||||
|
}
|
||||||
|
|
||||||
|
return $client;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user