From 5f03785f5c9fe9b176bd4f47aab9c60f9e38d69b Mon Sep 17 00:00:00 2001 From: andy Date: Mon, 15 Aug 2022 04:46:48 +0000 Subject: [PATCH 1/6] customer object standarization --- modules/gateways/xendit/lib/Link.php | 50 +++++++- .../gateways/xendit/tests/lib/LinkTest.php | 112 ++++++++++++++++++ 2 files changed, 157 insertions(+), 5 deletions(-) create mode 100644 modules/gateways/xendit/tests/lib/LinkTest.php diff --git a/modules/gateways/xendit/lib/Link.php b/modules/gateways/xendit/lib/Link.php index 3b9e6a0..454f3e7 100644 --- a/modules/gateways/xendit/lib/Link.php +++ b/modules/gateways/xendit/lib/Link.php @@ -34,12 +34,52 @@ protected function extractItems(\WHMCS\Billing\Invoice $invoice): array * @param array $params * @return array */ - protected function extractCustomer(array $params) + public function extractCustomer(array $params): array { - return [ - 'given_names' => $params['clientdetails']['firstname'] . ' ' . $params['clientdetails']['lastname'], - 'mobile_number' => $params['clientdetails']['phonenumber'] + $customerObject = []; + + if(!empty($params['firstname']) || !empty($params['lastname'])){ + $customerObject['given_names'] = trim(sprintf("%s %s", $params['firstname'], $params['lastname'])); + } + if(!empty($params['phonenumber'])){ + $customerObject['mobile_number'] = $params['phonenumber']; + } + + $customerAddressObject = $this->extractCustomerAddress($params); + if(!empty($customerAddressObject)){ + $customerObject['addresses'][] = $customerAddressObject; + } + + return $customerObject; + } + + /** + * extract customer address + * + * @param array $params + * @return array + */ + public function extractCustomerAddress(array $params): array + { + $customerAddressObject = []; + if(empty($params)) + return $customerAddressObject; + + // Map Xendit address key with WHMCS address key + $customerAddressObject = [ + 'country' => $params['country'], + 'street_line1' => $params['address1'], + 'street_line2' => $params['address2'], + 'city' => $params['city'], + 'province_state' => $params['state'], + 'postal_code' => $params['postcode'] ]; + foreach ($customerAddressObject as $key => $value){ + if(empty($value)){ + unset($customerAddressObject[$key]); + } + } + return $customerAddressObject; } /** @@ -63,7 +103,7 @@ protected function generateInvoicePayload(array $params, bool $retry = false): a 'success_redirect_url' => $this->invoiceUrl($params['invoiceid'], $params['systemurl']), 'failure_redirect_url' => $this->invoiceUrl($params['invoiceid'], $params['systemurl']), 'should_charge_multiple_use_token' => true, - 'customer' => $this->extractCustomer($params) + 'customer' => $this->extractCustomer($params['clientdetails']) ]; // Only add the payment fee if it's > 0 diff --git a/modules/gateways/xendit/tests/lib/LinkTest.php b/modules/gateways/xendit/tests/lib/LinkTest.php new file mode 100644 index 0000000..edb53bf --- /dev/null +++ b/modules/gateways/xendit/tests/lib/LinkTest.php @@ -0,0 +1,112 @@ + '', + 'address1' => '', + 'address2' => '', + 'city' => '', + 'state' => '', + 'postcode' => '' + ]; + + $link = new \Xendit\Lib\Link(); + $customerAddressObject = $link->extractCustomerAddress($mockCustomerDetails); + $this->assertIsArray($customerAddressObject, 'customerAddressObject should be array'); + $this->assertEquals([], $customerAddressObject); + } + + /** + * Test if xendit customer address object not empty if WHMCS customer address object has values + * + * @return void + */ + public function testCustomerAddressObjectReturnValues() + { + // WHMCS client details + $mockCustomerDetails = [ + 'country' => 'ID', + 'address1' => 'test address1', + 'address2' => '', + 'city' => 'test city', + 'state' => 'test state', + 'postcode' => 'test postcode', + ]; + + $link = new \Xendit\Lib\Link(); + $customerAddressObject = $link->extractCustomerAddress($mockCustomerDetails); + + $this->assertIsArray($customerAddressObject, 'customerAddressObject should be array'); + $this->assertEquals( + [ + 'country' => 'ID', + 'street_line1' => 'test address1', + 'city' => 'test city', + 'province_state' => 'test state', + 'postal_code' => 'test postcode' + ], + $customerAddressObject + ); + } + + /** + * Test if xendit customer object return empty if WHMCS customer object empty + * + * @return void + */ + public function testCustomerObjectReturnEmpty() + { + // WHMCS client details + $mockCustomerDetails = [ + 'firstname' => '', + 'lastname' => '', + 'phonenumber' => '' + ]; + + $link = new \Xendit\Lib\Link(); + $customerObject = $link->extractCustomer($mockCustomerDetails); + + $this->assertIsArray($customerObject, 'customerObject should be array'); + $this->assertEquals([], $customerObject); + } + + /** + * Test if xendit customer object not empty if WHMCS customer address object has values + * + * @return void + */ + public function testCustomerObjectReturnValues() + { + // WHMCS client details + $mockCustomerDetails = [ + 'firstname' => 'test', + 'lastname' => 'test', + 'phonenumber' => '123456789' + ]; + + $link = new \Xendit\Lib\Link(); + $customerObject = $link->extractCustomer($mockCustomerDetails); + + $this->assertIsArray($customerObject, 'customerObject should be array'); + $this->assertEquals( + [ + 'given_names' => 'test test', + 'mobile_number' => '123456789' + ], + $customerObject + ); + } +} From 2cbd832e4a4ea980865b9eff8c5702601e43f093 Mon Sep 17 00:00:00 2001 From: andy Date: Mon, 15 Aug 2022 05:08:57 +0000 Subject: [PATCH 2/6] update customer object for credit card charge --- modules/gateways/xendit/lib/ActionBase.php | 79 +++++++++++++++++++ modules/gateways/xendit/lib/CreditCard.php | 45 +---------- modules/gateways/xendit/lib/Link.php | 73 ----------------- .../lib/{LinkTest.php => ActionBaseTest.php} | 2 +- 4 files changed, 83 insertions(+), 116 deletions(-) rename modules/gateways/xendit/tests/lib/{LinkTest.php => ActionBaseTest.php} (98%) diff --git a/modules/gateways/xendit/lib/ActionBase.php b/modules/gateways/xendit/lib/ActionBase.php index 6fd123a..baac4c4 100644 --- a/modules/gateways/xendit/lib/ActionBase.php +++ b/modules/gateways/xendit/lib/ActionBase.php @@ -381,4 +381,83 @@ public function redirectUrl(string $url) $this->sendHeader("Location", $url); exit(); } + + /** + * @param \WHMCS\Billing\Invoice $invoice + * @return array + */ + public function extractItems(\WHMCS\Billing\Invoice $invoice): array + { + $items = array(); + foreach ($invoice->items()->get() as $item) { + if ($item->amount < 0) { + continue; + } + + $items[] = [ + 'quantity' => 1, + 'name' => $item->description, + 'price' => (float)$item->amount, + ]; + } + return $items; + } + + /** + * @param array $params + * @param bool $isCreditCard + * @return array + */ + public function extractCustomer(array $params, bool $isCreditCard = false): array + { + $customerObject = []; + + if (!empty($params['firstname']) || !empty($params['lastname'])) { + $customerObject['given_names'] = trim(sprintf("%s %s", $params['firstname'], $params['lastname'])); + } + if (!empty($params['phonenumber'])) { + $customerObject['mobile_number'] = $params['phonenumber']; + } + + $customerAddressObject = $this->extractCustomerAddress($params); + if (!empty($customerAddressObject)) { + if ($isCreditCard) { + $customerObject['address'] = $customerAddressObject; + } else { + $customerObject['addresses'][] = $customerAddressObject; + } + } + + return $customerObject; + } + + /** + * extract customer address + * + * @param array $params + * @return array + */ + public function extractCustomerAddress(array $params): array + { + $customerAddressObject = []; + if (empty($params)) { + return $customerAddressObject; + } + + // Map Xendit address key with WHMCS address key + $customerAddressObject = [ + 'country' => $params['country'], + 'street_line1' => $params['address1'], + 'street_line2' => $params['address2'], + 'city' => $params['city'], + 'province_state' => $params['state'], + 'postal_code' => $params['postcode'] + ]; + foreach ($customerAddressObject as $key => $value) { + if (empty($value)) { + unset($customerAddressObject[$key]); + } + } + return $customerAddressObject; + } } diff --git a/modules/gateways/xendit/lib/CreditCard.php b/modules/gateways/xendit/lib/CreditCard.php index 27c1a2c..7e95ecc 100644 --- a/modules/gateways/xendit/lib/CreditCard.php +++ b/modules/gateways/xendit/lib/CreditCard.php @@ -71,47 +71,6 @@ public function extractCardData(array $data) ]; } - /** - * @param array $params - * @return array[] - */ - public function extractCustomerDetail(array $params = []) - { - $customerDetails = [ - 'first_name' => $params['clientdetails']['firstname'], - 'last_name' => $params['clientdetails']['lastname'], - 'email' => $params['clientdetails']['email'], - 'phone_number' => $params['clientdetails']['phonenumber'], - 'address_city' => $params['clientdetails']['city'], - 'address_postal_code' => $params['clientdetails']['postcode'], - 'address_line_1' => $params['clientdetails']['address1'], - 'address_line_2' => $params['clientdetails']['address2'], - 'address_state' => $params['clientdetails']['state'], - 'address_country' => $params['clientdetails']['country'], - ]; - return [ - "billing_details" => $customerDetails, - "shipping_details" => $customerDetails - ]; - } - - /** - * @param $invoice - * @return array - */ - public function extractItems($invoice): array - { - $items = array(); - foreach ($invoice->items()->get() as $item) { - $items[] = [ - 'quantity' => 1, - 'name' => $item->description, - 'price' => (float)$item->amount, - ]; - } - return $items; - } - /** * @param array $params * @param int|null $auth_id @@ -133,7 +92,9 @@ public function generateCCPaymentRequest(array $params = [], int $auth_id = null "external_id" => $this->generateExternalId($params["invoiceid"], true), "store_name" => $params["companyname"], "items" => $this->extractItems($invoice), - "customer" => $this->extractCustomerDetail($params), + "customer" => [ + "billing_details" => $this->extractCustomer($params['clientdetails'], true) + ], "is_recurring" => true, "should_charge_multiple_use_token" => true ]; diff --git a/modules/gateways/xendit/lib/Link.php b/modules/gateways/xendit/lib/Link.php index 454f3e7..f5d8b05 100644 --- a/modules/gateways/xendit/lib/Link.php +++ b/modules/gateways/xendit/lib/Link.php @@ -9,79 +9,6 @@ class Link extends ActionBase /** @var string $callbackUrl */ protected $callbackUrl = 'modules/gateways/callback/xendit.php'; - /** - * @param \WHMCS\Billing\Invoice $invoice - * @return array - */ - protected function extractItems(\WHMCS\Billing\Invoice $invoice): array - { - $items = array(); - foreach ($invoice->items()->get() as $item) { - if ($item->amount < 0) { - continue; - } - - $items[] = [ - 'quantity' => 1, - 'name' => $item->description, - 'price' => (float)$item->amount, - ]; - } - return $items; - } - - /** - * @param array $params - * @return array - */ - public function extractCustomer(array $params): array - { - $customerObject = []; - - if(!empty($params['firstname']) || !empty($params['lastname'])){ - $customerObject['given_names'] = trim(sprintf("%s %s", $params['firstname'], $params['lastname'])); - } - if(!empty($params['phonenumber'])){ - $customerObject['mobile_number'] = $params['phonenumber']; - } - - $customerAddressObject = $this->extractCustomerAddress($params); - if(!empty($customerAddressObject)){ - $customerObject['addresses'][] = $customerAddressObject; - } - - return $customerObject; - } - - /** - * extract customer address - * - * @param array $params - * @return array - */ - public function extractCustomerAddress(array $params): array - { - $customerAddressObject = []; - if(empty($params)) - return $customerAddressObject; - - // Map Xendit address key with WHMCS address key - $customerAddressObject = [ - 'country' => $params['country'], - 'street_line1' => $params['address1'], - 'street_line2' => $params['address2'], - 'city' => $params['city'], - 'province_state' => $params['state'], - 'postal_code' => $params['postcode'] - ]; - foreach ($customerAddressObject as $key => $value){ - if(empty($value)){ - unset($customerAddressObject[$key]); - } - } - return $customerAddressObject; - } - /** * @param array $params * @param bool $retry diff --git a/modules/gateways/xendit/tests/lib/LinkTest.php b/modules/gateways/xendit/tests/lib/ActionBaseTest.php similarity index 98% rename from modules/gateways/xendit/tests/lib/LinkTest.php rename to modules/gateways/xendit/tests/lib/ActionBaseTest.php index edb53bf..b059ce2 100644 --- a/modules/gateways/xendit/tests/lib/LinkTest.php +++ b/modules/gateways/xendit/tests/lib/ActionBaseTest.php @@ -4,7 +4,7 @@ use PHPUnit\Framework\TestCase; -class LinkTest extends TestCase +class ActionBaseTest extends TestCase { /** * Test if xendit customer address object return empty if WHMCS customer address object empty From 1bde35f93788b2934a8b8f2f0cae8a3b6ea91131 Mon Sep 17 00:00:00 2001 From: andy Date: Mon, 15 Aug 2022 05:28:56 +0000 Subject: [PATCH 3/6] add missing email in customer object --- modules/gateways/xendit/lib/ActionBase.php | 14 +++++++++++--- modules/gateways/xendit/lib/CreditCard.php | 4 +--- .../gateways/xendit/tests/lib/ActionBaseTest.php | 9 +++++++-- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/modules/gateways/xendit/lib/ActionBase.php b/modules/gateways/xendit/lib/ActionBase.php index baac4c4..a742cf3 100644 --- a/modules/gateways/xendit/lib/ActionBase.php +++ b/modules/gateways/xendit/lib/ActionBase.php @@ -412,9 +412,18 @@ public function extractCustomer(array $params, bool $isCreditCard = false): arra { $customerObject = []; - if (!empty($params['firstname']) || !empty($params['lastname'])) { - $customerObject['given_names'] = trim(sprintf("%s %s", $params['firstname'], $params['lastname'])); + if (!empty($params['fullname'])) { + $customerObject['given_names'] = trim($params['fullname']); + } else { + if (!empty($params['firstname']) || !empty($params['lastname'])) { + $customerObject['given_names'] = trim(sprintf("%s %s", $params['firstname'], $params['lastname'])); + } + } + + if (!empty($params['email'])) { + $customerObject['email'] = $params['email']; } + if (!empty($params['phonenumber'])) { $customerObject['mobile_number'] = $params['phonenumber']; } @@ -427,7 +436,6 @@ public function extractCustomer(array $params, bool $isCreditCard = false): arra $customerObject['addresses'][] = $customerAddressObject; } } - return $customerObject; } diff --git a/modules/gateways/xendit/lib/CreditCard.php b/modules/gateways/xendit/lib/CreditCard.php index 7e95ecc..a539ff3 100644 --- a/modules/gateways/xendit/lib/CreditCard.php +++ b/modules/gateways/xendit/lib/CreditCard.php @@ -92,9 +92,7 @@ public function generateCCPaymentRequest(array $params = [], int $auth_id = null "external_id" => $this->generateExternalId($params["invoiceid"], true), "store_name" => $params["companyname"], "items" => $this->extractItems($invoice), - "customer" => [ - "billing_details" => $this->extractCustomer($params['clientdetails'], true) - ], + "billing_details" => $this->extractCustomer($params['clientdetails'], true), "is_recurring" => true, "should_charge_multiple_use_token" => true ]; diff --git a/modules/gateways/xendit/tests/lib/ActionBaseTest.php b/modules/gateways/xendit/tests/lib/ActionBaseTest.php index b059ce2..80f833c 100644 --- a/modules/gateways/xendit/tests/lib/ActionBaseTest.php +++ b/modules/gateways/xendit/tests/lib/ActionBaseTest.php @@ -71,6 +71,8 @@ public function testCustomerObjectReturnEmpty() { // WHMCS client details $mockCustomerDetails = [ + 'fullname' => '', + 'email' => '', 'firstname' => '', 'lastname' => '', 'phonenumber' => '' @@ -92,9 +94,11 @@ public function testCustomerObjectReturnValues() { // WHMCS client details $mockCustomerDetails = [ + 'fullname' => 'test test', 'firstname' => 'test', 'lastname' => 'test', - 'phonenumber' => '123456789' + 'phonenumber' => '123456789', + 'email' => 'test@example.com' ]; $link = new \Xendit\Lib\Link(); @@ -104,7 +108,8 @@ public function testCustomerObjectReturnValues() $this->assertEquals( [ 'given_names' => 'test test', - 'mobile_number' => '123456789' + 'mobile_number' => '123456789', + 'email' => 'test@example.com' ], $customerObject ); From 1c619fd9bc21f1341f2d0ac504a465b21a57b2b7 Mon Sep 17 00:00:00 2001 From: andy Date: Tue, 16 Aug 2022 10:25:02 +0000 Subject: [PATCH 4/6] Update code --- modules/gateways/xendit/lib/ActionBase.php | 35 +++++++------------ .../xendit/tests/lib/ActionBaseTest.php | 6 ++-- 2 files changed, 16 insertions(+), 25 deletions(-) diff --git a/modules/gateways/xendit/lib/ActionBase.php b/modules/gateways/xendit/lib/ActionBase.php index a742cf3..e1ddecc 100644 --- a/modules/gateways/xendit/lib/ActionBase.php +++ b/modules/gateways/xendit/lib/ActionBase.php @@ -410,23 +410,16 @@ public function extractItems(\WHMCS\Billing\Invoice $invoice): array */ public function extractCustomer(array $params, bool $isCreditCard = false): array { - $customerObject = []; - - if (!empty($params['fullname'])) { - $customerObject['given_names'] = trim($params['fullname']); - } else { - if (!empty($params['firstname']) || !empty($params['lastname'])) { - $customerObject['given_names'] = trim(sprintf("%s %s", $params['firstname'], $params['lastname'])); - } - } - - if (!empty($params['email'])) { - $customerObject['email'] = $params['email']; - } + $customerObject = [ + "given_names" => $params['firstname'], + "surname" => $params['lastname'], + "email" => $params['email'], + "mobile_number" => $params['phonenumber'], + ]; - if (!empty($params['phonenumber'])) { - $customerObject['mobile_number'] = $params['phonenumber']; - } + $customerObject = array_filter($customerObject, function ($value){ + return !empty($value); + }, ARRAY_FILTER_USE_BOTH); $customerAddressObject = $this->extractCustomerAddress($params); if (!empty($customerAddressObject)) { @@ -461,11 +454,9 @@ public function extractCustomerAddress(array $params): array 'province_state' => $params['state'], 'postal_code' => $params['postcode'] ]; - foreach ($customerAddressObject as $key => $value) { - if (empty($value)) { - unset($customerAddressObject[$key]); - } - } - return $customerAddressObject; + + return array_filter($customerAddressObject, function($value){ + return !empty($value); + }, ARRAY_FILTER_USE_BOTH); } } diff --git a/modules/gateways/xendit/tests/lib/ActionBaseTest.php b/modules/gateways/xendit/tests/lib/ActionBaseTest.php index 80f833c..dd50f93 100644 --- a/modules/gateways/xendit/tests/lib/ActionBaseTest.php +++ b/modules/gateways/xendit/tests/lib/ActionBaseTest.php @@ -94,7 +94,6 @@ public function testCustomerObjectReturnValues() { // WHMCS client details $mockCustomerDetails = [ - 'fullname' => 'test test', 'firstname' => 'test', 'lastname' => 'test', 'phonenumber' => '123456789', @@ -103,11 +102,12 @@ public function testCustomerObjectReturnValues() $link = new \Xendit\Lib\Link(); $customerObject = $link->extractCustomer($mockCustomerDetails); - + $this->assertIsArray($customerObject, 'customerObject should be array'); $this->assertEquals( [ - 'given_names' => 'test test', + 'given_names' => 'test', + 'surname' => 'test', 'mobile_number' => '123456789', 'email' => 'test@example.com' ], From 01816172d176a4c42d1de5a9966d0ebe693e1e09 Mon Sep 17 00:00:00 2001 From: andy Date: Tue, 16 Aug 2022 10:25:49 +0000 Subject: [PATCH 5/6] Fix PHP format --- modules/gateways/xendit/lib/ActionBase.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/gateways/xendit/lib/ActionBase.php b/modules/gateways/xendit/lib/ActionBase.php index e1ddecc..c878a03 100644 --- a/modules/gateways/xendit/lib/ActionBase.php +++ b/modules/gateways/xendit/lib/ActionBase.php @@ -417,7 +417,7 @@ public function extractCustomer(array $params, bool $isCreditCard = false): arra "mobile_number" => $params['phonenumber'], ]; - $customerObject = array_filter($customerObject, function ($value){ + $customerObject = array_filter($customerObject, function ($value) { return !empty($value); }, ARRAY_FILTER_USE_BOTH); @@ -455,7 +455,7 @@ public function extractCustomerAddress(array $params): array 'postal_code' => $params['postcode'] ]; - return array_filter($customerAddressObject, function($value){ + return array_filter($customerAddressObject, function ($value) { return !empty($value); }, ARRAY_FILTER_USE_BOTH); } From 00d59108fb13f969770c34641d115ed0b8fe04c2 Mon Sep 17 00:00:00 2001 From: andy Date: Tue, 16 Aug 2022 10:36:12 +0000 Subject: [PATCH 6/6] update array_filter --- modules/gateways/xendit/lib/ActionBase.php | 9 ++------- modules/gateways/xendit/tests/lib/ActionBaseTest.php | 2 +- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/modules/gateways/xendit/lib/ActionBase.php b/modules/gateways/xendit/lib/ActionBase.php index c878a03..9bc61d1 100644 --- a/modules/gateways/xendit/lib/ActionBase.php +++ b/modules/gateways/xendit/lib/ActionBase.php @@ -416,10 +416,7 @@ public function extractCustomer(array $params, bool $isCreditCard = false): arra "email" => $params['email'], "mobile_number" => $params['phonenumber'], ]; - - $customerObject = array_filter($customerObject, function ($value) { - return !empty($value); - }, ARRAY_FILTER_USE_BOTH); + $customerObject = array_filter($customerObject); $customerAddressObject = $this->extractCustomerAddress($params); if (!empty($customerAddressObject)) { @@ -455,8 +452,6 @@ public function extractCustomerAddress(array $params): array 'postal_code' => $params['postcode'] ]; - return array_filter($customerAddressObject, function ($value) { - return !empty($value); - }, ARRAY_FILTER_USE_BOTH); + return array_filter($customerAddressObject); } } diff --git a/modules/gateways/xendit/tests/lib/ActionBaseTest.php b/modules/gateways/xendit/tests/lib/ActionBaseTest.php index dd50f93..da1d6f6 100644 --- a/modules/gateways/xendit/tests/lib/ActionBaseTest.php +++ b/modules/gateways/xendit/tests/lib/ActionBaseTest.php @@ -102,7 +102,7 @@ public function testCustomerObjectReturnValues() $link = new \Xendit\Lib\Link(); $customerObject = $link->extractCustomer($mockCustomerDetails); - + $this->assertIsArray($customerObject, 'customerObject should be array'); $this->assertEquals( [