From 75efdfeb0a819a161ab568442a0f2aa9168c91af Mon Sep 17 00:00:00 2001 From: David Bomba Date: Tue, 21 Feb 2023 10:44:54 +1100 Subject: [PATCH] Payment Filters --- app/Filters/PaymentFilters.php | 65 ++++++++++++++++++++++++++++++++++ tests/Feature/PaymentTest.php | 10 ++++++ 2 files changed, 75 insertions(+) diff --git a/app/Filters/PaymentFilters.php b/app/Filters/PaymentFilters.php index c4ef5d9370a1..cb095e4aa3cc 100644 --- a/app/Filters/PaymentFilters.php +++ b/app/Filters/PaymentFilters.php @@ -11,6 +11,7 @@ namespace App\Filters; +use App\Models\Payment; use Illuminate\Database\Eloquent\Builder; /** @@ -41,6 +42,70 @@ class PaymentFilters extends QueryFilters }); } + + /** + * Filter based on client status. + * + * Statuses we need to handle + * - all + * - pending + * - cancelled + * - failed + * - completed + * - partially refunded + * - refunded + * + * @param string client_status The payment status as seen by the client + * @return Builder + */ + public function client_status(string $value = ''): Builder + { + if (strlen($value) == 0) { + return $this->builder; + } + + $status_parameters = explode(',', $value); + + if (in_array('all', $status_parameters)) { + return $this->builder; + } + + $this->builder->where(function ($query) use ($status_parameters) { + $payment_filters = []; + + if (in_array('pending', $status_parameters)) { + $payment_filters[] = Payment::STATUS_PENDING; + } + + if (in_array('cancelled', $status_parameters)) { + $payment_filters[] = Payment::STATUS_CANCELLED; + } + + if (in_array('failed', $status_parameters)) { + $payment_filters[] = Payment::STATUS_FAILED; + } + + if (in_array('completed', $status_parameters)) { + $payment_filters[] = Payment::STATUS_COMPLETED; + } + + if (in_array('partially_refunded', $status_parameters)) { + $payment_filters[] = Payment::STATUS_PARTIALLY_REFUNDED; + } + + if (in_array('refunded', $status_parameters)) { + $payment_filters[] = Payment::STATUS_REFUNDED; + } + + if (count($payment_filters) >0) { + $query->whereIn('status_id', $payment_filters); + } + + }); + + return $this->builder; + } + /** * Returns a list of payments that can be matched to bank transactions */ diff --git a/tests/Feature/PaymentTest.php b/tests/Feature/PaymentTest.php index aa963d7d46b3..04b6ef1f3667 100644 --- a/tests/Feature/PaymentTest.php +++ b/tests/Feature/PaymentTest.php @@ -62,6 +62,16 @@ class PaymentTest extends TestCase ); } + public function testPatymentGetClientStatus() + { + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->get('/api/v1/payments?client_status=completed'); + + $response->assertStatus(200); + } + public function testGetPaymentMatchList() { $response = $this->withHeaders([