119 lines
3.9 KiB
PHP
Executable File
119 lines
3.9 KiB
PHP
Executable File
<?php
|
||
/**
|
||
* @createtime 2022/3/14
|
||
* @author wild
|
||
* @copyright PhpStorm
|
||
*/
|
||
|
||
|
||
namespace App\Providers\Pay;
|
||
|
||
|
||
use Alipay\EasySDK\Kernel\Config;
|
||
use Alipay\EasySDK\Kernel\Factory;
|
||
use Illuminate\Support\Facades\Log;
|
||
|
||
class AgentAlipay
|
||
{
|
||
public function __construct()
|
||
{
|
||
$options = new Config();
|
||
$options->protocol = config('pay_agent_ali.protocol');
|
||
$options->gatewayHost = config('pay_agent_ali.gateway');
|
||
$options->signType = config('pay_agent_ali.sign_type');
|
||
|
||
$options->appId = config('pay_agent_ali.app_id'); // 为避免私钥随源码泄露,推荐从文件中读取私钥字符串而不是写入源码中
|
||
|
||
$options->merchantPrivateKey = config('pay_agent_ali.merchant_private_key');
|
||
|
||
if (config('pay_agent_ali.alipay_public_key')) {
|
||
// 公钥模式
|
||
$options->alipayPublicKey = config('pay_agent_ali.alipay_public_key');
|
||
} else {
|
||
// 证书模式
|
||
$options->merchantCertPath = config('pay_agent_ali.app_cert_path');
|
||
$options->alipayCertPath = config('pay_agent_ali.ali_cert_path');
|
||
$options->alipayRootCertPath = config('pay_agent_ali.ali_root_cert_path');
|
||
}
|
||
|
||
//可设置异步通知接收服务地址(可选)
|
||
if (config('pay_agent_ali.notify_url')) {
|
||
$options->notifyUrl = config('pay_agent_ali.notify_url');
|
||
}
|
||
|
||
//可设置AES密钥,调用AES加解密相关接口时需要(可选)
|
||
if (config('pay_agent_ali.encrypt_key')) {
|
||
$options->encryptKey = config('pay_agent_ali.encrypt_key');
|
||
}
|
||
Factory::setOptions($options);
|
||
}
|
||
|
||
public function pay($outTradeNo, $money, $subject)
|
||
{
|
||
$returnUrl = config('pay_agent_ali.return_url');
|
||
$result = Factory::payment()
|
||
->page()
|
||
->pay($subject, $outTradeNo, $money, $returnUrl);
|
||
return $result->body;
|
||
}
|
||
|
||
/**
|
||
* https://opendocs.alipay.com/apis/api_1/alipay.trade.fastpay.refund.query#%E5%93%8D%E5%BA%94%E7%A4%BA%E4%BE%8B
|
||
* @param $outTradeNo
|
||
* @return mixed
|
||
* @throws PayException
|
||
*/
|
||
public function tradeQuery($outTradeNo)
|
||
{
|
||
$result = Factory::payment()->common()->query($outTradeNo);
|
||
if ($result->code != 10000) {
|
||
Log::error('trade query error:'. json_encode($result));
|
||
throw new PayException($result->msg . ':' . $result->subMsg,$result->code);
|
||
}
|
||
$resp = json_decode($result->httpBody, true);
|
||
return $resp['alipay_trade_query_response'];
|
||
}
|
||
|
||
public function authToken()
|
||
{
|
||
$param = [
|
||
'grant_type' => 'authorization_code',
|
||
'app_auth_code' => 'Pa7f62411b8c642c7a2a546ede28b786',
|
||
// 'app_id' => '2021003199692625',
|
||
];
|
||
|
||
$text = [];
|
||
$res = Factory::util()->generic()->execute('alipay.open.auth.token.app', $text, $param);
|
||
}
|
||
|
||
/**
|
||
* 当面付
|
||
* @param $orderSn
|
||
* @param $money
|
||
* @param $subject
|
||
* @param $authToken
|
||
* @return mixed
|
||
* @throws PayException
|
||
*/
|
||
public function faceToFaceTradePreCreate($orderSn, $money, $subject, $authToken = null)
|
||
{
|
||
$face = Factory::payment()->faceToFace();
|
||
if ($authToken) {
|
||
$face = $face->agent($authToken);
|
||
}
|
||
$money = number_format($money,2, '.', '');
|
||
$result = $face->preCreate($subject,$orderSn,$money);
|
||
if ($result->code != 10000) {
|
||
Log::error('AgentAlipay faceToFaceTradePreCreate error:', [
|
||
'$result' => $result,
|
||
'$orderSn' => $orderSn,
|
||
'$money' => $money,
|
||
'$subject' => $subject,
|
||
]);
|
||
throw new PayException($result->msg . ':' . $result->subMsg, $result->code);
|
||
}
|
||
$resp = json_decode($result->httpBody, true);
|
||
return $resp['alipay_trade_precreate_response'];
|
||
}
|
||
}
|