Checkout v2 refund refactor

This commit is contained in:
David Bomba 2022-06-16 11:21:10 +10:00
parent 9856946302
commit 63468a1669
5 changed files with 183 additions and 220 deletions

View File

@ -44,16 +44,20 @@ class Checkout3dsRequest extends FormRequest
public function getCompanyGateway()
{
MultiDB::findAndSetDbByCompanyKey($this->company_key);
return CompanyGateway::find($this->decodePrimaryKey($this->company_gateway_id));
}
public function getPaymentHash()
{
MultiDB::findAndSetDbByCompanyKey($this->company_key);
return PaymentHash::where('hash', $this->hash)->first();
}
public function getClient()
{
return Client::find($this->getPaymentHash()->data->client_id);
return Client::withTrashed()->find($this->getPaymentHash()->data->client_id);
}
}

View File

@ -407,12 +407,7 @@ class BaseDriver extends AbstractPaymentDriver
$this->unWindGatewayFees($this->payment_hash);
}
if ($e instanceof CheckoutHttpException) {
$error = $e->getBody();
} else if ($e instanceof Exception) {
$error = $e->getMessage();
} else
$error = $e->getMessage();
$error = $e->getMessage();
if(!$this->payment_hash)
throw new PaymentFailed($error, $e->getCode());

View File

