From 79ed4e4305b5dd3388d055e2c2e3abfd2671cbb7 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Mon, 18 Mar 2024 17:01:00 +1100 Subject: [PATCH] Import Customers Forte --- .../Factory/ForteCustomerFactory.php | 134 ++++++++++++++++++ app/PaymentDrivers/FortePaymentDriver.php | 55 +++++-- 2 files changed, 181 insertions(+), 8 deletions(-) create mode 100644 app/PaymentDrivers/Factory/ForteCustomerFactory.php diff --git a/app/PaymentDrivers/Factory/ForteCustomerFactory.php b/app/PaymentDrivers/Factory/ForteCustomerFactory.php new file mode 100644 index 0000000000..1b127d58e7 --- /dev/null +++ b/app/PaymentDrivers/Factory/ForteCustomerFactory.php @@ -0,0 +1,134 @@ + $customer['company_name'] ?? $customer['first_name'], + 'contacts' => [ + [ + 'first_name' => $customer['first_name'], + 'last_name' => $customer['last_name'], + 'email' => $this->getBillingAddress($customer)['email'], + 'phone' => $this->getBillingAddress($customer)['phone'], + ] + ], + 'currency_id' => $company->settings->currency_id, + + ])->merge($this->getBillingAddress($customer)) + ->merge($this->getShippingAddress($customer)) + ->toArray(); + + } + + // public function convertToGateway(Client $client): array + // { + + // } + + private function getBillingAddress(array $customer): array + { + if(isset($customer['default_billing_address_token'])) { + + foreach($customer['addresses'] as $address) { + + if($address['address_token'] != $customer['default_billing_address_token']) + continue; + + return [ + 'address1' => $address['physical_address']['street_line1'], + 'address2' => $address['physical_address']['street_line2'], + 'city' => $address['physical_address']['locality'], + 'state' => $address['physical_address']['region'], + 'postal_code' => $address['physical_address']['postal_code'], + 'country_id' => '840', + 'email' => $address['email'], + 'phone' => $address['phone'], + ]; + + } + + } + + if(isset($customer['addresses'][0])) { + + $address = $customer['addresses'][0]; + + return [ + 'address1' => $address['physical_address']['street_line1'], + 'address2' => $address['physical_address']['street_line2'], + 'city' => $address['physical_address']['locality'], + 'state' => $address['physical_address']['region'], + 'postal_code' => $address['physical_address']['postal_code'], + 'email' => $address['email'], + 'phone' => $address['phone'], + 'country_id' => '840', + ]; + + } + + return ['email' => '', 'phone' => '']; + + } + + private function getShippingAddress(array $customer): array + { + + if(isset($customer['default_shipping_address_token'])) { + + foreach($customer['addresses'] as $address) { + + if($address['address_token'] != $customer['default_shipping_address_token']) { + continue; + } + + return [ + 'address1' => $address['physical_address']['street_line1'], + 'address2' => $address['physical_address']['street_line2'], + 'city' => $address['physical_address']['locality'], + 'state' => $address['physical_address']['region'], + 'postal_code' => $address['physical_address']['postal_code'], + 'country_id' => '840', + ]; + + } + + } + + if(isset($customer['addresses'][1])){ + + $address = $customer['addresses'][1]; + + return [ + 'address1' => $address['physical_address']['street_line1'], + 'address2' => $address['physical_address']['street_line2'], + 'city' => $address['physical_address']['locality'], + 'state' => $address['physical_address']['region'], + 'postal_code' => $address['physical_address']['postal_code'], + 'country_id' => '840', + ]; + + } + + return ['email' => '', 'phone' => '']; + + } +} diff --git a/app/PaymentDrivers/FortePaymentDriver.php b/app/PaymentDrivers/FortePaymentDriver.php index 5a38a8b0bc..5c3c4ce0d7 100644 --- a/app/PaymentDrivers/FortePaymentDriver.php +++ b/app/PaymentDrivers/FortePaymentDriver.php @@ -11,6 +11,7 @@ namespace App\PaymentDrivers; +use App\Factory\ClientFactory; use App\Models\Payment; use App\Models\SystemLog; use App\Models\GatewayType; @@ -18,7 +19,10 @@ use App\Jobs\Util\SystemLogger; use App\Utils\Traits\MakesHash; use App\PaymentDrivers\Forte\ACH; use Illuminate\Support\Facades\Http; +use App\Repositories\ClientRepository; use App\PaymentDrivers\Forte\CreditCard; +use App\Repositories\ClientContactRepository; +use App\PaymentDrivers\Factory\ForteCustomerFactory; class FortePaymentDriver extends BaseDriver { @@ -184,6 +188,9 @@ class FortePaymentDriver extends BaseDriver ]; } + //////////////////////////////////////////// + // DB + /////////////////////////////////////////// public function auth(): bool { @@ -204,29 +211,61 @@ class FortePaymentDriver extends BaseDriver return $response->successful(); } - - public function importCustomers() + + public function baseUri(): string { $forte_base_uri = "https://sandbox.forte.net/api/v3/"; if ($this->company_gateway->getConfigField('testMode') == false) { $forte_base_uri = "https://api.forte.net/v3/"; } + + return $forte_base_uri; + } + + private function getOrganisationId(): string + { + return $this->company_gateway->getConfigField('organizationId'); + } + + public function getLocationId(): string + { + return $this->company_gateway->getConfigField('locationId'); + } + + public function stubRequest() + { + $forte_api_access_id = $this->company_gateway->getConfigField('apiAccessId'); $forte_secure_key = $this->company_gateway->getConfigField('secureKey'); $forte_auth_organization_id = $this->company_gateway->getConfigField('authOrganizationId'); - $forte_organization_id = $this->company_gateway->getConfigField('organizationId'); - $forte_location_id = $this->company_gateway->getConfigField('locationId'); - $response = Http::withBasicAuth($forte_api_access_id, $forte_secure_key) - ->withHeaders(['X-Forte-Auth-Organization-Id' => $forte_organization_id]) - ->get("{$forte_base_uri}/organizations/{$forte_organization_id}/locations/{$forte_location_id}/customers/"); + return Http::withBasicAuth($forte_api_access_id, $forte_secure_key) + ->withHeaders(['X-Forte-Auth-Organization-Id' => $this->getOrganisationId()]); + } + + public function importCustomers() + { + + $response = $this->stubRequest() + ->withQueryParameters(['page_size' => 10000]) + ->get("{$this->baseUri()}/organizations/{$this->getOrganisationId()}/locations/{$this->getLocationId()}/customers"); if($response->successful()){ + + foreach($response->json()['results'] as $customer) + { - nlog($response->json()); + $client_repo = new ClientRepository(new ClientContactRepository()); + $factory = new ForteCustomerFactory(); + $data = $factory->convertToNinja($customer, $this->company_gateway->company); + + $client_repo->save($data, ClientFactory::create($this->company_gateway->company_id, $this->company_gateway->user_id)); + + } } } + }