1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-11 05:32:39 +01:00
invoiceninja/app/PaymentDrivers/CheckoutCom/CreditCard.php

408 lines
14 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
*
2022-04-27 05:20:41 +02:00
* @copyright Copyright (c) 2022. 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;
use App\Models\ClientGatewayToken;
2021-10-14 18:43:47 +02:00
use App\Models\GatewayType;
2022-06-15 14:47:25 +02:00
use App\Models\Payment;
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\Common\CustomerRequest;
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;
2022-06-15 14:47:25 +02:00
use Checkout\Payments\Four\Request\PaymentRequest;
2022-06-16 02:01:24 +02:00
use Checkout\Payments\Four\Request\Source\RequestIdSource as SourceRequestIdSource;
2022-06-15 14:47:25 +02:00
use Checkout\Payments\Four\Request\Source\RequestTokenSource;
2022-06-16 02:01:24 +02:00
use Checkout\Payments\PaymentRequest as PaymentsPaymentRequest;
use Checkout\Payments\Source\RequestIdSource;
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-15 14:47:25 +02:00
private function getCustomer()
{
try{
$response = $this->checkout->gateway->getCustomersClient()->get($this->checkout->client->present()->email());
return $response;
}
catch(\Exception $e){
$request = new CustomerRequest();
$request->email = $this->checkout->client->present()->email();
$request->name = $this->checkout->client->present()->name();
return $request;
}
}
2022-06-16 02:01:24 +02:00
public function bootRequest($token)
{
if($this->checkout->is_four_api){
$token_source = new RequestTokenSource();
$token_source->token = $token;
$request = new PaymentRequest();
$request->source = $token_source;
}
else {
$token_source = new SourceRequestTokenSource();
$token_source->token = $token;
$request = new PaymentsPaymentRequest();
$request->source = $token_source;
}
return $request;
}
public function bootTokenRequest($token)
{
if($this->checkout->is_four_api){
$token_source = new SourceRequestIdSource();
$token_source->id = $token;
$request = new PaymentRequest();
$request->source = $token_source;
}
else {
$token_source = new RequestIdSource();
$token_source->id = $token;
$request = new PaymentsPaymentRequest();
$request->source = $token_source;
}
return $request;
}
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-15 14:47:25 +02:00
// $method = new TokenSource(
// $gateway_response->token
// );
// $payment = new Payment($method, 'USD');
// $payment->amount = 100; // $1
// $payment->reference = '$1 payment for authorization.';
// $payment->capture = false;
2021-10-14 18:43:47 +02:00
2022-06-15 14:47:25 +02:00
$customerRequest = $this->getCustomer();
2022-06-16 02:01:24 +02:00
$request = $this->bootRequest($gateway_response->token);
2022-06-15 14:47:25 +02:00
$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
2022-06-16 02:01:24 +02:00
2021-10-14 18:43:47 +02:00
try {
2022-06-15 14:47:25 +02:00
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'],
2022-06-16 02:01:24 +02:00
'payment_method_id' => GatewayType::CREDIT_CARD,
2021-10-14 18:43:47 +02:00
];
2022-06-16 02:01:24 +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;
$error_details = $e->error_details;
2022-06-16 02:01:24 +02:00
dd($e);
throw new PaymentFailed($e->getMessage());
2022-06-15 14:47:25 +02:00
} catch (CheckoutArgumentException $e) {
// Bad arguments
dd($e->getMessage());
2022-06-16 02:01:24 +02:00
throw new PaymentFailed($e->getMessage());
2022-06-15 14:47:25 +02:00
} catch (CheckoutAuthorizationException $e) {
// Bad Invalid authorization
dd($e->getMessage());
2022-06-16 02:01:24 +02:00
throw new PaymentFailed($e->getMessage());
2021-10-14 18:43:47 +02:00
}
2022-06-15 14:47:25 +02:00
// try {
// $response = $this->checkout->gateway->payments()->request($payment);
// if ($response->approved && $response->status === 'Authorized') {
// $payment_meta = new \stdClass;
// $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'];
// $payment_meta->type = (int) GatewayType::CREDIT_CARD;
// $data = [
// 'payment_meta' => $payment_meta,
// 'token' => $response->source['id'],
// 'payment_method_id' => GatewayType::CREDIT_CARD,
// ];
// $payment_method = $this->checkout->storeGatewayToken($data);
// return redirect()->route('client.payment_methods.show', $payment_method->hashed_id);
// }
// } catch (CheckoutHttpException $exception) {
// throw new PaymentFailed(
// $exception->getMessage()
// );
// }
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
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()->guard('contact')->user()->client->company->id)
2021-02-03 12:36:10 +01:00
->first();
if (!$cgt) {
throw new PaymentFailed(ctrans('texts.payment_token_not_found'), 401);
}
2022-06-16 02:01:24 +02:00
// $method = new IdSource($cgt->token);
$paymentRequest = $this->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 02:01:24 +02:00
// $method = new TokenSource(
// $checkout_response->token
// );
$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-02-09 08:48:08 +01:00
2020-10-22 15:24:18 +02:00
2022-06-16 02:01:24 +02:00
$paymentRequest->amount = $this->checkout->payment_hash->data->value;
$paymentRequest->reference = $this->checkout->getDescription();
$paymentRequest->customer = $this->getCustomer();
$paymentRequest->metadata = ['udf1' => "Invoice Ninja"];
$paymentRequest->currency = $this->checkout->client->getCurrencyCode();
2022-02-09 08:48:08 +01:00
2022-06-16 02:01:24 +02:00
// $payment = new Payment($method, $this->checkout->payment_hash->data->currency);
// $payment->amount = $this->checkout->payment_hash->data->value;
// $payment->reference = $this->checkout->getDescription();
// $payment->customer = [
// 'name' => $this->checkout->client->present()->name() ,
// 'email' => $this->checkout->client->present()->email(),
// ];
// $payment->metadata = [
// 'udf1' => "Invoice Ninja",
// ];
$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 02:01:24 +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,
]);
// $payment->{'3ds'} = ['enabled' => true];
// $payment->{'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,
// ]);
// $payment->{'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->payments()->request($payment);
$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);
2021-11-27 05:54:49 +01:00
// $this->checkout->sendFailureMail($response->response_summary);
2021-10-17 12:40:40 +02:00
//@todo - this will double up the checkout . com failed mails
2021-11-27 05:54:49 +01:00
// $this->checkout->clientPaymentFailureMailer($response->status);
2021-10-13 11:24:15 +02:00
2020-10-22 15:24:18 +02:00
return $this->processUnsuccessfulPayment($response);
}
2022-06-16 02:01:24 +02:00
}
catch (CheckoutApiException $e) {
// API error
$request_id = $e->request_id;
$http_status_code = $e->http_status_code;
$error_details = $e->error_details;
dd($e);
$this->checkout->unWindGatewayFees($this->checkout->payment_hash);
return $this->checkout->processInternallyFailedPayment($this->checkout, $e);
} catch (CheckoutArgumentException $e) {
// Bad arguments
dd($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);
2022-06-16 02:01:24 +02:00
} catch (CheckoutAuthorizationException $e) {
// Bad Invalid authorization
dd($e);
$this->checkout->unWindGatewayFees($this->checkout->payment_hash);
return $this->checkout->processInternallyFailedPayment($this->checkout, $e);
2020-10-22 15:24:18 +02:00
}
2022-06-16 02:01:24 +02:00
2020-10-22 15:24:18 +02:00
}
}