1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-15 07:33:04 +01:00
invoiceninja/app/PaymentDrivers/GoCardless/DirectDebit.php

334 lines
12 KiB
PHP
Raw Normal View History

2021-10-16 15:27:56 +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-10-16 15:27:56 +02:00
*
* @license https://www.elastic.co/licensing/elastic-license
2021-10-16 15:27:56 +02:00
*/
namespace App\PaymentDrivers\GoCardless;
2021-10-16 15:48:26 +02:00
use App\Exceptions\PaymentFailed;
2021-10-16 15:27:56 +02:00
use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest;
2021-10-16 15:55:33 +02:00
use App\Jobs\Mail\PaymentFailureMailer;
2021-10-16 15:48:26 +02:00
use App\Jobs\Util\SystemLogger;
use App\Models\GatewayType;
2021-12-08 10:46:15 +01:00
use App\Models\Invoice;
2021-10-16 15:55:33 +02:00
use App\Models\Payment;
use App\Models\PaymentType;
2021-10-16 15:48:26 +02:00
use App\Models\SystemLog;
2021-10-16 15:27:56 +02:00
use App\PaymentDrivers\Common\MethodInterface;
2021-10-16 15:36:16 +02:00
use App\PaymentDrivers\GoCardlessPaymentDriver;
2021-10-16 15:55:33 +02:00
use App\Utils\Traits\MakesHash;
2021-10-16 15:48:26 +02:00
use Illuminate\Http\RedirectResponse;
2021-12-08 10:46:15 +01:00
use Illuminate\Http\Request;
2021-10-16 15:48:26 +02:00
use Illuminate\Routing\Redirector;
2021-10-16 15:55:33 +02:00
use Illuminate\View\View;
2021-10-16 15:27:56 +02:00
class DirectDebit implements MethodInterface
{
2021-10-19 16:16:57 +02:00
use MakesHash;
2021-10-16 15:55:33 +02:00
2021-10-16 15:36:16 +02:00
protected GoCardlessPaymentDriver $go_cardless;
public function __construct(GoCardlessPaymentDriver $go_cardless)
{
$this->go_cardless = $go_cardless;
$this->go_cardless->init();
}
2021-10-16 15:48:26 +02:00
/**
* Handle authorization for Direct Debit.
2021-10-19 16:16:57 +02:00
*
* @param array $data
* @return Redirector|RedirectResponse|void
2021-10-16 15:48:26 +02:00
*/
public function authorizeView(array $data)
2023-03-22 05:04:04 +01:00
{
return $this->billingRequestFlows($data);
}
/**
* Response
* {
* "billing_request_flows": {
* "authorisation_url": "https://pay.gocardless.com/flow/static/billing_request?id=<br_id>",
* "lock_customer_details": false,
* "lock_bank_account": false,
* "auto_fulfil": true,
* "created_at": "2021-03-30T16:23:10.679Z",
* "expires_at": "2021-04-06T16:23:10.679Z",
* "redirect_uri": "https://my-company.com/completed",
* "links": {
* "billing_request": "BRQ123"
* }
* }
* }
2023-10-26 04:57:44 +02:00
*
*
2023-03-22 05:04:04 +01:00
*/
public function billingRequestFlows(array $data)
2021-10-16 15:48:26 +02:00
{
$session_token = \Illuminate\Support\Str::uuid()->toString();
2023-03-22 05:04:04 +01:00
$exit_uri = route('client.payment_methods.index');
$response = $this->go_cardless->gateway->billingRequests()->create([
"params" => [
"mandate_request" => [
2023-10-28 02:13:55 +02:00
"currency" => auth()->guard('contact')->user()->client->currency()->code,
"verify" => "when_available"
2023-03-22 05:04:04 +01:00
]
]
]);
2021-10-16 15:48:26 +02:00
try {
2023-03-22 05:04:04 +01:00
$brf = $this->go_cardless->gateway->billingRequestFlows()->create([
"params" => [
"redirect_uri" => route('client.payment_methods.confirm', [
'method' => GatewayType::DIRECT_DEBIT,
'session_token' => $session_token,
'billing_request' => $response->id,
]),
"exit_uri" => $exit_uri,
"links" => [
"billing_request" => $response->id
2021-10-16 15:48:26 +02:00
],
2023-03-22 05:04:04 +01:00
"show_redirect_buttons" => true,
"show_success_redirect_button" => true,
]
2021-10-16 15:48:26 +02:00
]);
2023-03-22 05:04:04 +01:00
return redirect($brf->authorisation_url);
2021-10-16 15:48:26 +02:00
} catch (\Exception $exception) {
2023-03-22 05:04:04 +01:00
nlog($exception->getMessage());
2021-10-16 15:48:26 +02:00
return $this->processUnsuccessfulAuthorization($exception);
}
2023-03-22 05:04:04 +01:00
2021-10-16 15:48:26 +02:00
}
2023-03-22 05:04:04 +01:00
2021-10-16 15:48:26 +02:00
/**
* Handle unsuccessful authorization.
*
* @param \Exception $exception
2021-10-16 15:48:26 +02:00
* @throws PaymentFailed
* @return void
*/
public function processUnsuccessfulAuthorization(\Exception $exception): void
{
SystemLogger::dispatch(
$exception->getMessage(),
SystemLog::CATEGORY_GATEWAY_RESPONSE,
SystemLog::EVENT_GATEWAY_FAILURE,
SystemLog::TYPE_GOCARDLESS,
$this->go_cardless->client,
$this->go_cardless->client->company,
);
throw new PaymentFailed($exception->getMessage(), $exception->getCode());
}
/**
* Handle authorization response for Direct Debit.
2021-10-19 16:16:57 +02:00
*
* @param Request $request
* @return RedirectResponse|void
2021-10-16 15:48:26 +02:00
*/
public function authorizeResponse(Request $request)
{
2023-03-22 05:04:04 +01:00
2023-10-26 04:57:44 +02:00
try {
2024-01-14 05:05:00 +01:00
2023-03-22 05:04:04 +01:00
$billing_request = $this->go_cardless->gateway->billingRequests()->get($request->billing_request);
2021-10-16 15:48:26 +02:00
2024-01-14 05:05:00 +01:00
$payment_meta = new \stdClass();
2023-03-22 05:04:04 +01:00
$payment_meta->brand = $billing_request->resources->customer_bank_account->bank_name;
2023-03-22 07:45:33 +01:00
$payment_meta->type = $this->resolveScheme($billing_request->mandate_request->scheme);
2023-03-22 05:04:04 +01:00
$payment_meta->state = 'pending';
$payment_meta->last4 = $billing_request->resources->customer_bank_account->account_number_ending;
2021-10-16 15:27:56 +02:00
2021-10-16 15:48:26 +02:00
$data = [
'payment_meta' => $payment_meta,
2023-03-22 05:04:04 +01:00
'token' => $billing_request->mandate_request->links->mandate,
'payment_method_id' => $this->resolveScheme($billing_request->mandate_request->scheme),
2021-10-16 15:48:26 +02:00
];
2023-03-22 05:04:04 +01:00
$payment_method = $this->go_cardless->storeGatewayToken($data, ['gateway_customer_reference' => $billing_request->resources->customer->id]);
$mandate = $this->go_cardless->gateway->mandates()->get($billing_request->mandate_request->links->mandate);
2024-01-14 05:05:00 +01:00
2023-03-22 05:04:04 +01:00
nlog($mandate);
2021-10-16 15:48:26 +02:00
return redirect()->route('client.payment_methods.show', $payment_method->hashed_id);
2023-03-22 05:04:04 +01:00
2023-10-26 04:57:44 +02:00
} catch (\Exception $exception) {
2021-10-16 15:48:26 +02:00
return $this->processUnsuccessfulAuthorization($exception);
}
2023-03-22 05:04:04 +01:00
// try {
// $redirect_flow = $this->go_cardless->gateway->redirectFlows()->complete(
// $request->redirect_flow_id,
// ['params' => [
// 'session_token' => $request->session_token,
// ]],
// );
// $payment_meta = new \stdClass;
// $payment_meta->brand = ctrans('texts.payment_type_direct_debit');
// $payment_meta->type = GatewayType::DIRECT_DEBIT;
// $payment_meta->state = 'authorized';
// $data = [
// 'payment_meta' => $payment_meta,
// 'token' => $redirect_flow->links->mandate,
// 'payment_method_id' => $this->resolveScheme($redirect_flow->scheme),
// ];
// $payment_method = $this->go_cardless->storeGatewayToken($data, ['gateway_customer_reference' => $redirect_flow->links->customer]);
// return redirect()->route('client.payment_methods.show', $payment_method->hashed_id);
// } catch (\Exception $exception) {
// return $this->processUnsuccessfulAuthorization($exception);
// }
2021-10-16 15:48:26 +02:00
}
2021-10-16 15:27:56 +02:00
2023-03-17 23:56:03 +01:00
private function resolveScheme(string $scheme): int
{
2023-03-18 08:24:56 +01:00
match ($scheme) {
2023-03-17 23:56:03 +01:00
'sepa_core' => $type = GatewayType::SEPA,
2023-03-22 07:45:33 +01:00
'ach' => $type = GatewayType::BANK_TRANSFER,
2023-03-17 23:56:03 +01:00
default => $type = GatewayType::DIRECT_DEBIT,
};
return $type;
}
2021-10-16 15:55:33 +02:00
/**
* Payment view for Direct Debit.
2021-10-19 16:16:57 +02:00
*
* @param array $data
2023-10-26 04:57:44 +02:00
* @return \Illuminate\View\View
2021-10-16 15:55:33 +02:00
*/
public function paymentView(array $data): View
{
$data['gateway'] = $this->go_cardless;
$data['amount'] = $this->go_cardless->convertToGoCardlessAmount($data['total']['amount_with_fee'], $this->go_cardless->client->currency()->precision);
$data['currency'] = $this->go_cardless->client->getCurrencyCode();
2021-10-19 18:57:19 +02:00
return render('gateways.gocardless.direct_debit.pay', $data);
2021-10-16 15:55:33 +02:00
}
public function paymentResponse(PaymentResponseRequest $request)
{
2021-11-25 11:45:52 +01:00
$this->go_cardless->ensureMandateIsReady($request->source);
2023-08-06 09:03:12 +02:00
$invoice = Invoice::query()->whereIn('id', $this->transformKeys(array_column($this->go_cardless->payment_hash->invoices(), 'invoice_id')))
2021-12-08 10:46:15 +01:00
->withTrashed()
->first();
if ($invoice) {
2021-12-08 10:46:15 +01:00
$description = "Invoice {$invoice->number} for {$request->amount} for client {$this->go_cardless->client->present()->name()}";
} else {
2021-12-08 10:46:15 +01:00
$description = "Amount {$request->amount} from client {$this->go_cardless->client->present()->name()}";
}
2021-12-08 10:46:15 +01:00
2023-03-22 05:04:04 +01:00
$amount = $this->go_cardless->convertToGoCardlessAmount($this->go_cardless->payment_hash?->amount_with_fee(), $this->go_cardless->client->currency()->precision);
2021-10-16 15:55:33 +02:00
try {
$payment = $this->go_cardless->gateway->payments()->create([
'params' => [
// 'amount' => $request->amount,
2023-03-22 05:04:04 +01:00
'amount' => $amount,
2021-10-16 15:55:33 +02:00
'currency' => $request->currency,
2021-12-08 10:46:15 +01:00
'description' => $description,
2021-10-16 15:55:33 +02:00
'metadata' => [
'payment_hash' => $this->go_cardless->payment_hash->hash,
],
'links' => [
2021-11-25 11:45:52 +01:00
'mandate' => $request->source,
2021-10-16 15:55:33 +02:00
],
],
]);
if ($payment->status === 'pending_submission') {
2021-11-25 11:45:52 +01:00
return $this->processPendingPayment($payment, ['token' => $request->source]);
2021-10-16 15:55:33 +02:00
}
return $this->processUnsuccessfulPayment($payment);
} catch (\Exception $exception) {
throw new PaymentFailed($exception->getMessage(), $exception->getCode());
}
}
2021-10-19 16:16:57 +02:00
/**
2021-10-16 15:55:33 +02:00
* Handle pending payments for Direct Debit.
*
* @param ResourcesPayment $payment
* @param array $data
* @return RedirectResponse
*/
public function processPendingPayment(\GoCardlessPro\Resources\Payment $payment, array $data = [])
{
$data = [
'payment_type' => PaymentType::DIRECT_DEBIT,
'amount' => $this->go_cardless->payment_hash->data->amount_with_fee,
'transaction_reference' => $payment->id,
'gateway_type_id' => GatewayType::DIRECT_DEBIT,
];
$payment = $this->go_cardless->createPayment($data, Payment::STATUS_PENDING);
SystemLogger::dispatch(
['response' => $payment, 'data' => $data],
SystemLog::CATEGORY_GATEWAY_RESPONSE,
SystemLog::EVENT_GATEWAY_SUCCESS,
SystemLog::TYPE_GOCARDLESS,
$this->go_cardless->client,
$this->go_cardless->client->company,
);
return redirect()->route('client.payments.show', ['payment' => $this->go_cardless->encodePrimaryKey($payment->id)]);
}
/**
* Process unsuccessful payments for Direct Debit.
*
* @param ResourcesPayment $payment
* @return never
*/
public function processUnsuccessfulPayment(\GoCardlessPro\Resources\Payment $payment)
{
PaymentFailureMailer::dispatch($this->go_cardless->client, $payment->status, $this->go_cardless->client->company, $this->go_cardless->payment_hash->data->amount_with_fee);
PaymentFailureMailer::dispatch(
$this->go_cardless->client,
$payment,
$this->go_cardless->client->company,
$payment->amount
);
$message = [
'server_response' => $payment,
'data' => $this->go_cardless->payment_hash->data,
];
SystemLogger::dispatch(
$message,
SystemLog::CATEGORY_GATEWAY_RESPONSE,
SystemLog::EVENT_GATEWAY_FAILURE,
SystemLog::TYPE_GOCARDLESS,
$this->go_cardless->client,
$this->go_cardless->client->company,
);
2021-10-16 15:27:56 +02:00
2021-10-16 15:55:33 +02:00
throw new PaymentFailed('Failed to process the payment.', 500);
}
2021-10-19 16:16:57 +02:00
}