Refunds implementation v1 (#3270)

* Minor fixes for tests

* Refunds implementation v1
This commit is contained in:
David Bomba 2020-01-30 22:19:51 +11:00 committed by GitHub
parent 63f514f3bc
commit c3da9c80b3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 38 additions and 37 deletions

View File

@ -40,7 +40,6 @@ class ValidRefundableRequest implements Rule
$payment = Payment::whereId($this->decodePrimaryKey(request()->input('id')))->first(); $payment = Payment::whereId($this->decodePrimaryKey(request()->input('id')))->first();
if(!$payment) if(!$payment)
{ {
$this->error_msg = "Unable to retrieve specified payment"; $this->error_msg = "Unable to retrieve specified payment";

View File

@ -13,19 +13,19 @@ use App\Repositories\ActivityRepository;
trait Refundable trait Refundable
{ {
/**
* Entry point for processing of refunds
*/
public function processRefund(array $data) public function processRefund(array $data)
{ {
if(isset($data['invoices'])) if(isset($data['invoices']))
return $this->refundPaymentWithInvoicesAndOrCredits($data); return $this->refundPaymentWithInvoices($data);
if(!isset($data['invoices']) && isset($data['credits'])) return $this->refundPaymentWithNoInvoices($data);
return $this->refundPaymentWithCreditsOnly($data);
return $this->refundPaymentWithNoInvoicesOrCredits($data);
} }
private function refundPaymentWithNoInvoicesOrCredits(array $data) private function refundPaymentWithNoInvoices(array $data)
{ {
//adjust payment refunded column amount //adjust payment refunded column amount
$this->refunded = $data['amount']; $this->refunded = $data['amount'];
@ -66,13 +66,8 @@ trait Refundable
return $this; return $this;
} }
private function refundPaymentWithCreditsOnly($data)
{
} private function refundPaymentWithInvoices($data)
private function refundPaymentWithInvoicesAndOrCredits($data)
{ {
$total_refund = 0; $total_refund = 0;
@ -82,11 +77,13 @@ trait Refundable
$data['amount'] = $total_refund; $data['amount'] = $total_refund;
/* Set Payment Status*/
if($total_refund == $this->amount) if($total_refund == $this->amount)
$this->status_id = Payment::STATUS_REFUNDED; $this->status_id = Payment::STATUS_REFUNDED;
else else
$this->status_id = Payment::STATUS_PARTIALLY_REFUNDED; $this->status_id = Payment::STATUS_PARTIALLY_REFUNDED;
/* Build Credit Note*/
$credit_note = $this->buildCreditNote($data); $credit_note = $this->buildCreditNote($data);
$line_items = []; $line_items = [];
@ -121,34 +118,39 @@ trait Refundable
} }
} }
if($this->credits()->exists())
if(isset($data['credits']))
{ {
//Adjust credits first!!!
/* Update paymentable record */
foreach($this->credits as $paymentable_credit) foreach($this->credits as $paymentable_credit)
{ {
$available_credit = $paymentable_credit->pivot->amount - $paymentable_credit->pivot->refunded;
foreach($data['credits'] as $refunded_credit) if($available_credit > $total_refund){
{ $paymentable_credit->pivot->refunded += $total_refund;
$paymentable_credit->pivot->save();
if($refunded_credit['credit_id'] == $refunded_credit->id) $paymentable_credit->balance += $total_refund;
{ $paymentable_credit->save();
$refunded_credit->pivot->refunded += $refunded_credit['amount'];
$refunded_credit->pivot->save();
$refunded_credit->balance += $refunded_credit['amount'];
$refunded_credit->save();
} $total_refund = 0;
} }
else {
$paymentable_credit->pivot->refunded += $available_credit;
$paymentable_credit->pivot->save();
$paymentable_credit->balance += $available_credit;
$paymentable_credit->save();
$total_refund -= $available_credit;
}
if($total_refund == 0)
break;
} }
} }
$credit_note->line_items = $line_items; $credit_note->line_items = $line_items;
@ -160,6 +162,10 @@ trait Refundable
//todo process gateway refund, on success, reduce the credit note balance to 0 //todo process gateway refund, on success, reduce the credit note balance to 0
} }
if($total_refund > 0)
$this->refunded += $total_refund;
$this->save(); $this->save();
$this->adjustInvoices($data); $this->adjustInvoices($data);
@ -171,12 +177,6 @@ trait Refundable
return $this; return $this;
} }
private function refundPaymentWithInvoicesAndCredits($data)
{
return $this;
}
private function createActivity(array $data, int $credit_id) private function createActivity(array $data, int $credit_id)
{ {

View File

@ -41,7 +41,6 @@
"omnipay/stripe": "^3.0", "omnipay/stripe": "^3.0",
"predis/predis": "^1.1", "predis/predis": "^1.1",
"sentry/sentry-laravel": "^1.0", "sentry/sentry-laravel": "^1.0",
"simshaun/recurr": "^4.0",
"spatie/browsershot": "^3.29", "spatie/browsershot": "^3.29",
"staudenmeir/eloquent-has-many-deep": "^1.11", "staudenmeir/eloquent-has-many-deep": "^1.11",
"stripe/stripe-php": "^7.0", "stripe/stripe-php": "^7.0",
@ -52,6 +51,7 @@
"yajra/laravel-datatables-oracle": "~9.0" "yajra/laravel-datatables-oracle": "~9.0"
}, },
"require-dev": { "require-dev": {
"ext-json": "*",
"barryvdh/laravel-debugbar": "^3.2", "barryvdh/laravel-debugbar": "^3.2",
"beyondcode/laravel-dump-server": "^1.0", "beyondcode/laravel-dump-server": "^1.0",
"darkaonline/l5-swagger": "^6.0", "darkaonline/l5-swagger": "^6.0",
@ -60,8 +60,7 @@
"laravel/dusk": "^5.0", "laravel/dusk": "^5.0",
"mockery/mockery": "^1.0", "mockery/mockery": "^1.0",
"nunomaduro/collision": "^2.0", "nunomaduro/collision": "^2.0",
"phpunit/phpunit": "^7.0", "phpunit/phpunit": "^7.0"
"ext-json": "*"
}, },
"autoload": { "autoload": {
"classmap": [ "classmap": [

View File

@ -3,6 +3,7 @@
namespace Tests; namespace Tests;
use Illuminate\Contracts\Console\Kernel; use Illuminate\Contracts\Console\Kernel;
use Illuminate\Support\Facades\Hash;
trait CreatesApplication trait CreatesApplication
{ {
@ -17,6 +18,8 @@ trait CreatesApplication
$app->make(Kernel::class)->bootstrap(); $app->make(Kernel::class)->bootstrap();
Hash::setRounds(4);
return $app; return $app;
} }
} }