@ -23,18 +23,15 @@ use App\Utils\Traits\MakesHash;
use Checkout\CheckoutApiException;
use Checkout\CheckoutArgumentException;
use Checkout\CheckoutAuthorizationException;
use Checkout\Common\CustomerRequest;
use Checkout\Library\Exceptions\CheckoutHttpException;
use Checkout\Models\Payments\IdSource;
use Checkout\Payments\Four\Request\PaymentRequest;
use Checkout\Payments\Four\Request\Source\RequestIdSource as SourceRequestIdSource;
use Checkout\Payments\Four\Request\Source\RequestTokenSource;
use Checkout\Payments\PaymentRequest as PaymentsPaymentRequest;
use Checkout\Payments\Source\RequestIdSource;
use Checkout\Payments\Source\RequestTokenSource as SourceRequestTokenSource;
use Illuminate\Contracts\View\Factory;
use Illuminate\Http\Request;
use Illuminate\View\View;
use Checkout\Payments\PaymentRequest as PaymentsPaymentRequest;
use Checkout\Payments\Four\Request\PaymentRequest;
class CreditCard implements MethodInterface
{
@ -66,71 +63,30 @@ class CreditCard implements MethodInterface
return render('gateways.checkout.credit_card.authorize', $data);
}
private function getCustomer()
public function bootRequest($token)
{
try{
$response = $this->checkout->gateway->getCustomersClient()->get($this->checkout->client->present()->email());
return $response;
if($this->checkout->is_four_api){
$token_source = new RequestTokenSource();
$token_source->token = $token;
$request = new PaymentRequest();
$request->source = $token_source;
}
catch(\Exception $e){
else {
$token_source = new SourceRequestTokenSource();
$token_source->token = $token;
$request = new PaymentsPaymentRequest();
$request->source = $token_source;
$request = new CustomerRequest();
$request->email = $this->checkout->client->present()->email();
$request->name = $this->checkout->client->present()->name();
return $request;
}
}
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;
return $request;
}
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;
}
/**
* Handle authorization for credit card.
@ -142,22 +98,13 @@ public function bootTokenRequest($token)
{
$gateway_response = \json_decode($request->gateway_response);
// $method = new TokenSource(
// $gateway_response->token
// );
// $payment = new Payment($method, 'USD');
// $payment->amount = 100; // $1
// $payment->reference = '$1 payment for authorization.';
// $payment->capture = false;
$customerRequest = $this->getCustomer();
$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;
$customerRequest = $this->checkout->getCustomer();
$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;
try {
@ -190,44 +137,15 @@ $request->customer = $customerRequest;
$http_status_code = $e->http_status_code;
$error_details = $e->error_details;
dd($e);
throw new PaymentFailed($e->getMessage());
} catch (CheckoutArgumentException $e) {
// Bad arguments
dd($e->getMessage());
throw new PaymentFailed($e->getMessage());
} catch (CheckoutAuthorizationException $e) {
// Bad Invalid authorization
dd($e->getMessage());
throw new PaymentFailed($e->getMessage());
}
// 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()
// );
// }
}
public function paymentView($data)
@ -271,15 +189,14 @@ $request->customer = $customerRequest;
{
$cgt = ClientGatewayToken::query()
->where('id', $this->decodePrimaryKey($request->input('token')))
->where('company_id', auth()->guard('contact')->user()->client->company->id)
->where('company_id', auth()->guard('contact')->user()->client->company_id)
->first();
if (!$cgt) {
throw new PaymentFailed(ctrans('texts.payment_token_not_found'), 401);
}
// $method = new IdSource($cgt->token);
$paymentRequest = $this->bootTokenRequest($cgt->token);
$paymentRequest = $this->checkout->bootTokenRequest($cgt->token);
return $this->completePayment($paymentRequest, $request);
}
@ -288,11 +205,7 @@ $paymentRequest = $this->bootTokenRequest($cgt->token);
{
$checkout_response = $this->checkout->payment_hash->data->server_response;
// $method = new TokenSource(
// $checkout_response->token
// );
$paymentRequest = $this->bootRequest($checkout_response->token);
$paymentRequest = $this->bootRequest($checkout_response->token);
return $this->completePayment($paymentRequest, $request);
}
@ -300,57 +213,31 @@ $paymentRequest = $this->bootRequest($checkout_response->token);
private function completePayment($paymentRequest, PaymentResponseRequest $request)
{
$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();
// $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",
// ];
$paymentRequest->amount = $this->checkout->payment_hash->data->value;
$paymentRequest->reference = $this->checkout->getDescription();
$paymentRequest->customer = $this->checkout->getCustomer();
$paymentRequest->metadata = ['udf1' => "Invoice Ninja"];
$paymentRequest->currency = $this->checkout->client->getCurrencyCode();
$this->checkout->payment_hash->data = array_merge((array)$this->checkout->payment_hash->data, ['checkout_payment_ref' => $paymentRequest]);
$this->checkout->payment_hash->save();
if ($this->checkout->client->currency()->code == 'EUR' || $this->checkout->company_gateway->getConfigField('threeds')) {
$paymentRequest->{'3ds'} = ['enabled' => true];
$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->{'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,
]);
$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,
// ]);
}
try {
@ -369,13 +256,9 @@ $paymentRequest->{'failure_url'} = route('checkout.3ds_redirect', [
}
if ($response['status'] == 'Declined') {
$this->checkout->unWindGatewayFees($this->checkout->payment_hash);
// $this->checkout->sendFailureMail($response->response_summary);
//@todo - this will double up the checkout . com failed mails
// $this->checkout->clientPaymentFailureMailer($response->status);
return $this->processUnsuccessfulPayment($response);
}
}
@ -385,19 +268,19 @@ $paymentRequest->{'failure_url'} = route('checkout.3ds_redirect', [
$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);
$this->checkout->unWindGatewayFees($this->checkout->payment_hash);
return $this->checkout->processInternallyFailedPayment($this->checkout, $e);
} catch (CheckoutAuthorizationException $e) {
// Bad Invalid authorization
dd($e);
$this->checkout->unWindGatewayFees($this->checkout->payment_hash);
return $this->checkout->processInternallyFailedPayment($this->checkout, $e);

View File

@ -117,10 +117,11 @@ trait Utilities
private function processPendingPayment($_payment)
{
try {
return redirect($_payment['links']['redirect']['href']);
return redirect($_payment['_links']['redirect']['href']);
} catch (Exception $e) {
return $this->processInternallyFailedPayment($this->getParent(), $e);
return $this->getParent()->processInternallyFailedPayment($this->getParent(), $e);
}
}
@ -128,15 +129,15 @@ trait Utilities
{
try {
$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->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'],
'token' => $response['source']['id'],
'payment_method_id' => $this->getParent()->payment_hash->data->payment_method_id,
];

View File

@ -28,6 +28,9 @@ use App\PaymentDrivers\CheckoutCom\CreditCard;
use App\PaymentDrivers\CheckoutCom\Utilities;
use App\Utils\Traits\SystemLogTrait;
use Checkout\CheckoutApi;
use Checkout\CheckoutApiException;
use Checkout\CheckoutArgumentException;
use Checkout\CheckoutAuthorizationException;
use Checkout\CheckoutDefaultSdk;
use Checkout\CheckoutFourSdk;
use Checkout\Environment;
@ -35,7 +38,12 @@ use Checkout\Library\Exceptions\CheckoutHttpException;
use Checkout\Models\Payments\IdSource;
use Checkout\Models\Payments\Refund;
use Exception;
use JmesPath\Env;
use Checkout\Payments\Four\Request\PaymentRequest;
use Checkout\Payments\Four\Request\Source\RequestIdSource as SourceRequestIdSource;
use Checkout\Payments\PaymentRequest as PaymentsPaymentRequest;
use Checkout\Payments\Source\RequestIdSource;
use Checkout\Common\CustomerRequest;
use Checkout\Payments\RefundRequest;
class CheckoutComPaymentDriver extends BaseDriver
{
@ -142,9 +150,6 @@ class CheckoutComPaymentDriver extends BaseDriver
*/
public function viewForType($gateway_type_id)
{
// At the moment Checkout.com payment
// driver only supports payments using credit card.
return 'gateways.checkout.credit_card.pay';
}
@ -232,30 +237,104 @@ class CheckoutComPaymentDriver extends BaseDriver
{
$this->init();
$checkout_payment = new Refund($payment->transaction_reference);
$request = new RefundRequest();
$request->reference = "{$payment->transaction_reference} " . now();
$request->amount = $this->convertToCheckoutAmount($amount, $this->client->getCurrencyCode());
try {
$refund = $this->gateway->payments()->refund($checkout_payment);
$checkout_payment = $this->gateway->payments()->details($refund->id);
$response = ['refund_response' => $refund, 'checkout_payment_fetch' => $checkout_payment];
// or, refundPayment("payment_id") for a full refund
$response = $this->gateway->getPaymentsClient()->refundPayment($payment->transaction_reference, $request);
return [
'transaction_reference' => $refund->action_id,
'transaction_reference' => $response['action_id'],
'transaction_response' => json_encode($response),
'success' => $checkout_payment->status == 'Refunded',
'description' => $checkout_payment->status,
'code' => $checkout_payment->http_code,
];
} catch (CheckoutHttpException $e) {
return [
'transaction_reference' => null,
'transaction_response' => json_encode($e->getMessage()),
'success' => false,
'description' => $e->getMessage(),
'code' => $e->getCode(),
'success' => true,
'description' => $response['reference'],
'code' => 202,
];
} catch (CheckoutApiException $e) {
// API error
nlog($e);
$request_id = $e->request_id;
$http_status_code = $e->http_status_code;
$error_details = $e->error_details;
} catch (CheckoutArgumentException $e) {
// Bad arguments
nlog($e);
} catch (CheckoutAuthorizationException $e) {
// Bad Invalid authorization
nlog($e);
}
// $checkout_payment = new Refund($payment->transaction_reference);
// // try {
// // $refund = $this->gateway->payments()->refund($checkout_payment);
// // $checkout_payment = $this->gateway->payments()->details($refund->id);
// // $response = ['refund_response' => $refund, 'checkout_payment_fetch' => $checkout_payment];
// // return [
// // 'transaction_reference' => $refund->action_id,
// // 'transaction_response' => json_encode($response),
// // 'success' => $checkout_payment->status == 'Refunded',
// // 'description' => $checkout_payment->status,
// // 'code' => $checkout_payment->http_code,
// // ];
// // } catch (CheckoutApiException $e) {
// // return [
// // 'transaction_reference' => null,
// // 'transaction_response' => json_encode($e->getMessage()),
// // 'success' => false,
// // 'description' => $e->getMessage(),
// // 'code' => $e->getCode(),
// // ];
// // }
}
public function getCustomer()
{
try{
$response = $this->gateway->getCustomersClient()->get($this->client->present()->email());
return $response;
}
catch(\Exception $e){
$request = new CustomerRequest();
$request->email = $this->client->present()->email();
$request->name = $this->client->present()->name();
return $request;
}
}
public function bootTokenRequest($token)
{
if($this->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;
}
public function tokenBilling(ClientGatewayToken $cgt, PaymentHash $payment_hash)
@ -265,27 +344,29 @@ class CheckoutComPaymentDriver extends BaseDriver
$this->init();
$method = new IdSource($cgt->token);
$payment = new \Checkout\Models\Payments\Payment($method, $this->client->getCurrencyCode());
$payment->amount = $this->convertToCheckoutAmount($amount, $this->client->getCurrencyCode());
$payment->reference = $invoice->number . '-' . now();
$paymentRequest = $this->bootTokenRequest($cgt->token);
$paymentRequest->amount = $this->convertToCheckoutAmount($amount, $this->client->getCurrencyCode());
$paymentRequest->reference = '#' . $invoice->number . ' - ' . now();
$paymentRequest->customer = $this->getCustomer();
$paymentRequest->metadata = ['udf1' => "Invoice Ninja"];
$paymentRequest->currency = $this->client->getCurrencyCode();
$request = new PaymentResponseRequest();
$request->setMethod('POST');
$request->request->add(['payment_hash' => $payment_hash->hash]);
try {
$response = $this->gateway->payments()->request($payment);
// $response = $this->gateway->payments()->request($payment);
$response = $this->gateway->getPaymentsClient()->requestPayment($paymentRequest);
if ($response->status == 'Authorized') {
if ($response['status'] == 'Authorized') {
$this->confirmGatewayFee($request);
$data = [
'payment_method' => $response->source['id'],
'payment_type' => PaymentType::parseCardType(strtolower($response->source['scheme'])),
'payment_method' => $response['source']['id'],
'payment_type' => PaymentType::parseCardType(strtolower($response['source']['scheme'])),
'amount' => $amount,
'transaction_reference' => $response->id,
'transaction_reference' => $response['id'],
];
$payment = $this->createPayment($data, Payment::STATUS_COMPLETED);
@ -301,10 +382,10 @@ class CheckoutComPaymentDriver extends BaseDriver
return $payment;
}
if ($response->status == 'Declined') {
if ($response['status'] == 'Declined') {
$this->unWindGatewayFees($payment_hash);
$this->sendFailureMail($response->status . " " . $response->response_summary);
$this->sendFailureMail($response['status'] . " " . $response['response_summary']);
$message = [
'server_response' => $response,
@ -321,11 +402,9 @@ class CheckoutComPaymentDriver extends BaseDriver
return false;
}
} catch (Exception | CheckoutHttpException $e) {
} catch (Exception | CheckoutApiException $e) {
$this->unWindGatewayFees($payment_hash);
$message = $e instanceof CheckoutHttpException
? $e->getBody()
: $e->getMessage();
$message = $e->getMessage();
$data = [
'status' => '',
@ -355,20 +434,21 @@ class CheckoutComPaymentDriver extends BaseDriver
public function process3dsConfirmation(Checkout3dsRequest $request)
{
$this->init();
$this->setPaymentHash($request->getPaymentHash());
try {
$payment = $this->gateway->payments()->details(
$payment = $this->gateway->getPaymentsClient()->getPaymentDetails(
$request->query('cko-session-id')
);
if ($payment->approved) {
if ($payment['approved']) {
return $this->processSuccessfulPayment($payment);
} else {
return $this->processUnsuccessfulPayment($payment);
}
} catch (CheckoutHttpException | Exception $e) {
} catch (CheckoutApiException | Exception $e) {
return $this->processInternallyFailedPayment($this, $e);
}
}