'MPGS (mastercard gateway) returns error but works when using plugin in woocommerce or opencart
I am trying to implement MPGS payment gateway in php. The details I am using are same that are used and are working when using 3rd party plugin in woocommerce woo mpgs and in open cart using mpgs gateway
But I tried doing the same with core php following the official mastercard integration guide and also converted the source codes from the above plugins, but both give the same error as below in both LIVE and TEST mode
Here is the code I am using:
<?php
$orderid='223';
$merchant ='TestMYID';
$apipassword = 'xxx2b27cf8e45fffc6532f50xxxxxxxx';
$returnUrl = 'http://localhost/mpgs.php';
$currency = 'KWD';
$amount = 1;
$ch =curl_init();
curl_setopt($ch, CURLOPT_URL,'https://ap-gateway.mastercard.com/api/nvp/version/55');
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "apiOperation=CREATE_CHECKOUT_SESSION&apiPassword=$apipassword&apiUsername=merchant.$merchant&merchant=$merchant&interaction.operation=PURCHASE&interaction.returnUrl=$returnUrl&order.id=$orderid&order.amount=$amount&order.currency=$currency");
$headers = array();
$headers[] = "Content-Type: application/x-www-form-urlencoded";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if(curl_errno($ch)){
echo curl_error($ch);
}
curl_close($ch);
$a = explode('&', $result);
foreach ($a as $result) {
$b = explode('=', $result);
$array[$b[0]] = $b[1];
}
$sessionid = ($array['session.id']);
//exit;
?>
<script src="https://ap-gateway.mastercard.com/checkout/version/55/checkout.js"
data-error="errorCallback"
data-cancel="http://localhost/mpgs.php">
</script>
<script>
function errorCallback(error)
{
alert("Error: "+JSON.stringify(error));
}
Checkout.configure({
merchant: '<?=$merchant?>',
order:{
amount:function(){
return <?=$amount?>;
},
currency:'<?=$currency?>',
description:'Order Goods',
id:'<?=$orderid?>'
},
interaction:{
merchant:{
name:'Anesthesia Lenses',
address:{
line1:'Kuwait',
line2:'Kuwait'
}
}
},
session:{
id:'<?=$sessionid?>'
}
});
Checkout.showPaymentPage();
//Checkout.showLightbox()
</script>
I have also tried the following:
- Running it on live domain to rule out any domain based blocking condition
- Ran with test and live credentials for same error
- Tried multiple test cards from official guide and other sources
- Tried with asia-pacific and europe endpoints
This is ran for a website with bank account located in Kuwait if that helps to for the url being wrong or something.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

