Forums / Contribution-Writing Guidelines / Better way to format currencies

Better way to format currencies

Views: 3,944

Results 1 to 2 of 2
27 Jan 2024, 4:04 AM
#1
retched avatar

retched

Totally Zenned

Join Date:
Jun 2007
Location:
Bronx, New York, United States
Posts:
949
Plugin Contributions:
3

Better way to format currencies

This is in regards to the FraudLabsPro contribution.

Currently, the plugin pulls the raw number from the Orders table to fill in the "amount" field of the API call to FraudLabsPro server for processing. I've changed it from just pulling the value to the following:

$check_status = $db->Execute('select * FROM ' . TABLE_ORDERS . ' o LEFT JOIN ' . TABLE_CURRENCIES . ' c ON o.currency = c.code WHERE o.orders_id = ' . (int) $oID);


...

$response = fetch('https://api.fraudlabspro.com/v2/order/screen', [
'amount'          => round($check_status->fields['order_total'], $check_status->fields['decimal_places']), 
];

So far this does the job, but I feel there might be a better way to format and round the numerical portion of the currency. (The 'amount' field according to the API of Fraudlabspro only accepts numerical data. So I can't pull from, say, the orders_total table and present the ['text'] portion of that table.)

I tried to read through the ZenCart functions but couldn't figure out a better/easier way that reinventing the wheel per se.

Any advice would be appreciated.

27 Jan 2024, 1:31 PM
#2
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,119
Plugin Contributions:
56

Re: Better way to format currencies

retched:

This is in regards to the FraudLabsPro contribution.

Currently, the plugin pulls the raw number from the Orders table to fill in the "amount" field of the API call to FraudLabsPro server for processing. I've changed it from just pulling the value to the following:

$check_status = $db->Execute('select * FROM ' . TABLE_ORDERS . ' o LEFT JOIN ' . TABLE_CURRENCIES . ' c ON o.currency = c.code WHERE o.orders_id = ' . (int) $oID);

...

$response = fetch('https://api.fraudlabspro.com/v2/order/screen', [
'amount' => round($check_status->fields['order_total'], $check_status->fields['decimal_places']),
];

> 
> So far this does the job, but I feel there might be a better way to format and round the numerical portion of the currency. (The 'amount' field according to the API of Fraudlabspro only accepts numerical data. So I can't pull from, say, the orders_total table and present the ['text'] portion of that table.) 
> 
> I tried to read through the ZenCart functions but couldn't figure out a better/easier way that reinventing the wheel per se.
> 
> Any advice would be appreciated.
While not in the 'official' Zen Cart docs/functions, here's how I 'feed' currency values to PayPal (adapted for your usage):
'amount' => number_format((float)$check_status->fields['order_total'], (int)$check_status->fields['decimal_places'], '.', ''),