1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 07:02:34 +01:00
invoiceninja/app/PaymentDrivers/CheckoutCom/CreditCard.php

332 lines
12 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
*
2024-04-12 06:15:41 +02:00
* @copyright Copyright (c) 2024. 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;
2023-09-12 02:27:30 +02:00
use App\Exceptions\PaymentFailed;
2023-10-26 04:57:44 +02:00
use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest;
use App\Jobs\Util\SystemLogger;
2023-09-12 02:27:30 +02:00
use App\Models\ClientGatewayToken;
2023-10-26 04:57:44 +02:00
use App\Models\GatewayType;
use App\Models\SystemLog;
use App\PaymentDrivers\CheckoutComPaymentDriver;
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;
2023-09-12 02:27:30 +02:00
use Checkout\Payments\Previous\PaymentRequest as PreviousPaymentRequest;
2023-10-26 04:57:44 +02:00
use Checkout\Payments\Previous\Source\RequestTokenSource;
use Checkout\Payments\Request\PaymentRequest;
2023-09-12 02:27:30 +02:00
use Checkout\Payments\Request\Source\RequestTokenSource as SourceRequestTokenSource;
2023-10-26 04:57:44 +02:00
use Illuminate\Contracts\View\Factory;
use Illuminate\Http\Request;
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;
2023-09-12 02:27:30 +02:00
$request = new PreviousPaymentRequest();
2022-06-16 03:21:10 +02:00
$request->source = $token_source;
} else {
2022-06-16 03:21:10 +02:00
$token_source = new SourceRequestTokenSource();
$token_source->token = $token;
2023-09-12 02:27:30 +02:00
$request = new PaymentRequest();
2022-06-16 03:21:10 +02:00
$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();
2024-01-14 05:05:00 +01: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') {
2024-01-14 05:05:00 +01: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) {
2023-09-12 02:27:30 +02:00
2022-06-15 14:47:25 +02:00
$error_details = $e->error_details;
2023-09-12 02:27:30 +02:00
if (isset($e->error_details['error_codes']) ?? false) {
$error_details = end($e->error_details['error_codes']);
2023-10-26 04:57:44 +02:00
} else {
2023-09-12 02:27:30 +02:00
$error_details = $e->getMessage();
}
2023-09-12 02:27:30 +02:00
throw new PaymentFailed($error_details, $e->getCode());
2022-06-15 14:47:25 +02:00
} catch (CheckoutArgumentException $e) {
// Bad arguments
2023-09-12 02:27:30 +02:00
throw new PaymentFailed($e->getMessage(), $e->getCode());
2022-06-15 14:47:25 +02:00
} catch (CheckoutAuthorizationException $e) {
// Bad Invalid authorization
2023-09-12 02:27:30 +02:00
throw new PaymentFailed("There is a problem with your Checkout Gateway API keys", 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();
2023-07-21 07:48:10 +02:00
$paymentRequest->metadata = ['udf1' => 'Invoice Ninja', 'udf2' => $this->checkout->payment_hash->hash];
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);
2024-02-13 05:25:18 +01:00
if($this->checkout->company_gateway->update_details && isset($response['customer'])) {
$this->checkout->updateCustomer($response['customer']['id'] ?? '');
}
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
$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']);
SystemLogger::dispatch(
$error_details,
SystemLog::CATEGORY_GATEWAY_RESPONSE,
SystemLog::EVENT_GATEWAY_ERROR,
SystemLog::TYPE_CHECKOUT,
$this->checkout->client,
$this->checkout->client->company,
);
}
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(
2023-09-12 02:27:30 +02:00
$e->getMessage(),
2023-02-16 02:36:09 +01:00
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
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
2023-02-16 02:36:09 +01:00
SystemLogger::dispatch(
2023-09-12 02:27:30 +02:00
$e->getMessage(),
2023-02-16 02:36:09 +01:00
SystemLog::CATEGORY_GATEWAY_RESPONSE,
SystemLog::EVENT_GATEWAY_ERROR,
SystemLog::TYPE_CHECKOUT,
$this->checkout->client,
$this->checkout->client->company,
);
2023-09-12 02:27:30 +02:00
return new PaymentFailed($e->getMessage(), $e->getCode());
2022-06-16 02:01:24 +02:00
} catch (CheckoutAuthorizationException $e) {
// Bad Invalid authorization
2022-06-16 03:21:10 +02:00
2022-06-16 02:01:24 +02:00
$this->checkout->unWindGatewayFees($this->checkout->payment_hash);
2023-02-16 02:36:09 +01:00
SystemLogger::dispatch(
2023-09-12 02:27:30 +02:00
$e->getMessage(),
2023-02-16 02:36:09 +01:00
SystemLog::CATEGORY_GATEWAY_RESPONSE,
SystemLog::EVENT_GATEWAY_ERROR,
SystemLog::TYPE_CHECKOUT,
$this->checkout->client,
$this->checkout->client->company,
);
2023-09-12 02:27:30 +02:00
return new PaymentFailed("There was a problem communicating with the API credentials for Checkout", $e->getCode());
2020-10-22 15:24:18 +02:00
}
}
}