PHP code example of evit / php-alipaysdk-easysdk
1. Go to this page and download the library: Download evit/php-alipaysdk-easysdk 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/ */
evit / php-alipaysdk-easysdk example snippets
if (!Factory::util()->generic()->verify($response)) {
return ['code' => 40003, 'msg' => '验签失败'];
}
ipay\EasySDK\Kernel\Factory;
use Alipay\EasySDK\Kernel\Util\ResponseChecker;
use Alipay\EasySDK\Kernel\Config;
//1. 设置参数(全局只需设置一次)
Factory::setOptions(getOptions());
try {
//2. 发起API调用(以支付能力下的统一收单交易创建接口为例)
$result = Factory::payment()->common()->create("iPhone6 16G", "20200326235526001", "88.88", "2088002656718920");
$responseChecker = new ResponseChecker();
//3. 处理响应或异常
if ($responseChecker->success($result)) {
echo "调用成功". PHP_EOL;
} else {
echo "调用失败,原因:". $result->msg.",".$result->subMsg.PHP_EOL;
}
} catch (Exception $e) {
echo "调用失败,". $e->getMessage(). PHP_EOL;;
}
function getOptions()
{
$options = new Config();
$options->protocol = 'https';
$options->gatewayHost = 'openapi.alipay.com';
$options->signType = 'RSA2';
$options->appId = '<-- 请填写您的AppId,例如:2019022663440152 -->';
// 为避免私钥随源码泄露,推荐从文件中读取私钥字符串而不是写入源码中
$options->merchantPrivateKey = '<-- 请填写您的应用私钥,例如:MIIEvQIBADANB ... ... -->';
$options->alipayCertPath = '<-- 请填写您的支付宝公钥证书文件路径,例如:/foo/alipayCertPublicKey_RSA2.crt -->';
$options->alipayRootCertPath = '<-- 请填写您的支付宝根证书文件路径,例如:/foo/alipayRootCert.crt" -->';
$options->merchantCertPath = '<-- 请填写您的应用公钥证书文件路径,例如:/foo/appCertPublicKey_2019051064521003.crt -->';
//注:如果采用非证书模式,则无需赋值上面的三个证书路径,改为赋值如下的支付宝公钥字符串即可
// $options->alipayPublicKey = '<-- 请填写您的支付宝公钥,例如:MIIBIjANBg... -->';
//可设置异步通知接收服务地址(可选)
$options->notifyUrl = "<-- 请填写您的支付类接口异步通知接收服务地址,例如:https://www.test.com/callback -->";
//可设置AES密钥,调用AES加解密相关接口时需要(可选)
$options->encryptKey = "<-- 请填写您的AES密钥,例如:aa4BtZ4tspm2wnXLb1ThQA== -->";
return $options;
}
Factory::payment()->faceToFace()
// 调用agent扩展方法,设置app_auth_token,完成ISV代调用
->agent("ca34ea491e7146cc87d25fca24c4cD11")
->preCreate("Apple iPhone11 128G", "2234567890", "5799.00");
Factory::payment()->faceToFace()
// 调用asyncNotify扩展方法,可以为每此API调用,设置独立的异步通知地址
// 此处设置的异步通知地址的优先级高于全局Config中配置的异步通知地址
->asyncNotify("https://www.test.com/callback")
->preCreate("Apple iPhone11 128G", "2234567890", "5799.00");
$goodDetail = array(
"goods_id" => "apple-01",
"goods_name" => "iPhone6 16G",
"quantity" => 1,
"price" => "5799"
);
$goodsDetail[0] = $goodDetail;
Factory::payment()->faceToFace()
// 调用optional扩展方法,完成可选业务参数(biz_content下的可选字段)的设置
->optional("seller_id", "2088102146225135")
->optional("discountable_amount", "8.88")
->optional("goods_detail", $goodsDetail)
->preCreate("Apple iPhone11 128G", "2234567890", "5799.00");
$optionalArgs = array(
"timeout_express" => "10m",
"body" => "Iphone6 16G"
);
Factory::payment()->faceToFace()
// 也可以调用batchOptional扩展方法,批量设置可选业务参数(biz_content下的可选字段)
->batchOptional($optionalArgs)
->preCreate("Apple iPhone11 128G", "2234567890", "5799.00");
// 多种扩展方式可灵活组装(对扩展方法的调用顺序没有要求)
Factory::payment()->faceToFace()
->agent("ca34ea491e7146cc87d25fca24c4cD11")
->asyncNotify("https://www.test.com/callback")
->optional("seller_id", "2088102146225135")
->preCreate("Apple iPhone11 128G", "2234567890", "5799.00");