2021-10-19 17:59:38 +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-19 17:59:38 +02:00
|
|
|
*
|
2022-06-21 11:57:17 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2021-10-19 17:59:38 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\PaymentDrivers\GoCardless;
|
|
|
|
|
2021-10-19 18:25:47 +02:00
|
|
|
use App\Exceptions\PaymentFailed;
|
2021-10-19 17:59:38 +02:00
|
|
|
use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest;
|
2021-10-19 18:25:47 +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-19 18:35:03 +02:00
|
|
|
use App\Models\Payment;
|
|
|
|
use App\Models\PaymentType;
|
2021-10-19 18:25:47 +02:00
|
|
|
use App\Models\SystemLog;
|
2024-08-09 01:09:29 +02:00
|
|
|
use App\PaymentDrivers\Common\LivewireMethodInterface;
|
2021-10-19 18:35:03 +02:00
|
|
|
use App\PaymentDrivers\Common\MethodInterface;
|
2021-10-19 18:02:48 +02:00
|
|
|
use App\PaymentDrivers\GoCardlessPaymentDriver;
|
2021-10-19 18:35:03 +02:00
|
|
|
use App\Utils\Traits\MakesHash;
|
2021-10-19 18:25:47 +02:00
|
|
|
use Exception;
|
|
|
|
use Illuminate\Http\RedirectResponse;
|
2021-12-08 10:46:15 +01:00
|
|
|
use Illuminate\Http\Request;
|
2021-10-19 18:35:03 +02:00
|
|
|
use Illuminate\Routing\Redirector;
|
|
|
|
use Illuminate\View\View;
|
2021-10-19 17:59:38 +02:00
|
|
|
|
2024-08-09 01:09:29 +02:00
|
|
|
class SEPA implements MethodInterface, LivewireMethodInterface
|
2021-10-19 17:59:38 +02:00
|
|
|
{
|
2021-10-19 18:35:03 +02:00
|
|
|
use MakesHash;
|
|
|
|
|
2021-10-19 18:02:48 +02:00
|
|
|
protected GoCardlessPaymentDriver $go_cardless;
|
|
|
|
|
|
|
|
public function __construct(GoCardlessPaymentDriver $go_cardless)
|
|
|
|
{
|
|
|
|
$this->go_cardless = $go_cardless;
|
|
|
|
|
|
|
|
$this->go_cardless->init();
|
|
|
|
}
|
|
|
|
|
2021-10-19 18:25:47 +02:00
|
|
|
/**
|
|
|
|
* Handle authorization for SEPA.
|
2021-10-19 18:35:03 +02:00
|
|
|
*
|
|
|
|
* @param array $data
|
2024-06-16 00:30:25 +02:00
|
|
|
* @return \Illuminate\Http\RedirectResponseor|RedirectResponse|void
|
2021-10-19 18:25:47 +02:00
|
|
|
*/
|
|
|
|
public function authorizeView(array $data)
|
|
|
|
{
|
|
|
|
$session_token = \Illuminate\Support\Str::uuid()->toString();
|
|
|
|
|
|
|
|
try {
|
|
|
|
$redirect = $this->go_cardless->gateway->redirectFlows()->create([
|
|
|
|
'params' => [
|
|
|
|
'scheme' => 'sepa_core',
|
|
|
|
'session_token' => $session_token,
|
|
|
|
'success_redirect_url' => route('client.payment_methods.confirm', [
|
|
|
|
'method' => GatewayType::SEPA,
|
|
|
|
'session_token' => $session_token,
|
|
|
|
]),
|
|
|
|
'prefilled_customer' => [
|
2023-03-17 23:56:03 +01:00
|
|
|
'given_name' => auth()->guard('contact')->user()->client->present()->first_name(),
|
|
|
|
'family_name' => auth()->guard('contact')->user()->client->present()->last_name(),
|
|
|
|
'email' => auth()->guard('contact')->user()->client->present()->email(),
|
|
|
|
'address_line1' => auth()->guard('contact')->user()->client->address1 ?: '',
|
|
|
|
'city' => auth()->guard('contact')->user()->client->city ?: '',
|
|
|
|
'postal_code' => auth()->guard('contact')->user()->client->postal_code ?: '',
|
2021-10-19 18:25:47 +02:00
|
|
|
],
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
|
|
|
|
return redirect(
|
|
|
|
$redirect->redirect_url
|
|
|
|
);
|
|
|
|
} catch (\Exception $exception) {
|
|
|
|
return $this->processUnsuccessfulAuthorization($exception);
|
|
|
|
}
|
|
|
|
}
|
2021-10-19 17:59:38 +02:00
|
|
|
|
2021-10-19 18:25:47 +02:00
|
|
|
/**
|
|
|
|
* Handle unsuccessful authorization for SEPA.
|
2021-10-19 18:35:03 +02:00
|
|
|
*
|
|
|
|
* @param Exception $exception
|
|
|
|
* @return void
|
2021-10-19 18:25:47 +02:00
|
|
|
*/
|
2022-06-21 11:57:17 +02:00
|
|
|
public function processUnsuccessfulAuthorization(Exception $exception): void
|
2021-10-19 18:25:47 +02:00
|
|
|
{
|
|
|
|
$this->go_cardless->sendFailureMail($exception->getMessage());
|
|
|
|
|
|
|
|
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 SEPA.
|
2021-10-19 18:35:03 +02:00
|
|
|
*
|
|
|
|
* @param Request $request
|
2024-06-16 00:30:25 +02:00
|
|
|
* @return \Illuminate\Http\RedirectResponse|void
|
2021-10-19 18:25:47 +02:00
|
|
|
*/
|
|
|
|
public function authorizeResponse(Request $request)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$redirect_flow = $this->go_cardless->gateway->redirectFlows()->complete(
|
|
|
|
$request->redirect_flow_id,
|
|
|
|
['params' => [
|
2022-06-21 11:57:17 +02:00
|
|
|
'session_token' => $request->session_token,
|
2021-10-19 18:25:47 +02:00
|
|
|
]],
|
|
|
|
);
|
|
|
|
|
2024-01-14 05:05:00 +01:00
|
|
|
$payment_meta = new \stdClass();
|
2021-10-19 18:25:47 +02:00
|
|
|
$payment_meta->brand = ctrans('texts.sepa');
|
|
|
|
$payment_meta->type = GatewayType::SEPA;
|
|
|
|
$payment_meta->state = 'authorized';
|
|
|
|
|
|
|
|
$data = [
|
|
|
|
'payment_meta' => $payment_meta,
|
|
|
|
'token' => $redirect_flow->links->mandate,
|
|
|
|
'payment_method_id' => GatewayType::SEPA,
|
|
|
|
];
|
|
|
|
|
|
|
|
$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-19 17:59:38 +02:00
|
|
|
|
2021-10-19 18:35:03 +02:00
|
|
|
/**
|
|
|
|
* Payment view for SEPA.
|
|
|
|
*
|
|
|
|
* @param array $data
|
2023-10-26 04:57:44 +02:00
|
|
|
* @return \Illuminate\View\View
|
2021-10-19 18:35:03 +02:00
|
|
|
*/
|
|
|
|
public function paymentView(array $data): View
|
|
|
|
{
|
2024-08-09 01:09:29 +02:00
|
|
|
$data = $this->paymentData($data);
|
2021-10-19 18:35:03 +02:00
|
|
|
|
|
|
|
return render('gateways.gocardless.sepa.pay', $data);
|
|
|
|
}
|
2021-10-19 17:59:38 +02:00
|
|
|
|
2021-10-19 18:35:03 +02:00
|
|
|
/**
|
|
|
|
* Handle the payment page for SEPA.
|
|
|
|
*
|
|
|
|
* @param PaymentResponseRequest $request
|
2024-06-16 00:30:25 +02:00
|
|
|
* @return \Illuminate\Http\RedirectResponse|App\PaymentDrivers\GoCardless\never|void
|
2021-10-19 18:35:03 +02:00
|
|
|
*/
|
|
|
|
public function paymentResponse(PaymentResponseRequest $request)
|
|
|
|
{
|
2021-12-07 13:33:26 +01:00
|
|
|
$this->go_cardless->ensureMandateIsReady($request->source);
|
2021-11-05 17:01:19 +01:00
|
|
|
|
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();
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
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()}";
|
2022-06-21 11:57:17 +02:00
|
|
|
} else {
|
2021-12-08 10:46:15 +01:00
|
|
|
$description = "Amount {$request->amount} from client {$this->go_cardless->client->present()->name()}";
|
2022-06-21 11:57:17 +02:00
|
|
|
}
|
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-19 18:35:03 +02:00
|
|
|
try {
|
|
|
|
$payment = $this->go_cardless->gateway->payments()->create([
|
|
|
|
'params' => [
|
2023-03-22 07:45:33 +01:00
|
|
|
'amount' => $amount,
|
2021-10-19 18:35:03 +02:00
|
|
|
'currency' => $request->currency,
|
2021-12-08 10:46:15 +01:00
|
|
|
'description' => $description,
|
2021-10-19 18:35:03 +02:00
|
|
|
'metadata' => [
|
|
|
|
'payment_hash' => $this->go_cardless->payment_hash->hash,
|
|
|
|
],
|
|
|
|
'links' => [
|
2021-12-07 13:33:26 +01:00
|
|
|
'mandate' => $request->source,
|
2021-10-19 18:35:03 +02:00
|
|
|
],
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
|
|
|
|
if ($payment->status === 'pending_submission') {
|
2021-12-07 13:33:26 +01:00
|
|
|
return $this->processPendingPayment($payment, ['token' => $request->source]);
|
2021-10-19 18:35:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->processUnsuccessfulPayment($payment);
|
|
|
|
} catch (\Exception $exception) {
|
|
|
|
throw new PaymentFailed($exception->getMessage(), $exception->getCode());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle pending payments for Direct Debit.
|
|
|
|
*
|
|
|
|
* @param ResourcesPayment $payment
|
|
|
|
* @param array $data
|
2024-06-16 00:30:25 +02:00
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
2021-10-19 18:35:03 +02:00
|
|
|
*/
|
|
|
|
public function processPendingPayment(\GoCardlessPro\Resources\Payment $payment, array $data = [])
|
|
|
|
{
|
|
|
|
$data = [
|
|
|
|
'payment_type' => PaymentType::SEPA,
|
|
|
|
'amount' => $this->go_cardless->payment_hash->data->amount_with_fee,
|
|
|
|
'transaction_reference' => $payment->id,
|
|
|
|
'gateway_type_id' => GatewayType::SEPA,
|
|
|
|
];
|
|
|
|
|
|
|
|
$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)
|
|
|
|
{
|
|
|
|
$this->go_cardless->sendFailureMail(
|
|
|
|
$payment->status
|
|
|
|
);
|
|
|
|
|
|
|
|
$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,
|
|
|
|
);
|
|
|
|
|
|
|
|
throw new PaymentFailed('Failed to process the payment.', 500);
|
|
|
|
}
|
2024-08-09 01:09:29 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function livewirePaymentView(array $data): string
|
|
|
|
{
|
|
|
|
return 'gateways.gocardless.sepa.pay_livewire';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function paymentData(array $data): array
|
|
|
|
{
|
|
|
|
$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();
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
2021-10-19 18:35:03 +02:00
|
|
|
}
|