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-26 17:03:40 +02:00
|
|
|
use App\Jobs\Mail\PaymentFailureMailer;
|
2021-07-23 16:17:32 +02:00
|
|
|
use App\Jobs\Util\SystemLogger;
|
|
|
|
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-07-29 15:13:38 +02:00
|
|
|
// TODO: Unit tests.
|
|
|
|
$amount = number_format((float) $this->mollie->payment_hash->data->amount_with_fee, 2, '.', '');
|
|
|
|
|
|
|
|
$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
|
|
|
|
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-07-29 15:13:38 +02:00
|
|
|
'webhookUrl' => 'https://invoiceninja.com',
|
2021-07-23 16:17:32 +02:00
|
|
|
'cardToken' => $request->token,
|
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' => [
|
|
|
|
'id' => $this->mollie->client->hashed_id,
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
|
|
|
|
$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);
|
|
|
|
|
2021-07-26 17:03:40 +02:00
|
|
|
return redirect($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 14:36:14 +02:00
|
|
|
if ($payment_hash->data->shouldStoreToken) {
|
|
|
|
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-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,
|
|
|
|
];
|
|
|
|
|
|
|
|
$payment_record = $this->mollie->createPayment($data, Payment::STATUS_COMPLETED);
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
PaymentFailureMailer::dispatch(
|
|
|
|
$this->mollie->client,
|
|
|
|
$e->getMessage(),
|
|
|
|
$this->mollie->client->company,
|
|
|
|
$this->mollie->payment_hash->data->amount_with_fee
|
|
|
|
);
|
|
|
|
|
|
|
|
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
|
|
|
}
|