2021-09-28 18:24:52 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2024-04-12 06:15:41 +02:00
|
|
|
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
|
2021-09-28 18:24:52 +02:00
|
|
|
*
|
2022-06-21 11:57:17 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2021-09-28 18:24:52 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\PaymentDrivers;
|
|
|
|
|
2023-03-02 01:19:46 +01:00
|
|
|
use App\Models\Client;
|
|
|
|
use App\Models\Country;
|
2022-11-07 05:06:16 +01:00
|
|
|
use App\Models\Invoice;
|
2021-09-28 18:24:52 +02:00
|
|
|
use App\Models\Payment;
|
2024-01-28 00:24:31 +01:00
|
|
|
use App\Models\SystemLog;
|
|
|
|
use App\Models\GatewayType;
|
2021-09-28 18:24:52 +02:00
|
|
|
use App\Models\PaymentHash;
|
2021-09-30 10:03:44 +02:00
|
|
|
use App\Models\PaymentType;
|
2024-01-28 00:24:31 +01:00
|
|
|
use App\Factory\ClientFactory;
|
|
|
|
use App\Jobs\Util\SystemLogger;
|
2023-03-22 09:37:47 +01:00
|
|
|
use App\Utils\Traits\MakesHash;
|
2024-01-28 00:24:31 +01:00
|
|
|
use App\Models\ClientGatewayToken;
|
|
|
|
use App\Factory\ClientContactFactory;
|
|
|
|
use App\Jobs\Mail\PaymentFailedMailer;
|
|
|
|
use App\Utils\Traits\GeneratesCounter;
|
2023-03-02 01:19:46 +01:00
|
|
|
use Illuminate\Database\QueryException;
|
2024-01-28 00:24:31 +01:00
|
|
|
use App\Http\Requests\Payments\PaymentWebhookRequest;
|
2021-09-28 18:24:52 +02:00
|
|
|
|
|
|
|
class GoCardlessPaymentDriver extends BaseDriver
|
|
|
|
{
|
|
|
|
use MakesHash;
|
2023-03-02 01:19:46 +01:00
|
|
|
use GeneratesCounter;
|
2021-09-28 18:24:52 +02:00
|
|
|
|
|
|
|
public $refundable = true;
|
|
|
|
|
|
|
|
public $token_billing = true;
|
|
|
|
|
|
|
|
public $can_authorise_credit_card = true;
|
|
|
|
|
2021-09-29 16:54:14 +02:00
|
|
|
public \GoCardlessPro\Client $gateway;
|
2021-09-28 18:24:52 +02:00
|
|
|
|
|
|
|
public $payment_method;
|
|
|
|
|
2023-03-02 01:19:46 +01:00
|
|
|
private bool $completed = true;
|
|
|
|
|
2021-09-28 18:24:52 +02:00
|
|
|
public static $methods = [
|
2023-03-22 07:45:33 +01:00
|
|
|
GatewayType::BANK_TRANSFER => \App\PaymentDrivers\GoCardless\DirectDebit::class,
|
2021-10-16 15:35:07 +02:00
|
|
|
GatewayType::DIRECT_DEBIT => \App\PaymentDrivers\GoCardless\DirectDebit::class,
|
2021-10-19 18:01:42 +02:00
|
|
|
GatewayType::SEPA => \App\PaymentDrivers\GoCardless\SEPA::class,
|
2021-11-11 15:57:01 +01:00
|
|
|
GatewayType::INSTANT_BANK_PAY => \App\PaymentDrivers\GoCardless\InstantBankPay::class,
|
2021-09-28 18:24:52 +02:00
|
|
|
];
|
|
|
|
|
2024-01-14 05:05:00 +01:00
|
|
|
public const SYSTEM_LOG_TYPE = SystemLog::TYPE_GOCARDLESS;
|
2021-09-28 18:24:52 +02:00
|
|
|
|
|
|
|
public function setPaymentMethod($payment_method_id)
|
|
|
|
{
|
|
|
|
$class = self::$methods[$payment_method_id];
|
|
|
|
|
|
|
|
$this->payment_method = new $class($this);
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2021-09-29 14:12:46 +02:00
|
|
|
public function gatewayTypes(): array
|
|
|
|
{
|
2021-09-29 14:17:30 +02:00
|
|
|
$types = [];
|
|
|
|
|
|
|
|
if (
|
|
|
|
$this->client
|
|
|
|
&& isset($this->client->country)
|
|
|
|
&& in_array($this->client->country->iso_3166_3, ['USA'])
|
|
|
|
) {
|
|
|
|
$types[] = GatewayType::BANK_TRANSFER;
|
|
|
|
}
|
|
|
|
|
2021-10-16 15:35:07 +02:00
|
|
|
if (
|
|
|
|
$this->client
|
|
|
|
&& isset($this->client->country)
|
2024-01-28 00:24:31 +01:00
|
|
|
&& in_array($this->client->currency()->code, ['EUR', 'GBP','DKK','SEK','AUD','NZD','CAD'])
|
2021-10-16 15:35:07 +02:00
|
|
|
) {
|
|
|
|
$types[] = GatewayType::DIRECT_DEBIT;
|
|
|
|
}
|
|
|
|
|
2024-03-18 02:58:42 +01:00
|
|
|
if ($this->client && in_array($this->client->currency()->code, ['EUR', 'GBP'])) {
|
2021-10-19 18:01:42 +02:00
|
|
|
$types[] = GatewayType::SEPA;
|
|
|
|
}
|
|
|
|
|
2024-03-18 02:58:42 +01:00
|
|
|
if ($this->client && $this->client->currency()->code === 'GBP') {
|
2021-11-11 17:51:04 +01:00
|
|
|
$types[] = GatewayType::INSTANT_BANK_PAY;
|
|
|
|
}
|
2024-02-13 05:25:18 +01:00
|
|
|
|
2021-09-29 14:17:30 +02:00
|
|
|
return $types;
|
2021-09-29 14:12:46 +02:00
|
|
|
}
|
|
|
|
|
2021-09-29 14:44:23 +02:00
|
|
|
public function init(): self
|
|
|
|
{
|
2023-10-03 05:50:47 +02:00
|
|
|
try {
|
|
|
|
$this->gateway = new \GoCardlessPro\Client([
|
|
|
|
'access_token' => $this->company_gateway->getConfigField('accessToken'),
|
|
|
|
'environment' => $this->company_gateway->getConfigField('testMode') ? \GoCardlessPro\Environment::SANDBOX : \GoCardlessPro\Environment::LIVE,
|
|
|
|
]);
|
2023-10-26 04:57:44 +02:00
|
|
|
} catch(\GoCardlessPro\Core\Exception\AuthenticationException $e) {
|
2023-10-03 05:50:47 +02:00
|
|
|
|
|
|
|
throw new \Exception('GoCardless: Invalid Access Token', 403);
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2023-10-03 05:50:47 +02:00
|
|
|
}
|
2021-09-29 14:44:23 +02:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2021-09-28 18:24:52 +02:00
|
|
|
public function authorizeView(array $data)
|
|
|
|
{
|
|
|
|
return $this->payment_method->authorizeView($data);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function authorizeResponse($request)
|
|
|
|
{
|
|
|
|
return $this->payment_method->authorizeResponse($request);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function processPaymentView(array $data)
|
|
|
|
{
|
|
|
|
return $this->payment_method->paymentView($data);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function processPaymentResponse($request)
|
|
|
|
{
|
|
|
|
return $this->payment_method->paymentResponse($request);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function refund(Payment $payment, $amount, $return_client_response = false)
|
|
|
|
{
|
|
|
|
// ..
|
|
|
|
}
|
|
|
|
|
|
|
|
public function tokenBilling(ClientGatewayToken $cgt, PaymentHash $payment_hash)
|
2023-03-22 07:45:33 +01:00
|
|
|
{
|
2021-09-30 10:03:44 +02:00
|
|
|
$amount = array_sum(array_column($payment_hash->invoices(), 'amount')) + $payment_hash->fee_total;
|
|
|
|
$converted_amount = $this->convertToGoCardlessAmount($amount, $this->client->currency()->precision);
|
|
|
|
|
|
|
|
$this->init();
|
|
|
|
|
2023-10-28 00:45:41 +02:00
|
|
|
if ($payment_hash->fee_invoice) {
|
|
|
|
$description = "Invoice {$payment_hash->fee_invoice->number} for {$amount} for client {$this->client->present()->name()}";
|
|
|
|
} else {
|
|
|
|
$description = "Amount {$amount} from client {$this->client->present()->name()}";
|
|
|
|
}
|
|
|
|
|
2021-09-30 10:03:44 +02:00
|
|
|
try {
|
|
|
|
$payment = $this->gateway->payments()->create([
|
|
|
|
'params' => [
|
|
|
|
'amount' => $converted_amount,
|
|
|
|
'currency' => $this->client->getCurrencyCode(),
|
2023-10-28 00:45:41 +02:00
|
|
|
'description' => $description,
|
2021-09-30 10:03:44 +02:00
|
|
|
'metadata' => [
|
|
|
|
'payment_hash' => $this->payment_hash->hash,
|
|
|
|
],
|
|
|
|
'links' => [
|
|
|
|
'mandate' => $cgt->token,
|
|
|
|
],
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
|
2023-12-11 12:42:50 +01:00
|
|
|
if (in_array($payment->status, ['submitted', 'pending_submission'])) {
|
2024-08-13 07:14:41 +02:00
|
|
|
|
2021-09-30 10:03:44 +02:00
|
|
|
$data = [
|
|
|
|
'payment_method' => $cgt->hashed_id,
|
|
|
|
'payment_type' => PaymentType::ACH,
|
|
|
|
'amount' => $amount,
|
|
|
|
'transaction_reference' => $payment->id,
|
|
|
|
'gateway_type_id' => GatewayType::BANK_TRANSFER,
|
|
|
|
];
|
|
|
|
|
2024-08-13 07:14:41 +02:00
|
|
|
$this->confirmGatewayFee($data);
|
|
|
|
|
2022-03-19 11:21:36 +01:00
|
|
|
$payment = $this->createPayment($data, Payment::STATUS_PENDING);
|
2021-09-30 10:03:44 +02:00
|
|
|
|
|
|
|
SystemLogger::dispatch(
|
|
|
|
['response' => $payment, 'data' => $data],
|
|
|
|
SystemLog::CATEGORY_GATEWAY_RESPONSE,
|
|
|
|
SystemLog::EVENT_GATEWAY_SUCCESS,
|
|
|
|
SystemLog::TYPE_GOCARDLESS,
|
|
|
|
$this->client,
|
|
|
|
$this->client->company
|
|
|
|
);
|
|
|
|
|
|
|
|
return $payment;
|
|
|
|
}
|
|
|
|
|
2021-10-17 12:40:40 +02:00
|
|
|
$this->sendFailureMail($payment->status);
|
2021-09-30 10:03:44 +02:00
|
|
|
|
|
|
|
$message = [
|
|
|
|
'server_response' => $payment,
|
|
|
|
'data' => $payment_hash->data,
|
|
|
|
];
|
|
|
|
|
|
|
|
SystemLogger::dispatch(
|
|
|
|
$message,
|
|
|
|
SystemLog::CATEGORY_GATEWAY_RESPONSE,
|
|
|
|
SystemLog::EVENT_GATEWAY_FAILURE,
|
|
|
|
SystemLog::TYPE_GOCARDLESS,
|
|
|
|
$this->client,
|
|
|
|
$this->client->company
|
|
|
|
);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
} catch (\Exception $exception) {
|
|
|
|
$this->unWindGatewayFees($this->payment_hash);
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'status' => '',
|
|
|
|
'error_type' => '',
|
|
|
|
'error_code' => $exception->getCode(),
|
|
|
|
'param' => '',
|
|
|
|
'message' => $exception->getMessage(),
|
|
|
|
];
|
|
|
|
|
|
|
|
SystemLogger::dispatch($data, SystemLog::CATEGORY_GATEWAY_RESPONSE, SystemLog::EVENT_GATEWAY_FAILURE, SystemLog::TYPE_GOCARDLESS, $this->client, $this->client->company);
|
|
|
|
}
|
2021-09-28 18:24:52 +02:00
|
|
|
}
|
2021-09-30 08:42:30 +02:00
|
|
|
|
|
|
|
public function convertToGoCardlessAmount($amount, $precision)
|
|
|
|
{
|
|
|
|
return \round(($amount * pow(10, $precision)), 0);
|
|
|
|
}
|
2021-09-30 14:55:46 +02:00
|
|
|
|
|
|
|
public function detach(ClientGatewayToken $token)
|
|
|
|
{
|
|
|
|
$this->init();
|
|
|
|
|
|
|
|
try {
|
|
|
|
$this->gateway->mandates()->cancel($token->token);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
nlog($e->getMessage());
|
|
|
|
|
|
|
|
SystemLogger::dispatch(
|
|
|
|
[
|
|
|
|
'server_response' => $e->getMessage(),
|
|
|
|
'data' => request()->all(),
|
|
|
|
],
|
|
|
|
SystemLog::CATEGORY_GATEWAY_RESPONSE,
|
|
|
|
SystemLog::EVENT_GATEWAY_FAILURE,
|
|
|
|
SystemLog::TYPE_GOCARDLESS,
|
|
|
|
$this->client,
|
|
|
|
$this->client->company
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2021-09-30 15:55:34 +02:00
|
|
|
|
|
|
|
public function processWebhookRequest(PaymentWebhookRequest $request)
|
|
|
|
{
|
|
|
|
// Allow app to catch up with webhook request.
|
|
|
|
$this->init();
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
nlog('GoCardless Event');
|
2022-04-22 03:59:37 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (! $request->has('events')) {
|
|
|
|
nlog('No GoCardless events to process in response?');
|
2022-01-10 22:55:11 +01:00
|
|
|
|
|
|
|
return response()->json([], 200);
|
|
|
|
}
|
|
|
|
|
2022-05-13 23:30:40 +02:00
|
|
|
sleep(1);
|
|
|
|
|
2021-09-30 15:55:34 +02:00
|
|
|
foreach ($request->events as $event) {
|
2022-06-13 12:34:40 +02:00
|
|
|
if ($event['action'] === 'confirmed' || $event['action'] === 'paid_out') {
|
2022-06-21 11:57:17 +02:00
|
|
|
nlog('Searching for transaction reference');
|
2022-05-13 23:30:40 +02:00
|
|
|
|
2021-09-30 15:55:34 +02:00
|
|
|
$payment = Payment::query()
|
|
|
|
->where('transaction_reference', $event['links']['payment'])
|
2022-06-10 23:02:24 +02:00
|
|
|
->where('company_id', $request->getCompany()->id)
|
2021-09-30 15:55:34 +02:00
|
|
|
->first();
|
|
|
|
|
|
|
|
if ($payment) {
|
|
|
|
$payment->status_id = Payment::STATUS_COMPLETED;
|
|
|
|
$payment->save();
|
2022-06-21 11:57:17 +02:00
|
|
|
nlog('GoCardless completed');
|
|
|
|
} else {
|
|
|
|
nlog('I was unable to find the payment for this reference');
|
2021-09-30 15:55:34 +02:00
|
|
|
}
|
2022-03-19 11:33:53 +01:00
|
|
|
//finalize payments on invoices here.
|
2021-09-30 15:55:34 +02:00
|
|
|
}
|
|
|
|
|
2022-09-08 07:58:34 +02:00
|
|
|
if ($event['action'] === 'failed' && array_key_exists('payment', $event['links'])) {
|
2021-09-30 15:55:34 +02:00
|
|
|
$payment = Payment::query()
|
|
|
|
->where('transaction_reference', $event['links']['payment'])
|
2022-06-10 23:02:24 +02:00
|
|
|
->where('company_id', $request->getCompany()->id)
|
2021-09-30 15:55:34 +02:00
|
|
|
->first();
|
|
|
|
|
|
|
|
if ($payment) {
|
2023-03-22 07:45:33 +01:00
|
|
|
if ($payment->status_id == Payment::STATUS_PENDING) {
|
|
|
|
$payment->service()->deletePayment();
|
|
|
|
}
|
|
|
|
|
2021-09-30 15:55:34 +02:00
|
|
|
$payment->status_id = Payment::STATUS_FAILED;
|
|
|
|
$payment->save();
|
2023-03-22 07:45:33 +01:00
|
|
|
|
|
|
|
$payment_hash = PaymentHash::where('payment_id', $payment->id)->first();
|
|
|
|
$error = '';
|
|
|
|
|
|
|
|
if (isset($event['details']['description'])) {
|
|
|
|
$error = $event['details']['description'];
|
|
|
|
}
|
|
|
|
|
|
|
|
PaymentFailedMailer::dispatch(
|
|
|
|
$payment_hash,
|
|
|
|
$payment->client->company,
|
|
|
|
$payment->client,
|
|
|
|
$error
|
|
|
|
);
|
2021-09-30 15:55:34 +02:00
|
|
|
}
|
|
|
|
}
|
2022-11-07 03:37:29 +01:00
|
|
|
|
|
|
|
//billing_request fulfilled
|
2022-11-07 04:24:33 +01:00
|
|
|
//
|
2022-11-07 03:37:29 +01:00
|
|
|
|
|
|
|
//i need to build more context here, i need the client , the payment hash resolved and update the class properties.
|
2022-11-07 03:38:12 +01:00
|
|
|
//after i resolve the payment hash, ensure the invoice has not been marked as paid and the payment does not already exist.
|
|
|
|
//if it does exist, ensure it is completed and not pending.
|
2022-11-07 03:37:29 +01:00
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
if ($event['action'] == 'fulfilled' && array_key_exists('billing_request', $event['links'])) {
|
2022-11-07 04:24:33 +01:00
|
|
|
$hash = PaymentHash::whereJsonContains('data->billing_request', $event['links']['billing_request'])->first();
|
2022-11-07 03:37:29 +01:00
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
if (!$hash) {
|
2022-11-07 05:06:16 +01:00
|
|
|
nlog("GoCardless: couldn't find a hash, need to abort => Billing Request => " . $event['links']['billing_request']);
|
2022-11-08 22:09:42 +01:00
|
|
|
return response()->json([], 200);
|
2022-11-07 04:24:33 +01:00
|
|
|
}
|
|
|
|
|
2022-11-16 12:52:26 +01:00
|
|
|
$this->setPaymentHash($hash);
|
2022-11-07 04:24:33 +01:00
|
|
|
|
2022-11-16 12:52:26 +01:00
|
|
|
$billing_request = $this->gateway->billingRequests()->get(
|
2022-11-07 04:24:33 +01:00
|
|
|
$event['links']['billing_request']
|
|
|
|
);
|
2022-11-07 03:37:29 +01:00
|
|
|
|
2022-11-16 12:52:26 +01:00
|
|
|
$payment = $this->gateway->payments()->get(
|
2022-11-07 04:24:33 +01:00
|
|
|
$billing_request->payment_request->links->payment
|
|
|
|
);
|
|
|
|
|
2022-11-07 05:06:16 +01:00
|
|
|
if ($billing_request->status === 'fulfilled') {
|
2023-08-06 09:03:12 +02:00
|
|
|
$invoices = Invoice::query()->whereIn('id', $this->transformKeys(array_column($hash->invoices(), 'invoice_id')))->withTrashed()->get();
|
2022-11-07 05:06:16 +01:00
|
|
|
|
2022-11-16 12:52:26 +01:00
|
|
|
$this->client = $invoices->first()->client;
|
2022-11-07 05:06:16 +01:00
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
$invoices->each(function ($invoice) {
|
2022-11-07 05:06:16 +01:00
|
|
|
//if payments exist already, they just need to be confirmed.
|
2023-02-16 02:36:09 +01:00
|
|
|
if ($invoice->payments()->exists()) {
|
|
|
|
$invoice->payments()->where('status_id', 1)->cursor()->each(function ($payment) {
|
2022-11-07 05:06:16 +01:00
|
|
|
$payment->status_id = 4;
|
|
|
|
$payment->save();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// remove all paid invoices
|
2023-02-16 02:36:09 +01:00
|
|
|
$invoices->filter(function ($invoice) {
|
2022-11-07 05:06:16 +01:00
|
|
|
return $invoice->isPayable();
|
|
|
|
});
|
|
|
|
|
|
|
|
//return early if nothing to do
|
2023-02-16 02:36:09 +01:00
|
|
|
if ($invoices->count() == 0) {
|
2022-11-07 05:06:16 +01:00
|
|
|
nlog("GoCardless: Could not harvest any invoices - probably all paid!!");
|
2022-11-08 22:09:42 +01:00
|
|
|
return response()->json([], 200);
|
2022-11-07 04:24:33 +01:00
|
|
|
}
|
|
|
|
|
2022-11-07 05:06:16 +01:00
|
|
|
$this->processSuccessfulPayment($payment);
|
|
|
|
}
|
2022-11-07 04:24:33 +01:00
|
|
|
}
|
2021-09-30 15:55:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return response()->json([], 200);
|
|
|
|
}
|
2021-11-05 17:01:19 +01:00
|
|
|
|
2022-11-07 03:37:29 +01:00
|
|
|
|
|
|
|
public function processSuccessfulPayment(\GoCardlessPro\Resources\Payment $payment, array $data = [])
|
|
|
|
{
|
|
|
|
$data = [
|
|
|
|
'payment_method' => $payment->links->mandate,
|
|
|
|
'payment_type' => PaymentType::INSTANT_BANK_PAY,
|
2022-11-16 12:52:26 +01:00
|
|
|
'amount' => $this->payment_hash->data->amount_with_fee,
|
2022-11-07 03:37:29 +01:00
|
|
|
'transaction_reference' => $payment->id,
|
|
|
|
'gateway_type_id' => GatewayType::INSTANT_BANK_PAY,
|
|
|
|
];
|
|
|
|
|
2022-11-16 12:52:26 +01:00
|
|
|
$payment = $this->createPayment($data, Payment::STATUS_COMPLETED);
|
2022-11-07 21:58:24 +01:00
|
|
|
$payment->status_id = Payment::STATUS_COMPLETED;
|
|
|
|
$payment->save();
|
2022-11-07 03:37:29 +01:00
|
|
|
|
|
|
|
SystemLogger::dispatch(
|
|
|
|
['response' => $payment, 'data' => $data],
|
|
|
|
SystemLog::CATEGORY_GATEWAY_RESPONSE,
|
|
|
|
SystemLog::EVENT_GATEWAY_SUCCESS,
|
|
|
|
SystemLog::TYPE_GOCARDLESS,
|
2022-11-17 03:21:37 +01:00
|
|
|
$this->client,
|
|
|
|
$this->client->company,
|
2022-11-07 03:37:29 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-11-25 11:45:52 +01:00
|
|
|
public function ensureMandateIsReady($token)
|
2021-11-05 17:01:19 +01:00
|
|
|
{
|
|
|
|
try {
|
2021-11-25 11:45:52 +01:00
|
|
|
$this->init();
|
|
|
|
$mandate = $this->gateway->mandates()->get($token);
|
2021-11-05 17:01:19 +01:00
|
|
|
|
2023-11-03 23:53:20 +01:00
|
|
|
if(!in_array($mandate->status, ['pending_submission', 'submitted', 'active','pending_customer_approval'])) {
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2023-11-26 08:41:42 +01:00
|
|
|
// if ($mandate->status !== 'active') {
|
2021-11-05 17:01:19 +01:00
|
|
|
throw new \Exception(ctrans('texts.gocardless_mandate_not_ready'));
|
|
|
|
}
|
|
|
|
} catch (\Exception $exception) {
|
|
|
|
throw new \App\Exceptions\PaymentFailed($exception->getMessage());
|
|
|
|
}
|
|
|
|
}
|
2023-03-02 01:19:46 +01:00
|
|
|
|
|
|
|
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");
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2023-03-02 01:19:46 +01:00
|
|
|
$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
|
|
|
|
{
|
2023-03-02 04:32:47 +01:00
|
|
|
$this->client = $client;
|
|
|
|
|
|
|
|
$mandates = $this->gateway->mandates()->list();
|
|
|
|
|
2023-03-18 08:24:56 +01:00
|
|
|
foreach ($mandates->records as $mandate) {
|
|
|
|
if ($customer->id != $mandate->links->customer || $mandate->status != 'active' || ClientGatewayToken::where('token', $mandate->id)->where('gateway_customer_reference', $customer->id)->exists()) {
|
2023-03-02 04:32:47 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-01-14 05:05:00 +01:00
|
|
|
$payment_meta = new \stdClass();
|
2023-03-02 04:32:47 +01:00
|
|
|
|
|
|
|
if ($mandate->scheme == 'bacs') {
|
|
|
|
$payment_meta->brand = ctrans('texts.payment_type_direct_debit');
|
|
|
|
$payment_meta->type = GatewayType::DIRECT_DEBIT;
|
2023-03-18 08:24:56 +01:00
|
|
|
} elseif ($mandate->scheme == 'sepa_core') {
|
2023-03-02 04:32:47 +01:00
|
|
|
$payment_meta->brand = ctrans('texts.sepa');
|
|
|
|
$payment_meta->type = GatewayType::SEPA;
|
2023-03-18 08:24:56 +01:00
|
|
|
} else {
|
2023-03-02 04:32:47 +01:00
|
|
|
continue;
|
|
|
|
}
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2023-03-02 04:32:47 +01:00
|
|
|
$payment_meta->state = 'authorized';
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'payment_meta' => $payment_meta,
|
|
|
|
'token' => $mandate->id,
|
|
|
|
'payment_method_id' => GatewayType::DIRECT_DEBIT,
|
|
|
|
];
|
|
|
|
|
|
|
|
$payment_method = $this->storeGatewayToken($data, ['gateway_customer_reference' => $mandate->links->customer]);
|
|
|
|
}
|
2023-03-02 01:19:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
[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 ?: '';
|
2023-03-02 04:32:47 +01:00
|
|
|
$client->postal_code = $customer->postal_code ?: '';
|
|
|
|
$client->phone = $customer->phone_number ? $customer->phone_number : '';
|
2023-03-02 01:19:46 +01:00
|
|
|
$client->name = $customer->company_name;
|
|
|
|
|
2023-03-02 04:32:47 +01:00
|
|
|
if ($customer->country_code) {
|
2023-03-02 01:19:46 +01:00
|
|
|
$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);
|
2023-03-02 04:32:47 +01:00
|
|
|
$contact->first_name = $customer->given_name ?: '';
|
|
|
|
$contact->last_name = $customer->family_name ?: '';
|
2023-03-02 01:19:46 +01:00
|
|
|
$contact->email = $customer->email ?: '';
|
|
|
|
$contact->client_id = $client->id;
|
2023-03-02 04:37:32 +01:00
|
|
|
$contact->saveQuietly();
|
2024-01-14 05:05:00 +01:00
|
|
|
|
2023-03-02 01:19:46 +01:00
|
|
|
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;
|
|
|
|
}
|
2023-04-25 00:06:27 +02:00
|
|
|
|
|
|
|
public function verificationView()
|
|
|
|
{
|
|
|
|
return render('gateways.gocardless.verification');
|
|
|
|
}
|
2024-03-16 02:36:40 +01:00
|
|
|
|
|
|
|
public function auth(): bool
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$customers = $this->init()->gateway->customers()->list();
|
|
|
|
return true;
|
2024-06-14 09:09:44 +02:00
|
|
|
} catch(\Exception $e) {
|
2024-03-16 02:36:40 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2021-09-28 18:24:52 +02:00
|
|
|
}
|