| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\common\models;
- use app\models\LevelOrder;
- use app\models\PtActivityOrder;
- use app\models\ReOrder;
- use app\modules\common\models\NotifyForm;
- use app\utils\OrderNo;
- use app\utils\Wechat\Wechat;
- use app\utils\Wechat\WechatProfit;
- use app\utils\Wechat\WechatShare;
- use EasyWeChat\Kernel\Exceptions\Exception;
- use EasyWeChat\Kernel\BaseClient;
- use app\utils\IotCloudHelper;
- use app\models\Store;
- use app\models\OrderUnion;
- use app\models\Order;
- use app\models\WechatConfig;
- use yii\base\Model;
- /**
- * 微信回调类
- * Class WechatNotifyController
- * @package app\modules\common\controllers
- */
- class WechatNotifyForm extends Model
- {
- public $b2b = 0;
- public $xml = null;
-
- public function notifyIndex($store_id, $xml = null) {
- $xml && $this->xml = $xml;
- $orderNoHead = substr($xml->out_trade_no, 0, 2);
- $isAP = 0;
- if($orderNoHead == 'AP'){
- $isAP = 1;
- }
- $isPR = 0;
- if($orderNoHead == 'PR'){
- $isPR = 1;
- }
- if($orderNoHead == 'UN'){
- $order_union = OrderUnion::findOne([
- 'order_no' => $xml->out_trade_no,
- ]);
- if($order_union && ($order_union['type'] == 'AP')){
- $isAP = 1;
- }
- if($order_union && ($order_union['type'] == 'PR')){
- $isPR = 1;
- }
- }
- $isRG = 0;
- if($orderNoHead == 'RG'){
- $isRG = 1;
- }
- $isPT = 0;
- if($orderNoHead == 'PT'){
- $isPT = 1;
- }
- $isSC = 0;
- if($orderNoHead == 'SC'){
- $isSC = 1;
- }
- $isLv = 0;
- if($orderNoHead == 'LV'){
- $isLv = 1;
- }
- $wxMini = null;
- if (\Yii::$app->prod_is_dandianpu()) {
- if ($isRG) {
- $order = ReOrder::findOne(['order_no' => $xml->out_trade_no]);
- } elseif ($isPT) {
- $order = PtActivityOrder::findOne(['order_no' => $xml->out_trade_no]);
- } elseif ($isSC) {
- $order = \app\plugins\scanCodePay\models\Order::findOne(['order_no' => $xml->out_trade_no]);
- } else {
- $order = Order::findOne(['order_no' => $xml->out_trade_no]);
- }
- $self_mini = intval(\app\models\Option::get('self_mini', $store_id, 'store', 0)['value']);
- if (!Store::hasIncoming($store_id) && !$self_mini) {
- Wechat::init($store_id, Wechat::WECHAT_KIND_PAY);
- if ($order && $order->order_origin == Order::ORDER_SOURCE_WEB) {
- Wechat::init($store_id, Wechat::WECHAT_KIND_PAY, WechatConfig::TYPE_CONFIG_H5);
- } elseif ($order && $order->order_origin == Order::ORDER_SOURCE_MP) {
- Wechat::init($store_id, Wechat::WECHAT_KIND_PAY, WechatConfig::TYPE_CONFIG_MP);
- }
- $wechat_pay = Wechat::$wechat_pay;
- } else {
- if ($order && $order->order_origin == Order::ORDER_SOURCE_APP) {
- $from_type = WechatConfig::TYPE_CONFIG_APP;
- } else if ($order && $order->order_origin == Order::ORDER_SOURCE_MP) {
- $from_type = WechatConfig::TYPE_CONFIG_MP;
- } else if ($order && $order->order_origin == Order::ORDER_SOURCE_WEB) {
- $from_type = WechatConfig::TYPE_CONFIG_H5;
- } else {
- $from_type = WechatConfig::TYPE_CONFIG_MINI;
- }
- $is_platform = empty($order->is_platform) ? 0 : 1;
- WechatProfit::init($store_id, $is_platform, 0, $from_type);
- $wechat_pay = WechatProfit::$wechat_pay;
- }
- $this->notify($wechat_pay, $store_id);
- } elseif (!Store::hasIncoming($store_id) && \Yii::$app->isSaas()) {
- if ($isRG) {
- $order = ReOrder::findOne(['order_no' => $xml->out_trade_no]);
- } elseif ($isPT) {
- $order = PtActivityOrder::findOne(['order_no' => $xml->out_trade_no]);
- } else {
- $order = Order::findOne(['order_no' => $xml->out_trade_no]);
- }
- if ($order && intval($order->order_origin) == Order::ORDER_SOURCE_WEB || ($isRG && (int)$order->is_platform === 0)) {
- WechatProfit::init($store_id);
- $this->notify(WechatProfit::$wechat_pay, $store_id);
- } else {
- Wechat::init($store_id, Wechat::WECHAT_KIND_PAY);
- $this->notify(Wechat::$wechat_pay, $store_id);
- }
- }else {
- if (is_profit_pay('wx', $store_id) && !$isAP && !$isPR && $store_id > 0) {
- WechatProfit::init($store_id);
- WechatProfit::initSaas();
- $wxMini = WechatProfit::$wechat_mini;
- $this->notify(WechatProfit::$wechat_pay, $store_id);
- } else {
- $order = Order::findOne(['order_no' => $xml->out_trade_no]);
- if ($isRG) {
- $order = ReOrder::findOne(['order_no' => $xml->out_trade_no]);
- // TODO 临时处理, 充值订单表没有order_origin字段
- // $order_origin = Order::ORDER_SOURCE_MINI;
- }
- if ($isLv) {
- $order = LevelOrder::findOne(['order_no' => $xml->out_trade_no]);
- // TODO 临时处理, 充值订单表没有order_origin字段
- // $order_origin = Order::ORDER_SOURCE_MINI;
- }
- $order_origin = $order->order_origin;
- if (empty($store_id)) {
- $store_id = $order->store_id;
- }
- //20240816 独立部署存在的问题 查询订单号前是"IR"的查询不到 导致没有$order 后面的Wechat::$wechat_pay为空的问题
- $from = WechatConfig::TYPE_CONFIG_MINI;
- if ($order) {
- if (isset($order_origin)) {
- if (intval($order_origin) === Order::ORDER_SOURCE_WEB) {
- $from = WechatConfig::TYPE_CONFIG_MP;
- }
- if (intval($order_origin) === Order::ORDER_SOURCE_APP) {
- $from = WechatConfig::TYPE_CONFIG_APP;
- }
- if (intval($order_origin) === Order::ORDER_SOURCE_MP) {
- $from = WechatConfig::TYPE_CONFIG_MP;
- }
- }
- }
- Wechat::init($store_id, Wechat::WECHAT_KIND_PAY, $from);
- $this->notify(Wechat::$wechat_pay, $store_id);
- }
- }
- // try {
- // if(!$wxMini){
- // Wechat::init($store_id);
- // $wxMini = Wechat::$wechat_mini;
- // }
- // $this->wxUploadShippingInfo($store_id, $wxMini, $xml);
- // } catch (\Exception $e) {
- // \Yii::error($e);
- // }
- }
- /**
- * @param \EasyWeChat\Payment\Application $app
- * @param $store_id
- * 回调逻辑处理
- */
- private function notify($app, $store_id) {
- if($this->b2b){
- return $this->notifyB2b($app, $store_id);
- }
- try {
- $response = $app->handlePaidNotify(function($message, $fail) use ($store_id) {
- // return_code 通信状态
- if ($message['return_code'] != 'SUCCESS') {
- // 触发fail稍后再次继续通知
- return $fail('失败');
- }
- if ($message['result_code'] != 'SUCCESS') {
- // 交易状态,就算result_code=FAIL,也要通知微信处理完成
- return true;
- }
- if (isset($message['total_fee']) && isset($message['time_end'])) {
- $store = Store::findOne($store_id);
- if ($store && !empty($store->device_name)) {
- $total_fee = round($message['total_fee'] / 100, 2);
- IotCloudHelper::sendMessage($store_id, '{"cmd":"voice","msg":"微信收款'.$total_fee.'元","msgid":"'.$message['time_end'].'"}');
- }
- }
- $this->notifyForm($message);
- return true; // 返回处理完成
- });
- $response->send();// return $response;
- } catch (\Exception $e) {
- \Yii::warning(['WECHAT NOTIFY EXCEPTION <==========> ', $e->getMessage()]);
- }
- }
- public function notifyB2b($app, $store_id) {
- try {
- $message = (array)$this->xml;
- if ($message['pay_status'] != 'ORDER_PAY_SUCC') {
- throw new \Exception('pay_status:' . $message['pay_status']);
- }
- if (isset($message['amount']) && isset($message['amount']['order_amount'])) {
- $store = Store::findOne($store_id);
- if ($store && !empty($store->device_name)) {
- $total_fee = round($message['amount']['order_amount'] / 100, 2);
- IotCloudHelper::sendMessage($store_id, '{"cmd":"voice","msg":"微信收款'.$total_fee.'元","msgid":"'.$message['pay_time'].'"}');
- }
- }
- $this->notifyForm($message, Order::PAY_TYPE_WX_B2B);
- return true; // 返回处理完成
- } catch (\Exception $e) {
- \Yii::warning(['WECHAT NOTIFY EXCEPTION <==========> ', $e->getMessage()]);
- echo 'fail';
- }
- }
-
- public function notifyForm($message, $pay_type = null) {
- $orderNoHead = substr($message['out_trade_no'], 0, 2);
- $notify = new NotifyForm();
- if($pay_type){
- $notify->pay_type = $pay_type;
- }
- switch ($orderNoHead) {
- case 'UN':
- // 合并支付的订单
- $notify->UnionOrderNotify($message);
- break;
- case 'RG':
- // 充值订单
- $notify->RechargeOrderNotify($message);
- case OrderNo::INTEGRAL_RECHARGE:
- // 充值订单
- $notify->IntegralRechargeOrderNotify($message);
- break;
- case 'LV':
- // 会员购买
- $notify->LevelOrderNotify($message);
- break;
- case 'ML':
- // 商城订单
- $notify->MallOrderNotify($message);
- break;
- case 'SC':
- // 当面付订单
- $notify->ScanOrderNotify($message);
- break;
- case 'PC':
- // 联盟佣金提现订单
- $notify->saasProfitCash($message);
- break;
- case 'FO':
- // 点餐订单
- $notify->FoodNotify($message);
- break;
- case 'SG':
- // 店铺充值订单
- $notify->StoreReOrderNotify($message);
- break;
- case 'AM':
- //商盟会员卡订单
- $notify->BusinessMemberOrderNotify($message);
- break;
- case 'AL':
- //商盟会员购物订单
- $notify->BusinessNotify($message);
- break;
- case 'AD':
- // 认养费用
- $notify->AdoptCostOrderNotify($message);
- break;
- case 'AP':
- // 采购员订单
- $notify->APOrderNotify($message);
- break;
- case 'PR':
- \Yii::error([__METHOD__, 'PROrderNotify', $message]);
- // 采购员充值
- $notify->PROrderNotify($message);
- break;
- case 'LP':
- // 联盟券充值
- $notify->LPorderNotify($message);
- break;
- case 'PT':
- // 充值订单
- $notify->ptActivityOrderNotify($message);
- break;
- default:
- break;
- }
- }
- }
|