Working on token billing with gateway fees

This commit is contained in:
David Bomba 2020-09-04 08:01:17 +10:00
parent 9dce4c3de1
commit 5eacb1abf8
7 changed files with 55 additions and 33 deletions

View File

@ -101,17 +101,22 @@ class AuthorizeCreditCard
} }
private function tokenBilling($cgt, $amount, $invoice) private function tokenBilling($cgt, $payment_hash)
{ {
$data = (new ChargePaymentProfile($this->authorize))->chargeCustomerProfile($cgt->gateway_customer_reference, $cgt->token, $amounts); $amount = array_sum(array_column($payment_hash->invoices(), 'amount')) + $payment_hash->fee_total;
$data = (new ChargePaymentProfile($this->authorize))->chargeCustomerProfile($cgt->gateway_customer_reference, $cgt->token, $amount);
if($data['response'] != null && $data['response']->getMessages()->getResultCode() == "Ok") { if($data['response'] != null && $data['response']->getMessages()->getResultCode() == "Ok") {
$payment = $this->createPaymentRecord($data, $amount); $payment = $this->createPaymentRecord($data, $amount);
$payment->meta = $cgt->meta;
$payment->save();
$this->authorize->attachInvoices($payment, $payment_hash);
$payment->service()->updateInvoicePayment($payment_hash);
$this->authorize->attachInvoices($payment, $invoice->hashed_id);
event(new PaymentWasCreated($payment, $payment->company, Ninja::eventVars())); event(new PaymentWasCreated($payment, $payment->company, Ninja::eventVars()));
$vars = [ $vars = [

View File

@ -16,6 +16,7 @@ use App\Models\ClientGatewayToken;
use App\Models\GatewayType; use App\Models\GatewayType;
use App\Models\Invoice; use App\Models\Invoice;
use App\Models\Payment; use App\Models\Payment;
use App\Models\PaymentHash;
use App\PaymentDrivers\Authorize\AuthorizeCreditCard; use App\PaymentDrivers\Authorize\AuthorizeCreditCard;
use App\PaymentDrivers\Authorize\AuthorizePaymentMethod; use App\PaymentDrivers\Authorize\AuthorizePaymentMethod;
use App\PaymentDrivers\Authorize\ChargePaymentProfile; use App\PaymentDrivers\Authorize\ChargePaymentProfile;
@ -143,11 +144,11 @@ class AuthorizePaymentDriver extends BaseDriver
->first(); ->first();
} }
public function tokenBilling(ClientGatewayToken $cgt, float $amount, ?Invoice $invoice = null) public function tokenBilling(ClientGatewayToken $cgt, PaymentHash $payment_hash)
{ {
$this->setPaymentMethod($cgt->gateway_type_id); $this->setPaymentMethod($cgt->gateway_type_id);
return $this->payment_method->tokenBilling($cgt, $amount, $invoice); return $this->payment_method->tokenBilling($cgt, $payment_hash);
} }
} }

View File

@ -148,12 +148,11 @@ class BaseDriver extends AbstractPaymentDriver
/** /**
* Process an unattended payment * Process an unattended payment
* *
* @param ClientGatewayToken $cgt The client gateway token object * @param ClientGatewayToken $cgt The client gateway token object
* @param float $amount The amount to bill * @param PaymentHash $payment_hash The Payment hash containing the payment meta data
* @param Invoice $invoice Optional Invoice object being paid * @return Response The payment response
* @return Response The payment response
*/ */
public function tokenBilling(ClientGatewayToken $cgt, float $amount, ?Invoice $invoice = null) {} 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

View File

@ -318,6 +318,6 @@ class CheckoutComPaymentDriver extends BasePaymentDriver
} }
} }
public function tokenBilling(ClientGatewayToken $cgt, float $amount) {} public function tokenBilling(ClientGatewayToken $cgt, PaymentHash $payment_hash) {}
} }

View File

