You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

82 lines
2.8 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace App\Libs;
use Alipay\EasySDK\Kernel\Factory;
use Alipay\EasySDK\Kernel\Util\ResponseChecker;
use Alipay\EasySDK\Kernel\Config;
use App\Events\RechargeSucceed;
use App\Models\Recharge;
use Illuminate\Support\Facades\Log;
class AlipayF2F
{
public function pay(Recharge $recharge)
{
try {
$auth_code = request()->auth_code;
$config = $this->getOptions();
$result = Factory::setOptions($config)::payment()
->faceToFace()
->pay("充值{$recharge->money}", $recharge->serial, $recharge->money, $auth_code);
$responseChecker = new ResponseChecker();
//处理响应或异常
if ($responseChecker->success($result)) {
$transaction_id = "sdahf";
Log::info("支付成功:",(array)$result);
$update = [
"paid_at" => date("Y-m-d H:i:s"),
"payment_serial" => $transaction_id
];
$recharge->update($update);
//充值成功后处理
event(new RechargeSucceed($recharge));
return [
"status" => true
];
} else {
Log::info("支付宝错误:" . $result->code . "" . $result->msg . "" . $result->subMsg);
return [
"status" => false,
"code" => $result->code,
"msg" => $result->msg . "" . $result->subMsg
];
}
} catch (\Exception $exception) {
return [
"status" => false,
"code" => $exception->getCode(),
"msg" => $exception->getMessage()
];
}
}
public function getOptions()
{
$options = new Config();
$options->protocol = 'https';
$options->gatewayHost = 'openapi.alipay.com';
$options->signType = 'RSA2';
// 为避免私钥随源码泄露,推荐从文件中读取私钥字符串而不是写入源码中
$options->merchantPrivateKey = env("ALI_APP_PRIVATE_KEY");
$options->appId = env("ALI_APP_ID");
//$options->alipayCertPath = env("ALI_CERT_ALIPAY");
//$options->alipayRootCertPath = env("ALI_CERT_ROOT");
//$options->merchantCertPath = env("ALI_CERT_APP");
//注:如果采用非证书模式,则无需赋值上面的三个证书路径,改为赋值如下的支付宝公钥字符串即可
$options->alipayPublicKey = env("ALI_ALIPAY_KEY");
//可设置异步通知接收服务地址(可选)
$options->notifyUrl = "";
//可设置AES密钥调用AES加解密相关接口时需要可选
$options->encryptKey = "";
return $options;
}
}