composer require alipaysdk/easysdk:^2.0
use Alipay\EasySDK\Kernel\Factory;
use Alipay\EasySDK\Kernel\Util\ResponseChecker;
use Alipay\EasySDK\Kernel\Config as AliConfig;
$options = new AliConfig();
$options->protocol = 'https';
$options->gatewayHost = 'openapi.alipay.com';
$options->signType = 'RSA2';
$options->appId = '2021435345';
$return_url = 'https://api.abc.cn/api/charge_notify/ali_charge_notify';
// 为避免私钥随源码泄露,推荐从文件中读取私钥字符串而不是写入源码中
$options->merchantPrivateKey = '';
//注:如果采用非证书模式,则无需赋值上面的三个证书路径,改为赋值如下的支付宝公钥字符串即可
$options->alipayPublicKey = '';
//可设置异步通知接收服务地址(可选)
$options->notifyUrl =$return_url;
//可设置AES密钥,调用AES加解密相关接口时需要(可选)
$options->encryptKey = "";
Factory::setOptions( $options );
try {
//2. 发起API调用(以支付能力下的统一收单交易创建接口为例) $subject, $outTradeNo, $totalAmount, $buyerId
$result = Factory::payment()->App()->pay("充值",$order_sn , 0.01 );
$responseChecker = new ResponseChecker();
//3. 处理响应或异常
if ($responseChecker->success($result)) {
$this->success( 'ok' , $result->body );
echo "调用成功". PHP_EOL;
} else {
echo "调用失败,原因:". $result->msg.",".$result->subMsg.PHP_EOL;
}
$this->success( 'ok' , $result );
} catch (Exception $e) {
echo "调用失败,". $e->getMessage(). PHP_EOL;
$this->error( $e->getMessage() );
}