@ -16,6 +16,7 @@ use App\Events\Payment\PaymentWasCreated;
use App\Jobs\Util\SystemLogger; use App\Jobs\Util\SystemLogger;
use App\Models\ClientGatewayToken; use App\Models\ClientGatewayToken;
use App\Models\Invoice; use App\Models\Invoice;
use App\Models\PaymentHash;
use App\Models\PaymentType; use App\Models\PaymentType;
use App\Models\SystemLog; use App\Models\SystemLog;
use App\PaymentDrivers\StripePaymentDriver; use App\PaymentDrivers\StripePaymentDriver;
@ -35,9 +36,12 @@ class Charge
* Create a charge against a payment method * Create a charge against a payment method
* @return bool success/failure * @return bool success/failure
*/ */
public function tokenBilling(ClientGatewayToken $cgt, $amount, ?Invoice $invoice) public function tokenBilling(ClientGatewayToken $cgt, PaymentHash $payment_hash)
{ {
$amount = array_sum(array_column($payment_hash->invoices(), 'amount')) + $payment_hash->fee_total;
$invoice = sInvoice::whereIn('id', $this->transformKeys(array_column($payment_hash->invoices(), 'invoice_id')))->first();
if($invoice) if($invoice)
$description = "Invoice {$invoice->number} for {$amount} for client {$this->stripe->client->present()->name()}"; $description = "Invoice {$invoice->number} for {$amount} for client {$this->stripe->client->present()->name()}";
else else
@ -169,11 +173,12 @@ class Charge
]; ];
$payment = $this->stripe->createPaymentRecord($data, $amount); $payment = $this->stripe->createPaymentRecord($data, $amount);
$payment->meta = $cgt->meta;
$payment->save();
if($invoice) $this->stripe->attachInvoices($payment, $payment_hash);
$this->stripe->attachInvoices($payment, $invoice->hashed_id);
$payment->service()->updateInvoicePayment(); $payment->service()->updateInvoicePayment($payment_hash);
event(new PaymentWasCreated($payment, $payment->company, Ninja::eventVars())); event(new PaymentWasCreated($payment, $payment->company, Ninja::eventVars()));

View File

@ -23,6 +23,7 @@ use App\Models\CompanyGateway;
use App\Models\GatewayType; use App\Models\GatewayType;
use App\Models\Invoice; use App\Models\Invoice;
use App\Models\Payment; use App\Models\Payment;
use App\Models\PaymentHash;
use App\Models\PaymentType; use App\Models\PaymentType;
use App\Models\SystemLog; use App\Models\SystemLog;
use App\PaymentDrivers\Stripe\ACH; use App\PaymentDrivers\Stripe\ACH;
@ -367,9 +368,9 @@ class StripePaymentDriver extends BasePaymentDriver
return response([], 200); return response([], 200);
} }
public function tokenBilling(ClientGatewayToken $cgt, float $amount, ?Invoice $invoice = null) public function tokenBilling(ClientGatewayToken $cgt, PaymentHash $payment_hash)
{ {
return (new Charge($this))->tokenBilling($cgt, $amount, $invoice); return (new Charge($this))->tokenBilling($cgt, $payment_hash);
} }
/** /**

View File

@ -17,10 +17,12 @@ use App\Factory\PaymentFactory;
use App\Models\Client; use App\Models\Client;
use App\Models\Invoice; use App\Models\Invoice;
use App\Models\Payment; use App\Models\Payment;
use App\Models\PaymentHash;
use App\Services\AbstractService; use App\Services\AbstractService;
use App\Services\Client\ClientService; use App\Services\Client\ClientService;
use App\Services\Payment\PaymentService; use App\Services\Payment\PaymentService;
use App\Utils\Traits\GeneratesCounter; use App\Utils\Traits\GeneratesCounter;
use Illuminate\Support\Str;
class AutoBillInvoice extends AbstractService class AutoBillInvoice extends AbstractService
{ {
@ -55,30 +57,39 @@ class AutoBillInvoice extends AbstractService
if($this->invoice->partial > 0){ if($this->invoice->partial > 0){
$fee = $gateway_token->gateway->calcGatewayFee($this->invoice->partial); $fee = $gateway_token->gateway->calcGatewayFee($this->invoice->partial);
$amount = $this->invoice->partial + $fee; // $amount = $this->invoice->partial + $fee;
$amount = $this->invoice->partial;
} }
else{ else{
$fee = $gateway_token->gateway->calcGatewayFee($this->invoice->balance); $fee = $gateway_token->gateway->calcGatewayFee($this->invoice->balance);
$amount = $this->invoice->balance + $fee; // $amount = $this->invoice->balance + $fee;
$amount = $this->invoice->balance;
} }
$payment = $gateway_token->gateway->driver($this->client)->tokenBilling($gateway_token, $amount, $this->invoice); $payment_hash = PaymentHash::create([
'hash' => Str::random(128),
'data' => ['invoice_id' => $this->invoice->hashed_id, 'amount' => $amount],
'fee_total' => $fee,
'fee_invoice_id' => $this->invoice->id,
]);
if($payment){ $payment = $gateway_token->gateway->driver($this->client)->tokenBilling($gateway_token, $payment_hash);
if($this->invoice->partial > 0) //this is redundant - taken care of much further down.
$amount = $this->invoice->partial; // if($payment){
else
$amount = $this->invoice->balance;
$this->invoice = $this->invoice->service()->addGatewayFee($gateway_token->gateway, $amount)->save(); // if($this->invoice->partial > 0)
// $amount = $this->invoice->partial;
// else
// $amount = $this->invoice->balance;
} // $this->invoice = $this->invoice->service()->addGatewayFee($gateway_token->gateway, $amount)->save();
else
{
//TODO autobill failed
}
// }
// else
// {
// //TODO autobill failed
// }
return $this->invoice; return $this->invoice;
} }