Introduction
In a nutshell, prices are transmitted in cents. That means that a price of 9.95 is transmitted as a price of 995. Quantities are transmitted as integer values, with an optional decimalPlaces property that signifies the position of the decimal point. A quantity of 1.25 is transmitted with a quantity of 125 and a decimalPlaces of 2.
See below for more examples, calculations and code samples.
Contents
Prices
Let’s take a look at the following order:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?php $order = array( 'extOrderId' => 'ORDER0001359', ... 'lineList' => array( array( 'articleNumber' => 14, 'data' => array( 'quantity' => 1, 'price' => 1995, // the actual price here is 19.95 ), 'text' => 'Colored Tie', ), array( 'articleNumber' => 999, 'data' => array( 'quantity' => 1, 'price' => 495, // the actual price here is 4.95 ), 'text' => 'Shipping costs', ), ), ); |
The actual price of article “Colored Tie” is 19.95. This is because the price in cents is 1995. To convert from cents to the actual price, you can divide the value by 100: 1995 / 100 = 19.95. To convert from the actual price to cents, you can multiply the value by 100: 19.95 * 100 = 1995
The actual price of article “Shipping costs” is 4.95. This is because the price in cents is 495. To convert from cents to the actual price, you can divide the value by 100: 495 / 100 = 4.95. To convert from the actual price to cents, you can multiply the value by 100: 4.95 * 100 = 495
Quantities
Let’s take a look at the following order:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?php $order = array( 'extOrderId' => 'ORDER0001360', ... 'lineList' => array( array( 'articleNumber' => 35, 'data' => array( 'quantity' => 530, // the actual quantity here is 0.530 'decimalPlaces' => 3, // because this value means the decimal point is shifted 3 positions to the left ), 'text' => 'Gehakt', ), ), ); |
The actual quantity of article “Gehakt” is 0.530. This is because the quantity is 530, but the decimalPlaces is 3.
530 can also be written as 0530.0. If you take that decimal point, and shift if 3 places to the left, as indicated by the value of decimalPlaces, you get the actual quantity: 0.530.
Here is a PHP function to convert quantity and decimalPlaces to the proper decimal value.
1 2 3 4 5 6 |
function from_quantity_and_decimal_places($quantity, $decimalPlaces) { $decimalValue = (float)$quantity; $decimalPlaces = (float)$decimalPlaces; $decimalValue = $decimalValue / pow(10, $decimalPlaces); return $decimalValue; } |
And here is a PHP function to convert a decimal value to its quantity and decimalPlaces values.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
function get_quantity_and_decimal_places($decimalValue) { $decimalValue = str_replace(',', '.', $decimalValue); $decimalValue = round($decimalValue, 5); $originalDecimalValue = $decimalValue; $decimalPlaces = -1; do { $int_part = (int)$decimalValue; $decimalValue -= $int_part; $decimalValue *= 10; $decimalValue = round($decimalValue, 5); $decimalPlaces++; } while ($decimalValue >= 0.0000001); $quantity = (int)($originalDecimalValue * pow(10, $decimalPlaces)); return array($quantity, $decimalPlaces); } |