PHP code example of ducksoft / alipay-php-sdk
1. Go to this page and download the library: Download ducksoft/alipay-php-sdk library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
ducksoft / alipay-php-sdk example snippets
$config = [
// 支付宝网关 此为沙盒测试网关,请修改为正式网关
'gatewayUrl' => 'https://openapi.alipaydev.com/gateway.do',
// 应用ID 请求改为你的应用appId
'appId' => '2016051900098985',
// 支付宝公钥
'alipayrsaPublicKey' => '',
// 应用私钥
'rsaPrivateKey' => '',
// 加密方式
'signType' => 'RSA2',
'postCharset' => 'UTF-8',
'format' => 'json',
// 异步通知地址
'notifyUrl' => '',
];
use duckSoft\alipay\aop\request\AlipayTradeQueryRequest;
use duckSoft\alipay\aop\request\AlipayTradePrecreateRequest;
use duckSoft\alipay\AopClientBuilder;
use duckSoft\alipay\AopResult;
uest = new AlipayTradeQueryRequest();
$request->setBizContent(json_encode($bizContent, JSON_UNESCAPED_UNICODE));
try {
$result = $aop->execute($request);
$response = (new AopResult($result, $request->getApiMethodName()))->getResponse();
var_dump($response);
} catch (\ErrorException $e) {
return ['status' => 'error', 'message' => $e->getMessage()];
}
// 创建订单
$bizContent = [
'out_trade_no' => '7895552OPUTYGG',
'subject' => '测试订单',
'total_amount' => '0.01'
];
$request = new AlipayTradePrecreateRequest();
$request->setBizContent(json_encode($bizContent, JSON_UNESCAPED_UNICODE));
$request->setNotifyUrl($config['notifyUrl']);
try {
$result = $aop->execute($request);
$response = (new AopResult($result, $request->getApiMethodName()))->getResponse();
var_dump($response);
} catch (\ErrorException $e) {
return ['status' => 'error', 'message' => $e->getMessage()];
}
use duckSoft\alipay\AopClientBuilder;
$config = ient();
if ($aop->rsaCheckV1($_POST, null, $config['signType'])) {
// 校验签名成功
if ($_POST['trade_status'] === "TRADE_SUCCESS") {
// 处理订单逻辑
} else {
// 订单失败逻辑
}
echo 'success';
} else {
echo 'error';
}
bash
composer