phpajaxapicurlmastercard

Master card MIGS api


I want to integrate the payment partal without displaying the MasterCard interface to user the will only fill their information on my web here

my portal

enter image description here

and not here

mastercard portal

enter image description here

I am trying to implement CommWeb on my PHP site using curl. It isn't working. I am getting following error message:

if ($_SERVER["REQUEST_METHOD"] == "GET") {
    if (!isset($_GET['id']) || !isset($_GET['motif']) || !isset($_GET['code'])){
        die("Invalid request");
    }

    $amount = 10000;
    $id = 2;
    $motif = 'save';
    $action_code = 'ookkk';
    $orderInfo = 'fhhsd'.$id;
    $MerchTxnRef = $orderInfo.'-'.generateMerchTxnRef();
    $accountData = array(
        'merchant_id' => 'TESTID', // for test card
        'access_code' => '77867878', // for test card
        'secret'      => 'TYUJHGFDFGHJ87654567GFDFGHGF' // for test card

    );

    $currency_str = "USD";
    $mult = 100;

    $queryData = array(
        'vpc_AccessCode' => $accountData['access_code'],
        'vpc_Merchant' => $accountData['merchant_id'],
        'vpc_Amount' => 1000000, // Multiplying by 100 to convert to the smallest unit
        'vpc_OrderInfo' => $orderInfo,
        'vpc_MerchTxnRef' => $MerchTxnRef,
        'vpc_Command' => 'pay',
        'vpc_Currency' => $currency_str,
        'vpc_Locale' => 'en',
        'vpc_Version' => 2,
        'vpc_ReturnURL' => ('http://theeventsfactory.biz/the_events_factory/logics/payment_return.php?id='.$id.'&motif='.$motif.'&code='.$action_code),
        'vpc_SecureHashType' => 'SHA256',
        'vpc_CardNum' => '5123456789012346',
        'vpc_CardExp' => '0521',
        'vpc_CardSecurityCode'=> '123'
    );

    // Add secure secret after hashing
    // $queryData['vpc_SecureHash'] = generateSecureHash($accountData['secret'], $queryData);

    // $migsUrl = 'https://migs.mastercard.com.au/vpcpay?'.http_build_query($queryData);


    $ch = curl_init("https://migs.mastercard.com.au/vpcdps");
        curl_setopt_array($ch, array(
            CURLOPT_POST => true,
            CURLOPT_POSTFIELDS => $queryData,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_HEADER => false
        ));

        $response = curl_exec($ch);

    print_r($response);

}

function generateMerchTxnRef() {
    $txnRef = rand(9, 9999999999999999);

    // Saved in the database associated with the order id

    return $txnRef;
}

function generateSecureHash($secret, array $params) {
    $secureHash = "";

    // Sorting params first based on the keys
    ksort($params);

    foreach ($params as $key => $value)
    {        
        // Check if key equals to vpc_SecureHash or vpc_SecureHashType to discard it
        if(in_array($key, array('vpc_SecureHash', 'vpc_SecureHashType'))) continue;

        // If key either starts with vpc_ or user_
        if(substr( $key, 0, 4 ) === "vpc_" || substr($key, 0, 5) === "user_") {

            $secureHash .= $key."=".$value."&";
        }
    }

    // Remove the last `&` character from string
    $secureHash = rtrim($secureHash, "&");

    //
    return strtoupper(hash_hmac('sha256', $secureHash, pack('H*', $secret)));
}

This is the response i am receiving

vpc_Amount=0&vpc_BatchNo=0&vpc_Locale=en&vpc_Message=Required+field+vpc_Merchant+was+not+present+in+the+request&vpc_TransactionNo=0&vpc_TxnResponseCode=7 

How will i solve this problem


Solution

  • You need to convert the array to a url encoded string with http_build_query

    after setting the queryData array, add

    $queryData_string = http_build_query($queryData);
    

    Then the curlpost would be set to that

    CURLOPT_POSTFIELDS => $queryData_string,