Working on unattended token billing

This commit is contained in:
David Bomba 2020-07-08 12:20:44 +10:00
parent 3ec593f7ef
commit 4b81e209c4
6 changed files with 57 additions and 8 deletions

View File

@ -44,7 +44,7 @@ class ClientGatewayToken extends BaseModel
public function gateway() public function gateway()
{ {
return $this->belongsTo(CompanyGateway::class); return $this->hasOne(CompanyGateway::class, 'id', 'company_gateway_id');
} }
public function gateway_type() public function gateway_type()

View File

@ -136,4 +136,7 @@ class AuthorizePaymentDriver extends BaseDriver
->where('company_gateway_id', $this->company_gateway->id) ->where('company_gateway_id', $this->company_gateway->id)
->first(); ->first();
} }
public function tokenBilling(ClientGatewayToken $cgt, float $amount) {}
} }

View File

@ -13,6 +13,7 @@
namespace App\PaymentDrivers; namespace App\PaymentDrivers;
use App\Models\Client; use App\Models\Client;
use App\Models\ClientGatewayToken;
use App\Models\CompanyGateway; use App\Models\CompanyGateway;
use App\Models\Invoice; use App\Models\Invoice;
use App\Models\Payment; use App\Models\Payment;
@ -48,6 +49,7 @@ class BaseDriver extends AbstractPaymentDriver
/* The client */ /* The client */
public $client; public $client;
/* The initiated gateway driver class*/
public $payment_method; public $payment_method;
public static $methods = []; public static $methods = [];
@ -117,4 +119,13 @@ class BaseDriver extends AbstractPaymentDriver
return $payment; return $payment;
} }
/**
* Process an unattended payment
*
* @param ClientGatewayToken $cgt the client gateway token object
* @param float $amount the amount to bill
* @return Response The payment response
*/
public function tokenBilling(ClientGatewayToken $cgt, float $amount) {}
} }

View File

@ -311,4 +311,7 @@ class CheckoutComPaymentDriver extends BasePaymentDriver
]; ];
} }
} }
public function tokenBilling(ClientGatewayToken $cgt, float $amount) {}
} }

View File

@ -366,5 +366,7 @@ class StripePaymentDriver extends BasePaymentDriver
return response([], 200); return response([], 200);
} }
/************************************** Omnipay API methods **********************************************************/ public function tokenBilling(ClientGatewayToken $cgt, float $amount) {}
} }

View File

@ -11,6 +11,7 @@
namespace App\Services\Invoice; namespace App\Services\Invoice;
use App\DataMapper\InvoiceItem;
use App\Events\Payment\PaymentWasCreated; use App\Events\Payment\PaymentWasCreated;
use App\Factory\PaymentFactory; use App\Factory\PaymentFactory;
use App\Models\Client; use App\Models\Client;
@ -38,20 +39,30 @@ class AutoBillInvoice extends AbstractService
public function run() public function run()
{ {
if(!$invoice->isPayable()) if(!$this->invoice->isPayable())
return $invoice; return $this->invoice;
if($this->invoice->balance > 0)
$gateway_token = $this->getGateway($this->invoice->balance);
else
return $this->invoice->service()->markPaid()->save();
$fee = $gateway_token->gateway->calcGatewayFee($this->invoice->balance);
if($fee > 0)
$this->addFeeToInvoice($fee);
$response = $gateway_token->gateway->driver($this->client)->tokenBilling($gateway_token, $amount);
//if response was successful, toggle the fee type_id to paid
} }
private function getGateway($amount) private function getGateway($amount)
{ {
$gateway_tokens = $this->client->gateway_tokens->orderBy('is_default', 'DESC');
$billing_gateway_token = null; $gateway_tokens = $this->client->gateway_tokens()->orderBy('is_default', 'DESC');
$gateway_tokens->filter(function ($token) use ($amount){ return $gateway_tokens->filter(function ($token) use ($amount){
return $this->validGatewayLimits($token, $amount); return $this->validGatewayLimits($token, $amount);
@ -59,6 +70,25 @@ class AutoBillInvoice extends AbstractService
} }
private function addFeeToInvoice(float $fee)
{
$item = new InvoiceItem;
$item->quantity = 1;
$item->cost = $fee;
$item->notes = ctrans('texts.online_payment_surcharge');
$item->type_id = 3;
$items = (array)$this->invoice->line_items;
$items[] = $item;
$this->invoice->line_items = $items;
$this->invoice->save();
$this->invoice = $this->invoice->calc()->getInvoice()->save();
return $this;
}
/** /**
* Checks whether a given gateway token is able * Checks whether a given gateway token is able
* to process the payment after passing through the * to process the payment after passing through the