|
|
<?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\AlipayAccount;
|
|
|
use App\Models\Recharge;
|
|
|
use App\Models\Refund;
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
|
|
class AlipayF2F
|
|
|
{
|
|
|
public function pay(Recharge $recharge)
|
|
|
{
|
|
|
try {
|
|
|
$auth_code = request()->auth_code;
|
|
|
$project = $recharge->project;
|
|
|
$alipayAccount = $project->alipayAccount;
|
|
|
$config = $this->getOptions($alipayAccount);
|
|
|
|
|
|
$result = Factory::setOptions($config)::payment()
|
|
|
->faceToFace()
|
|
|
->pay("充值{$recharge->money}元" . ($recharge->project ? "-" . $recharge->project->name : ""), $recharge->serial, $recharge->money, $auth_code);
|
|
|
$responseChecker = new ResponseChecker();
|
|
|
//处理响应或异常
|
|
|
if ($responseChecker->success($result)) {
|
|
|
$transaction_id = $result->tradeNo;
|
|
|
$update = [
|
|
|
"paid_at" => date("Y-m-d H:i:s"),
|
|
|
"payment_serial" => $transaction_id
|
|
|
];
|
|
|
$recharge->update($update);
|
|
|
//充值成功后处理
|
|
|
event(new RechargeSucceed($recharge));
|
|
|
return [
|
|
|
"status" => true
|
|
|
];
|
|
|
} elseif ($result->code == "10003") {
|
|
|
Log::info("支付宝状态:" . $result->code . ";" . $result->msg . ";" . $result->subMsg);
|
|
|
return [
|
|
|
"status" => false,
|
|
|
"code" => $result->code,
|
|
|
"msg" => "正在支付中,请稍后查询支付到账状态"
|
|
|
];
|
|
|
} 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 manualQuery(Recharge $recharge)
|
|
|
{
|
|
|
$merchantId = $recharge->merchant_id;
|
|
|
$alipayAccount = AlipayAccount::where('appid', $merchantId)->withTrashed()->first();
|
|
|
$config = $this->getOptions($alipayAccount);
|
|
|
$query_result = Factory::setOptions($config)::payment()
|
|
|
->common()
|
|
|
->query($recharge->serial);
|
|
|
|
|
|
$responseChecker = new ResponseChecker();
|
|
|
//处理响应或异常
|
|
|
if ($responseChecker->success($query_result)) {
|
|
|
if ($query_result->tradeStatus !== "TRADE_SUCCESS") {
|
|
|
return false;
|
|
|
}
|
|
|
if (!$recharge->paid_at) {
|
|
|
$transaction_id = $query_result->tradeNo;
|
|
|
$datetime = $query_result->sendPayDate;
|
|
|
$update = [
|
|
|
"paid_at" => $datetime,
|
|
|
"payment_serial" => $transaction_id
|
|
|
];
|
|
|
$recharge->update($update);
|
|
|
//充值成功后处理
|
|
|
event(new RechargeSucceed($recharge));
|
|
|
}
|
|
|
return true;
|
|
|
} else {
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public function refund(Refund $refund)
|
|
|
{
|
|
|
try {
|
|
|
$alipayAccount = AlipayAccount::where('appid', $refund->merchant_id)->withTrashed()->first();
|
|
|
$config = $this->getOptions($alipayAccount);
|
|
|
|
|
|
$result = Factory::setOptions($config)::payment()
|
|
|
->common()
|
|
|
->refund($refund->relatedRecharge->serial, $refund->money, $refund->serial);
|
|
|
$responseChecker = new ResponseChecker();
|
|
|
//处理响应或异常
|
|
|
if ($responseChecker->success($result)) {
|
|
|
$transaction_id = $result->tradeNo;
|
|
|
$update = [
|
|
|
"paid_at" => date("Y-m-d H:i:s"),
|
|
|
"payment_serial" => $transaction_id
|
|
|
];
|
|
|
$refund->update($update);
|
|
|
} else {
|
|
|
Log::info("退款(refund)ID:".$refund->id.",参数1:".$refund->relatedRecharge->serial.",参数2:".$refund->money."。支付宝退款失败:" . $result->code . ";" . $result->msg . ";" . $result->subMsg);
|
|
|
$refund->increment("try_times");
|
|
|
return false;
|
|
|
}
|
|
|
} catch (\Exception $exception) {
|
|
|
Log::info("支付宝退款失败:" . $exception->getCode() . ";" . $exception->getMessage());
|
|
|
$refund->increment("try_times");
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public function getOptions(AlipayAccount $alipayAccount)
|
|
|
{
|
|
|
$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->alipayPublicKey = env("ALI_ALIPAY_KEY");
|
|
|
|
|
|
// 为避免私钥随源码泄露,推荐从文件中读取私钥字符串而不是写入源码中
|
|
|
$options->merchantPrivateKey = $alipayAccount->private_key;
|
|
|
$options->appId = $alipayAccount->appid;
|
|
|
//注:如果采用非证书模式,则无需赋值上面的三个证书路径,改为赋值如下的支付宝公钥字符串即可
|
|
|
$options->alipayPublicKey = $alipayAccount->alipay_key;
|
|
|
|
|
|
//可设置异步通知接收服务地址(可选)
|
|
|
$options->notifyUrl = "";
|
|
|
|
|
|
//可设置AES密钥,调用AES加解密相关接口时需要(可选)
|
|
|
$options->encryptKey = "";
|
|
|
|
|
|
return $options;
|
|
|
}
|
|
|
}
|