Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add missmatch to request lines #917

Merged
merged 3 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mmaymo it looks good to me, but I have one question. There is one scenario that could cause problems. Maybe you know what happens. For example:
$orderTotalRounded - $linesTotal = 0.001
$this->order_lines[] = $missmatch;
Mollie may round this to 0.00 and we have a surcharge with 0.00. Can that happen?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, I'll check it

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, seems if the rounding makes the diff line send a 0 value there is no problem. The transaction went through without error.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, thank you :)

$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
Loading