diff --git a/app/PaymentDrivers/GoCardlessPaymentDriver.php b/app/PaymentDrivers/GoCardlessPaymentDriver.php index a1f6f49ee709..853c66cc0a83 100644 --- a/app/PaymentDrivers/GoCardlessPaymentDriver.php +++ b/app/PaymentDrivers/GoCardlessPaymentDriver.php @@ -11,20 +11,27 @@ namespace App\PaymentDrivers; +use App\Factory\ClientContactFactory; +use App\Factory\ClientFactory; use App\Http\Requests\Payments\PaymentWebhookRequest; use App\Jobs\Util\SystemLogger; +use App\Models\Client; use App\Models\ClientGatewayToken; +use App\Models\Country; use App\Models\GatewayType; use App\Models\Invoice; use App\Models\Payment; use App\Models\PaymentHash; use App\Models\PaymentType; use App\Models\SystemLog; +use App\Utils\Traits\GeneratesCounter; use App\Utils\Traits\MakesHash; +use Illuminate\Database\QueryException; class GoCardlessPaymentDriver extends BaseDriver { use MakesHash; + use GeneratesCounter; public $refundable = true; @@ -36,6 +43,8 @@ class GoCardlessPaymentDriver extends BaseDriver public $payment_method; + private bool $completed = true; + public static $methods = [ GatewayType::BANK_TRANSFER => \App\PaymentDrivers\GoCardless\ACH::class, GatewayType::DIRECT_DEBIT => \App\PaymentDrivers\GoCardless\DirectDebit::class, @@ -374,4 +383,113 @@ class GoCardlessPaymentDriver extends BaseDriver 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; + } }