2023-11-15 12:02:33 +08:00
|
|
|
|
<?php
|
|
|
|
|
// +----------------------------------------------------------------------
|
|
|
|
|
// | likeadmin快速开发前后端分离管理后台(PHP版)
|
|
|
|
|
// +----------------------------------------------------------------------
|
|
|
|
|
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
|
|
|
|
|
// | 开源版本可自由商用,可去除界面版权logo
|
|
|
|
|
// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
|
|
|
|
|
// | github下载:https://github.com/likeshop-github/likeadmin
|
|
|
|
|
// | 访问官网:https://www.likeadmin.cn
|
|
|
|
|
// | likeadmin团队 版权所有 拥有最终解释权
|
|
|
|
|
// +----------------------------------------------------------------------
|
|
|
|
|
// | author: likeadminTeam
|
|
|
|
|
// +----------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace app\common\service\pay;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use app\common\enum\PayEnum;
|
|
|
|
|
use app\common\enum\PayOrderEnum;
|
2023-11-21 10:44:21 +08:00
|
|
|
|
use app\common\logic\PaymentLogic;
|
2023-11-21 17:19:34 +08:00
|
|
|
|
use app\common\logic\RefundLogic;
|
2023-11-16 14:04:58 +08:00
|
|
|
|
use app\common\model\pay\PayConfig;
|
2023-11-16 17:33:16 +08:00
|
|
|
|
use app\common\model\pay\PayOrder;
|
2023-11-21 17:19:34 +08:00
|
|
|
|
use app\common\model\pay\RefundOrder;
|
2023-11-15 12:02:33 +08:00
|
|
|
|
use app\common\model\user\UserAuth;
|
|
|
|
|
use app\common\service\wechat\WeChatPayMerchantConfigService;
|
|
|
|
|
use EasyWeChat\Pay\Application;
|
|
|
|
|
use EasyWeChat\Pay\Message;
|
2023-11-21 10:44:21 +08:00
|
|
|
|
use think\Exception;
|
2023-11-15 12:02:33 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 微信支付
|
|
|
|
|
* Class WeChatPayService
|
|
|
|
|
* @package app\common\server
|
|
|
|
|
*/
|
|
|
|
|
class WeChatPayMerchantService extends BasePayService
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* 授权信息
|
|
|
|
|
* @var UserAuth|array|\think\Model
|
|
|
|
|
*/
|
|
|
|
|
protected $auth;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 微信配置
|
|
|
|
|
* @var
|
|
|
|
|
*/
|
|
|
|
|
protected $config;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* easyWeChat实例
|
|
|
|
|
* @var
|
|
|
|
|
*/
|
|
|
|
|
protected $app;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 当前使用客户端
|
|
|
|
|
* @var
|
|
|
|
|
*/
|
|
|
|
|
protected $terminal;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 初始化微信支付配置
|
|
|
|
|
*/
|
|
|
|
|
public function __construct()
|
|
|
|
|
{
|
|
|
|
|
$this->config = WeChatPayMerchantConfigService::getPayConfig();
|
|
|
|
|
$this->app = new Application($this->config);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function wechatPayServiceMerchantAppPay($order)
|
|
|
|
|
{
|
2023-11-16 17:33:16 +08:00
|
|
|
|
try {
|
2023-11-16 14:04:58 +08:00
|
|
|
|
$pay = PayConfig::where(['pay_way' => PayEnum::WECHAT_PAY])->findOrEmpty()->toArray();
|
2023-11-16 17:33:16 +08:00
|
|
|
|
$response = $this->app->getClient()->postJson('/v3/pay/partner/transactions/app', [
|
|
|
|
|
'sp_appid' => $pay['config']['app_id'],
|
|
|
|
|
'sp_mchid' => $pay['config']['mch_id'],
|
|
|
|
|
'sub_mchid' => $order['collection_account'],
|
2023-11-15 12:02:33 +08:00
|
|
|
|
'description' => PayOrderEnum::getPayOrderTypeDesc($order['order_type']),
|
|
|
|
|
'out_trade_no' => $order['order_no'],
|
|
|
|
|
'notify_url' => $this->config['notify_url'],
|
2023-11-16 17:33:16 +08:00
|
|
|
|
'amount' => [
|
2023-11-20 14:21:20 +08:00
|
|
|
|
'total' => intval($order['total_fee']),
|
2023-11-16 17:33:16 +08:00
|
|
|
|
'currency' => 'CNY'
|
|
|
|
|
],
|
2023-11-16 14:04:58 +08:00
|
|
|
|
'attach' => $order['order_no'],
|
2023-11-15 12:02:33 +08:00
|
|
|
|
]);
|
2023-11-21 10:52:59 +08:00
|
|
|
|
\think\facade\Log::info(['微信服务商APP支付api-config', $this->config]);
|
2023-11-16 17:33:16 +08:00
|
|
|
|
$result = $response->toArray(false);
|
2023-11-20 12:07:06 +08:00
|
|
|
|
\think\facade\Log::info(['微信服务商APP支付api-微信支付返回值', $result]);
|
2023-11-15 12:02:33 +08:00
|
|
|
|
$this->checkResultFail($result);
|
2023-11-16 17:33:16 +08:00
|
|
|
|
$re = $this->app->getUtils()->buildAppConfig($result['prepay_id'], $pay['config']['app_id']);
|
|
|
|
|
$re['partnerid'] = $order['collection_account'];
|
2023-11-15 12:02:33 +08:00
|
|
|
|
return $re;
|
2023-11-16 17:33:16 +08:00
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
$this->setError($e->getMessage());
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2023-11-15 12:02:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @notes 退款
|
|
|
|
|
* @param array $refundData
|
|
|
|
|
* @return mixed
|
|
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
|
|
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
|
|
|
|
|
* @author 段誉
|
|
|
|
|
* @date 2023/2/28 16:53
|
|
|
|
|
*/
|
2023-11-16 17:33:16 +08:00
|
|
|
|
public function refund($refundOrder)
|
2023-11-15 12:02:33 +08:00
|
|
|
|
{
|
2023-11-16 17:33:16 +08:00
|
|
|
|
$payOrder = PayOrder::where(['order_no' => $refundOrder['order_sn'], 'pay_status'=>PayOrderEnum::PAY_STATUS_ISPAID])->find();
|
2023-11-21 16:06:27 +08:00
|
|
|
|
|
2023-11-16 17:33:16 +08:00
|
|
|
|
$response = $this->app->getClient()->postJson('/v3/refund/domestic/refunds', [
|
|
|
|
|
'sub_mchid' => $payOrder['collection_account'],
|
2023-11-21 16:06:27 +08:00
|
|
|
|
'out_trade_no' => $payOrder['order_no'],
|
|
|
|
|
'out_refund_no' => $refundOrder['refund_sn'],
|
|
|
|
|
'notify_url' => (string)url('wechat_pay_service_merchant_pay/refundNotify', [], false, true),
|
2023-11-15 12:02:33 +08:00
|
|
|
|
'amount' => [
|
2023-11-16 17:33:16 +08:00
|
|
|
|
'refund' => $refundOrder['refund_amount'],
|
|
|
|
|
'total' => $refundOrder['refund_amount'],
|
2023-11-15 12:02:33 +08:00
|
|
|
|
'currency' => 'CNY',
|
|
|
|
|
]
|
|
|
|
|
]);
|
|
|
|
|
$result = $response->toArray(false);
|
|
|
|
|
$this->checkResultFail($result);
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-21 16:06:27 +08:00
|
|
|
|
/**
|
|
|
|
|
* @notes 查询退款
|
|
|
|
|
* @param $refundSn
|
|
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
|
|
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
|
|
|
|
|
* @author 段誉
|
|
|
|
|
* @date 2023/3/1 11:16
|
|
|
|
|
*/
|
|
|
|
|
public function queryPayOrder($order)
|
|
|
|
|
{
|
|
|
|
|
$response = $this->app->getClient()->get("/v3/pay/partner/transactions/out-trade-no/{$order['order_no']}", [
|
|
|
|
|
'query' => [
|
|
|
|
|
'sp_mchid' => $this->config['mch_id'],
|
|
|
|
|
'sub_mchid' => $order['collection_account']
|
|
|
|
|
]
|
|
|
|
|
]);
|
|
|
|
|
$result = $response->toArray(false);
|
|
|
|
|
$this->checkResultFail($result);
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
2023-11-15 12:02:33 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @notes 查询退款
|
|
|
|
|
* @param $refundSn
|
|
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
|
|
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
|
|
|
|
|
* @author 段誉
|
|
|
|
|
* @date 2023/3/1 11:16
|
|
|
|
|
*/
|
|
|
|
|
public function queryRefund($refundSn)
|
|
|
|
|
{
|
|
|
|
|
$response = $this->app->getClient()->get("v3/refund/domestic/refunds/{$refundSn}");
|
2023-11-21 16:06:27 +08:00
|
|
|
|
$result = $response->toArray(false);
|
|
|
|
|
$this->checkResultFail($result);
|
|
|
|
|
return $result;
|
2023-11-15 12:02:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @notes 捕获错误
|
|
|
|
|
* @param $result
|
|
|
|
|
* @throws \Exception
|
|
|
|
|
* @author 段誉
|
|
|
|
|
* @date 2023/2/28 12:09
|
|
|
|
|
*/
|
|
|
|
|
public function checkResultFail($result)
|
|
|
|
|
{
|
|
|
|
|
if (!empty($result['code']) || !empty($result['message'])) {
|
|
|
|
|
throw new \Exception('微信:'. $result['code'] . '-' . $result['message']);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @notes 预支付配置
|
|
|
|
|
* @param $prepayId
|
|
|
|
|
* @param $appId
|
|
|
|
|
* @return mixed[]
|
|
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
|
|
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
|
|
|
|
|
* @author 段誉
|
|
|
|
|
* @date 2023/2/28 17:38
|
|
|
|
|
*/
|
|
|
|
|
public function getPrepayConfig($prepayId, $appId)
|
|
|
|
|
{
|
|
|
|
|
return $this->app->getUtils()->buildBridgeConfig($prepayId, $appId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @notes 支付回调
|
|
|
|
|
* @return \Psr\Http\Message\ResponseInterface
|
|
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
|
|
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
|
|
|
|
|
* @throws \ReflectionException
|
|
|
|
|
* @throws \Throwable
|
|
|
|
|
* @author 段誉
|
|
|
|
|
* @date 2023/2/28 14:20
|
|
|
|
|
*/
|
|
|
|
|
public function notify()
|
|
|
|
|
{
|
|
|
|
|
$server = $this->app->getServer();
|
|
|
|
|
// 支付通知
|
|
|
|
|
$server->handlePaid(function (Message $message) {
|
2023-11-21 11:07:13 +08:00
|
|
|
|
\think\facade\Log::info(['支付回调信息', $message]);
|
2023-11-15 12:02:33 +08:00
|
|
|
|
if ($message['trade_state'] === 'SUCCESS') {
|
2023-11-21 11:18:03 +08:00
|
|
|
|
return $this->handlePaid($message);
|
2023-11-21 10:44:21 +08:00
|
|
|
|
} else {
|
2023-11-21 16:06:27 +08:00
|
|
|
|
\think\facade\Log::info('支付失败', (array)$message);
|
2023-11-15 12:02:33 +08:00
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
});
|
2023-11-16 17:33:16 +08:00
|
|
|
|
return $server->serve();
|
|
|
|
|
}
|
2023-11-15 12:02:33 +08:00
|
|
|
|
|
2023-11-21 10:44:21 +08:00
|
|
|
|
/**
|
|
|
|
|
* @return void
|
|
|
|
|
* 修改支付状态,回写回调信息。
|
|
|
|
|
* 通知业务系统
|
|
|
|
|
*/
|
|
|
|
|
public function handlePaid($message)
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
$transaction_id = $message['transaction_id'];
|
|
|
|
|
$out_trade_no = $message['out_trade_no'];
|
|
|
|
|
$order = PayOrder::where('order_no', $out_trade_no)->find();
|
|
|
|
|
if (empty($order)) {
|
|
|
|
|
throw new Exception('回调订单不存在');
|
|
|
|
|
}
|
2023-11-21 17:22:45 +08:00
|
|
|
|
// 防止重复操作
|
2023-11-21 11:18:03 +08:00
|
|
|
|
if ($order['pay_status'] == PayOrderEnum::PAY_STATUS_ISPAID && !empty($order['transaction_id'])) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2023-11-21 10:44:21 +08:00
|
|
|
|
$order->transaction_id = $transaction_id;
|
|
|
|
|
$order->pay_status = PayOrderEnum::PAY_STATUS_ISPAID;
|
|
|
|
|
$order->finish_time = time();
|
|
|
|
|
$order->save();
|
|
|
|
|
|
|
|
|
|
// 回调业务系统
|
|
|
|
|
PaymentLogic::callBusiness($order['business_callback_url'], ['out_trade_no'=>$order['business_order_no'], 'transaction_id'=>$transaction_id, 'pay_status'=>1]);
|
|
|
|
|
} catch (\Exception $e) {
|
2023-11-21 11:07:13 +08:00
|
|
|
|
\think\facade\Log::error(['支付回调信息处理失败', $e->getMessage(),$message]);
|
2023-11-21 10:44:21 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-21 16:06:27 +08:00
|
|
|
|
/**
|
|
|
|
|
* @return \Psr\Http\Message\ResponseInterface
|
|
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
|
|
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
|
|
|
|
|
* @throws \ReflectionException
|
|
|
|
|
* @throws \Throwable
|
|
|
|
|
* 修改状态,回写信息
|
|
|
|
|
* 通知业务系统
|
|
|
|
|
*/
|
2023-11-16 17:33:16 +08:00
|
|
|
|
public function refundNotify()
|
|
|
|
|
{
|
|
|
|
|
$server = $this->app->getServer();
|
2023-11-15 12:02:33 +08:00
|
|
|
|
// 退款通知
|
|
|
|
|
$server->handleRefunded(function (Message $message) {
|
2023-11-21 11:07:13 +08:00
|
|
|
|
\think\facade\Log::info(['退款回调信息', $message]);
|
2023-11-21 17:37:10 +08:00
|
|
|
|
\think\facade\Log::info(['退款回调信息', $message['refund_status']]);
|
2023-11-21 17:19:34 +08:00
|
|
|
|
if($message['refund_status'] === 'SUCCESS') {
|
2023-11-21 17:37:10 +08:00
|
|
|
|
$refundOrder = RefundOrder::where(['order_sn'=>$message['out_trade_no'], 'refund_sn'=>$message['out_refund_no']])->find();
|
2023-11-21 17:22:45 +08:00
|
|
|
|
// 防止重复操作
|
2023-11-21 17:29:04 +08:00
|
|
|
|
if ($refundOrder['refund_status'] == 1 && !empty($refundOrder['refund_id'])) {
|
2023-11-21 17:22:45 +08:00
|
|
|
|
return true;
|
|
|
|
|
}
|
2023-11-21 17:19:34 +08:00
|
|
|
|
$refundOrder->refund_status = 1;
|
|
|
|
|
$refundOrder->refund_id = $message['refund_id'];
|
|
|
|
|
$refundOrder->finish_time = time();
|
|
|
|
|
$refundOrder->save();
|
|
|
|
|
|
|
|
|
|
// 通知业务系统
|
|
|
|
|
RefundLogic::callBusiness($refundOrder['business_callback_url'], ['out_trade_no'=>$refundOrder['business_order_sn'], 'transaction_id'=>$message['refund_id'], 'refund_status'=>1]);
|
|
|
|
|
}
|
2023-11-15 12:02:33 +08:00
|
|
|
|
return true;
|
|
|
|
|
});
|
|
|
|
|
return $server->serve();
|
|
|
|
|
}
|
|
|
|
|
}
|