1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-15 23:52:33 +01:00
invoiceninja/app/PaymentDrivers/CheckoutCom/CreditCard.php

182 lines
6.1 KiB
PHP
Raw Normal View History

2020-10-22 15:24:18 +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)
2020-10-22 15:24:18 +02:00
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
2020-10-22 15:24:18 +02:00
*/
namespace App\PaymentDrivers\CheckoutCom;
use App\Exceptions\PaymentFailed;
use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest;
2021-01-26 13:09:08 +01:00
use App\Jobs\Mail\PaymentFailureMailer;
use App\Models\ClientGatewayToken;
2020-10-22 15:24:18 +02:00
use App\PaymentDrivers\CheckoutComPaymentDriver;
use App\Utils\Traits\MakesHash;
2020-10-28 11:10:49 +01:00
use Checkout\Library\Exceptions\CheckoutHttpException;
2020-10-22 15:24:18 +02:00
use Checkout\Models\Payments\IdSource;
use Checkout\Models\Payments\Payment;
use Checkout\Models\Payments\TokenSource;
2020-10-28 11:10:49 +01:00
use Illuminate\Contracts\View\Factory;
use Illuminate\View\View;
2020-10-22 15:24:18 +02:00
class CreditCard
{
use Utilities;
use MakesHash;
2020-10-22 15:24:18 +02:00
/**
2020-10-28 11:10:49 +01:00
* @var CheckoutComPaymentDriver
2020-10-22 15:24:18 +02:00
*/
public $checkout;
public function __construct(CheckoutComPaymentDriver $checkout)
{
$this->checkout = $checkout;
}
/**
* An authorization view for credit card.
2020-10-28 11:10:49 +01:00
*
* @param mixed $data
* @return Factory|View
2020-10-22 15:24:18 +02:00
*/
public function authorizeView($data)
{
2021-01-25 16:46:40 +01:00
$data['gateway'] = $this->checkout;
return render('gateways.checkout.credit_card.authorize', $data);
2020-10-22 15:24:18 +02:00
}
/**
* Checkout.com supports doesn't support direct authorization of the credit card.
* Token can be saved after the first (successful) purchase.
2020-10-28 11:10:49 +01:00
*
* @param mixed $data
* @return void
2020-10-22 15:24:18 +02:00
*/
public function authorizeResponse($data)
{
return;
}
public function paymentView($data)
{
$data['gateway'] = $this->checkout;
$data['company_gateway'] = $this->checkout->company_gateway;
$data['client'] = $this->checkout->client;
$data['currency'] = $this->checkout->client->getCurrencyCode();
2020-12-21 12:10:39 +01:00
$data['value'] = $this->checkout->convertToCheckoutAmount($data['total']['amount_with_fee'], $this->checkout->client->getCurrencyCode());
$data['raw_value'] = $data['total']['amount_with_fee'];
2020-10-22 15:24:18 +02:00
$data['customer_email'] = $this->checkout->client->present()->email;
return render('gateways.checkout.credit_card.pay', $data);
}
public function paymentResponse(PaymentResponseRequest $request)
2020-10-22 15:24:18 +02:00
{
$this->checkout->init();
$state = [
'server_response' => json_decode($request->gateway_response),
'value' => $request->value,
'raw_value' => $request->raw_value,
'currency' => $request->currency,
'payment_hash' => $request->payment_hash,
'reference' => $request->payment_hash,
2020-12-07 14:50:43 +01:00
'client_id' => $this->checkout->client->id,
2020-10-22 15:24:18 +02:00
];
$state = array_merge($state, $request->all());
$state['store_card'] = boolval($state['store_card']);
$this->checkout->payment_hash->data = array_merge((array)$this->checkout->payment_hash->data, $state);
$this->checkout->payment_hash->save();
2020-10-22 15:24:18 +02:00
2021-02-03 12:36:10 +01:00
if ($request->has('token') && !is_null($request->token) && !empty($request->token)) {
return $this->attemptPaymentUsingToken($request);
2020-10-22 15:24:18 +02:00
}
return $this->attemptPaymentUsingCreditCard($request);
2020-10-22 15:24:18 +02:00
}
private function attemptPaymentUsingToken(PaymentResponseRequest $request)
2020-10-22 15:24:18 +02:00
{
2021-02-03 12:36:10 +01:00
$cgt = ClientGatewayToken::query()
->where('id', $this->decodePrimaryKey($request->input('token')))
->where('company_id', auth('contact')->user()->client->company->id)
->first();
if (!$cgt) {
throw new PaymentFailed(ctrans('texts.payment_token_not_found'), 401);
}
$method = new IdSource($cgt->token);
2020-10-22 15:24:18 +02:00
return $this->completePayment($method, $request);
2020-10-22 15:24:18 +02:00
}
private function attemptPaymentUsingCreditCard(PaymentResponseRequest $request)
{
$checkout_response = $this->checkout->payment_hash->data->server_response;
2020-10-22 15:24:18 +02:00
$method = new TokenSource(
$checkout_response->token
2020-10-22 15:24:18 +02:00
);
return $this->completePayment($method, $request);
2020-10-22 15:24:18 +02:00
}
private function completePayment($method, PaymentResponseRequest $request)
2020-10-22 15:24:18 +02:00
{
$payment = new Payment($method, $this->checkout->payment_hash->data->currency);
$payment->amount = $this->checkout->payment_hash->data->value;
$payment->reference = $this->checkout->payment_hash->data->reference;
2020-10-22 15:24:18 +02:00
$this->checkout->payment_hash->data = array_merge((array)$this->checkout->payment_hash->data, ['checkout_payment_ref' => $payment]);
2020-12-07 14:50:43 +01:00
$this->checkout->payment_hash->save();
2021-04-22 13:53:21 +02:00
if ($this->checkout->client->currency()->code == 'EUR' || $this->checkout->company_gateway->getConfigField('threeds')) {
2020-10-22 15:24:18 +02:00
$payment->{'3ds'} = ['enabled' => true];
2020-12-07 14:50:43 +01:00
$payment->{'success_url'} = route('checkout.3ds_redirect', [
2020-12-07 14:50:43 +01:00
'company_key' => $this->checkout->client->company->company_key,
2020-12-17 12:47:46 +01:00
'company_gateway_id' => $this->checkout->company_gateway->hashed_id,
2020-12-07 14:50:43 +01:00
'hash' => $this->checkout->payment_hash->hash,
]);
2020-10-22 15:24:18 +02:00
}
try {
$response = $this->checkout->gateway->payments()->request($payment);
if ($response->status == 'Authorized') {
2020-10-22 15:24:18 +02:00
return $this->processSuccessfulPayment($response);
}
if ($response->status == 'Pending') {
2021-02-03 12:36:10 +01:00
$this->checkout->confirmGatewayFee();
2020-10-22 15:24:18 +02:00
return $this->processPendingPayment($response);
}
if ($response->status == 'Declined') {
$this->checkout->unWindGatewayFees($this->checkout->payment_hash);
PaymentFailureMailer::dispatch($this->checkout->client, $response->response_summary, $this->checkout->client->company, $this->checkout->payment_hash->data->value);
2021-01-26 13:09:08 +01:00
2020-10-22 15:24:18 +02:00
return $this->processUnsuccessfulPayment($response);
}
2020-10-28 11:10:49 +01:00
} catch (CheckoutHttpException $e) {
2021-01-26 13:09:08 +01:00
$this->checkout->unWindGatewayFees($this->checkout->payment_hash);
2020-10-27 13:44:16 +01:00
return $this->checkout->processInternallyFailedPayment($this->checkout, $e);
2020-10-22 15:24:18 +02:00
}
}
}