Skip to content

Commit

Permalink
Merge pull request #27 from xendit/TPI-7489/whmcs
Browse files Browse the repository at this point in the history
customer object standarization
  • Loading branch information
andykim authored Aug 18, 2022
2 parents e4f2df8 + 00d5910 commit 1a1d21a
Show file tree
Hide file tree
Showing 4 changed files with 192 additions and 76 deletions.
73 changes: 73 additions & 0 deletions modules/gateways/xendit/lib/ActionBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
43 changes: 1 addition & 42 deletions modules/gateways/xendit/lib/CreditCard.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
];
Expand Down
35 changes: 1 addition & 34 deletions modules/gateways/xendit/lib/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
117 changes: 117 additions & 0 deletions modules/gateways/xendit/tests/lib/ActionBaseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php

namespace Xendit\Tests\Lib;

use PHPUnit\Framework\TestCase;

class ActionBaseTest extends TestCase
{
/**
* Test if xendit customer address object return empty if WHMCS customer address object empty
*
* @return void
*/
public function testCustomerAddressObjectReturnEmpty()
{
// WHMCS client details
$mockCustomerDetails = [
'country' => '',
'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' => '[email protected]'
];

$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' => '[email protected]'
],
$customerObject
);
}
}

0 comments on commit 1a1d21a

Please sign in to comment.