Fixes for payments with gateway fees

This commit is contained in:
David Bomba 2020-09-01 09:28:37 +10:00
parent e342c02063
commit 2bd8581592
6 changed files with 39 additions and 37 deletions

View File

@ -76,6 +76,7 @@ class Payment extends BaseModel
'created_at' => 'timestamp', 'created_at' => 'timestamp',
'deleted_at' => 'timestamp', 'deleted_at' => 'timestamp',
'is_deleted' => 'bool', 'is_deleted' => 'bool',
'meta' => 'object',
]; ];
protected $with = [ protected $with = [

View File

@ -20,6 +20,7 @@ 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;
use App\Models\PaymentHash;
use App\PaymentDrivers\AbstractPaymentDriver; use App\PaymentDrivers\AbstractPaymentDriver;
use App\Utils\Ninja; use App\Utils\Ninja;
use App\Utils\Traits\MakesHash; use App\Utils\Traits\MakesHash;
@ -110,25 +111,19 @@ class BaseDriver extends AbstractPaymentDriver
* @param array $hashed_ids The array of invoice hashed_ids * @param array $hashed_ids The array of invoice hashed_ids
* @return Payment The payment object * @return Payment The payment object
*/ */
public function attachInvoices(Payment $payment, $hashed_ids): Payment
public function attachInvoices(Payment $payment, PaymentHash $payment_hash): Payment
{ {
$transformed = $this->transformKeys($hashed_ids);
$array = is_array($transformed) ? $transformed : [$transformed];
$invoices = Invoice::whereIn('id', $array)
->whereClientId($this->client->id)
->get();
$paid_invoices = $payment_hash->invoices();
$invoices = Invoice::whereIn('id', $this->transformKeys(array_column($paid_invoices, 'invoice_id')))->get();
$payment->invoices()->sync($invoices); $payment->invoices()->sync($invoices);
$payment->save();
$payment->service()->applyNumber()->save();
$invoices->each(function ($invoice) use($payment){ $invoices->each(function ($invoice) use($payment){
event(new InvoiceWasPaid($invoice, $payment->company, Ninja::eventVars())); event(new InvoiceWasPaid($invoice, $payment->company, Ninja::eventVars()));
}); });
return $payment; return $payment->service()->applyNumber()->save();
} }
/** /**

View File

@ -20,6 +20,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\Utils\Traits\MakesHash; use App\Utils\Traits\MakesHash;
use App\Utils\Traits\SystemLogTrait; use App\Utils\Traits\SystemLogTrait;
use Illuminate\Support\Carbon; use Illuminate\Support\Carbon;
@ -241,14 +242,13 @@ class BasePaymentDriver
{ {
$this->gateway(); $this->gateway();
$response = $this->gateway $response = $this->gateway
->purchase($data) ->purchase($data)
->setItems($items) ->setItems($items)
->send(); ->send();
return $response; return $response;
/*
$this->purchaseResponse = (array)$response->getData();*/
} }
public function completePurchase($data) public function completePurchase($data)
@ -273,15 +273,11 @@ class BasePaymentDriver
} }
public function attachInvoices(Payment $payment, $hashed_ids): Payment public function attachInvoices(Payment $payment, PaymentHash $payment_hash): Payment
{ {
$transformed = $this->transformKeys($hashed_ids);
$array = is_array($transformed) ? $transformed : [$transformed];
$invoices = Invoice::whereIn('id', $array)
->whereClientId($this->client->id)
->get();
$paid_invoices = $payment_hash->invoices();
$invoices = Invoice::whereIn('id', $this->transformKeys(array_column($paid_invoices, 'invoice_id')))->get();
$payment->invoices()->sync($invoices); $payment->invoices()->sync($invoices);
$payment->save(); $payment->save();

View File

@ -169,6 +169,13 @@ class CreditCard
'type' => $payment_method_object['type'], 'type' => $payment_method_object['type'],
]; ];
$payment_meta = new \stdClass;
$payment_meta->exp_month = $payment_method_object['card']['exp_month'];
$payment_meta->exp_year = $payment_method_object['card']['exp_year'];
$payment_meta->brand = $payment_method_object['card']['brand'];
$payment_meta->last4 = $payment_method_object['card']['last4'];
$payment_meta->type = $payment_method_object['type'];
$payment_type = PaymentType::parseCardType($payment_method_object['card']['brand']); $payment_type = PaymentType::parseCardType($payment_method_object['card']['brand']);
if ($state['save_card'] == true) { if ($state['save_card'] == true) {
@ -188,8 +195,9 @@ class CreditCard
]; ];
$payment = $this->stripe->createPayment($data, $status = Payment::STATUS_COMPLETED); $payment = $this->stripe->createPayment($data, $status = Payment::STATUS_COMPLETED);
$payment->meta = $payment_meta;
$this->stripe->attachInvoices($payment, $state['hashed_ids']); $payment = $this->stripe->attachInvoices($payment, $state['payment_hash']);
$payment->service()->updateInvoicePayment($state['payment_hash']); $payment->service()->updateInvoicePayment($state['payment_hash']);

View File

@ -38,9 +38,6 @@ class UpdateInvoicePayment
public function run() public function run()
{ {
// $invoices = $this->payment->invoices()->get();
// $invoices_total = $invoices->sum('balance');
$paid_invoices = $this->payment_hash->invoices(); $paid_invoices = $this->payment_hash->invoices();
$invoices = Invoice::whereIn('id', $this->transformKeys(array_column($paid_invoices, 'invoice_id')))->get(); $invoices = Invoice::whereIn('id', $this->transformKeys(array_column($paid_invoices, 'invoice_id')))->get();
@ -66,14 +63,19 @@ class UpdateInvoicePayment
->updatePaidToDate($paid_amount) ->updatePaidToDate($paid_amount)
->save(); ->save();
/*i think to interact with this correct - we need to do this form $payment->invoice()->pivot*/ $pivot_invoice = $this->payment->invoices->first(function ($inv) use($paid_invoice){
// $invoice->pivot->amount = $paid_amount; return $inv->hashed_id == $paid_invoice->invoice_id;
// $invoice->pivot->save(); });
$invoice->service() //caution what if we amount paid was less than partial - we wipe it! /*update paymentable record*/
->clearPartial() $pivot_invoice->pivot->amount = $paid_amount;
->updateBalance($paid_amount*-1) $pivot_invoice->save();
->save();
$invoice->service() //caution what if we amount paid was less than partial - we wipe it!
->clearPartial()
->updateBalance($paid_amount*-1)
->save();
}); });

View File

@ -34,13 +34,13 @@
@include('setup._issues') @include('setup._issues')
@else @else
@if(!$check['npm_status']) @if(isset($check['npm_status']) && !$check['npm_status'])
<div class="alert alert-success mt-4"> <div class="alert alert-success mt-4">
<p>NPM Version => {{$check['npm_status']}}</p> <p>NPM Version => {{$check['npm_status']}}</p>
</div> </div>
@endif @endif
@if(!$check['node_status']) @if(isset($check['node_status']) && !$check['node_status'])
<div class="alert alert-success mt-4"> <div class="alert alert-success mt-4">
<p>Node Version => {{$check['node_status']}}</p> <p>Node Version => {{$check['node_status']}}</p>
</div> </div>