From c3da9c80b32734a6eb74ce1062a6909706147540 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Thu, 30 Jan 2020 22:19:51 +1100 Subject: [PATCH] Refunds implementation v1 (#3270) * Minor fixes for tests * Refunds implementation v1 --- .../Payment/ValidRefundableRequest.php | 1 - app/Utils/Traits/Payment/Refundable.php | 66 +++++++++---------- composer.json | 5 +- tests/CreatesApplication.php | 3 + 4 files changed, 38 insertions(+), 37 deletions(-) diff --git a/app/Http/ValidationRules/Payment/ValidRefundableRequest.php b/app/Http/ValidationRules/Payment/ValidRefundableRequest.php index 5ef931620b..17d50b59fc 100644 --- a/app/Http/ValidationRules/Payment/ValidRefundableRequest.php +++ b/app/Http/ValidationRules/Payment/ValidRefundableRequest.php @@ -40,7 +40,6 @@ class ValidRefundableRequest implements Rule $payment = Payment::whereId($this->decodePrimaryKey(request()->input('id')))->first(); - if(!$payment) { $this->error_msg = "Unable to retrieve specified payment"; diff --git a/app/Utils/Traits/Payment/Refundable.php b/app/Utils/Traits/Payment/Refundable.php index b5c5b61f24..86c5fa57a8 100644 --- a/app/Utils/Traits/Payment/Refundable.php +++ b/app/Utils/Traits/Payment/Refundable.php @@ -13,19 +13,19 @@ use App\Repositories\ActivityRepository; trait Refundable { + /** + * Entry point for processing of refunds + */ public function processRefund(array $data) { if(isset($data['invoices'])) - return $this->refundPaymentWithInvoicesAndOrCredits($data); + return $this->refundPaymentWithInvoices($data); - if(!isset($data['invoices']) && isset($data['credits'])) - return $this->refundPaymentWithCreditsOnly($data); - - return $this->refundPaymentWithNoInvoicesOrCredits($data); + return $this->refundPaymentWithNoInvoices($data); } - private function refundPaymentWithNoInvoicesOrCredits(array $data) + private function refundPaymentWithNoInvoices(array $data) { //adjust payment refunded column amount $this->refunded = $data['amount']; @@ -66,13 +66,8 @@ trait Refundable return $this; } - private function refundPaymentWithCreditsOnly($data) - { - } - - - private function refundPaymentWithInvoicesAndOrCredits($data) + private function refundPaymentWithInvoices($data) { $total_refund = 0; @@ -82,11 +77,13 @@ trait Refundable $data['amount'] = $total_refund; + /* Set Payment Status*/ if($total_refund == $this->amount) $this->status_id = Payment::STATUS_REFUNDED; else $this->status_id = Payment::STATUS_PARTIALLY_REFUNDED; + /* Build Credit Note*/ $credit_note = $this->buildCreditNote($data); $line_items = []; @@ -121,34 +118,39 @@ trait Refundable } - } - - if(isset($data['credits'])) + if($this->credits()->exists()) { - - /* Update paymentable record */ + //Adjust credits first!!! 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) - { - $refunded_credit->pivot->refunded += $refunded_credit['amount']; - $refunded_credit->pivot->save(); - - $refunded_credit->balance += $refunded_credit['amount']; - $refunded_credit->save(); + $paymentable_credit->balance += $total_refund; + $paymentable_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; @@ -160,6 +162,10 @@ trait Refundable //todo process gateway refund, on success, reduce the credit note balance to 0 } + + if($total_refund > 0) + $this->refunded += $total_refund; + $this->save(); $this->adjustInvoices($data); @@ -171,12 +177,6 @@ trait Refundable return $this; } - private function refundPaymentWithInvoicesAndCredits($data) - { - return $this; - } - - private function createActivity(array $data, int $credit_id) { diff --git a/composer.json b/composer.json index 6d39feb3f0..265f3161e3 100644 --- a/composer.json +++ b/composer.json @@ -41,7 +41,6 @@ "omnipay/stripe": "^3.0", "predis/predis": "^1.1", "sentry/sentry-laravel": "^1.0", - "simshaun/recurr": "^4.0", "spatie/browsershot": "^3.29", "staudenmeir/eloquent-has-many-deep": "^1.11", "stripe/stripe-php": "^7.0", @@ -52,6 +51,7 @@ "yajra/laravel-datatables-oracle": "~9.0" }, "require-dev": { + "ext-json": "*", "barryvdh/laravel-debugbar": "^3.2", "beyondcode/laravel-dump-server": "^1.0", "darkaonline/l5-swagger": "^6.0", @@ -60,8 +60,7 @@ "laravel/dusk": "^5.0", "mockery/mockery": "^1.0", "nunomaduro/collision": "^2.0", - "phpunit/phpunit": "^7.0", - "ext-json": "*" + "phpunit/phpunit": "^7.0" }, "autoload": { "classmap": [ diff --git a/tests/CreatesApplication.php b/tests/CreatesApplication.php index 547152f6a9..4a7b288daa 100644 --- a/tests/CreatesApplication.php +++ b/tests/CreatesApplication.php @@ -3,6 +3,7 @@ namespace Tests; use Illuminate\Contracts\Console\Kernel; +use Illuminate\Support\Facades\Hash; trait CreatesApplication { @@ -17,6 +18,8 @@ trait CreatesApplication $app->make(Kernel::class)->bootstrap(); + Hash::setRounds(4); + return $app; } }