跳到主要内容

PHP示例使用说明

该示例适用于PHP编程语言,可以调用所有OpenAPI接口。示例封装了执行集成的过程,包括添加和验证调用支付网关API的签名。这只是一个例子,我们不保证代码适用于任何编程环境。它只是演示如何调用我们的API。您可以参考此代码来编写自己的程序。

使用条件

适用于PHP 5.5以上的开发环境。

下载地址

使用步骤

1. 添加依赖项,使用“Composer”下载以下包

{
"require": {
"ext-json": "*",
"ext-openssl": "*",
"ext-curl": "*"
}
}

'ext-curlL' is used for packets sent over the HTTP network, 'ext-json' parses and processes JSON packets, and 'ext-openssl' is used for RSA signature calculation packets.

2. 请参阅GitHub示例代码并编写您的程序。以下是关键代码

    // 1. Global parameter settings
$appRsaPrivateKeyPem = "<YOUR APP RSA PRIVATE KEY>";
$gatewayRsaPublicKeyPem = "<YOUR GATEWAY RSA PUBLIC KEY>";
$gatewayUrl = "<YOUR GATEWAY URL>";
$appId = "<YOUR APP ID>";

// 2. Set parameters
// Common parameters
$parameters["app_id"] = $appId;
$parameters["charset"] = "UTF-8";
$parameters["format"] = "JSON";
$parameters["sign_type"] = "RSA2";
$parameters["version"] = "1.0";
$parameters["timestamp"] = getMillisecond();
$parameters["method"] = "order.query";
// API owned parameters
$parameters["merchant_no"] = "312100000164";
$parameters["merchant_order_no"] = "TEST_1685946062143";

// 3. Build a string to be signed
$stringToBeSigned = buildToBeSignString($parameters);
echo "StringToBeSigned : " . $stringToBeSigned;
echo "</br></br>";

// 4. Calculate signature
$sign = generateSign($stringToBeSigned, $appRsaPrivateKeyPem);
$parameters["sign"] = $sign;

// 5. Send HTTP request
$jsonString = json_encode($parameters);
echo "Request to gateway[" . $gatewayUrl . "] send data -->> " . $jsonString;
echo "</br></br>";
$responseStr = json_post($gatewayUrl, $jsonString);
echo "Response from gateway[" . $gatewayUrl . "] receive data <<-- " . $responseStr;
echo "</br></br>";

// 6. Verify the signature of the response message
$respObject = json_decode($responseStr, true);
$respStringToBeSigned = buildToBeSignString($respObject);
echo "RespStringToBeSigned : " . $respStringToBeSigned;
echo "</br></br>";
$respSignature = $respObject["sign"];
$verified = verify($respStringToBeSigned, $respSignature, $gatewayRsaPublicKeyPem);

echo "SignVerifyResult : " . $verified;
echo "</br></br>";