Skip to content

Commit

Permalink
Merge pull request #13 from xendit/TPI/6935-invoice-discrepency
Browse files Browse the repository at this point in the history
Fix the WHMCS total does not match with Xendit Total
  • Loading branch information
andykim authored Apr 21, 2022
2 parents 5a227ce + 3106a21 commit 1a0c83b
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 9 deletions.
25 changes: 17 additions & 8 deletions modules/gateways/xendit/lib/ActionBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,15 +180,13 @@ public function updateTransactions($transactions, array $attributes = []): bool
}

/**
* @param int $invoiceId
* @return bool
* @param $xenditTotal
* @param $whmcsTotal
* @return float
*/
public function isInvoiceUsedCreditCard(int $invoiceId): bool
public function extractPaidAmount($xenditTotal, $whmcsTotal): float
{
$transaction = XenditTransaction::where("invoiceid", $invoiceId)
->where("payment_method", "CREDIT_CARD")
->get();
return $transaction->count() > 0;
return $xenditTotal - $whmcsTotal > 0 && $xenditTotal - $whmcsTotal < 1 ? (float)$whmcsTotal : (float)$xenditTotal;
}

/**
Expand All @@ -205,8 +203,10 @@ public function confirmInvoice(int $invoiceId, array $xenditInvoiceData, bool $s
return false;
}

$invoice = $this->getInvoice($invoiceId);

$transactionId = $xenditInvoiceData['id'];
$paymentAmount = $xenditInvoiceData['paid_amount'];
$paymentAmount = $this->extractPaidAmount($xenditInvoiceData['paid_amount'], $invoice->total);
$paymentFee = $xenditInvoiceData['fees'][0]["value"];
$transactionStatus = 'Success';

Expand Down Expand Up @@ -278,4 +278,13 @@ public function isRecurring(int $invoiceId): bool
}
return false;
}

/**
* @param float $total
* @return float
*/
public function roundUpTotal(float $total): float
{
return ceil($total);
}
}
2 changes: 1 addition & 1 deletion modules/gateways/xendit/lib/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ protected function generateInvoicePayload(array $params, bool $retry = false): a
'description' => $params["description"],
'items' => $this->extractItems($invoice),
'fees' => array(['type' => 'Payment Fee', 'value' => (float)$params['paymentfee']]),
'amount' => $params['amount'] + (float)$params['paymentfee'],
'amount' => $this->roundUpTotal($params['amount'] + (float)$params['paymentfee']),
'client_type' => 'INTEGRATION',
'platform_callback_url' => $params["systemurl"] . $this->callbackUrl,
'success_redirect_url' => $this->invoiceUrl($params['invoiceid'], $params['systemurl']),
Expand Down
61 changes: 61 additions & 0 deletions modules/gateways/xendit/tests/WHMCSModuleTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Xendit\Tests;

/**
Expand Down Expand Up @@ -47,4 +48,64 @@ public function testRequiredConfigOptionsParametersAreDefined()
$this->assertArrayHasKey('xenditSecretKey', $result);
$this->assertArrayHasKey('xenditExternalPrefix', $result);
}

/**
* @return array
*/
public function totalDataProvider(): array
{
return [
[
"xenditTotal" => 1000,
"whmcsTotal" => 999.9555555,
"expectTotal" => 999.9555555
],
[
"xenditTotal" => 1000,
"whmcsTotal" => 999,
"expectTotal" => 1000
],
[
"xenditTotal" => 9999,
"whmcsTotal" => 9999.5,
"expectTotal" => 9999
],
[
"xenditTotal" => 9999,
"whmcsTotal" => 9998,
"expectTotal" => 9999
],
[
"xenditTotal" => 20000,
"whmcsTotal" => 19999.01,
"expectTotal" => 19999.01
]
];
}

/**
* Test the round up total should have no decimal
*/
public function testRoundUpTotalNotHasDecimal()
{
$actionBase = new \Xendit\Lib\ActionBase();

foreach ($this->totalDataProvider() as $total) {
$roundedTotal = $actionBase->roundUpTotal($total["whmcsTotal"]);
$this->assertTrue($roundedTotal == ceil($total["whmcsTotal"]));
}
}

/**
* Test the callback paid total
*/
public function testCallbackPaidTotal()
{
$actionBase = new \Xendit\Lib\ActionBase();

foreach ($this->totalDataProvider() as $total) {
$this->assertIsFloat($actionBase->extractPaidAmount($total["xenditTotal"], $total["whmcsTotal"]));
$this->assertEquals($total["expectTotal"], $actionBase->extractPaidAmount($total["xenditTotal"], $total["whmcsTotal"]));
}
}
}

0 comments on commit 1a0c83b

Please sign in to comment.