Skip to content

Commit

Permalink
Merge pull request #917 from mollie/fix/decimal-missmatch-PIWOO-501
Browse files Browse the repository at this point in the history
Add missmatch to request lines
  • Loading branch information
mmaymo authored Jul 8, 2024
2 parents f18c7e5 + 0761cd7 commit d9f84e9
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 11 deletions.
6 changes: 1 addition & 5 deletions inc/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,7 @@ function mollieWooCommerceFormatCurrencyValue($value, $currency)
if (in_array($currency, $currenciesWithNoDecimals)) {
return number_format($value, 0, '.', '');
}
// trying to avoid floating point issues
$value = $value * 1000;
$value = (int) $value / 1000; //drop the last decimal after the third
$value = round($value, 3);
$value = round($value, 2, PHP_ROUND_HALF_DOWN); //round down, as seems woo like it :)

return number_format($value, 2, '.', '');
}

Expand Down
36 changes: 36 additions & 0 deletions src/Payment/OrderLines.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,48 @@ public function order_lines($order, $voucherDefaultCategory)
$this->process_shipping();
$this->process_fees();
$this->process_gift_cards();
$this->process_missmatch();

return [
'lines' => $this->get_order_lines(),
];
}

private function process_missmatch()
{
$orderTotal = $this->order->get_total();
$orderTotalRounded = round($orderTotal, 2);
$linesTotal = array_sum(array_map(function ($line) {
return $line['totalAmount']['value'];
}, $this->order_lines));
$orderTotalDiff = $orderTotalRounded - $linesTotal;
if (abs($orderTotalDiff) > 0) {
$missmatch = [
'type' => 'surcharge',
'name' => __('Rounding difference', 'mollie-payments-for-woocommerce'),
'quantity' => 1,
'vatRate' => 0,
'unitPrice' => [
'currency' => $this->currency,
'value' => $this->dataHelper->formatCurrencyValue($orderTotalDiff, $this->currency),
],
'totalAmount' => [
'currency' => $this->currency,
'value' => $this->dataHelper->formatCurrencyValue($orderTotalDiff, $this->currency),
],
'vatAmount' => [
'currency' => $this->currency,
'value' => $this->dataHelper->formatCurrencyValue(0, $this->currency),
],
'metadata' => [
'order_item_id' => 'rounding_diff',
],
];

$this->order_lines[] = $missmatch;
}
}

/**
* Get order lines formatted for Mollie Orders API.
*
Expand Down
10 changes: 4 additions & 6 deletions tests/php/Functional/Payment/PaymentServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,7 @@ public function processPayment_Order_success(){
'wc_clean' => null,
]
);
$gateway->expects($this->once())
->method('getSelectedIssuer')
->willReturn('ideal_INGBNL2A');

$expectedRequestToMollie = $this->expectedRequestData($wcOrder);
$orderEndpoints->method('create')->with($expectedRequestToMollie);

Expand Down Expand Up @@ -212,7 +210,7 @@ private function wcOrder($id, $orderKey)
[
'get_id' => $id,
'get_order_key' => $orderKey,
'get_total' => '20',
'get_total' => 40.00,
'get_items' => [$this->wcOrderItem()],
'get_billing_first_name' => 'billingggivenName',
'get_billing_last_name' => 'billingfamilyName',
Expand Down Expand Up @@ -307,7 +305,7 @@ private function expectedRequestData($order){
return [
'amount' => [
'currency' => 'EUR',
'value' => '20.00'
'value' => '40.00'
],
'redirectUrl' =>
'https://webshop.example.org/wc-api/mollie_return?order_id=1&key=wc_order_hxZniP1zDcnM8',
Expand All @@ -317,7 +315,7 @@ private function expectedRequestData($order){
'ideal',
'payment' =>
[
'issuer' => 'ideal_INGBNL2A'
'issuer' => null
],
'locale' => 'en_US',
'billingAddress' => $this->billingAddress($order),
Expand Down

0 comments on commit d9f84e9

Please sign in to comment.