2023-11-15 12:02:33 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace app\api\controller;
|
|
|
|
|
2023-11-21 16:06:27 +08:00
|
|
|
use app\common\enum\PayOrderEnum;
|
2023-11-15 12:02:33 +08:00
|
|
|
use app\common\enum\user\UserTerminalEnum;
|
|
|
|
use app\common\logic\PaymentLogic;
|
|
|
|
use app\common\logic\WechatPayServiceMerchantPaymentLogic;
|
2023-11-21 16:06:27 +08:00
|
|
|
use app\common\model\pay\PayOrder;
|
|
|
|
use app\common\model\pay\RefundOrder;
|
2023-11-15 12:02:33 +08:00
|
|
|
use app\common\service\pay\WeChatPayMerchantService;
|
|
|
|
use app\common\service\pay\WeChatPayService;
|
2023-11-20 12:07:06 +08:00
|
|
|
use think\Log;
|
2023-11-15 12:02:33 +08:00
|
|
|
|
|
|
|
class WechatPayServiceMerchantPayController extends BaseApiController
|
|
|
|
{
|
|
|
|
|
2023-11-21 10:57:38 +08:00
|
|
|
public array $notNeedLogin = ['appPrePay', 'notifyApp', 'refund', 'refundNotify'];
|
2023-11-15 12:02:33 +08:00
|
|
|
/**
|
|
|
|
* 微信服务商APP支付
|
|
|
|
*/
|
|
|
|
public function appPrePay()
|
|
|
|
{
|
|
|
|
$params = $this->request->param();
|
2023-11-20 12:07:06 +08:00
|
|
|
\think\facade\Log::info(['微信服务商APP支付接收参数', $params]);
|
2023-11-15 12:02:33 +08:00
|
|
|
//订单信息
|
|
|
|
$order = WechatPayServiceMerchantPaymentLogic::createPayOrder($params);
|
|
|
|
if (false === $order) {
|
|
|
|
return $this->fail('支付订单创建失败', $params);
|
|
|
|
}
|
|
|
|
//支付流程
|
2023-11-16 14:04:58 +08:00
|
|
|
$result = (new WeChatPayMerchantService())->wechatPayServiceMerchantAppPay($order);
|
2023-11-20 12:07:06 +08:00
|
|
|
|
2023-11-15 12:02:33 +08:00
|
|
|
if (false === $result) {
|
2023-11-16 14:04:58 +08:00
|
|
|
return $this->fail((new WeChatPayMerchantService())->getError(), $params);
|
2023-11-15 12:02:33 +08:00
|
|
|
}
|
|
|
|
return $this->success('', $result);
|
|
|
|
}
|
|
|
|
|
2023-11-16 17:33:16 +08:00
|
|
|
/**
|
|
|
|
* 申请退款
|
2023-11-21 16:06:27 +08:00
|
|
|
* 先查询有没有支付订单
|
|
|
|
* 在查询微信支付有没有支付记录
|
|
|
|
* 最后再发起退款申请
|
2023-11-16 17:33:16 +08:00
|
|
|
*/
|
|
|
|
public function refund()
|
|
|
|
{
|
2023-11-21 16:06:27 +08:00
|
|
|
$params = $this->request->param(); // business_order_no business_callback_url
|
|
|
|
|
|
|
|
$payOrder = PayOrder::where(['business_order_no' => $params['business_order_no'], 'pay_status'=>PayOrderEnum::PAY_STATUS_ISPAID])->find();
|
|
|
|
if (empty($payOrder)) {
|
|
|
|
return $this->fail('系统未找到该订单的支付记录');
|
|
|
|
}
|
|
|
|
|
|
|
|
$wechatPayOrder = (new WeChatPayMerchantService())->queryPayOrder($payOrder);
|
|
|
|
if ($wechatPayOrder['trade_state'] === 'REFUND') {
|
|
|
|
return $this->fail('该订单已申请退款,请勿重复申请');
|
|
|
|
}
|
|
|
|
if ($wechatPayOrder['trade_state'] !== 'SUCCESS') {
|
|
|
|
return $this->fail('交易平台查询到该笔订单未支付成功,无法发起退款');
|
|
|
|
}
|
2023-11-16 17:33:16 +08:00
|
|
|
$refundOrder = WechatPayServiceMerchantPaymentLogic::createRefundOrder($params);
|
2023-11-21 16:06:27 +08:00
|
|
|
|
2023-11-16 17:33:16 +08:00
|
|
|
if (false === $refundOrder) {
|
|
|
|
return $this->fail('退款订单创建失败', $params);
|
|
|
|
}
|
2023-11-21 16:06:27 +08:00
|
|
|
// 发起退款申请
|
2023-11-16 17:33:16 +08:00
|
|
|
$result = (new WeChatPayMerchantService())->refund($refundOrder);
|
|
|
|
if (false === $result) {
|
|
|
|
return $this->fail((new WeChatPayMerchantService())->getError(), $params);
|
|
|
|
}
|
2023-11-21 16:06:27 +08:00
|
|
|
return $this->success('成功', $result);
|
2023-11-16 17:33:16 +08:00
|
|
|
}
|
|
|
|
|
2023-11-15 12:02:33 +08:00
|
|
|
/**
|
|
|
|
* @notes app支付回调
|
|
|
|
* @return \Psr\Http\Message\ResponseInterface
|
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
|
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
|
|
|
|
* @throws \ReflectionException
|
|
|
|
* @throws \Throwable
|
|
|
|
* @date 2023/2/28 14:21
|
|
|
|
*/
|
|
|
|
public function notifyApp()
|
|
|
|
{
|
|
|
|
return (new WeChatPayMerchantService())->notify();
|
|
|
|
}
|
2023-11-16 17:33:16 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 退款回调
|
|
|
|
*/
|
|
|
|
public function refundNotify()
|
|
|
|
{
|
|
|
|
return (new WeChatPayMerchantService())->refundNotify();
|
|
|
|
}
|
2023-11-15 12:02:33 +08:00
|
|
|
}
|