2021-07-23 14:43:32 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\PaymentDrivers\Mollie;
|
|
|
|
|
|
|
|
use App\Exceptions\PaymentFailed;
|
|
|
|
use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest;
|
2021-07-23 16:17:32 +02:00
|
|
|
use App\Jobs\Util\SystemLogger;
|
2021-07-30 16:04:26 +02:00
|
|
|
use App\Models\ClientGatewayToken;
|
2021-07-23 16:17:32 +02:00
|
|
|
use App\Models\GatewayType;
|
|
|
|
use App\Models\Payment;
|
|
|
|
use App\Models\PaymentType;
|
|
|
|
use App\Models\SystemLog;
|
2021-07-23 14:43:32 +02:00
|
|
|
use App\PaymentDrivers\MolliePaymentDriver;
|
|
|
|
use Illuminate\Contracts\View\Factory;
|
2021-07-30 14:09:29 +02:00
|
|
|
use Illuminate\Http\RedirectResponse;
|
2021-07-23 14:43:32 +02:00
|
|
|
use Illuminate\View\View;
|
|
|
|
|
|
|
|
class CreditCard
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var MolliePaymentDriver
|
|
|
|
*/
|
|
|
|
protected $mollie;
|
|
|
|
|
|
|
|
public function __construct(MolliePaymentDriver $mollie)
|
|
|
|
{
|
|
|
|
$this->mollie = $mollie;
|
|
|
|
|
|
|
|
$this->mollie->init();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the page for credit card payments.
|
2021-07-30 14:36:14 +02:00
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
* @return Factory|View
|
2021-07-23 14:43:32 +02:00
|
|
|
*/
|
|
|
|
public function paymentView(array $data)
|
|
|
|
{
|
|
|
|
$data['gateway'] = $this->mollie;
|
|
|
|
|
|
|
|
return render('gateways.mollie.credit_card.pay', $data);
|
|
|
|
}
|
|
|
|
|
2021-07-23 16:17:32 +02:00
|
|
|
/**
|
|
|
|
* Create a payment object.
|
2021-07-30 14:36:14 +02:00
|
|
|
*
|
|
|
|
* @param PaymentResponseRequest $request
|
|
|
|
* @return mixed
|
2021-07-23 16:17:32 +02:00
|
|
|
*/
|
2021-07-23 14:43:32 +02:00
|
|
|
public function paymentResponse(PaymentResponseRequest $request)
|
|
|
|
{
|
2021-08-02 15:49:50 +02:00
|
|
|
$amount = $this->mollie->convertToMollieAmount((float) $this->mollie->payment_hash->data->amount_with_fee);
|
2021-07-29 15:13:38 +02:00
|
|
|
|
|
|
|
$this->mollie->payment_hash
|
|
|
|
->withData('gateway_type_id', GatewayType::CREDIT_CARD)
|
|
|
|
->withData('client_id', $this->mollie->client->id);
|
2021-07-23 16:17:32 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (! empty($request->token)) {
|
2021-07-30 16:04:26 +02:00
|
|
|
try {
|
|
|
|
$cgt = ClientGatewayToken::where('token', $request->token)->firstOrFail();
|
|
|
|
|
|
|
|
$payment = $this->mollie->gateway->payments->create([
|
|
|
|
'amount' => [
|
|
|
|
'currency' => $this->mollie->client->currency()->code,
|
|
|
|
'value' => $amount,
|
|
|
|
],
|
|
|
|
'mandateId' => $request->token,
|
|
|
|
'customerId' => $cgt->gateway_customer_reference,
|
|
|
|
'sequenceType' => 'recurring',
|
|
|
|
'description' => \sprintf('Hash: %s', $this->mollie->payment_hash->hash),
|
2021-08-02 14:53:44 +02:00
|
|
|
'webhookUrl' => $this->mollie->company_gateway->webhookUrl(),
|
2022-12-07 10:16:14 +01:00
|
|
|
'idempotencyKey' => uniqid("st",true),
|
2022-02-20 09:02:57 +01:00
|
|
|
'metadata' => [
|
|
|
|
'client_id' => $this->mollie->client->hashed_id,
|
|
|
|
'hash' => $this->mollie->payment_hash->hash,
|
|
|
|
'gateway_type_id' => GatewayType::CREDIT_CARD,
|
|
|
|
'payment_type_id' => PaymentType::CREDIT_CARD_OTHER,
|
|
|
|
],
|
2021-07-30 16:04:26 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
if ($payment->status === 'paid') {
|
|
|
|
$this->mollie->logSuccessfulGatewayResponse(
|
|
|
|
['response' => $payment, 'data' => $this->mollie->payment_hash],
|
|
|
|
SystemLog::TYPE_MOLLIE
|
|
|
|
);
|
|
|
|
|
|
|
|
return $this->processSuccessfulPayment($payment);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($payment->status === 'open') {
|
|
|
|
$this->mollie->payment_hash->withData('payment_id', $payment->id);
|
|
|
|
|
2022-12-08 12:27:09 +01:00
|
|
|
return redirect()->away($payment->getCheckoutUrl());
|
2021-07-30 16:04:26 +02:00
|
|
|
}
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
return $this->processUnsuccessfulPayment($e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-23 14:43:32 +02:00
|
|
|
try {
|
2021-07-30 14:36:14 +02:00
|
|
|
$data = [
|
2021-07-23 16:17:32 +02:00
|
|
|
'amount' => [
|
|
|
|
'currency' => $this->mollie->client->currency()->code,
|
2021-07-29 15:13:38 +02:00
|
|
|
'value' => $amount,
|
2021-07-23 14:43:32 +02:00
|
|
|
],
|
2021-07-23 16:17:32 +02:00
|
|
|
'description' => \sprintf('Hash: %s', $this->mollie->payment_hash->hash),
|
2021-07-29 15:13:38 +02:00
|
|
|
'redirectUrl' => route('mollie.3ds_redirect', [
|
2021-07-26 17:03:40 +02:00
|
|
|
'company_key' => $this->mollie->client->company->company_key,
|
|
|
|
'company_gateway_id' => $this->mollie->company_gateway->hashed_id,
|
|
|
|
'hash' => $this->mollie->payment_hash->hash,
|
|
|
|
]),
|
2021-08-02 14:53:44 +02:00
|
|
|
'webhookUrl' => $this->mollie->company_gateway->webhookUrl(),
|
2022-02-20 09:02:57 +01:00
|
|
|
'metadata' => [
|
2022-06-21 11:57:17 +02:00
|
|
|
'client_id' => $this->mollie->client->hashed_id,
|
|
|
|
'hash' => $this->mollie->payment_hash->hash,
|
|
|
|
'gateway_type_id' => GatewayType::CREDIT_CARD,
|
|
|
|
'payment_type_id' => PaymentType::CREDIT_CARD_OTHER,
|
|
|
|
],
|
2021-07-30 16:04:26 +02:00
|
|
|
'cardToken' => $request->gateway_response,
|
2021-07-30 14:36:14 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
if ($request->shouldStoreToken()) {
|
|
|
|
$customer = $this->mollie->gateway->customers->create([
|
|
|
|
'name' => $this->mollie->client->name,
|
|
|
|
'email' => $this->mollie->client->present()->email(),
|
|
|
|
'metadata' => [
|
2022-06-21 11:57:17 +02:00
|
|
|
'id' => $this->mollie->client->hashed_id,
|
2021-07-30 14:36:14 +02:00
|
|
|
],
|
|
|
|
]);
|
|
|
|
|
|
|
|
$data['customerId'] = $customer->id;
|
|
|
|
$data['sequenceType'] = 'first';
|
|
|
|
|
|
|
|
$this->mollie->payment_hash
|
|
|
|
->withData('mollieCustomerId', $customer->id)
|
|
|
|
->withData('shouldStoreToken', true);
|
|
|
|
}
|
|
|
|
|
|
|
|
$payment = $this->mollie->gateway->payments->create($data);
|
2021-07-23 14:43:32 +02:00
|
|
|
|
2021-07-23 16:17:32 +02:00
|
|
|
if ($payment->status === 'paid') {
|
|
|
|
$this->mollie->logSuccessfulGatewayResponse(
|
|
|
|
['response' => $payment, 'data' => $this->mollie->payment_hash],
|
|
|
|
SystemLog::TYPE_MOLLIE
|
|
|
|
);
|
|
|
|
|
2021-07-29 16:15:27 +02:00
|
|
|
return $this->processSuccessfulPayment($payment);
|
2021-07-23 16:17:32 +02:00
|
|
|
}
|
|
|
|
|
2021-07-23 14:43:32 +02:00
|
|
|
if ($payment->status === 'open') {
|
2021-07-29 15:13:38 +02:00
|
|
|
$this->mollie->payment_hash->withData('payment_id', $payment->id);
|
|
|
|
|
2022-12-08 12:27:09 +01:00
|
|
|
return redirect()->away($payment->getCheckoutUrl());
|
2021-07-23 14:43:32 +02:00
|
|
|
}
|
|
|
|
} catch (\Exception $e) {
|
2021-07-26 17:03:40 +02:00
|
|
|
$this->processUnsuccessfulPayment($e);
|
|
|
|
|
2021-07-23 14:43:32 +02:00
|
|
|
throw new PaymentFailed($e->getMessage(), $e->getCode());
|
|
|
|
}
|
2021-07-23 16:17:32 +02:00
|
|
|
}
|
|
|
|
|
2021-07-29 15:13:38 +02:00
|
|
|
public function processSuccessfulPayment(\Mollie\Api\Resources\Payment $payment)
|
2021-07-23 16:17:32 +02:00
|
|
|
{
|
|
|
|
$payment_hash = $this->mollie->payment_hash;
|
|
|
|
|
2021-07-30 16:04:26 +02:00
|
|
|
if (property_exists($payment_hash->data, 'shouldStoreToken') && $payment_hash->data->shouldStoreToken) {
|
2021-07-30 14:36:14 +02:00
|
|
|
try {
|
|
|
|
$mandates = \iterator_to_array($this->mollie->gateway->mandates->listForId($payment_hash->data->mollieCustomerId));
|
|
|
|
} catch (\Mollie\Api\Exceptions\ApiException $e) {
|
|
|
|
return $this->processUnsuccessfulPayment($e);
|
|
|
|
}
|
|
|
|
|
|
|
|
$payment_meta = new \stdClass;
|
|
|
|
$payment_meta->exp_month = (string) $mandates[0]->details->cardExpiryDate;
|
|
|
|
$payment_meta->exp_year = (string) '';
|
|
|
|
$payment_meta->brand = (string) $mandates[0]->details->cardLabel;
|
|
|
|
$payment_meta->last4 = (string) $mandates[0]->details->cardNumber;
|
|
|
|
$payment_meta->type = GatewayType::CREDIT_CARD;
|
|
|
|
|
|
|
|
$this->mollie->storeGatewayToken([
|
|
|
|
'token' => $mandates[0]->id,
|
|
|
|
'payment_method_id' => GatewayType::CREDIT_CARD,
|
|
|
|
'payment_meta' => $payment_meta,
|
2021-07-30 16:04:26 +02:00
|
|
|
], ['gateway_customer_reference' => $payment_hash->data->mollieCustomerId]);
|
2021-07-30 14:36:14 +02:00
|
|
|
}
|
|
|
|
|
2021-07-23 16:17:32 +02:00
|
|
|
$data = [
|
|
|
|
'gateway_type_id' => GatewayType::CREDIT_CARD,
|
|
|
|
'amount' => array_sum(array_column($payment_hash->invoices(), 'amount')) + $payment_hash->fee_total,
|
|
|
|
'payment_type' => PaymentType::CREDIT_CARD_OTHER,
|
|
|
|
'transaction_reference' => $payment->id,
|
|
|
|
];
|
|
|
|
|
2021-08-13 07:56:41 +02:00
|
|
|
$payment_record = $this->mollie->createPayment($data, $payment->status === 'paid' ? Payment::STATUS_COMPLETED : Payment::STATUS_PENDING);
|
2021-07-23 16:17:32 +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,
|
|
|
|
);
|
2021-07-23 14:43:32 +02:00
|
|
|
|
2021-07-23 16:17:32 +02:00
|
|
|
return redirect()->route('client.payments.show', ['payment' => $this->mollie->encodePrimaryKey($payment_record->id)]);
|
2021-07-23 14:43:32 +02:00
|
|
|
}
|
2021-07-26 17:03:40 +02:00
|
|
|
|
|
|
|
public function processUnsuccessfulPayment(\Exception $e)
|
|
|
|
{
|
2021-10-17 12:40:40 +02:00
|
|
|
$this->mollie->sendFailureMail($e->getMessage());
|
2021-07-26 17:03:40 +02:00
|
|
|
|
|
|
|
SystemLogger::dispatch(
|
|
|
|
$e->getMessage(),
|
|
|
|
SystemLog::CATEGORY_GATEWAY_RESPONSE,
|
|
|
|
SystemLog::EVENT_GATEWAY_FAILURE,
|
|
|
|
SystemLog::TYPE_MOLLIE,
|
|
|
|
$this->mollie->client,
|
|
|
|
$this->mollie->client->company,
|
|
|
|
);
|
2021-07-29 15:13:38 +02:00
|
|
|
|
|
|
|
throw new PaymentFailed($e->getMessage(), $e->getCode());
|
2021-07-26 17:03:40 +02:00
|
|
|
}
|
2021-07-29 16:15:27 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Show authorization page.
|
2021-07-30 14:36:14 +02:00
|
|
|
*
|
|
|
|
* @param array $data
|
|
|
|
* @return Factory|View
|
2021-07-29 16:15:27 +02:00
|
|
|
*/
|
|
|
|
public function authorizeView(array $data)
|
|
|
|
{
|
|
|
|
return render('gateways.mollie.credit_card.authorize', $data);
|
|
|
|
}
|
|
|
|
|
2021-07-30 14:09:29 +02:00
|
|
|
/**
|
|
|
|
* Handle authorization response.
|
2021-07-30 14:36:14 +02:00
|
|
|
*
|
|
|
|
* @param mixed $request
|
|
|
|
* @return RedirectResponse
|
2021-07-30 14:09:29 +02:00
|
|
|
*/
|
|
|
|
public function authorizeResponse($request): RedirectResponse
|
2021-07-29 16:15:27 +02:00
|
|
|
{
|
2021-07-30 14:09:29 +02:00
|
|
|
return redirect()->route('client.payment_methods.index');
|
2021-07-29 16:15:27 +02:00
|
|
|
}
|
2021-07-23 14:43:32 +02:00
|
|
|
}
|