2021-09-24 20:42:39 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\PaymentDrivers\Mollie;
|
|
|
|
|
2021-09-24 21:17:43 +02:00
|
|
|
use App\Exceptions\PaymentFailed;
|
2021-09-24 20:42:39 +02:00
|
|
|
use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest;
|
2021-10-26 16:21:26 +02:00
|
|
|
use Illuminate\Http\Request;
|
2021-09-24 21:17:43 +02:00
|
|
|
use App\Jobs\Util\SystemLogger;
|
|
|
|
use App\Models\GatewayType;
|
|
|
|
use App\Models\Payment;
|
|
|
|
use App\Models\PaymentType;
|
|
|
|
use App\Models\SystemLog;
|
2021-09-24 20:42:39 +02:00
|
|
|
use App\PaymentDrivers\Common\MethodInterface;
|
2021-09-24 20:44:47 +02:00
|
|
|
use App\PaymentDrivers\MolliePaymentDriver;
|
2021-09-24 21:17:43 +02:00
|
|
|
use Exception;
|
2021-09-24 20:49:40 +02:00
|
|
|
use Illuminate\Http\RedirectResponse;
|
2021-09-24 21:17:43 +02:00
|
|
|
use Illuminate\Routing\Redirector;
|
2021-09-24 20:49:40 +02:00
|
|
|
use Illuminate\View\View;
|
2021-09-24 21:17:43 +02:00
|
|
|
use Mollie\Api\Resources\Payment as ResourcesPayment;
|
2021-09-24 20:42:39 +02:00
|
|
|
|
|
|
|
class BankTransfer implements MethodInterface
|
|
|
|
{
|
2021-09-24 20:44:47 +02:00
|
|
|
protected MolliePaymentDriver $mollie;
|
|
|
|
|
|
|
|
public function __construct(MolliePaymentDriver $mollie)
|
|
|
|
{
|
|
|
|
$this->mollie = $mollie;
|
|
|
|
|
|
|
|
$this->mollie->init();
|
|
|
|
}
|
|
|
|
|
2021-09-24 20:49:40 +02:00
|
|
|
/**
|
|
|
|
* Show the authorization page for bank transfer.
|
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
* @return View
|
|
|
|
*/
|
|
|
|
public function authorizeView(array $data): View
|
|
|
|
{
|
|
|
|
return render('gateways.mollie.bank_transfer.authorize', $data);
|
|
|
|
}
|
2021-09-24 20:42:39 +02:00
|
|
|
|
2021-09-24 20:49:40 +02:00
|
|
|
/**
|
|
|
|
* Handle the authorization for bank transfer.
|
|
|
|
*
|
|
|
|
* @param Request $request
|
|
|
|
* @return RedirectResponse
|
|
|
|
*/
|
|
|
|
public function authorizeResponse(Request $request): RedirectResponse
|
|
|
|
{
|
|
|
|
return redirect()->route('client.payment_methods.index');
|
|
|
|
}
|
2021-09-24 20:42:39 +02:00
|
|
|
|
2021-09-24 21:17:43 +02:00
|
|
|
/**
|
|
|
|
* Show the payment page for bank transfer.
|
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
* @return Redirector|RedirectResponse
|
|
|
|
*/
|
2021-09-24 20:49:40 +02:00
|
|
|
public function paymentView(array $data)
|
|
|
|
{
|
2021-09-24 21:17:43 +02:00
|
|
|
$this->mollie->payment_hash
|
|
|
|
->withData('gateway_type_id', GatewayType::BANK_TRANSFER)
|
|
|
|
->withData('client_id', $this->mollie->client->id);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$payment = $this->mollie->gateway->payments->create([
|
|
|
|
'method' => 'banktransfer',
|
|
|
|
'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-09-24 21:17:43 +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::BANK_TRANSFER,
|
|
|
|
]),
|
2021-09-24 21:18:36 +02:00
|
|
|
'webhookUrl' => $this->mollie->company_gateway->webhookUrl(),
|
2021-09-24 21:17:43 +02:00
|
|
|
'metadata' => [
|
|
|
|
'client_id' => $this->mollie->client->hashed_id,
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
|
|
|
|
$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 $e
|
|
|
|
* @throws PaymentFailed
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function processUnsuccessfulPayment(\Exception $e): void
|
|
|
|
{
|
2021-10-17 12:40:40 +02:00
|
|
|
|
|
|
|
$this->mollie->sendFailureMail($e->getMessage());
|
2021-09-24 21:17:43 +02:00
|
|
|
|
|
|
|
SystemLogger::dispatch(
|
|
|
|
$e->getMessage(),
|
|
|
|
SystemLog::CATEGORY_GATEWAY_RESPONSE,
|
|
|
|
SystemLog::EVENT_GATEWAY_FAILURE,
|
|
|
|
SystemLog::TYPE_MOLLIE,
|
|
|
|
$this->mollie->client,
|
|
|
|
$this->mollie->client->company,
|
|
|
|
);
|
|
|
|
|
|
|
|
throw new PaymentFailed($e->getMessage(), $e->getCode());
|
2021-09-24 20:49:40 +02:00
|
|
|
}
|
2021-09-24 20:42:39 +02:00
|
|
|
|
2021-09-24 21:17:43 +02:00
|
|
|
/**
|
|
|
|
* Handle the payments for the bank transfer.
|
|
|
|
*
|
|
|
|
* @param PaymentResponseRequest $request
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2021-09-24 20:49:40 +02:00
|
|
|
public function paymentResponse(PaymentResponseRequest $request)
|
|
|
|
{
|
2021-09-24 21:17:43 +02:00
|
|
|
if (! \property_exists($this->mollie->payment_hash->data, 'payment_id')) {
|
|
|
|
return $this->processUnsuccessfulPayment(
|
|
|
|
new PaymentFailed('Whoops, something went wrong. Missing required [payment_id] parameter. Please contact administrator. Reference hash: ' . $this->mollie->payment_hash->hash)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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 bank transfer.
|
|
|
|
*
|
|
|
|
* @param ResourcesPayment $payment
|
|
|
|
* @param string $status
|
|
|
|
* @return RedirectResponse
|
|
|
|
*/
|
|
|
|
public function processSuccessfulPayment(\Mollie\Api\Resources\Payment $payment, $status = 'paid'): RedirectResponse
|
|
|
|
{
|
|
|
|
$data = [
|
|
|
|
'gateway_type_id' => GatewayType::BANK_TRANSFER,
|
|
|
|
'amount' => array_sum(array_column($this->mollie->payment_hash->invoices(), 'amount')) + $this->mollie->payment_hash->fee_total,
|
2021-09-24 22:27:54 +02:00
|
|
|
'payment_type' => PaymentType::MOLLIE_BANK_TRANSFER,
|
2021-09-24 21:17:43 +02:00
|
|
|
'transaction_reference' => $payment->id,
|
|
|
|
];
|
|
|
|
|
|
|
|
$payment_record = $this->mollie->createPayment(
|
|
|
|
$data,
|
2021-09-24 22:27:54 +02:00
|
|
|
$status === 'paid' ? Payment::STATUS_COMPLETED : Payment::STATUS_PENDING
|
2021-09-24 21:17:43 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
SystemLogger::dispatch(
|
|
|
|
['response' => $payment, 'data' => $data],
|
|
|
|
SystemLog::CATEGORY_GATEWAY_RESPONSE,
|
|
|
|
SystemLog::EVENT_GATEWAY_SUCCESS,
|
|
|
|
SystemLog::TYPE_MOLLIE,
|
|
|
|
$this->mollie->client,
|
|
|
|
$this->mollie->client->company,
|
|
|
|
);
|
|
|
|
|
|
|
|
return redirect()->route('client.payments.show', ['payment' => $this->mollie->encodePrimaryKey($payment_record->id)]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle 'open' payment status for bank transfer.
|
|
|
|
*
|
|
|
|
* @param ResourcesPayment $payment
|
|
|
|
* @return RedirectResponse
|
|
|
|
*/
|
|
|
|
public function processOpenPayment(\Mollie\Api\Resources\Payment $payment): RedirectResponse
|
|
|
|
{
|
|
|
|
return $this->processSuccessfulPayment($payment, 'open');
|
2021-09-24 20:49:40 +02:00
|
|
|
}
|
2021-09-24 20:42:39 +02:00
|
|
|
}
|