1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 21:22:58 +01:00

Handle authorization on backend

This commit is contained in:
Benjamin Beganović 2021-10-14 18:43:47 +02:00
parent 5f2de60c9c
commit 1b3a1092a6

View File

@ -14,9 +14,12 @@ namespace App\PaymentDrivers\CheckoutCom;
use App\Exceptions\PaymentFailed; use App\Exceptions\PaymentFailed;
use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest; use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest;
use App\Http\Requests\Request;
use App\Jobs\Mail\PaymentFailureMailer; use App\Jobs\Mail\PaymentFailureMailer;
use App\Models\ClientGatewayToken; use App\Models\ClientGatewayToken;
use App\Models\GatewayType;
use App\PaymentDrivers\CheckoutComPaymentDriver; use App\PaymentDrivers\CheckoutComPaymentDriver;
use App\PaymentDrivers\Common\MethodInterface;
use App\Utils\Traits\MakesHash; use App\Utils\Traits\MakesHash;
use Checkout\Library\Exceptions\CheckoutHttpException; use Checkout\Library\Exceptions\CheckoutHttpException;
use Checkout\Models\Payments\IdSource; use Checkout\Models\Payments\IdSource;
@ -24,8 +27,9 @@ use Checkout\Models\Payments\Payment;
use Checkout\Models\Payments\TokenSource; use Checkout\Models\Payments\TokenSource;
use Illuminate\Contracts\View\Factory; use Illuminate\Contracts\View\Factory;
use Illuminate\View\View; use Illuminate\View\View;
use Omnipay\Common\Message\RedirectResponseInterface;
class CreditCard class CreditCard implements MethodInterface
{ {
use Utilities; use Utilities;
use MakesHash; use MakesHash;
@ -38,6 +42,8 @@ class CreditCard
public function __construct(CheckoutComPaymentDriver $checkout) public function __construct(CheckoutComPaymentDriver $checkout)
{ {
$this->checkout = $checkout; $this->checkout = $checkout;
$this->checkout->init();
} }
/** /**
@ -54,15 +60,50 @@ class CreditCard
} }
/** /**
* Checkout.com supports doesn't support direct authorization of the credit card. * Handle authorization for credit card.
* Token can be saved after the first (successful) purchase.
* *
* @param mixed $data * @param Request $request
* @return void * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
*/ */
public function authorizeResponse($data) public function authorizeResponse(Request $request)
{ {
return; $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;
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) public function paymentView($data)
@ -80,8 +121,6 @@ class CreditCard
public function paymentResponse(PaymentResponseRequest $request) public function paymentResponse(PaymentResponseRequest $request)
{ {
$this->checkout->init();
$state = [ $state = [
'server_response' => json_decode($request->gateway_response), 'server_response' => json_decode($request->gateway_response),
'value' => $request->value, 'value' => $request->value,