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; } } }