mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2024-11-10 13:12:50 +01:00
Checkout v2 refactor
This commit is contained in:
parent
3f8449ba01
commit
9856946302
@ -27,7 +27,11 @@ use Checkout\Common\CustomerRequest;
|
|||||||
use Checkout\Library\Exceptions\CheckoutHttpException;
|
use Checkout\Library\Exceptions\CheckoutHttpException;
|
||||||
use Checkout\Models\Payments\IdSource;
|
use Checkout\Models\Payments\IdSource;
|
||||||
use Checkout\Payments\Four\Request\PaymentRequest;
|
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\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\Contracts\View\Factory;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\View\View;
|
use Illuminate\View\View;
|
||||||
@ -79,6 +83,55 @@ class CreditCard implements MethodInterface
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handle authorization for credit card.
|
* Handle authorization for credit card.
|
||||||
*
|
*
|
||||||
@ -99,21 +152,17 @@ class CreditCard implements MethodInterface
|
|||||||
// $payment->capture = false;
|
// $payment->capture = false;
|
||||||
|
|
||||||
$customerRequest = $this->getCustomer();
|
$customerRequest = $this->getCustomer();
|
||||||
|
$request = $this->bootRequest($gateway_response->token);
|
||||||
$token_source = new RequestTokenSource();
|
|
||||||
$token_source->token = $gateway_response->token;
|
|
||||||
|
|
||||||
$request = new PaymentRequest();
|
|
||||||
$request->source = $token_source;
|
|
||||||
$request->capture = false;
|
$request->capture = false;
|
||||||
$request->reference = '$1 payment for authorization.';
|
$request->reference = '$1 payment for authorization.';
|
||||||
$request->amount = 100;
|
$request->amount = 100;
|
||||||
$request->currency = $this->checkout->client->getCurrencyCode();
|
$request->currency = $this->checkout->client->getCurrencyCode();
|
||||||
$request->customer = $customerRequest;
|
$request->customer = $customerRequest;
|
||||||
|
|
||||||
try {
|
|
||||||
$response = $this->checkout->gateway->getPaymentsClient()->requestPayment($request);
|
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
$response = $this->checkout->gateway->getPaymentsClient()->requestPayment($request);
|
||||||
|
|
||||||
if ($response['approved'] && $response['status'] === 'Authorized') {
|
if ($response['approved'] && $response['status'] === 'Authorized') {
|
||||||
$payment_meta = new \stdClass;
|
$payment_meta = new \stdClass;
|
||||||
@ -126,10 +175,10 @@ $request->customer = $customerRequest;
|
|||||||
$data = [
|
$data = [
|
||||||
'payment_meta' => $payment_meta,
|
'payment_meta' => $payment_meta,
|
||||||
'token' => $response['source']['id'],
|
'token' => $response['source']['id'],
|
||||||
'payment_method_id' => GatewayType::CREDIT_CARD,
|
'payment_method_id' => GatewayType::CREDIT_CARD,
|
||||||
];
|
];
|
||||||
|
|
||||||
$payment_method = $this->checkout->storeGatewayToken($data);
|
$payment_method = $this->checkout->storeGatewayToken($data,['gateway_customer_reference' => $customerRequest['id']]);
|
||||||
|
|
||||||
return redirect()->route('client.payment_methods.show', $payment_method->hashed_id);
|
return redirect()->route('client.payment_methods.show', $payment_method->hashed_id);
|
||||||
}
|
}
|
||||||
@ -141,15 +190,16 @@ $request->customer = $customerRequest;
|
|||||||
$http_status_code = $e->http_status_code;
|
$http_status_code = $e->http_status_code;
|
||||||
$error_details = $e->error_details;
|
$error_details = $e->error_details;
|
||||||
|
|
||||||
dd($e);
|
dd($e);
|
||||||
|
throw new PaymentFailed($e->getMessage());
|
||||||
} catch (CheckoutArgumentException $e) {
|
} catch (CheckoutArgumentException $e) {
|
||||||
// Bad arguments
|
// Bad arguments
|
||||||
dd($e->getMessage());
|
dd($e->getMessage());
|
||||||
|
throw new PaymentFailed($e->getMessage());
|
||||||
} catch (CheckoutAuthorizationException $e) {
|
} catch (CheckoutAuthorizationException $e) {
|
||||||
// Bad Invalid authorization
|
// Bad Invalid authorization
|
||||||
dd($e->getMessage());
|
dd($e->getMessage());
|
||||||
|
throw new PaymentFailed($e->getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
// try {
|
// try {
|
||||||
@ -228,70 +278,97 @@ $request->customer = $customerRequest;
|
|||||||
throw new PaymentFailed(ctrans('texts.payment_token_not_found'), 401);
|
throw new PaymentFailed(ctrans('texts.payment_token_not_found'), 401);
|
||||||
}
|
}
|
||||||
|
|
||||||
$method = new IdSource($cgt->token);
|
// $method = new IdSource($cgt->token);
|
||||||
|
$paymentRequest = $this->bootTokenRequest($cgt->token);
|
||||||
|
|
||||||
return $this->completePayment($method, $request);
|
return $this->completePayment($paymentRequest, $request);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function attemptPaymentUsingCreditCard(PaymentResponseRequest $request)
|
private function attemptPaymentUsingCreditCard(PaymentResponseRequest $request)
|
||||||
{
|
{
|
||||||
$checkout_response = $this->checkout->payment_hash->data->server_response;
|
$checkout_response = $this->checkout->payment_hash->data->server_response;
|
||||||
|
|
||||||
$method = new TokenSource(
|
// $method = new TokenSource(
|
||||||
$checkout_response->token
|
// $checkout_response->token
|
||||||
);
|
// );
|
||||||
|
|
||||||
return $this->completePayment($method, $request);
|
$paymentRequest = $this->bootRequest($checkout_response->token);
|
||||||
|
|
||||||
|
return $this->completePayment($paymentRequest, $request);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function completePayment($method, PaymentResponseRequest $request)
|
private function completePayment($paymentRequest, PaymentResponseRequest $request)
|
||||||
{
|
{
|
||||||
|
|
||||||
$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 = [
|
$paymentRequest->amount = $this->checkout->payment_hash->data->value;
|
||||||
'udf1' => "Invoice Ninja",
|
$paymentRequest->reference = $this->checkout->getDescription();
|
||||||
];
|
$paymentRequest->customer = $this->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' => $payment]);
|
// $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]);
|
||||||
$this->checkout->payment_hash->save();
|
$this->checkout->payment_hash->save();
|
||||||
|
|
||||||
if ($this->checkout->client->currency()->code == 'EUR' || $this->checkout->company_gateway->getConfigField('threeds')) {
|
if ($this->checkout->client->currency()->code == 'EUR' || $this->checkout->company_gateway->getConfigField('threeds')) {
|
||||||
$payment->{'3ds'} = ['enabled' => true];
|
|
||||||
|
|
||||||
$payment->{'success_url'} = route('checkout.3ds_redirect', [
|
$paymentRequest->{'3ds'} = ['enabled' => true];
|
||||||
'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', [
|
$paymentRequest->{'success_url'} = route('checkout.3ds_redirect', [
|
||||||
'company_key' => $this->checkout->client->company->company_key,
|
'company_key' => $this->checkout->client->company->company_key,
|
||||||
'company_gateway_id' => $this->checkout->company_gateway->hashed_id,
|
'company_gateway_id' => $this->checkout->company_gateway->hashed_id,
|
||||||
'hash' => $this->checkout->payment_hash->hash,
|
'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 {
|
try {
|
||||||
$response = $this->checkout->gateway->payments()->request($payment);
|
// $response = $this->checkout->gateway->payments()->request($payment);
|
||||||
|
|
||||||
if ($response->status == 'Authorized') {
|
$response = $this->checkout->gateway->getPaymentsClient()->requestPayment($paymentRequest);
|
||||||
|
|
||||||
|
if ($response['status'] == 'Authorized') {
|
||||||
return $this->processSuccessfulPayment($response);
|
return $this->processSuccessfulPayment($response);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($response->status == 'Pending') {
|
if ($response['status'] == 'Pending') {
|
||||||
$this->checkout->confirmGatewayFee();
|
$this->checkout->confirmGatewayFee();
|
||||||
|
|
||||||
return $this->processPendingPayment($response);
|
return $this->processPendingPayment($response);
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($response->status == 'Declined') {
|
if ($response['status'] == 'Declined') {
|
||||||
$this->checkout->unWindGatewayFees($this->checkout->payment_hash);
|
$this->checkout->unWindGatewayFees($this->checkout->payment_hash);
|
||||||
|
|
||||||
// $this->checkout->sendFailureMail($response->response_summary);
|
// $this->checkout->sendFailureMail($response->response_summary);
|
||||||
@ -301,9 +378,30 @@ $request->customer = $customerRequest;
|
|||||||
|
|
||||||
return $this->processUnsuccessfulPayment($response);
|
return $this->processUnsuccessfulPayment($response);
|
||||||
}
|
}
|
||||||
} catch (CheckoutHttpException $e) {
|
}
|
||||||
|
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);
|
$this->checkout->unWindGatewayFees($this->checkout->payment_hash);
|
||||||
return $this->checkout->processInternallyFailedPayment($this->checkout, $e);
|
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);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -54,17 +54,18 @@ trait Utilities
|
|||||||
return round($amount * 100);
|
return round($amount * 100);
|
||||||
}
|
}
|
||||||
|
|
||||||
private function processSuccessfulPayment(Payment $_payment)
|
private function processSuccessfulPayment($_payment)
|
||||||
{
|
{
|
||||||
|
|
||||||
if ($this->getParent()->payment_hash->data->store_card) {
|
if ($this->getParent()->payment_hash->data->store_card) {
|
||||||
$this->storeLocalPaymentMethod($_payment);
|
$this->storeLocalPaymentMethod($_payment);
|
||||||
}
|
}
|
||||||
|
|
||||||
$data = [
|
$data = [
|
||||||
'payment_method' => $_payment->source['id'],
|
'payment_method' => $_payment['source']['id'],
|
||||||
'payment_type' => 12,
|
'payment_type' => 12,
|
||||||
'amount' => $this->getParent()->payment_hash->data->raw_value,
|
'amount' => $this->getParent()->payment_hash->data->raw_value,
|
||||||
'transaction_reference' => $_payment->id,
|
'transaction_reference' => $_payment['id'],
|
||||||
'gateway_type_id' => GatewayType::CREDIT_CARD,
|
'gateway_type_id' => GatewayType::CREDIT_CARD,
|
||||||
];
|
];
|
||||||
|
|
||||||
@ -82,15 +83,15 @@ trait Utilities
|
|||||||
return redirect()->route('client.payments.show', ['payment' => $this->getParent()->encodePrimaryKey($payment->id)]);
|
return redirect()->route('client.payments.show', ['payment' => $this->getParent()->encodePrimaryKey($payment->id)]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function processUnsuccessfulPayment(Payment $_payment, $throw_exception = true)
|
public function processUnsuccessfulPayment($_payment, $throw_exception = true)
|
||||||
{
|
{
|
||||||
|
|
||||||
$error_message = '';
|
$error_message = '';
|
||||||
|
|
||||||
if(property_exists($_payment, 'server_response'))
|
if(array_key_exists('response_summary',$_payment))
|
||||||
$error_message = $_payment->response_summary;
|
$error_message = $_payment['response_summary'];
|
||||||
elseif(property_exists($_payment, 'status'))
|
elseif(array_key_exists('status',$_payment))
|
||||||
$error_message = $_payment->status;
|
$error_message = $_payment['status'];
|
||||||
|
|
||||||
$this->getParent()->sendFailureMail($error_message);
|
$this->getParent()->sendFailureMail($error_message);
|
||||||
|
|
||||||
@ -110,20 +111,20 @@ trait Utilities
|
|||||||
|
|
||||||
if ($throw_exception) {
|
if ($throw_exception) {
|
||||||
|
|
||||||
throw new PaymentFailed($_payment->status . " " . $error_message, $_payment->http_code);
|
throw new PaymentFailed($_payment['status'] . " " . $error_message, 500);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function processPendingPayment(Payment $_payment)
|
private function processPendingPayment($_payment)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
return redirect($_payment->_links['redirect']['href']);
|
return redirect($_payment['links']['redirect']['href']);
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
return $this->processInternallyFailedPayment($this->getParent(), $e);
|
return $this->processInternallyFailedPayment($this->getParent(), $e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function storeLocalPaymentMethod(Payment $response)
|
private function storeLocalPaymentMethod($response)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$payment_meta = new stdClass;
|
$payment_meta = new stdClass;
|
||||||
@ -139,7 +140,7 @@ trait Utilities
|
|||||||
'payment_method_id' => $this->getParent()->payment_hash->data->payment_method_id,
|
'payment_method_id' => $this->getParent()->payment_hash->data->payment_method_id,
|
||||||
];
|
];
|
||||||
|
|
||||||
return $this->getParent()->storePaymentMethod($data);
|
return $this->getParent()->storePaymentMethod($data, ['gateway_customer_reference' => $response['customer']['id']]);
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
session()->flash('message', ctrans('texts.payment_method_saving_failed'));
|
session()->flash('message', ctrans('texts.payment_method_saving_failed'));
|
||||||
}
|
}
|
||||||
|
@ -56,6 +56,8 @@ class CheckoutComPaymentDriver extends BaseDriver
|
|||||||
/* Authorise payment methods */
|
/* Authorise payment methods */
|
||||||
public $can_authorise_credit_card = true;
|
public $can_authorise_credit_card = true;
|
||||||
|
|
||||||
|
public $is_four_api = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var CheckoutApi;
|
* @var CheckoutApi;
|
||||||
*/
|
*/
|
||||||
@ -114,7 +116,8 @@ class CheckoutComPaymentDriver extends BaseDriver
|
|||||||
];
|
];
|
||||||
|
|
||||||
|
|
||||||
if(strlen($config['secret']) === 35){
|
if(strlen($config['secret']) <= 38){
|
||||||
|
$this->is_four_api = true;
|
||||||
$builder = CheckoutFourSdk::staticKeys();
|
$builder = CheckoutFourSdk::staticKeys();
|
||||||
$builder->setPublicKey($config['public']); // optional, only required for operations related with tokens
|
$builder->setPublicKey($config['public']); // optional, only required for operations related with tokens
|
||||||
$builder->setSecretKey($config['secret']);
|
$builder->setSecretKey($config['secret']);
|
||||||
|
Loading…
Reference in New Issue
Block a user