user3615115 user3615115 - 5 months ago 24
PHP Question

Woocommerce is trimming the last 0 from order_total

I have created a custom woocommerce payment gateway, everything is working smoothly, I set the digits after decimals to two digits but whenever the price end with 0, order_total is being trimmed when its being sent to the gateway.

Example: if the amount is 122.98 the order_total go to the gateway as 122.98 but if the amount is 122.80 the order_total go to gateway as 122.8 and that is an issue with the gateway they need to receive a double digits after decimal even if double zeros. However the prices are showing well on the website the double digits is working well on site display only when it's sent to gateway the zeros disappear.

Gateway code below:
/**
* Get netcommerce Args for passing to netcommerce
**/
function get_netcommerce_args( $order ) {
global $woocommerce;

$order_id = $order->id;
$currencyUSD ='840';
$order_total = $order->get_total();
$merchantID = $this->txtMerchNum;
$notify_url = add_query_arg( 'wc-api', 'wc_tbz_netcommerce_gateway', home_url( '/checkout' ) );

$signature_elts =$order_total.$currencyUSD.$order_id.$this->txtMerchNum.$notify_url.$this->sha_key;
$signature =hash('sha256',(string)$signature_elts);

// netcommerce Args
$netcommerce_args = array(
'txtIndex' => $order_id,
'txthttp' => $notify_url,
'txtAmount' => $order_total,
'txtCurrency' => $currencyUSD,
'txtMerchNum' => $this->txtMerchNum,
'payment_mode' => $this->payment_mode,
'first_name' => $order->billing_first_name,
'last_name' => $order->billing_last_name,
'email' => $order->billing_email,
'mobile' => $order->billing_phone,
'address_line1' => $order->billing_address_1,
'address_line2' => $order->billing_address_2,
'postal_code' => $order->billing_postcode,
'city' => $order->billing_city,
'state' => $order->billing_state,
'country' => $order->billing_country,
'signature' => $signature

);

$netcommerce_args = apply_filters( 'woocommerce_netcommerce_args', $netcommerce_args );
return $netcommerce_args;
}


/**
* Generate the netcommerce Payment button link
**/
function generate_netcommerce_form( $order_id ) {
global $woocommerce;

$order = new WC_Order( $order_id );

$netcommerce_adr = $this->gateway_link;

$netcommerce_args = $this->get_netcommerce_args( $order );

$netcommerce_args_array = array();

foreach ($netcommerce_args as $key => $value) {
$netcommerce_args_array[] = '<input type="hidden" name="'.esc_attr( $key ).'" value="'.esc_attr( $value ).'" />';
}

return '<form action="'.esc_url( $netcommerce_adr ).'" method="post" id="netcommerce_payment_form" target="_top">
' . implode('', $netcommerce_args_array) . '
<input type="submit" class="button-alt" id="submit_netcommerce_payment_form" value="'.__('Make Payment', 'woocommerce').'" /> <a class="button cancel" href="'.esc_url( $order->get_cancel_order_url() ).'">'.__('Cancel order &amp; restore cart', 'woocommerce').'</a>
</form>';

}


/**
* Process the payment and return the result
**/
function process_payment( $order_id ) {

$order = new WC_Order( $order_id );
return array(
'result' => 'success',
'redirect' => $order->get_checkout_payment_url( true )
);
}


/**
* Output for the order received page.
**/
function receipt_page( $order ) {
echo '<p>'.__('Thank you for your order, please click the button below to make payment.', 'woocommerce').'</p>';
echo $this->generate_netcommerce_form( $order );
}


/**
* Verify a successful Payment!
**/
function check_netcommerce_response(){
global $woocommerce;
if(isset($_POST['txtIndex']))
{


$transaction_id = $_POST['txtIndex'];
$order_id = $_POST['txtIndex'];


$order = new WC_Order($order_id);
$order_total = $order->get_total();

$amount_paid = $_POST['txtAmount'];
$RespVal = $_POST['RespVal'];
$RespMsg = $_POST['RespMsg'];
$txtMerchNum = $_POST['txtMerchNum'];
$txtNumAut = $_POST['txtNumAut'];
$Signature = $_POST['signature'];

$txtCurrency = $_POST['txtCurrency'];
$sha=$this->sha_key ;
$SignatureElts=$txtMerchNum.$order_id.$amount_paid.$txtCurrency.$txtNumAut.$RespVal.$RespMsg.$sha;
$ReturnSignature= hash('sha256',$SignatureElts);

if($ReturnSignature != $Signature){
$message1="Signature error" ;
if ( function_exists( 'wc_add_notice' ) ) {
wc_add_notice( $message1, 'error' );

} else { // WC < 2.1
$woocommerce->add_error( $message1 );
$woocommerce->set_messages();
}
}


Please advise
Thank you

Answer

Replace $order_total = $order->get_total(); with $order_total = number_format($order->get_total(), 2, ".", "") to get a string padded to 2 decimal places instead of a float.