0

我是支付集成的新手。我需要在我的 php 页面中集成 MasterCard 支付网关服务。我有一个按钮PAY,一旦点击,我就会重定向到下一页。我有以下代码:

    <script type="text/javascript">
        function errorCallback(error) {
              console.log(JSON.stringify(error));
        }
        function cancelCallback() {
              console.log('Payment cancelled');
        }

        Checkout.configure({
            merchant: 'test001000000052',
            order: {
                amount: 100,
                currency: 'AED',
                description: 'Ordered goods',
               id: '123'
            },
            interaction: {
                merchant: {
                    name: 'test',
                    address: {
                        line1: '200 Sample St',
                        line2: '1234 Example Town'            
                    },
                 cancelUrl:'10.0.1.100/?load=parents/online_payment'    
                }
              }
        });
    </script>
</head>
<body> 
    <input type="button" value="Pay with Lightbox" onclick="Checkout.showLightbox();" />
    <input type="button" value="Pay with Payment Page" onclick="Checkout.showPaymentPage();" /> 
</body>

当我单击任一按钮时,我在控制台中收到以下错误消息。

XHR failed loading: POST "https://eu-gateway.mastercard.com/api/page/version/51/pay". 
{"cause":"INVALID_REQUEST","explanation":"Invalid credentials."}

我正确使用了商家 ID,可能是我遗漏了什么。我有用户名和密码,不知道在哪里添加这些参数。请帮我

我已从https://eu-gateway.mastercard.com/api/documentation/integrationGuidelines/hostedCheckout/integrationModelHostedCheckout.html?locale=en_US获取代码

4

3 回答 3

1

您应该创建一个会话,然后打开结帐页面或灯箱

创建会话变量

$session_request = [
                'apiOperation' => "CREATE_CHECKOUT_SESSION",
                'interaction'  => [
                    'operation' => "PURCHASE",
                    'returnUrl' => "<return url>",
                ],
                'order'        => [
                    'amount'      => <amount>,
                    'currency'    => '<Currency Code like USD>',
                    'description' => 'Full Payment',
                    'id'          => <transaction reference>,
                ],
                'userId'       => <customer ID>,
            ];

调用以下函数将会话数据发送到万事达卡服务器

/**
     * @param        $inputs
     * @param        $link
     * @param        $username
     * @param        $password
     * @param string $type
     *
     * @return \Exception|\GuzzleHttp\Exception\ClientException|\GuzzleHttp\Exception\RequestException|mixed|string
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public static function postJsonRequestWithToken( $inputs, $link, $username, $password, $type = "assoc" ) {
        try {
            $client     = new Client();
            $response   = $client->post( $link, [
                'body' => json_encode( $inputs ),
                'auth' => [
                    $username,
                    $password,
                ],
            ] );
            $statusCode = $response->getStatusCode();
            $body       = $response->getBody()->getContents();
            if ( $type == 'json' ) {
                return $body;
            }
            if ( $type == 'object' ) {
                return json_decode( $body );
            }
            if ( $type == 'assoc' ) {
                return json_decode( $body, true );
            }
            
            return json_decode( $body );
        } catch( ClientException $e ) {
            //return json_decode( $e->getResponse()->getBody(), true );
            return $e;
        } catch( RequestException $e ) {
            //return json_decode( $e->getResponse()->getBody(), true );
            return $e;
        }
        /*} catch( RequestException $e ) {
            echo Psr7\str($e->getRequest());
            if ($e->hasResponse()) {
                echo Psr7\str($e->getResponse());
            }
        }*/
    }
于 2021-01-12T18:29:48.163 回答
0

有两种情况 1. 当您收到商家 ID 时。他们应该共享一个文档链接,请参考。大多数情况下 checkout.js 文件路径是错误的。2. 调用API时必须进行基本的用户认证,并传递用户名和密码。您可以从管理面板生成密码

于 2020-04-29T12:59:06.800 回答
0
static public function initPaymentSession($amount,$orderID,$description,$currency = 'EGP')
{
    // Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, 'https://banquemisr.gateway.mastercard.com/api/nvp/version/60');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, "apiOperation=CREATE_CHECKOUT_SESSION&apiPassword=xxxxxxxxxxxxxxx&apiUsername=merchant.xxxxxxxxx&merchant=xxxxxxxxx&interaction.operation=PURCHASE&order.id=$orderID&order.amount=$amount&order.currency=EGP");

    $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 'Error:' . curl_error($ch);
    }
    if (curl_errno($ch)) {
        return 'Error:' . curl_error($ch);
    }
    curl_close($ch);

    if ($result){
        $res1 = explode('&session.id=', $result);
        $res2 = explode('&', $res1[1]);
        return $res2[0];
    }

}
于 2021-06-08T23:11:35.023 回答