2021-10-04 15:41:15 +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-04 15:41:15 +02:00
|
|
|
*
|
2022-06-21 11:57:17 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2021-10-04 15:41:15 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\PaymentDrivers\Mollie;
|
|
|
|
|
2021-10-04 16:06:41 +02:00
|
|
|
use App\Exceptions\PaymentFailed;
|
2021-10-04 15:41:15 +02:00
|
|
|
use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest;
|
2021-10-04 16:06:41 +02:00
|
|
|
use App\Jobs\Util\SystemLogger;
|
|
|
|
use App\Models\GatewayType;
|
|
|
|
use App\Models\Payment;
|
|
|
|
use App\Models\PaymentType;
|
|
|
|
use App\Models\SystemLog;
|
2024-08-09 01:09:20 +02:00
|
|
|
use App\PaymentDrivers\Common\LivewireMethodInterface;
|
2021-10-04 15:41:15 +02:00
|
|
|
use App\PaymentDrivers\Common\MethodInterface;
|
2021-10-04 15:43:48 +02:00
|
|
|
use App\PaymentDrivers\MolliePaymentDriver;
|
2021-10-04 15:48:13 +02:00
|
|
|
use Illuminate\Http\RedirectResponse;
|
2022-06-21 11:57:17 +02:00
|
|
|
use Illuminate\Http\Request;
|
2021-10-04 15:48:13 +02:00
|
|
|
use Illuminate\View\View;
|
2021-10-04 15:41:15 +02:00
|
|
|
|
2024-08-09 01:09:20 +02:00
|
|
|
class IDEAL implements MethodInterface, LivewireMethodInterface
|
2021-10-04 15:41:15 +02:00
|
|
|
{
|
2021-10-04 15:43:48 +02:00
|
|
|
protected MolliePaymentDriver $mollie;
|
|
|
|
|
|
|
|
public function __construct(MolliePaymentDriver $mollie)
|
|
|
|
{
|
|
|
|
$this->mollie = $mollie;
|
|
|
|
|
|
|
|
$this->mollie->init();
|
|
|
|
}
|
|
|
|
|
2021-10-04 15:48:13 +02:00
|
|
|
/**
|
|
|
|
* Show the authorization page for iDEAL.
|
|
|
|
*
|
|
|
|
* @param array $data
|
2023-10-26 04:57:44 +02:00
|
|
|
* @return \Illuminate\View\View
|
2021-10-04 15:48:13 +02:00
|
|
|
*/
|
|
|
|
public function authorizeView(array $data): View
|
|
|
|
{
|
|
|
|
return render('gateways.mollie.ideal.authorize', $data);
|
|
|
|
}
|
2021-10-04 15:41:15 +02:00
|
|
|
|
2021-10-04 15:48:13 +02:00
|
|
|
/**
|
|
|
|
* Handle the authorization for iDEAL.
|
|
|
|
*
|
|
|
|
* @param Request $request
|
2024-06-16 00:30:25 +02:00
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
2021-10-04 15:48:13 +02:00
|
|
|
*/
|
|
|
|
public function authorizeResponse(Request $request): RedirectResponse
|
|
|
|
{
|
|
|
|
return redirect()->route('client.payment_methods.index');
|
|
|
|
}
|
2021-10-04 15:41:15 +02:00
|
|
|
|
2021-10-04 16:06:41 +02:00
|
|
|
/**
|
|
|
|
* Show the payment page for iDEAL.
|
|
|
|
*
|
|
|
|
* @param array $data
|
2024-06-16 00:30:25 +02:00
|
|
|
* @return \Illuminate\Http\RedirectResponseor|RedirectResponse
|
2021-10-04 16:06:41 +02:00
|
|
|
*/
|
|
|
|
public function paymentView(array $data)
|
|
|
|
{
|
|
|
|
$this->mollie->payment_hash
|
|
|
|
->withData('gateway_type_id', GatewayType::IDEAL)
|
|
|
|
->withData('client_id', $this->mollie->client->id);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$payment = $this->mollie->gateway->payments->create([
|
|
|
|
'method' => 'ideal',
|
|
|
|
'amount' => [
|
|
|
|
'currency' => $this->mollie->client->currency()->code,
|
|
|
|
'value' => $this->mollie->convertToMollieAmount((float) $this->mollie->payment_hash->data->amount_with_fee),
|
|
|
|
],
|
2021-10-12 16:14:49 +02:00
|
|
|
'description' => \sprintf('%s: %s', ctrans('texts.invoices'), \implode(', ', collect($data['invoices'])->pluck('invoice_number')->toArray())),
|
2021-10-04 16:06:41 +02:00
|
|
|
'redirectUrl' => route('client.payments.response', [
|
|
|
|
'company_gateway_id' => $this->mollie->company_gateway->id,
|
|
|
|
'payment_hash' => $this->mollie->payment_hash->hash,
|
|
|
|
'payment_method_id' => GatewayType::IDEAL,
|
|
|
|
]),
|
|
|
|
'webhookUrl' => $this->mollie->company_gateway->webhookUrl(),
|
|
|
|
'metadata' => [
|
|
|
|
'client_id' => $this->mollie->client->hashed_id,
|
2021-11-30 10:53:38 +01:00
|
|
|
'hash' => $this->mollie->payment_hash->hash,
|
|
|
|
'gateway_type_id' => GatewayType::IDEAL,
|
|
|
|
'payment_type_id' => PaymentType::IDEAL,
|
2021-10-04 16:06:41 +02:00
|
|
|
],
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->mollie->payment_hash->withData('payment_id', $payment->id);
|
|
|
|
|
|
|
|
return redirect(
|
|
|
|
$payment->getCheckoutUrl()
|
|
|
|
);
|
|
|
|
} catch (\Mollie\Api\Exceptions\ApiException | \Exception $exception) {
|
|
|
|
return $this->processUnsuccessfulPayment($exception);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle unsuccessful payment.
|
|
|
|
*
|
|
|
|
* @param Exception $exception
|
|
|
|
* @throws PaymentFailed
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function processUnsuccessfulPayment(\Exception $exception): void
|
|
|
|
{
|
2021-10-17 12:40:40 +02:00
|
|
|
$this->mollie->sendFailureMail($exception->getMessage());
|
2021-10-04 16:06:41 +02:00
|
|
|
|
|
|
|
SystemLogger::dispatch(
|
|
|
|
$exception->getMessage(),
|
|
|
|
SystemLog::CATEGORY_GATEWAY_RESPONSE,
|
|
|
|
SystemLog::EVENT_GATEWAY_FAILURE,
|
|
|
|
SystemLog::TYPE_MOLLIE,
|
|
|
|
$this->mollie->client,
|
|
|
|
$this->mollie->client->company,
|
|
|
|
);
|
|
|
|
|
|
|
|
throw new PaymentFailed($exception->getMessage(), $exception->getCode());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle the payments for the iDEAL.
|
|
|
|
*
|
|
|
|
* @param PaymentResponseRequest $request
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function paymentResponse(PaymentResponseRequest $request)
|
|
|
|
{
|
2022-06-21 11:57:17 +02:00
|
|
|
if (! \property_exists($this->mollie->payment_hash->data, 'payment_id')) {
|
2021-10-04 16:06:41 +02:00
|
|
|
return $this->processUnsuccessfulPayment(
|
2022-06-21 11:57:17 +02:00
|
|
|
new PaymentFailed('Whoops, something went wrong. Missing required [payment_id] parameter. Please contact administrator. Reference hash: '.$this->mollie->payment_hash->hash)
|
2021-10-04 16:06:41 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$payment = $this->mollie->gateway->payments->get(
|
|
|
|
$this->mollie->payment_hash->data->payment_id
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($payment->status === 'paid') {
|
|
|
|
return $this->processSuccessfulPayment($payment);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($payment->status === 'open') {
|
|
|
|
return $this->processOpenPayment($payment);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($payment->status === 'failed') {
|
|
|
|
return $this->processUnsuccessfulPayment(
|
|
|
|
new PaymentFailed(ctrans('texts.status_failed'))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->processUnsuccessfulPayment(
|
|
|
|
new PaymentFailed(ctrans('texts.status_voided'))
|
|
|
|
);
|
|
|
|
} catch (\Mollie\Api\Exceptions\ApiException | \Exception $exception) {
|
|
|
|
return $this->processUnsuccessfulPayment($exception);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle the successful payment for iDEAL.
|
|
|
|
*
|
|
|
|
* @param string $status
|
|
|
|
* @param ResourcesPayment $payment
|
2024-06-16 00:30:25 +02:00
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
2021-10-04 16:06:41 +02:00
|
|
|
*/
|
|
|
|
public function processSuccessfulPayment(\Mollie\Api\Resources\Payment $payment, string $status = 'paid'): RedirectResponse
|
|
|
|
{
|
|
|
|
$data = [
|
|
|
|
'gateway_type_id' => GatewayType::IDEAL,
|
|
|
|
'amount' => array_sum(array_column($this->mollie->payment_hash->invoices(), 'amount')) + $this->mollie->payment_hash->fee_total,
|
|
|
|
'payment_type' => PaymentType::IDEAL,
|
|
|
|
'transaction_reference' => $payment->id,
|
|
|
|
];
|
|
|
|
|
|
|
|
$payment_record = $this->mollie->createPayment(
|
|
|
|
$data,
|
|
|
|
$status === 'paid' ? Payment::STATUS_COMPLETED : Payment::STATUS_PENDING
|
|
|
|
);
|
|
|
|
|
|
|
|
SystemLogger::dispatch(
|
|
|
|
['response' => $payment, 'data' => $data],
|
|
|
|
SystemLog::CATEGORY_GATEWAY_RESPONSE,
|
|
|
|
SystemLog::EVENT_GATEWAY_SUCCESS,
|
|
|
|
SystemLog::TYPE_MOLLIE,
|
|
|
|
$this->mollie->client,
|
|
|
|
$this->mollie->client->company,
|
|
|
|
);
|
2021-10-04 15:41:15 +02:00
|
|
|
|
2021-10-04 16:06:41 +02:00
|
|
|
return redirect()->route('client.payments.show', ['payment' => $this->mollie->encodePrimaryKey($payment_record->id)]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle 'open' payment status for IDEAL.
|
|
|
|
*
|
|
|
|
* @param ResourcesPayment $payment
|
2024-06-16 00:30:25 +02:00
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
2021-10-04 16:06:41 +02:00
|
|
|
*/
|
|
|
|
public function processOpenPayment(\Mollie\Api\Resources\Payment $payment): RedirectResponse
|
|
|
|
{
|
|
|
|
return $this->processSuccessfulPayment($payment, 'open');
|
|
|
|
}
|
2024-08-09 01:09:20 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function livewirePaymentView(array $data): string
|
|
|
|
{
|
|
|
|
// Doesn't support, it's offsite payment method.
|
|
|
|
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function paymentData(array $data): array
|
|
|
|
{
|
|
|
|
$this->paymentView($data);
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
2021-10-04 16:06:41 +02:00
|
|
|
}
|