PHP code example of easyalipay / easyalipay
1. Go to this page and download the library: Download easyalipay/easyalipay 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/ */
easyalipay / easyalipay example snippets
use EasyAlipay\Builder;
use EasyAlipay\Crypto\Rsa;
//应用app_id
$appId = '2014072300007148';
//商户RSA私钥,入参是'从官方工具获取到的BASE64字符串'
$privateKey = Rsa::fromPkcs1('MIIEpAIBAAKCAQEApdXuft3as2x...');
// 以上是下列代码的语法糖,格式为 'private.pkcs1://' + '从官方工具获取到的字符串'
// $privateKey = Rsa::from('private.pkcs1://MIIEpAIBAAKCAQEApdXuft3as2x...');
// 也支持以下方式,须保证`private_key.pem`为完整X509格式
// $privateKey = Rsa::from('file:///your/openapi/private_key.pem');
//支付宝RSA公钥,入参是'从官方工具获取到的BASE64字符串'
$publicKey = Rsa::fromSpki('MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCg...');
// 以上是下列代码的语法糖,格式为 'public.spki://' + '从官方工具获取到的字符串'
// $publicKey = Rsa::from('public.spki://MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCg...', Rsa::KEY_TYPE_PUBLIC);
// 也支持以下方式,须保证`public_key.pem`为完整X509格式
// $publicKey = Rsa::from('file:///the/alipay/public_key.pem', Rsa::KEY_TYPE_PUBLIC);
//如果是公钥证书模式,可以在工厂方法内传入 `$appCertSn` 及 `$alipayRootCertSn`
// $appCertFilePath = '/my/cert/app_cert.crt';
// $appCertSn = \EasyAlipay\Helpers::sn($appCertFilePath);
// $alipayRootCertFilePath = '/alipay/cert/alipayRootCert.crt';
// $alipayRootCertSn = \EasyAlipay\Helpers::sn($alipayRootCertFilePath);
// 工厂方法构造一个实例
$instance = Builder::factory([
'privateKey' => $privateKey,
'publicKey' => $publicKey,
'params' => [
'app_id' => $appId,
// 'app_auth_token' => $appAuthToken,
// 'app_cert_sn' => $appCertSn,
// 'alipay_root_cert_sn' => $alipayRootCertSn,
],
]);
use GuzzleHttp\Utils;
use GuzzleHttp\Exception\RequestException;
try {
$res = $instance
->alipay->trade->query
->get(['content' => [
'out_trade_no' => '20150320010101001',
]]);
echo $res->getBody(), PHP_EOL;
} catch (RequestException $e) {
// 进行错误处理
if ($e->hasResponse()) {
$r = $e->getResponse();
echo $r->getStatusCode() . ' ' . $r->getReasonPhrase(), PHP_EOL;
echo $r->getBody(), PHP_EOL, PHP_EOL, PHP_EOL;
}
} catch (\Throwable $e) {
// 进行错误处理
echo $e->getMessage(), PHP_EOL;
echo $e->getTraceAsString(), PHP_EOL;
}
use GuzzleHttp\Utils;
use GuzzleHttp\Exception\RequestException;
use Psr\Http\Message\ResponseInterface;
$res = $instance
->alipay->trade->pay
->postAsync(['content' => [
'out_trade_no' => '20150320010101001',
'scene' => 'bar_code',
'auth_code' => '28763443825664394',
'product_code' => 'FACE_TO_FACE_PAYMENT',
'subject' => 'Iphone6 16G',
'total_amount' => '88.88',
]])
->then(static function(ResponseInterface $response) {
// 正常逻辑回调处理
return Utils::jsonDecode((string) $response->getBody(), true);
})
->otherwise(static function($e) {
// 异常错误处理
echo $e->getMessage(), PHP_EOL;
if ($e instanceof RequestException && $e->hasResponse()) {
$r = $e->getResponse();
echo $r->getStatusCode() . ' ' . $r->getReasonPhrase(), PHP_EOL;
echo $r->getBody(), PHP_EOL, PHP_EOL, PHP_EOL;
}
echo $e->getTraceAsString(), PHP_EOL;
})
->wait();
print_r($res);
use GuzzleHttp\Utils;
use GuzzleHttp\Exception\RequestException;
use Psr\Http\Message\ResponseInterface;
$res = $instance
->Alipay->Trade->Precreate
->postAsync([
'out_trade_no' => '20150320010101001',
'subject' => 'Iphone6 16G',
'total_amount' => '88.88',
], ['query' => [
'notify_url' => 'http://api.test.alipay.net/atinterface/receive_notify.htm'
]])
->then(static function(ResponseInterface $response) {
// 正常逻辑回调处理
return Utils::jsonDecode((string) $response->getBody(), true);
})
->otherwise(static function($e) {
// 异常错误处理
})
->wait();
print_r($res);
use Psr\Http\Message\ResponseInterface;
$res = $instance
->chain('alipay.trade.wap.pay')
->postAsync([
'subject' => '商品名称',
'out_trade_no' => '22',
'total_amount' => '0.01',
'product_code' => 'FAST_INSTANT_TRADE_PAY',
'quit_url' => 'https://forum.alipay.com/mini-app/post/15501011',
], ['pager' => true])
->then(static function(ResponseInterface $response) {
// 正常逻辑回调处理
return (string) $response->getBody();
})
->otherwise(static function($e) {
// 异常错误处理
})
->wait();
print_r($res);
use GuzzleHttp\Utils;
use GuzzleHttp\Exception\RequestException;
try {
$res = $instance['alipay.trade.page.pay']
->post(['content' => [
'subject' => '商品名称',
'out_trade_no' => '22',
'total_amount' => '0.01',
'product_code' => 'FAST_INSTANT_TRADE_PAY',
], 'pager' => true]);
echo $resp->getBody(), PHP_EOL;
} catch (RequestException $e) {
// 进行错误处理
} catch (\Throwable $e) {
// 异常错误处理
}
use GuzzleHttp\Utils;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Psr\MultipartStream;
use Psr\Http\Message\ResponseInterface;
$media = new MultipartStream([
'name' => 'image_content',
'contents' => 'file:///path/for/uploading.jpg',
]);
$res = $instance
->chain('alipay.offline.material.image.upload')
->postAsync([
'body' => $media,
])
->then(static function(ResponseInterface $response) {
// 正常逻辑回调处理
return Utils::jsonDecode((string) $response->getBody(), true);
})
->otherwise(static function($e) {
// 异常错误处理
})
->wait();
print_r($res);
use EasyAlipay\Crypto\AesCbc;
use GuzzleHttp\Utils;
use Psr\Http\Message\ResponseInterface;
$aesCipherKey = '';
$res = $instance
->chain('some.method.response.by.aes.encrypted')
->postAsync([])
->then(static function(ResponseInterface $response) use ($aesCipherKey) {
$json = Utils::jsonDecode((string) $response->getBody());
return AesCbc::decrypt((string) $json->response, $aesCipherKey);
})
->wait();
print_r($res);