diff --git a/modules/gateways/xendit/lib/ActionBase.php b/modules/gateways/xendit/lib/ActionBase.php index 6fd123a..9bc61d1 100644 --- a/modules/gateways/xendit/lib/ActionBase.php +++ b/modules/gateways/xendit/lib/ActionBase.php @@ -381,4 +381,77 @@ 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 = [ + "given_names" => $params['firstname'], + "surname" => $params['lastname'], + "email" => $params['email'], + "mobile_number" => $params['phonenumber'], + ]; + $customerObject = array_filter($customerObject); + + $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'] + ]; + + return array_filter($customerAddressObject); + } } diff --git a/modules/gateways/xendit/lib/CreditCard.php b/modules/gateways/xendit/lib/CreditCard.php index 27c1a2c..a539ff3 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,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" => $this->extractCustomerDetail($params), + "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 3b9e6a0..f5d8b05 100644 --- a/modules/gateways/xendit/lib/Link.php +++ b/modules/gateways/xendit/lib/Link.php @@ -9,39 +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 - */ - protected function extractCustomer(array $params) - { - return [ - 'given_names' => $params['clientdetails']['firstname'] . ' ' . $params['clientdetails']['lastname'], - 'mobile_number' => $params['clientdetails']['phonenumber'] - ]; - } - /** * @param array $params * @param bool $retry @@ -63,7 +30,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/ActionBaseTest.php b/modules/gateways/xendit/tests/lib/ActionBaseTest.php new file mode 100644 index 0000000..da1d6f6 --- /dev/null +++ b/modules/gateways/xendit/tests/lib/ActionBaseTest.php @@ -0,0 +1,117 @@ + '', + '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 = [ + 'fullname' => '', + 'email' => '', + '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', + 'email' => 'test@example.com' + ]; + + $link = new \Xendit\Lib\Link(); + $customerObject = $link->extractCustomer($mockCustomerDetails); + + $this->assertIsArray($customerObject, 'customerObject should be array'); + $this->assertEquals( + [ + 'given_names' => 'test', + 'surname' => 'test', + 'mobile_number' => '123456789', + 'email' => 'test@example.com' + ], + $customerObject + ); + } +}