87 lines
2.7 KiB
PHP
Executable File
87 lines
2.7 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 Alipay
|
||
{
|
||
public function __construct()
|
||
{
|
||
$options = new Config();
|
||
$options->protocol = config('pay_ali.protocol');
|
||
$options->gatewayHost = config('pay_ali.gateway');
|
||
$options->signType = config('pay_ali.sign_type');
|
||
|
||
$options->appId = config('pay_ali.app_id'); // 为避免私钥随源码泄露,推荐从文件中读取私钥字符串而不是写入源码中
|
||
|
||
$options->merchantPrivateKey = config('pay_ali.merchant_private_key');
|
||
|
||
if (config('pay_ali.alipay_public_key')) {
|
||
// 公钥模式
|
||
$options->alipayPublicKey = config('pay_ali.alipay_public_key');
|
||
} else {
|
||
// 证书模式
|
||
$options->merchantCertPath = config('pay_ali.app_cert_path');
|
||
$options->alipayCertPath = config('pay_ali.ali_cert_path');
|
||
$options->alipayRootCertPath = config('pay_ali.ali_root_cert_path');
|
||
}
|
||
|
||
//可设置异步通知接收服务地址(可选)
|
||
if (config('pay_ali.notify_url')) {
|
||
$options->notifyUrl = config('pay_ali.notify_url');
|
||
}
|
||
|
||
//可设置AES密钥,调用AES加解密相关接口时需要(可选)
|
||
if (config('pay_ali.encrypt_key')) {
|
||
$options->encryptKey = config('pay_ali.encrypt_key');
|
||
}
|
||
Factory::setOptions($options);
|
||
}
|
||
|
||
public function pay($outTradeNo, $money, $subject)
|
||
{
|
||
$returnUrl = config('pay_ali.return_url');
|
||
$result = Factory::payment()
|
||
->page()
|
||
->pay($subject, $outTradeNo, $money, $returnUrl);
|
||
return $result->body;
|
||
}
|
||
|
||
/**
|
||
* 当面付
|
||
* @param $orderSn
|
||
* @param $money
|
||
* @param $subject
|
||
* @param $authToken
|
||
* @return mixed
|
||
* @throws PayException
|
||
*/
|
||
public function faceToFaceTradePreCreate($orderSn, $money, $subject)
|
||
{
|
||
$face = Factory::payment()->faceToFace();
|
||
$money = number_format($money,2, '.', '');
|
||
$result = $face->preCreate($subject,$orderSn,$money);
|
||
if ($result->code != 10000) {
|
||
Log::error('Alipay 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'];
|
||
}
|
||
}
|