PHP code example of yng / alipay-global
1. Go to this page and download the library: Download yng/alipay-global 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/ */
yng / alipay-global example snippets
use Yng\AlipayGlobal\AliPayGlobal;
use Yng\AlipayGlobal\Tool\Tool;
$auth_state = Tool::CreateAuthState();
$alipayGlobal = new AliPayGlobal(array(
'client_id' => 'SANDBOX_123456789123456', // Client ID
'endpoint_area' => 'ASIA', // 地区选择: NORTH_AMERIA / ASIA / EUROPE / GLOBAL
'merchantPrivateKey' => '', // 商户私钥
'alipayPublicKey' => '', // 支付宝公钥
'is_sandbox' => true, // 是否打开沙盒环境
));
$res = $alipayGlobal->run([
'model' => 'AuthConsult',// 默认值
'customerBelongsTo' => 'ALIPAY_CN',
'authRedirectUrl' => 'http://www.example.com/',
'scopes' => 'BASE_USER_INFO',
'authState' => $auth_state,
'terminalType' => 'WAP',
'osType' => 'ANDROID',
]);
$res = $alipayGlobal->run([
'model' => 'ApplyToken',// 默认值
'customerBelongsTo' => 'ALIPAY_CN',
'authRedirectUrl' => 'http://www.example.com/',
'scopes' => 'BASE_USER_INFO',
'authState' => $auth_state,
'terminalType' => 'WAP',
'osType' => 'ANDROID',
]);
$res = $alipayGlobal->run([
'model' => 'RevokeToken',// 默认值
'accessToken' => 'xxxxxxxxx',
]);
$order_id = 'DEMOPAY_'.date('YmdHis').rand(10000,99999);// 也可以使用工具包里的 Tool::CreatePaymentRequestId()
$res = $alipayGlobal->run([
'model' => 'CreatePay',
'paymentRequestId' => $order_id,// 自定义订单id
// 订单信息
'order' => [
'orderAmount' => [
'currency' => 'CNY',
'value' => '200',
],
'referenceOrderId' => $order_id,// 标识商家端订单的唯一ID,用于投诉,建议和订单id一样,也可自定义
'orderDescription' => 'DEMO 描述',// 订单描述
'env' => [
'terminalType' => 'WEB',// 终端类型
],
],
// 付款金额,与订单信息里的保持一致
'paymentAmount' => [
'currency' => 'CNY',
'value' => '200',
],
// 支付方式
'paymentMethod' => [
'paymentMethodType' => 'ALIPAY_CN',
],
'paymentRedirectUrl' => 'http://www.example1.com/',// 支付后重定向地址
'paymentNotifyUrl' => 'http://www.example2.com/',// 支付后的异步回调地址
// 结算策略/结算货币
'settlementStrategy' => [
'settlementCurrency' => 'CNY',
]
]);
if($res && in_array($res['result']['resultStatus'],['U','S'])){
$normalUrl = $res['normalUrl'];// 创建后跳转地址
}else{
return $res['result']['resultMessage'];// 创建支付订单失败原因
}
$res = $alipayGlobal->run([
'model' => 'CancelPay',
'paymentId' => 'xxxxx',// 支付宝返回的订单id,二选一,同时填写paymentId级别优先
'paymentRequestId' => 'xxxxx',// 自定义订单id,二选一,同时填写paymentId级别优先
]);
$res = $alipayGlobal->run([
'model' => 'CheckPayStatus',
'paymentId' => 'xxxxx',// 支付宝返回的订单id,二选一,同时填写paymentId级别优先
'paymentRequestId' => 'xxxxx',// 自定义订单id,二选一,同时填写paymentId级别优先
]);
$order_id = 'DEMOPAY_'.date('YmdHis').rand(10000,99999);// 也可以使用工具包里的 Tool::CreatePaymentRequestId()
$res = $alipayGlobal->run([
'model' => 'PayByDebit',
'paymentRequestId' => $order_id,// 自定义订单id
// 订单信息
'order' => [
'orderAmount' => [
'currency' => 'CNY',
'value' => '300',
],
'referenceOrderId' => $order_id,// 标识商家端订单的唯一ID,用于投诉
'orderDescription' => 'DEMO 描述',// 订单描述
],
// 付款金额,与订单信息里的保持一致
'paymentAmount' => [
'currency' => 'CNY',
'value' => '300',
],
// 支付方式
'paymentMethod' => [
'paymentMethodType' => 'ALIPAY_CN',
'paymentMethodId' => 'access_token:xxxx',// applyToken申请的access token值
],
// 结算策略/结算货币
'settlementStrategy' => ['settlementCurrency' => 'CNY'],
]);
$order_id = 'PayEntryCode_'.date('YmdHis').rand(10000,99999);// 也可以使用工具包里的 Tool::CreatePaymentRequestId()
$res = $alipayGlobal->run([
'model' => 'PayEntryCode',
'paymentRequestId' => $order_id,// 自定义订单id
// 订单信息
'order' => [
'orderAmount' => [
'currency' => 'CNY',
'value' => '200',
],
'referenceOrderId' => $order_id,// 标识商家端订单的唯一ID,用于投诉
'orderDescription' => 'PayEntryCode demo 描述',// 订单描述
'merchant' => [
'referenceMerchantId' => 'xxx',// 服务或商品的商家的 ID
'merchantMCC' => 'xxx',// 商家mcc码,具体看文档
'merchantName' => 'xxx',// 商家名
// 商店信息
'store' => [
'referenceStoreId' => 'xxx',// 由拥有商店的商家分配的唯一商店 ID
'storeName' => 'xxx',// 商店名
'storeMCC' => 'xxx',// 商店mcc码
],
],
'env' => [
'userAgent' => 'Mozilla/5.0 (Linux; Android 9; LYA-AL00 Build/HUAWEILYA-AL00L; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/70.0.3538.110 Mobile Safari/537.36 NebulaSDK/1.8.100112 Nebula AlipayDefined(nt:WIFI,ws:360|0|3.0) AliApp(APHK/2.6.0.160) AlipayClientHK/2.6.0.160 Language/zh-HK useStatusBar/true isConcaveScreen/true AlipayClient/10.1.32.600 Alipay Language/zh-Hans',// 此字段用于通过包含用户使用的钱包的标识符来指示用户身份,用户的请求头,这只是个demo,获取到的是怎么样就怎么样,无格式要求
],
],
// 付款金额,与订单信息里的保持一致
'paymentAmount' => [
'currency' => 'CNY',
'value' => '200',
],
// 支付方式
'paymentMethod' => [
'paymentMethodType' => 'CONNECT_WALLET',// 支付方式,EntryCode模式默认
],
'paymentNotifyUrl' => 'http://www.example.com/notifyurl',// 支付后的异步回调地址
// 结算策略/结算货币,因为是CONNECT_WALLET,此字段必填
'settlementStrategy' => [
'settlementCurrency' => 'CNY',
]
]);
$order_id = 'REFUND_DEMOPAY_'.date('YmdHis').rand(10000,99999);
$res = $alipayGlobal->run([
'model' => 'Refund',
'refundRequestId' => $order_id,// 自定义订单id
'paymentId' => 'xxxx',// 支付宝返回的订单id
// 退款金额
'refundAmount' => [
'currency' => 'CNY',
'value' => '100',
],
]);
$res = $alipayGlobal->run([
'model' => 'CheckRefundStatus',
'refundRequestId' => 'xxxx',// 自定义订单id,二选一,同时填写refundId级别优先
'refundId' => 'xxxx',// 支付宝返回的退款订单id,二选一,同时填写refundId级别优先
]);