diff --git a/modules/gateways/xendit/lib/ActionBase.php b/modules/gateways/xendit/lib/ActionBase.php index 6b21d8a..4f021ec 100644 --- a/modules/gateways/xendit/lib/ActionBase.php +++ b/modules/gateways/xendit/lib/ActionBase.php @@ -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; } /** @@ -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'; @@ -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); + } } diff --git a/modules/gateways/xendit/lib/Link.php b/modules/gateways/xendit/lib/Link.php index f081bee..9310363 100644 --- a/modules/gateways/xendit/lib/Link.php +++ b/modules/gateways/xendit/lib/Link.php @@ -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']), diff --git a/modules/gateways/xendit/tests/WHMCSModuleTest.php b/modules/gateways/xendit/tests/WHMCSModuleTest.php index 3631a13..2490563 100644 --- a/modules/gateways/xendit/tests/WHMCSModuleTest.php +++ b/modules/gateways/xendit/tests/WHMCSModuleTest.php @@ -1,4 +1,5 @@ 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"])); + } + } }