Migs Two Party

November 24, 2019 (5y ago)

Last month, I have one freelance project need to do the payment integration with MasterCard. I just realised there are multiple type of integration model for MasterCard such as 2-Party, 2.5 Party and 3-Party Model.

Since, the documentation is so shit, I even can’t find how to do the integration for 2-Party Integration. Even, stackoverflow don’t have many question for this use case.

After several code inspection from multiple public repository, I found a way to implement it.

2-Party Integration

Create transaction

URL [POST] https://migs.mastercard.com.au/vpcdps

Body - x-www-form-urlencoded

    vpc_Version: 1
    vpc_Command: pay
    vpc_AccessCode: // get from mastercard
    vpc_vpc_MerchTxnRef: 123456
    vpc_Merchant: // get from mastercard
    vpc_OrderInfo: 'Purchase Books'
    vpc_Amount: 100 // RM1
    vpc_CardNum: 424242424242
    vpc_CardExp: 1203 // YYMM
    vpc_CardSecurityCode: 123

Void Transaction

URL [POST] https://migs.mastercard.com.au/vpcdps

Body

x-www-form-urlencoded

    vpc_Version: 1
    vpc_Command: voidPurchase
    vpc_AccessCode: 123457 // get from mastercard
    vpc_vpc_MerchTxnRef: 123456
    vpc_Merchant: 123456 // get from mastercard
    vpc_OrderInfo: 'Purchase Books'
    vpc_User: jakzaizzat // your MIGS username,
    vpc_Password: 123456 // MIGS password,

How to implement in Laravel

Create Transaction

    public function create(Request $request) {
    
            $transaction =  Transaction::create([
                'uuid' => utf8_encode((string) Uuid::generate()), // For utf8
                'name' => $request->detail,
                'amount' => $request->amount,
                'user_id' => $request->user()->id,
                'merchant_id' => $request->user()->merchant_id
            ]);
    
            $http = new \GuzzleHttp\Client;
            try {
                $response = $http->request('post', 'https://migs.mastercard.com.au/vpcdps', [
                    'form_params' => [
                        'vpc_Version' => '1',
                        'vpc_Command' => 'pay',
                        'vpc_AccessCode' => env('MIGS_ACCESSCODE'),
                        'vpc_MerchTxnRef' => $transaction->uuid,
                        'vpc_Merchant' => env('MIGS_MERCHANT_ID'),
                        'vpc_OrderInfo' => $request->detail,
                        'vpc_Amount' => $request->amount,
                        'vpc_CardNum' => $request->cc_number,
                        'vpc_CardExp' => $request->cc_exp,
                        'vpc_CardSecurityCode' => $request->cc_cvv
                    ]
                ]);
                $result = (string) $response->getBody();
                $result = 'http://senangpay.my?' . $result;
    
                $query_str = parse_url($result, PHP_URL_QUERY);
                parse_str($query_str, $query_params);
                $transaction->migs_id = $query_params["vpc_TransactionNo"];
                if($query_params["vpc_TxnResponseCode"] == 0) {
                    $transaction->status = "successful";
                    $transaction->save();
                    return Response::json(array(
                        'status'      =>  1,
                        'transaction_reference'   =>  $transaction->uuid,
                        'amount_paid' => $transaction->amount,
                        'msg' => 'Payment_was_successful'
                    ), 200);
                } else {
                    $transaction->status = "cancelled";
                    $transaction->save();
                    return Response::json(array(
                        'status'      =>  0,
                        'transaction_reference'   =>  $transaction->uuid,
                        'amount_paid' => $transaction->amount,
                        'msg' => $query_params["vpc_Message"]
                    ), 200);
                }
    
            } catch (\GuzzleHttp\Exception\BadResponseException $e) {
                if ($e->getCode() === 400) {
                    return response()->json('Invalid Request. Please enter a username or a password.', $e->getCode());
                } else if ($e->getCode() === 401) {
                    return response()->json('Your credentials are incorrect. Please try again', $e->getCode());
                }
                return response()->json('Something went wrong on the server.', $e->getCode());
            }
        }

Void Transaction

    public function delete(Request $request) {
            $transaction = Transaction::where('uuid', $request->transaction_reference)->first();
    
            $http = new \GuzzleHttp\Client;
            try {
                $response = $http->request('post', 'https://migs.mastercard.com.au/vpcdps', [
                    'form_params' => [
                        'vpc_Version' => '1',
                        'vpc_Command' => 'voidPurchase',
                        'vpc_AccessCode' => env('MIGS_ACCESSCODE'),
                        'vpc_MerchTxnRef' => $request->transaction_reference,
                        'vpc_Merchant' => env('MIGS_MERCHANT_ID'),
                        'vpc_User' => env('MIGS_USERNAME'),
                        'vpc_Password' => env('MIGS_PASSWORD'),
                        'vpc_TransNo' => $transaction->migs_id
                    ]
                ]);
                $result = (string) $response->getBody();
                $result = 'http://senangpay.my?' . $result;
    
                $query_str = parse_url($result, PHP_URL_QUERY);
                parse_str($query_str, $query_params);
                if($query_params["vpc_TxnResponseCode"] == 0) {
                    $transaction->status = "refund";
                    $transaction->save();
                    return Response::json(array(
                        'status'      =>  1,
                        'msg' => 'Transaction void successfully'
                    ), 200);
                } else {
                    return Response::json(array(
                        'status'      =>  0,
                        'transaction_reference'   =>  $transaction->uuid,
                        'amount_paid' => $transaction->amount,
                        'msg' => $query_params["vpc_Message"]
                    ), 200);
                }
    
            } catch (\GuzzleHttp\Exception\BadResponseException $e) {
                if ($e->getCode() === 400) {
                    return response()->json('Invalid Request. Please enter a username or a password.', $e->getCode());
                } else if ($e->getCode() === 401) {
                    return response()->json('Your credentials are incorrect. Please try again', $e->getCode());
                }
                return response()->json('Something went wrong on the server.', $e->getCode());
            }
        }