mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-06-23 20:00:33 -04:00
update import process to import/ sync new/existing customers
This commit is contained in:
parent
5934242e85
commit
734b34c33b
@ -24,6 +24,7 @@ use App\Utils\Traits\MakesHash;
|
||||
use App\Jobs\Util\SystemLogger;
|
||||
use App\PaymentDrivers\BaseDriver;
|
||||
use App\Models\ClientGatewayToken;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use App\PaymentDrivers\Rotessa\Resources\Customer;
|
||||
use App\PaymentDrivers\Rotessa\PaymentMethod as Acss;
|
||||
@ -115,38 +116,107 @@ class RotessaPaymentDriver extends BaseDriver
|
||||
public function importCustomers() {
|
||||
$this->init();
|
||||
try {
|
||||
$result = $this->gateway->getCustomers()->send();
|
||||
if(!$result->isSuccessful()) throw new \Exception($result->getMessage(), (int) $result->getCode());
|
||||
if(!$result = Cache::has('rotessa-import_customers')) {
|
||||
$result = $this->gateway->getCustomers()->send();
|
||||
if(!$result->isSuccessful()) throw new \Exception($result->getMessage(), (int) $result->getCode());
|
||||
// cache results
|
||||
Cache::put('rotessa-import_customers', $result->getData(), 60 * 60 * 24);
|
||||
}
|
||||
|
||||
$customers = collect($result->getData())->unique('email');
|
||||
$result = Cache::get('rotessa-import_customers');
|
||||
$customers = collect($result)->unique('email');
|
||||
$client_emails = $customers->pluck('email')->all();
|
||||
$company_id = $this->company_gateway->company->id;
|
||||
// get existing customers
|
||||
$client_contacts = ClientContact::where('company_id', $company_id)->whereIn('email', $client_emails )->whereNull('deleted_at')->get();
|
||||
$client_contacts = $client_contacts->map(function($item, $key) use ($customers) {
|
||||
return array_merge([], (array) $customers->firstWhere("email", $item->email) , ['custom_identifier' => $item->client->number, 'identifier' => $item->client->number ]);
|
||||
return array_merge([], (array) $customers->firstWhere("email", $item->email) , ['custom_identifier' => $item->client->number, 'identifier' => $item->client->number, 'client_id' => $item->client->id ]);
|
||||
} );
|
||||
|
||||
// create payment methods
|
||||
$client_contacts->each(
|
||||
function($contact) use ($customers) {
|
||||
sleep(10);
|
||||
$result = $this->gateway->getCustomersId(['id' => ($contact = (object) $contact)->id])->send();
|
||||
$this->client = Client::find($contact->custom_identifier);
|
||||
$this->client = Client::find($contact->client_id);
|
||||
$customer = (new Customer($result->getData()))->additional(['id' => $contact->id, 'custom_identifier' => $contact->custom_identifier ] );
|
||||
$this->findOrCreateCustomer($customer->additional + $customer->jsonSerialize());
|
||||
}
|
||||
);
|
||||
|
||||
// create new clients from rotessa customers
|
||||
$client_emails = $client_contacts->pluck('email')->all();
|
||||
$client_contacts = $customers->filter(function ($value, $key) use ($client_emails) {
|
||||
return !in_array(((object) $value)->email, $client_emails);
|
||||
})->each( function($customer) use ($company_id) {
|
||||
sleep(10);
|
||||
// create new client contact from rotess customer
|
||||
$customer = (object) $this->gateway->getCustomersId(['id' => ($customer = (object) $customer)->id])->send()->getData();
|
||||
/**
|
||||
{
|
||||
"account_number": "11111111"
|
||||
"active": true,
|
||||
"address": {
|
||||
"address_1": "123 Main Street",
|
||||
"address_2": "Unit 4",
|
||||
"city": "Birmingham",
|
||||
"id": 114397,
|
||||
"postal_code": "36016",
|
||||
"province_code": "AL"
|
||||
},
|
||||
"authorization_type": "Online",
|
||||
"bank_account_type": "Checking",
|
||||
"bank_name": "Scotiabank",
|
||||
"created_at": "2015-02-10T23:50:45.000-06:00",
|
||||
"custom_identifier": "Mikey",
|
||||
"customer_type": "Personal",
|
||||
"email": "mikesmith@test.com",
|
||||
"financial_transactions": [],
|
||||
"home_phone": "(204) 555 5555",
|
||||
"id": 1,
|
||||
"identifier": "Mikey",
|
||||
"institution_number": "",
|
||||
"name": "Mike Smith",
|
||||
"phone": "(204) 555 4444",
|
||||
"routing_number": "111111111",
|
||||
"transaction_schedules": [],
|
||||
"transit_number": "",
|
||||
"updated_at": "2015-02-10T23:50:45.000-06:00"
|
||||
}
|
||||
*/
|
||||
$client = (\App\Factory\ClientFactory::create($this->company_gateway->company_id, $this->company_gateway->user_id))->fill(
|
||||
[
|
||||
'address1' => $customer->address['address_1'] ?? '',
|
||||
'address2' =>$customer->address['address_2'] ?? '',
|
||||
'city' => $customer->address['city'] ?? '',
|
||||
'postal_code' => $customer->address['postal_code'] ?? '',
|
||||
'state' => $customer->address['province_code'] ?? '',
|
||||
'country_id' => empty($customer->transit_number) ? 840 : 124,
|
||||
'routing_id' => empty(($r = $customer->routing_number))? null : $r,
|
||||
"number" => str_pad($customer->account_number,3,'0',STR_PAD_LEFT)
|
||||
]
|
||||
);
|
||||
$client->saveQuietly();
|
||||
$contact = (\App\Factory\ClientContactFactory::create($company_id, $this->company_gateway->user_id))->fill([
|
||||
"first_name" => substr($customer->name, 0, stripos($customer->name, " ")),
|
||||
"last_name" => substr($customer->name, stripos($customer->name, " ")),
|
||||
"email" => $customer->email,
|
||||
"phone" => $customer->phone
|
||||
]);
|
||||
$client->contacts()->saveMany([$contact]);
|
||||
$contact = $client->contacts()->first();
|
||||
$this->client = $client;
|
||||
$customer = (new Customer((array) $customer))->additional(['id' => $customer->id, 'custom_identifier' => $customer->custom_identifier ?? $contact->id ] );
|
||||
$this->findOrCreateCustomer($customer->additional + $customer->jsonSerialize());
|
||||
});
|
||||
} catch (\Throwable $th) {
|
||||
$data = [
|
||||
$data = [
|
||||
'transaction_reference' => null,
|
||||
'transaction_response' => $th->getMessage(),
|
||||
'success' => false,
|
||||
'description' => $th->getMessage(),
|
||||
'code' =>(int) $th->getCode()
|
||||
];
|
||||
|
||||
SystemLogger::dispatch(['server_response' => $th->getMessage(), 'data' => $data], SystemLog::CATEGORY_GATEWAY_RESPONSE, SystemLog::EVENT_GATEWAY_FAILURE, 880 , $this->client , $this->company_gateway->company);
|
||||
|
||||
SystemLogger::dispatch(['server_response' => $th->getMessage(), 'data' => $data], SystemLog::CATEGORY_GATEWAY_RESPONSE, SystemLog::EVENT_GATEWAY_FAILURE, 880 , $this->company_gateway->client , $this->company_gateway->company);
|
||||
|
||||
throw $th;
|
||||
}
|
||||
@ -198,7 +268,7 @@ class RotessaPaymentDriver extends BaseDriver
|
||||
'code' =>(int) $th->getCode()
|
||||
];
|
||||
|
||||
SystemLogger::dispatch(['server_response' => is_null($result) ? '' : $result->getData(), 'data' => $data], SystemLog::CATEGORY_GATEWAY_RESPONSE, SystemLog::EVENT_GATEWAY_FAILURE, 880 , $this->client, $this->client->company);
|
||||
SystemLogger::dispatch(['server_response' => is_null($result) ? '' : (array) $result, 'data' => $data], SystemLog::CATEGORY_GATEWAY_RESPONSE, SystemLog::EVENT_GATEWAY_FAILURE, 880 , $this->client, $this->company_gateway->company);
|
||||
|
||||
throw $th;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user