mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-07-09 03:14:30 -04:00
refactor classes to facilitiate import customers
This commit is contained in:
parent
94a4605e38
commit
dbfc1046ec
@ -54,12 +54,12 @@ class PaymentMethod implements MethodInterface
|
||||
{
|
||||
$data['contact'] = collect($data['client']->contacts->firstWhere('is_primary', 1)->toArray())->merge([
|
||||
'home_phone' => $data['client']->phone,
|
||||
'custom_identifier' => $data['client']->number . substr(uniqid(),0,4),
|
||||
'custom_identifier' => $data['client']->number,
|
||||
'name' => $data['client']->name,
|
||||
'id' => null
|
||||
] )->all();
|
||||
$data['gateway'] = $this->rotessa;
|
||||
$data['gateway_type_id'] = (int) request('method');
|
||||
$data['gateway_type_id'] = $data['client']->country->iso_3166_2 == 'US' ? GatewayType::BANK_TRANSFER : ( $data['client']->country->iso_3166_2 == 'CA' ? GatewayType::ACSS : (int) request('method'));
|
||||
$data['account'] = [
|
||||
'routing_number' => $data['client']->routing_id,
|
||||
'country' => $data['client']->country->iso_3166_2
|
||||
@ -68,54 +68,6 @@ class PaymentMethod implements MethodInterface
|
||||
|
||||
return view('rotessa::bank_transfer.authorize', $data);
|
||||
}
|
||||
|
||||
protected function findOrCreateCustomer(Request $request)
|
||||
{
|
||||
$result = null; $data = [];
|
||||
$customer = new Customer(
|
||||
$request->merge(['id' => $request->input('id') ] +
|
||||
['address' => $request->only('address_1','address_2','city','postal_code','province_code','country') ])->all()
|
||||
);
|
||||
try {
|
||||
$existing = ClientGatewayToken::query()
|
||||
->where('company_gateway_id', $this->rotessa->company_gateway->id)
|
||||
->where('client_id', $this->rotessa->client->id)
|
||||
->first();
|
||||
$data = array_filter(Arr::except($customer->jsonSerialize(),['custom_identifier']));
|
||||
if ($existing && $existing->token == encrypt($data)) return $existing->gateway_customer_reference;
|
||||
|
||||
$result = $this->rotessa->gateway->authorize($customer->resolve())->send();
|
||||
if ($result->isSuccessful()) {
|
||||
$customer = new Customer($result->getData());
|
||||
$data = array_filter(Arr::except($customer->jsonSerialize(),['custom_identifier']));
|
||||
$this->rotessa->storeGatewayToken( [
|
||||
'payment_meta' => $customer->resolve() + ['brand' => 'Rotessa'],
|
||||
'token' => encrypt($data),
|
||||
'payment_method_id' => (int) $request->input("gateway_type_id"),
|
||||
], ['gateway_customer_reference' =>
|
||||
$result->getParameter('id')
|
||||
, 'routing_number' => $result->getParameter('routing_number') ?? $result->getParameter('transit_number')]);
|
||||
|
||||
return $result->getParameter('id');
|
||||
}
|
||||
|
||||
throw new \Exception($result->getMessage(), (int) $result->getCode());
|
||||
|
||||
} catch (\Throwable $th) {
|
||||
$data = [
|
||||
'transaction_reference' => null,
|
||||
'transaction_response' => $th->getMessage(),
|
||||
'success' => false,
|
||||
'description' => $th->getMessage(),
|
||||
'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->rotessa->client, $this->rotessa->client->company);
|
||||
|
||||
throw $th;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the authorization page for Rotessa.
|
||||
*
|
||||
@ -147,7 +99,11 @@ class PaymentMethod implements MethodInterface
|
||||
'custom_identifier'=>['required_without:customer_id'],
|
||||
'customer_id'=>['required_without:custom_identifier','integer'],
|
||||
]);
|
||||
$this->findOrCreateCustomer($request);
|
||||
$customer = new Customer(
|
||||
$request->merge(['custom_identifier' => $request->input('id') ] +
|
||||
['address' => $request->only('address_1','address_2','city','postal_code','province_code','country') ])->all()
|
||||
);
|
||||
$this->rotessa->findOrCreateCustomer($customer->resolve());
|
||||
|
||||
return redirect()->route('client.payment_methods.index')->withMessage(ctrans('texts.payment_method_added'));
|
||||
|
||||
|
@ -18,12 +18,14 @@ use App\Models\PaymentHash;
|
||||
use Illuminate\Support\Arr;
|
||||
use App\Models\GatewayType;
|
||||
use Omnipay\Rotessa\Gateway;
|
||||
use App\Models\ClientContact;
|
||||
use App\Utils\Traits\MakesHash;
|
||||
use App\Jobs\Util\SystemLogger;
|
||||
use App\PaymentDrivers\BaseDriver;
|
||||
use App\PaymentDrivers\Rotessa\Bacs;
|
||||
use App\Models\ClientGatewayToken;
|
||||
use App\PaymentDrivers\Rotessa\Acss;
|
||||
use App\PaymentDrivers\Rotessa\DirectDebit;
|
||||
use App\PaymentDrivers\Rotessa\BankTransfer;
|
||||
use App\PaymentDrivers\Rotessa\Resources\Customer;
|
||||
|
||||
class RotessaPaymentDriver extends BaseDriver
|
||||
{
|
||||
@ -41,9 +43,9 @@ class RotessaPaymentDriver extends BaseDriver
|
||||
|
||||
public static $methods = [
|
||||
GatewayType::BANK_TRANSFER => BankTransfer::class,
|
||||
GatewayType::BACS => Bacs::class,
|
||||
//GatewayType::BACS => Bacs::class,
|
||||
GatewayType::ACSS => Acss::class,
|
||||
GatewayType::DIRECT_DEBIT => DirectDebit::class
|
||||
// GatewayType::DIRECT_DEBIT => DirectDebit::class
|
||||
];
|
||||
|
||||
public function init(): self
|
||||
@ -61,18 +63,18 @@ class RotessaPaymentDriver extends BaseDriver
|
||||
$types = [];
|
||||
|
||||
if ($this->client
|
||||
&& $this->client->currency()
|
||||
&& in_array($this->client->currency()->code, ['USD'])
|
||||
&& isset($this->client->country)
|
||||
&& (in_array($this->client->country->iso_3166_2, ['US']) || ($this->client->gateway_tokens()->where('gateway_type_id', GatewayType::BANK_TRANSFER)->exists()))
|
||||
) {
|
||||
&& in_array($this->client->country->iso_3166_2, ['US'])) {
|
||||
$types[] = GatewayType::BANK_TRANSFER;
|
||||
}
|
||||
|
||||
if ($this->client
|
||||
&& $this->client->currency()
|
||||
&& in_array($this->client->currency()->code, ['CAD', 'USD'])
|
||||
&& in_array($this->client->currency()->code, ['CAD'])
|
||||
&& isset($this->client->country)
|
||||
&& in_array($this->client->country->iso_3166_2, ['CA', 'US'])) {
|
||||
$types[] = GatewayType::DIRECT_DEBIT;
|
||||
&& in_array($this->client->country->iso_3166_2, ['CA'])) {
|
||||
$types[] = GatewayType::ACSS;
|
||||
}
|
||||
|
||||
@ -108,5 +110,85 @@ class RotessaPaymentDriver extends BaseDriver
|
||||
return $this->payment_method->paymentResponse($request);
|
||||
}
|
||||
|
||||
public function importCustomers() {
|
||||
try {
|
||||
$customers = collect($this->gateway->getCustomers()->getData())->pluck('email','id');
|
||||
$client_emails = $customers->pluck('email')->all();
|
||||
$company_id = $this->company_gateway->company->id;
|
||||
$client_contacts = ClientContact::select('email','id','client_id',)->where('company_id', $companY_id)->whereIn('email', $client_emails )->whereNull('deleted_at')->where('is_deleted', false)->get();
|
||||
$client_contacts->each(
|
||||
function($contact) use ($customers) {
|
||||
$result = $this->gateway->postCustomersShowWithCustomIdentifier(['custom_identifier' => $contact->id ]);
|
||||
$customer = new Customer($result->getData());
|
||||
$this->findOrCreateCustomer(array_filter($customer->toArray()));
|
||||
}
|
||||
);
|
||||
} catch (\Throwable $th) {
|
||||
$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->client->company);
|
||||
|
||||
|
||||
throw $th;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
protected function findOrCreateCustomer(array $customer)
|
||||
{
|
||||
$result = null; $data = []; $id = null;
|
||||
|
||||
try {
|
||||
|
||||
$id = $data['id'] ?? null;
|
||||
$existing = ClientGatewayToken::query()
|
||||
->where('company_gateway_id', $this->company_gateway->id)
|
||||
->where('client_id', $this->client->id)
|
||||
->orWhere(function (Builder $query) use ($data, $id) {
|
||||
$uqery->where('token', encrypt($data))
|
||||
->where('gateway_customer_reference', $id);
|
||||
})
|
||||
->exists();
|
||||
if ($existing) return $existing->gateway_customer_reference;
|
||||
else if(is_null($id)) {
|
||||
$result = $this->gateway->authorize($data)->send();
|
||||
if ($result->isSuccessful()) {
|
||||
$customer = new Customer($result->getData());
|
||||
$data = array_filter($customer->toArray());
|
||||
}
|
||||
}
|
||||
|
||||
$this->storeGatewayToken( [
|
||||
'payment_meta' => $data + ['brand' => 'Rotessa'],
|
||||
'token' => encrypt($data),
|
||||
'payment_method_id' => (int) $request->input("gateway_type_id"),
|
||||
], ['gateway_customer_reference' =>
|
||||
$result->getParameter('id')
|
||||
, 'routing_number' => $result->getParameter('routing_number') ?? $result->getParameter('transit_number')]);
|
||||
|
||||
return $result->getParameter('id');
|
||||
|
||||
throw new \Exception($result->getMessage(), (int) $result->getCode());
|
||||
|
||||
} catch (\Throwable $th) {
|
||||
$data = [
|
||||
'transaction_reference' => null,
|
||||
'transaction_response' => $th->getMessage(),
|
||||
'success' => false,
|
||||
'description' => $th->getMessage(),
|
||||
'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);
|
||||
|
||||
throw $th;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user