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

Merge pull request #4365 from turbo124/authorize_refactor

Authorize refactor
This commit is contained in:
David Bomba 2020-11-26 22:43:31 +11:00 committed by GitHub
commit e57eee713a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 236 additions and 153 deletions

View File

@ -155,19 +155,19 @@ class InvoiceSum
{ {
$this->total += $this->total_taxes; $this->total += $this->total_taxes;
if (is_numeric($this->invoice->custom_value1) && $this->invoice->custom_value1 > 0) { if (is_numeric($this->invoice->custom_value1)) {
$this->total += $this->invoice->custom_value1; $this->total += $this->invoice->custom_value1;
} }
if (is_numeric($this->invoice->custom_value2) && $this->invoice->custom_value2 > 0) { if (is_numeric($this->invoice->custom_value2)) {
$this->total += $this->invoice->custom_value2; $this->total += $this->invoice->custom_value2;
} }
if (is_numeric($this->invoice->custom_value3) && $this->invoice->custom_value3 > 0) { if (is_numeric($this->invoice->custom_value3)) {
$this->total += $this->invoice->custom_value3; $this->total += $this->invoice->custom_value3;
} }
if (is_numeric($this->invoice->custom_value4) && $this->invoice->custom_value4 > 0) { if (is_numeric($this->invoice->custom_value4)) {
$this->total += $this->invoice->custom_value4; $this->total += $this->invoice->custom_value4;
} }

View File

@ -166,19 +166,19 @@ class InvoiceSumInclusive
{ {
//$this->total += $this->total_taxes; //$this->total += $this->total_taxes;
if (is_numeric($this->invoice->custom_value1) && $this->invoice->custom_value1 > 0) { if (is_numeric($this->invoice->custom_value1)) {
$this->total += $this->invoice->custom_value1; $this->total += $this->invoice->custom_value1;
} }
if (is_numeric($this->invoice->custom_value2) && $this->invoice->custom_value2 > 0) { if (is_numeric($this->invoice->custom_value2)) {
$this->total += $this->invoice->custom_value2; $this->total += $this->invoice->custom_value2;
} }
if (is_numeric($this->invoice->custom_value3) && $this->invoice->custom_value3 > 0) { if (is_numeric($this->invoice->custom_value3)) {
$this->total += $this->invoice->custom_value3; $this->total += $this->invoice->custom_value3;
} }
if (is_numeric($this->invoice->custom_value4) && $this->invoice->custom_value4 > 0) { if (is_numeric($this->invoice->custom_value4)) {
$this->total += $this->invoice->custom_value4; $this->total += $this->invoice->custom_value4;
} }

View File

@ -11,15 +11,24 @@
namespace App\PaymentDrivers; namespace App\PaymentDrivers;
use App\Models\ClientGatewayToken;
use App\Models\Payment; use App\Models\Payment;
use App\Models\PaymentHash;
use Illuminate\Http\Request;
abstract class AbstractPaymentDriver abstract class AbstractPaymentDriver
{ {
abstract public function authorize($payment_method); abstract public function authorizeView(array $data);
abstract public function purchase($amount, $return_client_response = false); abstract public function authorizeResponse(Request $request);
abstract public function processPaymentView(array $data);
abstract public function processPaymentResponse(Request $request);
abstract public function refund(Payment $payment, $refund_amount, $return_client_response = false); abstract public function refund(Payment $payment, $refund_amount, $return_client_response = false);
abstract public function tokenBilling(ClientGatewayToken $cgt, PaymentHash $payment_hash);
abstract public function setPaymentMethod($payment_method_id); abstract public function setPaymentMethod($payment_method_id);
} }

View File

@ -95,18 +95,31 @@ class AuthorizeCreditCard
$data = (new ChargePaymentProfile($this->authorize))->chargeCustomerProfile($cgt->gateway_customer_reference, $cgt->token, $amount); $data = (new ChargePaymentProfile($this->authorize))->chargeCustomerProfile($cgt->gateway_customer_reference, $cgt->token, $amount);
/*Refactor and push to BaseDriver*/
if ($data['response'] != null && $data['response']->getMessages()->getResultCode() == 'Ok') { if ($data['response'] != null && $data['response']->getMessages()->getResultCode() == 'Ok') {
$payment = $this->createPaymentRecord($data, $amount);
$payment->meta = $cgt->meta;
$payment->save();
$payment_hash->payment_id = $payment->id; // $response = $data['response'];
$payment_hash->save();
$this->authorize->attachInvoices($payment, $payment_hash); // $payment_record = [];
$payment->service()->updateInvoicePayment($payment_hash); // $payment_record['amount'] = $amount;
// $payment_record['payment_type'] = PaymentType::CREDIT_CARD_OTHER;;
// $payment_record['transaction_reference'] = $response->getTransactionResponse()->getTransId();
event(new PaymentWasCreated($payment, $payment->company, Ninja::eventVars())); // $this->authorize->createPayment($payment_record);
$this->storePayment($payment_hash, $data);
// $payment = $this->createPaymentRecord($data, $amount);
// $payment->meta = $cgt->meta;
// $payment->save();
// $payment_hash->payment_id = $payment->id;
// $payment_hash->save();
// $this->authorize->attachInvoices($payment, $payment_hash);
// $payment->service()->updateInvoicePayment($payment_hash);
// event(new PaymentWasCreated($payment, $payment->company, Ninja::eventVars()));
$vars = [ $vars = [
'hashed_ids' => $invoice->hashed_id, 'hashed_ids' => $invoice->hashed_id,
@ -126,6 +139,12 @@ class AuthorizeCreditCard
} }
} }
private function handleResponse($data, $request) private function handleResponse($data, $request)
{ {
$response = $data['response']; $response = $data['response'];
@ -143,16 +162,14 @@ class AuthorizeCreditCard
{ {
$amount = array_sum(array_column($payment_hash->invoices(), 'amount')) + $payment_hash->fee_total; $amount = array_sum(array_column($payment_hash->invoices(), 'amount')) + $payment_hash->fee_total;
$payment = $this->createPaymentRecord($data, $amount); $response = $data['response'];
$payment_hash->payment_id = $payment->id; $payment_record = [];
$payment_hash->save(); $payment_record['amount'] = $amount;
$payment_record['payment_type'] = PaymentType::CREDIT_CARD_OTHER;;
$payment_record['transaction_reference'] = $response->getTransactionResponse()->getTransId();
$this->authorize->attachInvoices($payment, $payment_hash); $payment = $this->authorize->createPayment($payment_record);
$payment->service()->updateInvoicePayment($payment_hash);
event(new PaymentWasCreated($payment, $payment->company, Ninja::eventVars()));
return $payment; return $payment;
} }

View File

@ -16,6 +16,8 @@ use App\Exceptions\GenericPaymentDriverFailure;
use App\Models\ClientGatewayToken; use App\Models\ClientGatewayToken;
use App\Models\GatewayType; use App\Models\GatewayType;
use App\PaymentDrivers\AuthorizePaymentDriver; use App\PaymentDrivers\AuthorizePaymentDriver;
use App\PaymentDrivers\Authorize\AuthorizeCreateCustomer;
use App\PaymentDrivers\Authorize\AuthorizeCreditCard;
use net\authorize\api\contract\v1\CreateCustomerPaymentProfileRequest; use net\authorize\api\contract\v1\CreateCustomerPaymentProfileRequest;
use net\authorize\api\contract\v1\CustomerAddressType; use net\authorize\api\contract\v1\CustomerAddressType;
use net\authorize\api\contract\v1\CustomerPaymentProfileType; use net\authorize\api\contract\v1\CustomerPaymentProfileType;
@ -35,34 +37,37 @@ class AuthorizePaymentMethod
public $payment_method; public $payment_method;
private $payment_method_id;
public function __construct(AuthorizePaymentDriver $authorize) public function __construct(AuthorizePaymentDriver $authorize)
{ {
$this->authorize = $authorize; $this->authorize = $authorize;
} }
public function authorizeView($payment_method) public function authorizeView()
{ {
$this->payment_method = $payment_method; if($this->authorize->payment_method instanceof AuthorizeCreditCard){
$this->payment_method_id = GatewayType::CREDIT_CARD;
switch ($payment_method) {
case GatewayType::CREDIT_CARD:
return $this->authorizeCreditCard(); return $this->authorizeCreditCard();
break;
case GatewayType::BANK_TRANSFER:
return $this->authorizeBankTransfer();
break;
default:
// code...
break;
}
} }
public function authorizeResponseView($data)
// case GatewayType::BANK_TRANSFER:
// return $this->authorizeBankTransfer();
// break;
}
public function authorizeResponseView($request)
{ {
$this->payment_method = $data['payment_method_id']; $data = $request->all();
switch ($this->payment_method) { $this->payment_method_id = $data['method'];
switch ($this->payment_method_id) {
case GatewayType::CREDIT_CARD: case GatewayType::CREDIT_CARD:
return $this->authorizeCreditCardResponse($data); return $this->authorizeCreditCardResponse($data);
break; break;
@ -111,19 +116,17 @@ class AuthorizePaymentMethod
public function createClientGatewayToken($payment_profile, $gateway_customer_reference) public function createClientGatewayToken($payment_profile, $gateway_customer_reference)
{ {
// info(print_r($payment_profile,1));
$client_gateway_token = new ClientGatewayToken(); $data = [];
$client_gateway_token->company_id = $this->authorize->client->company_id; $additonal = [];
$client_gateway_token->client_id = $this->authorize->client->id;
$client_gateway_token->token = $payment_profile->getPaymentProfile()->getCustomerPaymentProfileId();
$client_gateway_token->company_gateway_id = $this->authorize->company_gateway->id;
$client_gateway_token->gateway_type_id = $this->payment_method;
$client_gateway_token->gateway_customer_reference = $gateway_customer_reference;
$client_gateway_token->meta = $this->buildPaymentMethod($payment_profile);
$client_gateway_token->save();
return $client_gateway_token; $data['token'] = $payment_profile->getPaymentProfile()->getCustomerPaymentProfileId();
$data['payment_method_id'] = $this->payment_method_id;
$data['payment_meta'] = $this->buildPaymentMethod($payment_profile);
$additional['gateway_customer_reference'] = $gateway_customer_reference;
$this->authorize->storeGatewayToken($data, $additional);
} }
public function buildPaymentMethod($payment_profile) public function buildPaymentMethod($payment_profile)

View File

@ -63,6 +63,38 @@ class AuthorizePaymentDriver extends BaseDriver
return $types; return $types;
} }
public function authorizeView($payment_method)
{
return (new AuthorizePaymentMethod($this))->authorizeView();
}
public function authorizeResponse($request)
{
return (new AuthorizePaymentMethod($this))->authorizeResponseView($request);
}
public function processPaymentView($data)
{
return $this->payment_method->processPaymentView($data);
}
public function processPaymentResponse($request)
{
return $this->payment_method->processPaymentResponse($request);
}
public function refund(Payment $payment, $refund_amount, $return_client_response = false)
{
return (new RefundTransaction($this))->refundTransaction($payment, $refund_amount);
}
public function tokenBilling(ClientGatewayToken $cgt, PaymentHash $payment_hash)
{
$this->setPaymentMethod($cgt->gateway_type_id);
return $this->payment_method->tokenBilling($cgt, $payment_hash);
}
public function init() public function init()
{ {
error_reporting(E_ALL & ~E_DEPRECATED); error_reporting(E_ALL & ~E_DEPRECATED);
@ -94,59 +126,6 @@ class AuthorizePaymentDriver extends BaseDriver
return $env = ANetEnvironment::PRODUCTION; return $env = ANetEnvironment::PRODUCTION;
} }
public function authorizeView($payment_method)
{
if (count($this->required_fields) > 0) {
return redirect()
->route('client.profile.edit', ['client_contact' => auth()->user()->hashed_id])
->with('missing_required_fields', $this->required_fields);
}
return (new AuthorizePaymentMethod($this))->authorizeView($payment_method);
}
public function authorizeResponseView(array $data)
{
return (new AuthorizePaymentMethod($this))->authorizeResponseView($data);
}
public function authorize($payment_method)
{
return $this->authorizeView($payment_method);
}
public function processPaymentView($data)
{
if (count($this->required_fields) > 0) {
return redirect()
->route('client.profile.edit', ['client_contact' => auth()->user()->hashed_id])
->with('missing_required_fields', $this->required_fields);
}
return $this->payment_method->processPaymentView($data);
}
public function processPaymentResponse($request)
{
if (count($this->required_fields) > 0) {
return redirect()
->route('client.profile.edit', ['client_contact' => auth()->user()->hashed_id])
->with('missing_required_fields', $this->required_fields);
}
return $this->payment_method->processPaymentResponse($request);
}
public function purchase($amount, $return_client_response = false)
{
return false;
}
public function refund(Payment $payment, $refund_amount, $return_client_response = false)
{
return (new RefundTransaction($this))->refundTransaction($payment, $refund_amount);
}
public function findClientGatewayRecord() :?ClientGatewayToken public function findClientGatewayRecord() :?ClientGatewayToken
{ {
return ClientGatewayToken::where('client_id', $this->client->id) return ClientGatewayToken::where('client_id', $this->client->id)
@ -154,12 +133,6 @@ class AuthorizePaymentDriver extends BaseDriver
->first(); ->first();
} }
public function tokenBilling(ClientGatewayToken $cgt, PaymentHash $payment_hash)
{
$this->setPaymentMethod($cgt->gateway_type_id);
return $this->payment_method->tokenBilling($cgt, $payment_hash);
}
/** /**
* Detach payment method from Authorize.net. * Detach payment method from Authorize.net.

View File

@ -32,7 +32,7 @@ use App\Utils\Traits\SystemLogTrait;
use Checkout\Library\Exceptions\CheckoutHttpException; use Checkout\Library\Exceptions\CheckoutHttpException;
use Exception; use Exception;
use Illuminate\Support\Carbon; use Illuminate\Support\Carbon;
use Illuminate\Support\Str; use Illuminate\Http\Request;
/** /**
* Class BaseDriver. * Class BaseDriver.
@ -60,14 +60,13 @@ class BaseDriver extends AbstractPaymentDriver
/* The client */ /* The client */
public $client; public $client;
/* The initiated gateway driver class*/ /* The initialized gateway driver class*/
public $payment_method; public $payment_method;
/** /* PaymentHash */
* @var PaymentHash
*/
public $payment_hash; public $payment_hash;
/* Array of payment methods */
public static $methods = []; public static $methods = [];
/** @var array */ /** @var array */
@ -76,9 +75,7 @@ class BaseDriver extends AbstractPaymentDriver
public function __construct(CompanyGateway $company_gateway, Client $client = null, $invitation = false) public function __construct(CompanyGateway $company_gateway, Client $client = null, $invitation = false)
{ {
$this->company_gateway = $company_gateway; $this->company_gateway = $company_gateway;
$this->invitation = $invitation; $this->invitation = $invitation;
$this->client = $client; $this->client = $client;
} }
@ -86,23 +83,35 @@ class BaseDriver extends AbstractPaymentDriver
* Authorize a payment method. * Authorize a payment method.
* *
* Returns a reusable token for storage for future payments * Returns a reusable token for storage for future payments
* @param const $payment_method The GatewayType::constant *
* @return void Return a view for collecting payment method information * @param array $data
* @return mixed Return a view for collecting payment method information
*/ */
public function authorize($payment_method) public function authorizeView(array $data) {}
{
}
/** /**
* Executes purchase attempt for a given amount. * The payment authorization response
* *
* @param float $amount The amount to be collected * @param Request $request
* @param bool $return_client_response Whether the method needs to return a response (otherwise we assume an unattended payment) * @return mixed Return a response for collecting payment method information
* @return mixed
*/ */
public function purchase($amount, $return_client_response = false) public function authorizeResponse(Request $request) {}
{
} /**
* Process a payment
*
* @param array $data
* @return mixed Return a view for the payment
*/
public function processPaymentView(array $data) {}
/**
* Process payment response
*
* @param Request $request
* @return mixed Return a response for the payment
*/
public function processPaymentResponse(Request $request) {}
/** /**
* Executes a refund attempt for a given amount with a transaction_reference. * Executes a refund attempt for a given amount with a transaction_reference.
@ -112,23 +121,30 @@ class BaseDriver extends AbstractPaymentDriver
* @param bool $return_client_response Whether the method needs to return a response (otherwise we assume an unattended payment) * @param bool $return_client_response Whether the method needs to return a response (otherwise we assume an unattended payment)
* @return mixed * @return mixed
*/ */
public function refund(Payment $payment, $amount, $return_client_response = false) public function refund(Payment $payment, $amount, $return_client_response = false) {}
{
} /**
* Process an unattended payment.
*
* @param ClientGatewayToken $cgt The client gateway token object
* @param PaymentHash $payment_hash The Payment hash containing the payment meta data
* @return void The payment response
*/
public function tokenBilling(ClientGatewayToken $cgt, PaymentHash $payment_hash){}
/** /**
* Set the inbound request payment method type for access. * Set the inbound request payment method type for access.
* *
* @param int $payment_method_id The Payment Method ID * @param int $payment_method_id The Payment Method ID
*/ */
public function setPaymentMethod($payment_method_id) public function setPaymentMethod($payment_method_id){}
{
}
/************************** Helper methods *************************************/
public function setPaymentHash(PaymentHash $payment_hash) public function setPaymentHash(PaymentHash $payment_hash)
{ {
$this->payment_hash = $payment_hash; $this->payment_hash = $payment_hash;
return $this; return $this;
} }
@ -189,17 +205,6 @@ class BaseDriver extends AbstractPaymentDriver
return $payment->service()->applyNumber()->save(); return $payment->service()->applyNumber()->save();
} }
/**
* Process an unattended payment.
*
* @param ClientGatewayToken $cgt The client gateway token object
* @param PaymentHash $payment_hash The Payment hash containing the payment meta data
* @return void The payment response
*/
public function tokenBilling(ClientGatewayToken $cgt, PaymentHash $payment_hash)
{
}
/** /**
* When a successful payment is made, we need to append the gateway fee * When a successful payment is made, we need to append the gateway fee
* to an invoice. * to an invoice.
@ -215,7 +220,7 @@ class BaseDriver extends AbstractPaymentDriver
/*Payment invoices*/ /*Payment invoices*/
$payment_invoices = $payment_hash->invoices(); $payment_invoices = $payment_hash->invoices();
// /*Fee charged at gateway*/ /*Fee charged at gateway*/
$fee_total = $payment_hash->fee_total; $fee_total = $payment_hash->fee_total;
// Sum of invoice amounts // Sum of invoice amounts
@ -265,7 +270,6 @@ class BaseDriver extends AbstractPaymentDriver
} }
} }
/** /**
* Store payment method as company gateway token. * Store payment method as company gateway token.
* *

View File

@ -0,0 +1,73 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\PaymentDrivers;
use App\Utils\Traits\MakesHash;
class YourGatewayPaymentDriver extends BaseDriver
{
use MakesHash;
public $refundable = true; //does this gateway support refunds?
public $token_billing = true; //does this gateway support token billing?
public $can_authorise_credit_card = true; //does this gateway support authorizations?
public $gateway; //initialized gateway
public $payment_method; //initialized payment method
public static $methods = [
GatewayType::CREDIT_CARD => CreditCard::class, //maps GatewayType => Implementation class
];
const SYSTEM_LOG_TYPE = SystemLog::TYPE_STRIPE;
public function setPaymentMethod($payment_method_id)
{
$class = self::$methods[$payment_method_id];
$this->payment_method = new $class($this);
return $this;
}
public function authorizeView(array $data)
{
return $this->payment_method->authorizeView($data); //this is your custom implementation from here
}
public function authorizeResponse($request)
{
return $this->payment_method->authorizeResponse($request); //this is your custom implementation from here
}
public function processPaymentView(array $data)
{
return $this->payment_method->paymentView($data); //this is your custom implementation from here
}
public function processPaymentResponse($request)
{
return $this->payment_method->paymentResponse($request); //this is your custom implementation from here
}
public function refund(Payment $payment, $amount, $return_client_response = false)
{
return $this->payment_method->yourRefundImplementationHere(); //this is your custom implementation from here
}
public function tokenBilling(ClientGatewayToken $cgt, PaymentHash $payment_hash)
{
return $this->payment_method->yourTokenBillingImplmentation(); //this is your custom implementation from here
}
}

View File

@ -75,6 +75,8 @@ class Phantom
$phantom_url = "https://phantomjscloud.com/api/browser/v2/{$key}/?request=%7Burl:%22{$url}%22,renderType:%22pdf%22%7D"; $phantom_url = "https://phantomjscloud.com/api/browser/v2/{$key}/?request=%7Burl:%22{$url}%22,renderType:%22pdf%22%7D";
$pdf = CurlUtils::get($phantom_url); $pdf = CurlUtils::get($phantom_url);
//info($pdf);
Storage::makeDirectory($path, 0775); Storage::makeDirectory($path, 0775);
$instance = Storage::disk(config('filesystems.default'))->put($file_path, $pdf); $instance = Storage::disk(config('filesystems.default'))->put($file_path, $pdf);

View File

@ -60,7 +60,8 @@ return [
'public' => [ 'public' => [
'driver' => 'local', 'driver' => 'local',
'root' => storage_path('app/public'), 'root' => 'storage/',
// 'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage', 'url' => env('APP_URL').'/storage',
'visibility' => 'public', 'visibility' => 'public',
'permissions' => [ 'permissions' => [

View File

@ -17,7 +17,8 @@
@endpush @endpush
@section('body') @section('body')
<form action="{{ route('client.payment_methods.store') }}" method="post" id="server_response"> <form action="{{ route('client.payment_methods.store', ['method' => App\Models\GatewayType::CREDIT_CARD]) }}" method="post" id="server_response">
@csrf @csrf
<input type="hidden" name="company_gateway_id" value="{{ $gateway->id }}"> <input type="hidden" name="company_gateway_id" value="{{ $gateway->id }}">
<input type="hidden" name="payment_method_id" value="1"> <input type="hidden" name="payment_method_id" value="1">