|
|
<?php
namespace App\Controller\v3;
use App\Constants\v3\LogLabel;use App\Constants\v3\OrderState;use App\Constants\v3\OrderType;use App\Controller\BaseController;use App\Exception\BusinessException;use App\Model\v3\Goods;use App\Model\v3\GoodsActivity;use App\Model\v3\Order;use App\Model\v3\OrderGoods;use App\Model\v3\OrderMain;use App\Model\v3\User;use App\Service\v3\Interfaces\BadgeServiceInterface;use App\Service\v3\Interfaces\CouponRebateServiceInterface;use App\Service\v3\Interfaces\CouponServiceInterface;use App\Service\v3\Interfaces\DeviceServiceInterface;use App\Service\v3\Interfaces\FeiePrintServiceInterface;use App\Service\v3\Interfaces\FinancialRecordServiceInterface;use App\Service\v3\Interfaces\GoodsActivityServiceInterface;use App\Service\v3\Interfaces\MiniprogramServiceInterface;use App\Service\v3\Interfaces\MqttServiceInterface;use App\Service\v3\Interfaces\OrderOnlineServiceInterface;use App\Service\v3\Interfaces\OrderStatisticsServiceInterface;use App\Service\v3\Interfaces\SeparateAccountsServiceInterface;use App\Service\v3\Interfaces\UserInfoServiceInterface;use Exception;use Hyperf\DbConnection\Db;use Hyperf\Di\Annotation\Inject;use Psr\Http\Message\ResponseInterface;
/** @var Inject 注解使用 */
/** * 建行结果通知接口 */class CcbNotifyController extends BaseController{ /** * @Inject * @var OrderOnlineServiceInterface */ protected $orderOnlineService;
/** * @Inject * @var SeparateAccountsServiceInterface */ protected $separateAccountsService;
/** * @Inject * @var OrderStatisticsServiceInterface */ protected $orderStatisticsService;
/** * @Inject * @var CouponRebateServiceInterface */ protected $couponRebateService;
/** * @Inject * @var MqttServiceInterface */ protected $mqttService;
/** * @Inject * @var DeviceServiceInterface */ protected $deviceService;
/** * @Inject * @var FeiePrintServiceInterface */ protected $feiePrintService;
/** * @Inject * @var BadgeServiceInterface */ protected $badgeService;
/** * @Inject * @var MiniprogramServiceInterface */ protected $miniprogramService;
/** * @Inject * @var UserInfoServiceInterface */ protected $userInfoService;
/** * @Inject * @var CouponServiceInterface */ protected $couponService;
/** * @Inject * @var GoodsActivityServiceInterface */ protected $goodsActivityService;
/** * @Inject * @var FinancialRecordServiceInterface */ protected $financialService;
/** * 支付结果通知 */ public function payResult(): ResponseInterface { /*$formData = [ 'Main_Ordr_No' => '主订单编号', 'Py_Trn_No' => 'Py_Trn_No', 'Ordr_Amt' => '订单金额', 'Txnamt' => '支付金额', 'Pay_Time' => '支付时间', 'Ordr_Stcd' => '2成功 3失败 4失效', 'TYPE' => '非必输。Pymd_cd=‘05’返回,支付宝:ALIPAY 微信:WEIXIN 建行:CCB 银联:UNIONPAY(暂不支付)', 'Sign_Inf' => '签名参数', ];*/
$formData = $this->request->all(); try { /*if (!CcbPay::getInstance()->SHA256WithRSAVerify($formData)) { throw new BusinessException(500, '签名验证失败,收到的数据为:' . json_encode($formData)); }*/
if (!isset($formData['Ordr_Stcd']) || $formData['Ordr_Stcd'] != 2) { throw new BusinessException(500, 'Ordr_Stcd状态码异常:' . ($formData['Ordr_Stcd'] ?? 'null')); } else if (!isset($formData['Main_Ordr_No'])) { throw new BusinessException(500, '未获取到订单号Main_Ordr_No'); }
$orderMain = OrderMain::where([ 'global_order_id' => $formData['Main_Ordr_No'], 'type' => OrderType::ONLINE, 'state' => OrderState::UNPAID ])->first();
if (!$orderMain) { throw new BusinessException(500, $formData['Main_Ordr_No'] . '订单不存在!'); }
$this->orderOnlineService->doByPaid($orderMain->global_order_id); $this->separateAccountsService->orderOnlinePaid($orderMain->global_order_id);
//记录当前市场的当天外卖订单数
$this->orderStatisticsService->setForMarket($orderMain->market_id);
// 优惠券返券
$this->couponRebateService->couponRebateInTask($orderMain->global_order_id);
// 喇叭通知,兼容旧音响,MQTT+IOT
$res = $this->mqttService->speakToStore($orderMain->global_order_id); $res = $this->deviceService->pubMsgToStoreByOrderMainId($orderMain->global_order_id);
// 打印订单,自动打印
$res = $this->feiePrintService->feiePrint($orderMain->global_order_id);
// 记录badge
$orderChildIds = Order::query()->where(['order_main_id' => $orderMain->global_order_id])->pluck('store_id'); $this->badgeService->doByOrder($orderMain->user_id, $orderChildIds, $orderMain->global_order_id, OrderState::PAID);
// 公众号模板消息
$res = $this->miniprogramService->sendTemMsgForOnlineOrder($orderMain->global_order_id);
co(function () use ($orderMain) { $openid = User::query()->where(['id' => $orderMain->user_id])->value('openid'); $unionid = $this->userInfoService->getPaidUnionId($openid, ['mch_id' => config('wxpay.mch_id'), 'out_trade_no' => $orderMain->global_order_id]); if ($unionid) { User::query()->where(['id' => $orderMain->user_id, 'openid' => $openid])->update(['unionid' => $unionid]); } });
return $this->response->json(['Svc_Rsp_St' => '00', 'Rsp_Inf' => 'success']); // 00成功;01失败
} catch (Exception $exception) { $this->log->event( LogLabel::ORDER_ONLINE_PAY_NOTIFY_LOG, ['exception_fail' => $exception->getMessage()] ); return $this->response->json(['Svc_Rsp_St' => '01', 'Rsp_Inf' => $exception->getMessage()]); // 00成功;01失败
} }
/** * 退款通知 */ public function refund(): ResponseInterface { /*$formData = [ 'Ittparty_Tms' => '发起方时间戳 yyyymmddhhmmssfff', 'Ittparty_Jrnl_No' => '该笔直连交易的客户方流水号(不允许重复)', 'Py_Trn_No' => '支付流水号', 'Cust_Rfnd_Trcno' => '请求时送入客户方退款流水号,则返回送入的值,否则该字段为空。', 'Super_Refund_No' => '惠市宝生成,与退款动作唯一匹配', 'Rfnd_Amt' => '退款金额', 'Refund_Rsp_St' => '00-退款成功 01-退款失败', 'Refund_Rsp_Inf' => '退款响应信息', 'Sign_Inf' => '签名信息', ];*/
$formData = $this->request->all(); Db::beginTransaction(); try { /*if (!CcbPay::getInstance()->SHA256WithRSAVerify($formData)) { throw new BusinessException(500, '签名验证失败,收到的数据为:' . json_encode($formData)); }*/
if (!isset($formData['Refund_Rsp_St'])) { throw new BusinessException(500, 'Refund_Rsp_St状态码异常:' . ($formData['Ordr_Stcd'] ?? 'null')); } else if (!isset($formData['Ittparty_Jrnl_No'])) { throw new BusinessException(500, '未获取到订单号Ittparty_Jrnl_No'); }
$orderMain = OrderMain::whereNotIn('state', [OrderState::UNPAID]) ->where(['global_order_id' => $formData['Ittparty_Jrnl_No']]) ->first();
if (!$orderMain) { throw new BusinessException(500, $formData['Ittparty_Jrnl_No'] . '订单不存在!'); } else if ($orderMain->state == OrderState::REFUNDED) { return $this->response->json(['Svc_Rsp_St' => '00', 'Ittparty_Tms' => date('YmdHis000'), 'Rsp_Inf' => 'success']); } else if ($formData['Refund_Rsp_St'] != '00') { // 建行返回退款失败
$orderMain->state = OrderState::REFUND_REFUSE; $orderMain->refuse_refund_note = '建行拒绝退款'; $orderMain->save(); Db::commit();
return $this->response->json(['Svc_Rsp_St' => '00', 'Ittparty_Tms' => date('YmdHis000'), 'Rsp_Inf' => 'success']); // 00成功;01失败
}
// 添加退款时间
$orderMain->refund_time = time(); $orderMain->state = OrderState::REFUNDED; $orderMain->save();
// 退款返还优惠券
$this->couponService->orderRefundCoupons($orderMain->global_order_id);
// 处理特价商品缓存
$orderChildren = Order::query()->where(['order_main_id' => $orderMain->global_order_id])->get()->toArray(); $orderGoods = OrderGoods::query()->whereIn('order_id', array_values(array_column($orderChildren, 'id')))->get()->toArray(); foreach ($orderGoods as &$item) { if ($item['activity_type'] == 2) { # 活动商品
$this->goodsActivityService->clearCacheRecord($item['goods_id'], $item['number'], $orderMain->user_id); $goods = GoodsActivity::find($item['goods_id']); } else { $goods = Goods::find($item['goods_id']); } $goods->inventory = $goods->inventory + $item['number']; $goods->sales = $goods->sales - $item['number']; $goods->save(); }
// 添加用户的流水
$this->financialService->userByOLOrderRefund($orderMain->user_id, $orderMain->global_order_id, $orderMain->money);
Db::commit();
// 记录badge
$orderChildIds = array_values(array_column($orderChildren, 'store_id')); $this->badgeService->doByOrder($orderMain->user_id, $orderChildIds, $orderMain->global_order_id, OrderState::REFUNDED);
return $this->response->json(['Svc_Rsp_St' => '00', 'Ittparty_Tms' => date('YmdHis000'), 'Rsp_Inf' => 'success']); // 00成功;01失败
} catch (\Exception $exception) { Db::rollBack(); $this->log->event( LogLabel::ORDER_ONLINE_PAY_NOTIFY_LOG, ['exception_fail' => $exception->getMessage()] ); return $this->response->json(['Svc_Rsp_St' => '01', 'Ittparty_Tms' => date('YmdHis000'), 'Rsp_Inf' => $exception->getMessage()]); // 00成功;01失败
} }}
|