mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-05-24 02:14:21 -04:00
Working on unattended token billing
This commit is contained in:
parent
3ec593f7ef
commit
4b81e209c4
@ -44,7 +44,7 @@ class ClientGatewayToken extends BaseModel
|
||||
|
||||
public function gateway()
|
||||
{
|
||||
return $this->belongsTo(CompanyGateway::class);
|
||||
return $this->hasOne(CompanyGateway::class, 'id', 'company_gateway_id');
|
||||
}
|
||||
|
||||
public function gateway_type()
|
||||
|
@ -136,4 +136,7 @@ class AuthorizePaymentDriver extends BaseDriver
|
||||
->where('company_gateway_id', $this->company_gateway->id)
|
||||
->first();
|
||||
}
|
||||
|
||||
public function tokenBilling(ClientGatewayToken $cgt, float $amount) {}
|
||||
|
||||
}
|
||||
|
@ -13,6 +13,7 @@
|
||||
namespace App\PaymentDrivers;
|
||||
|
||||
use App\Models\Client;
|
||||
use App\Models\ClientGatewayToken;
|
||||
use App\Models\CompanyGateway;
|
||||
use App\Models\Invoice;
|
||||
use App\Models\Payment;
|
||||
@ -48,6 +49,7 @@ class BaseDriver extends AbstractPaymentDriver
|
||||
/* The client */
|
||||
public $client;
|
||||
|
||||
/* The initiated gateway driver class*/
|
||||
public $payment_method;
|
||||
|
||||
public static $methods = [];
|
||||
@ -117,4 +119,13 @@ class BaseDriver extends AbstractPaymentDriver
|
||||
|
||||
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) {}
|
||||
}
|
||||
|
@ -311,4 +311,7 @@ class CheckoutComPaymentDriver extends BasePaymentDriver
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
public function tokenBilling(ClientGatewayToken $cgt, float $amount) {}
|
||||
|
||||
}
|
||||
|
@ -366,5 +366,7 @@ class StripePaymentDriver extends BasePaymentDriver
|
||||
return response([], 200);
|
||||
}
|
||||
|
||||
/************************************** Omnipay API methods **********************************************************/
|
||||
public function tokenBilling(ClientGatewayToken $cgt, float $amount) {}
|
||||
|
||||
|
||||
}
|
||||
|
@ -11,6 +11,7 @@
|
||||
|
||||
namespace App\Services\Invoice;
|
||||
|
||||
use App\DataMapper\InvoiceItem;
|
||||
use App\Events\Payment\PaymentWasCreated;
|
||||
use App\Factory\PaymentFactory;
|
||||
use App\Models\Client;
|
||||
@ -38,20 +39,30 @@ class AutoBillInvoice extends AbstractService
|
||||
public function run()
|
||||
{
|
||||
|
||||
if(!$invoice->isPayable())
|
||||
return $invoice;
|
||||
if(!$this->invoice->isPayable())
|
||||
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)
|
||||
{
|
||||
|
||||
$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);
|
||||
|
||||
@ -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
|
||||
* to process the payment after passing through the
|
||||
|
Loading…
x
Reference in New Issue
Block a user