1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-21 17:01:33 +02:00
invoiceninja/app/PaymentDrivers/CheckoutCom/CreditCard.php

355 lines
13 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
*
2023-01-28 23:21:40 +01:00
* @copyright Copyright (c) 2023. 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;
2022-10-18 11:30:55 +02:00
use App\Jobs\Util\SystemLogger;
use App\Models\ClientGatewayToken;
2021-10-14 18:43:47 +02:00
use App\Models\GatewayType;
2022-10-18 11:30:55 +02:00
use App\Models\SystemLog;
2020-10-22 15:24:18 +02:00
use App\PaymentDrivers\CheckoutComPaymentDriver;
2021-10-14 18:43:47 +02:00
use App\PaymentDrivers\Common\MethodInterface;
use App\Utils\Traits\MakesHash;
2022-06-15 14:47:25 +02:00
use Checkout\CheckoutApiException;
use Checkout\CheckoutArgumentException;
use Checkout\CheckoutAuthorizationException;
use Checkout\Payments\Four\Request\PaymentRequest;
2022-06-15 14:47:25 +02:00
use Checkout\Payments\Four\Request\Source\RequestTokenSource;
use Checkout\Payments\PaymentRequest as PaymentsPaymentRequest;
2022-06-16 02:01:24 +02:00
use Checkout\Payments\Source\RequestTokenSource as SourceRequestTokenSource;
2020-10-28 11:10:49 +01:00
use Illuminate\Contracts\View\Factory;
2022-06-15 14:47:25 +02:00
use Illuminate\Http\Request;
2020-10-28 11:10:49 +01:00
use Illuminate\View\View;
2020-10-22 15:24:18 +02:00
2021-10-14 18:43:47 +02:00
class CreditCard implements MethodInterface
2020-10-22 15:24:18 +02:00
{
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;
2021-10-14 18:43:47 +02:00
$this->checkout->init();
2020-10-22 15:24:18 +02:00
}
/**
* 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
}
2022-06-16 03:21:10 +02:00
public function bootRequest($token)
2022-06-15 14:47:25 +02:00
{
if ($this->checkout->is_four_api) {
2022-06-16 03:21:10 +02:00
$token_source = new RequestTokenSource();
$token_source->token = $token;
$request = new PaymentRequest();
$request->source = $token_source;
} else {
2022-06-16 03:21:10 +02:00
$token_source = new SourceRequestTokenSource();
$token_source->token = $token;
$request = new PaymentsPaymentRequest();
$request->source = $token_source;
}
2022-06-16 02:01:24 +02:00
2022-06-16 03:21:10 +02:00
return $request;
2022-06-16 02:01:24 +02:00
}
2020-10-22 15:24:18 +02:00
/**
2021-10-14 18:43:47 +02:00
* Handle authorization for credit card.
2021-10-14 18:44:07 +02:00
*
* @param Request $request
* @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
2020-10-22 15:24:18 +02:00
*/
2021-10-14 18:43:47 +02:00
public function authorizeResponse(Request $request)
2020-10-22 15:24:18 +02:00
{
2021-10-14 18:43:47 +02:00
$gateway_response = \json_decode($request->gateway_response);
2022-06-16 03:21:10 +02:00
$customerRequest = $this->checkout->getCustomer();
2022-09-14 11:49:23 +02:00
2022-06-16 03:21:10 +02:00
$request = $this->bootRequest($gateway_response->token);
$request->capture = false;
$request->reference = '$1 payment for authorization.';
$request->amount = 100;
$request->currency = $this->checkout->client->getCurrencyCode();
$request->customer = $customerRequest;
2021-10-14 18:43:47 +02:00
try {
2022-06-16 02:01:24 +02:00
$response = $this->checkout->gateway->getPaymentsClient()->requestPayment($request);
2021-10-14 18:43:47 +02:00
2022-06-15 14:47:25 +02:00
if ($response['approved'] && $response['status'] === 'Authorized') {
2021-10-14 18:43:47 +02:00
$payment_meta = new \stdClass;
2022-06-15 14:47:25 +02:00
$payment_meta->exp_month = (string) $response['source']['expiry_month'];
$payment_meta->exp_year = (string) $response['source']['expiry_year'];
$payment_meta->brand = (string) $response['source']['scheme'];
$payment_meta->last4 = (string) $response['source']['last4'];
2021-10-14 18:43:47 +02:00
$payment_meta->type = (int) GatewayType::CREDIT_CARD;
$data = [
'payment_meta' => $payment_meta,
2022-06-15 14:47:25 +02:00
'token' => $response['source']['id'],
'payment_method_id' => GatewayType::CREDIT_CARD,
2021-10-14 18:43:47 +02:00
];
$payment_method = $this->checkout->storeGatewayToken($data, ['gateway_customer_reference' => $customerRequest['id']]);
2021-10-14 18:43:47 +02:00
return redirect()->route('client.payment_methods.show', $payment_method->hashed_id);
}
2022-06-15 14:47:25 +02:00
} catch (CheckoutApiException $e) {
// API error
$request_id = $e->request_id ?: '';
$http_status_code = $e->http_status_code ?: '';
2022-06-15 14:47:25 +02:00
$error_details = $e->error_details;
2023-02-16 02:36:09 +01:00
if (is_array($error_details)) {
$error_details = end($e->error_details['error_codes']);
}
$human_exception = $error_details ? $error_details : $e->getMessage();
$human_exception = "{$human_exception} - Request ID: {$request_id}";
throw new PaymentFailed($human_exception, $http_status_code);
2022-06-15 14:47:25 +02:00
} catch (CheckoutArgumentException $e) {
// Bad arguments
$error_details = $e->error_details;
2023-02-16 02:36:09 +01:00
if (is_array($error_details)) {
$error_details = end($e->error_details['error_codes']);
}
$human_exception = $error_details ? $error_details : $e->getMessage();
throw new PaymentFailed($human_exception, 422);
2022-06-15 14:47:25 +02:00
} catch (CheckoutAuthorizationException $e) {
// Bad Invalid authorization
$error_details = $e->error_details;
2023-02-16 02:36:09 +01:00
if (is_array($error_details)) {
$error_details = end($e->error_details['error_codes']);
}
$human_exception = $error_details ? $error_details : $e->getMessage();
throw new PaymentFailed($human_exception, 401);
2021-10-14 18:43:47 +02:00
}
2020-10-22 15:24:18 +02:00
}
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'];
$data['customer_email'] = $this->checkout->client->present()->email();
2020-10-22 15:24:18 +02:00
return render('gateways.checkout.credit_card.pay', $data);
}
public function paymentResponse(PaymentResponseRequest $request)
2020-10-22 15:24:18 +02:00
{
$state = [
'server_response' => json_decode($request->gateway_response),
'value' => $request->value,
'raw_value' => $request->raw_value,
'currency' => $request->currency,
'payment_hash' => $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
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')))
2022-06-16 03:21:10 +02:00
->where('company_id', auth()->guard('contact')->user()->client->company_id)
2021-02-03 12:36:10 +01:00
->first();
if (! $cgt) {
2021-02-03 12:36:10 +01:00
throw new PaymentFailed(ctrans('texts.payment_token_not_found'), 401);
}
2022-06-16 03:21:10 +02:00
$paymentRequest = $this->checkout->bootTokenRequest($cgt->token);
2020-10-22 15:24:18 +02:00
2022-06-16 02:01:24 +02:00
return $this->completePayment($paymentRequest, $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
2022-06-16 03:21:10 +02:00
$paymentRequest = $this->bootRequest($checkout_response->token);
2020-10-22 15:24:18 +02:00
2022-06-16 02:01:24 +02:00
return $this->completePayment($paymentRequest, $request);
2020-10-22 15:24:18 +02:00
}
2022-06-16 02:01:24 +02:00
private function completePayment($paymentRequest, PaymentResponseRequest $request)
2020-10-22 15:24:18 +02:00
{
2022-06-16 03:21:10 +02:00
$paymentRequest->amount = $this->checkout->payment_hash->data->value;
2023-02-16 02:36:09 +01:00
$paymentRequest->reference = substr($this->checkout->getDescription(), 0, 49);
2022-06-16 03:21:10 +02:00
$paymentRequest->customer = $this->checkout->getCustomer();
$paymentRequest->metadata = ['udf1' => 'Invoice Ninja'];
2022-06-16 03:21:10 +02:00
$paymentRequest->currency = $this->checkout->client->getCurrencyCode();
2022-06-16 02:01:24 +02:00
$this->checkout->payment_hash->data = array_merge((array) $this->checkout->payment_hash->data, ['checkout_payment_ref' => $paymentRequest]);
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')) {
2022-06-16 03:21:10 +02:00
$paymentRequest->{'3ds'} = ['enabled' => true];
$paymentRequest->{'success_url'} = route('checkout.3ds_redirect', [
'company_key' => $this->checkout->client->company->company_key,
'company_gateway_id' => $this->checkout->company_gateway->hashed_id,
'hash' => $this->checkout->payment_hash->hash,
]);
$paymentRequest->{'failure_url'} = route('checkout.3ds_redirect', [
'company_key' => $this->checkout->client->company->company_key,
'company_gateway_id' => $this->checkout->company_gateway->hashed_id,
'hash' => $this->checkout->payment_hash->hash,
]);
2020-10-22 15:24:18 +02:00
}
try {
2022-06-16 02:01:24 +02:00
$response = $this->checkout->gateway->getPaymentsClient()->requestPayment($paymentRequest);
2020-10-22 15:24:18 +02:00
2022-06-16 02:01:24 +02:00
if ($response['status'] == 'Authorized') {
2020-10-22 15:24:18 +02:00
return $this->processSuccessfulPayment($response);
}
2022-06-16 02:01:24 +02:00
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);
}
2022-06-16 02:01:24 +02:00
if ($response['status'] == 'Declined') {
$this->checkout->unWindGatewayFees($this->checkout->payment_hash);
2022-10-18 11:30:55 +02:00
//18-10-2022
SystemLogger::dispatch(
$response,
SystemLog::CATEGORY_GATEWAY_RESPONSE,
SystemLog::EVENT_GATEWAY_ERROR,
SystemLog::TYPE_CHECKOUT,
$this->checkout->client,
$this->checkout->client->company,
);
2020-10-22 15:24:18 +02:00
return $this->processUnsuccessfulPayment($response);
}
} catch (CheckoutApiException $e) {
2022-06-16 02:01:24 +02:00
// API error
$request_id = $e->request_id;
$http_status_code = $e->http_status_code;
$error_details = $e->error_details;
2023-02-16 02:36:09 +01:00
if (is_array($error_details)) {
$error_details = end($e->error_details['error_codes']);
}
2022-06-16 02:01:24 +02:00
$this->checkout->unWindGatewayFees($this->checkout->payment_hash);
$human_exception = $error_details ? new \Exception($error_details, 400) : $e;
2023-02-16 02:36:09 +01:00
SystemLogger::dispatch(
$human_exception->getMessage(),
SystemLog::CATEGORY_GATEWAY_RESPONSE,
SystemLog::EVENT_GATEWAY_ERROR,
SystemLog::TYPE_CHECKOUT,
$this->checkout->client,
$this->checkout->client->company,
);
return $this->checkout->processInternallyFailedPayment($this->checkout, $human_exception);
2022-06-16 02:01:24 +02:00
} catch (CheckoutArgumentException $e) {
// Bad arguments
$error_details = $e->error_details;
2023-02-16 02:36:09 +01:00
if (is_array($error_details)) {
$error_details = end($e->error_details['error_codes']);
}
2022-06-16 03:21:10 +02:00
2021-01-26 13:09:08 +01:00
$this->checkout->unWindGatewayFees($this->checkout->payment_hash);
2022-06-16 02:01:24 +02:00
$human_exception = $error_details ? new \Exception($error_details, 400) : $e;
2023-02-16 02:36:09 +01:00
SystemLogger::dispatch(
$human_exception->getMessage(),
SystemLog::CATEGORY_GATEWAY_RESPONSE,
SystemLog::EVENT_GATEWAY_ERROR,
SystemLog::TYPE_CHECKOUT,
$this->checkout->client,
$this->checkout->client->company,
);
return $this->checkout->processInternallyFailedPayment($this->checkout, $human_exception);
2022-06-16 02:01:24 +02:00
} catch (CheckoutAuthorizationException $e) {
// Bad Invalid authorization
2022-06-16 03:21:10 +02:00
$error_details = $e->error_details;
2023-02-16 02:36:09 +01:00
if (is_array($error_details)) {
$error_details = end($e->error_details['error_codes']);
}
2022-06-16 02:01:24 +02:00
$this->checkout->unWindGatewayFees($this->checkout->payment_hash);
$human_exception = $error_details ? new \Exception($error_details, 400) : $e;
2023-02-16 02:36:09 +01:00
SystemLogger::dispatch(
$human_exception->getMessage(),
SystemLog::CATEGORY_GATEWAY_RESPONSE,
SystemLog::EVENT_GATEWAY_ERROR,
SystemLog::TYPE_CHECKOUT,
$this->checkout->client,
$this->checkout->client->company,
);
return $this->checkout->processInternallyFailedPayment($this->checkout, $human_exception);
2020-10-22 15:24:18 +02:00
}
}
}