1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-20 08:21:34 +02:00

Import Customers Forte

This commit is contained in:
David Bomba 2024-03-18 17:01:00 +11:00
parent 0a87aeaf8f
commit 79ed4e4305
2 changed files with 181 additions and 8 deletions

View File

@ -0,0 +1,134 @@
<?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://www.elastic.co/licensing/elastic-license
*/
namespace App\PaymentDrivers\Factory;
use App\Models\Client;
use App\Models\Company;
class ForteCustomerFactory
{
public function convertToNinja(array $customer, Company $company): array
{
return
collect([
'name' => $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' => ''];
}
}

View File

@ -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
{
@ -205,28 +212,60 @@ class FortePaymentDriver extends BaseDriver
}
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()){
nlog($response->json());
foreach($response->json()['results'] as $customer)
{
$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));
}
}
}
}
}