1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 15:13:29 +01:00
invoiceninja/app/PaymentDrivers/Mollie/CreditCard.php

261 lines
9.4 KiB
PHP
Raw Normal View History

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.
*
* @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.
*
* @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
$description = sprintf('%s: %s', ctrans('texts.invoices'), \implode(', ', collect($this->mollie->payment_hash->invoices())->pluck('invoice_number')->toArray()));
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
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' => $description,
2021-08-02 14:53:44 +02:00
'webhookUrl' => $this->mollie->company_gateway->webhookUrl(),
2023-02-21 08:04:59 +01:00
// 'idempotencyKey' => uniqid("st", true),
'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);
2023-02-16 02:36:09 +01:00
if (!$payment->getCheckoutUrl()) {
return render('gateways.mollie.mollie_placeholder');
2023-02-16 02:36:09 +01:00
} else {
return redirect()->away($payment->getCheckoutUrl());
2023-02-16 02:36:09 +01:00
}
2021-07-30 16:04:26 +02:00
}
} catch (\Exception $e) {
return $this->processUnsuccessfulPayment($e);
}
}
2021-07-23 14:43:32 +02:00
try {
$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
],
'description' => $description,
2023-02-21 08:04:59 +01:00
// 'idempotencyKey' => uniqid("st", true),
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(),
'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
'cardToken' => $request->gateway_response,
];
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);
nlog("Mollie");
nlog($payment);
2023-02-16 02:36:09 +01:00
if (!$payment->getCheckoutUrl()) {
return render('gateways.mollie.mollie_placeholder');
2023-02-16 02:36:09 +01:00
} else {
return redirect()->away($payment->getCheckoutUrl());
2023-02-16 02:36:09 +01:00
}
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) {
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-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 === '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.
*
* @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.
*
* @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
}