WechatNotifyForm.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\common\models;
  8. use app\models\LevelOrder;
  9. use app\models\PtActivityOrder;
  10. use app\models\ReOrder;
  11. use app\modules\common\models\NotifyForm;
  12. use app\utils\OrderNo;
  13. use app\utils\Wechat\Wechat;
  14. use app\utils\Wechat\WechatProfit;
  15. use app\utils\Wechat\WechatShare;
  16. use EasyWeChat\Kernel\Exceptions\Exception;
  17. use EasyWeChat\Kernel\BaseClient;
  18. use app\utils\IotCloudHelper;
  19. use app\models\Store;
  20. use app\models\OrderUnion;
  21. use app\models\Order;
  22. use app\models\WechatConfig;
  23. use yii\base\Model;
  24. /**
  25. * 微信回调类
  26. * Class WechatNotifyController
  27. * @package app\modules\common\controllers
  28. */
  29. class WechatNotifyForm extends Model
  30. {
  31. public $b2b = 0;
  32. public $xml = null;
  33. public function notifyIndex($store_id, $xml = null) {
  34. $xml && $this->xml = $xml;
  35. $orderNoHead = substr($xml->out_trade_no, 0, 2);
  36. $isAP = 0;
  37. if($orderNoHead == 'AP'){
  38. $isAP = 1;
  39. }
  40. $isPR = 0;
  41. if($orderNoHead == 'PR'){
  42. $isPR = 1;
  43. }
  44. if($orderNoHead == 'UN'){
  45. $order_union = OrderUnion::findOne([
  46. 'order_no' => $xml->out_trade_no,
  47. ]);
  48. if($order_union && ($order_union['type'] == 'AP')){
  49. $isAP = 1;
  50. }
  51. if($order_union && ($order_union['type'] == 'PR')){
  52. $isPR = 1;
  53. }
  54. }
  55. $isRG = 0;
  56. if($orderNoHead == 'RG'){
  57. $isRG = 1;
  58. }
  59. $isPT = 0;
  60. if($orderNoHead == 'PT'){
  61. $isPT = 1;
  62. }
  63. $isSC = 0;
  64. if($orderNoHead == 'SC'){
  65. $isSC = 1;
  66. }
  67. $isLv = 0;
  68. if($orderNoHead == 'LV'){
  69. $isLv = 1;
  70. }
  71. $wxMini = null;
  72. if (\Yii::$app->prod_is_dandianpu()) {
  73. if ($isRG) {
  74. $order = ReOrder::findOne(['order_no' => $xml->out_trade_no]);
  75. } elseif ($isPT) {
  76. $order = PtActivityOrder::findOne(['order_no' => $xml->out_trade_no]);
  77. } elseif ($isSC) {
  78. $order = \app\plugins\scanCodePay\models\Order::findOne(['order_no' => $xml->out_trade_no]);
  79. } else {
  80. $order = Order::findOne(['order_no' => $xml->out_trade_no]);
  81. }
  82. $self_mini = intval(\app\models\Option::get('self_mini', $store_id, 'store', 0)['value']);
  83. if (!Store::hasIncoming($store_id) && !$self_mini) {
  84. Wechat::init($store_id, Wechat::WECHAT_KIND_PAY);
  85. if ($order && $order->order_origin == Order::ORDER_SOURCE_WEB) {
  86. Wechat::init($store_id, Wechat::WECHAT_KIND_PAY, WechatConfig::TYPE_CONFIG_H5);
  87. } elseif ($order && $order->order_origin == Order::ORDER_SOURCE_MP) {
  88. Wechat::init($store_id, Wechat::WECHAT_KIND_PAY, WechatConfig::TYPE_CONFIG_MP);
  89. }
  90. $wechat_pay = Wechat::$wechat_pay;
  91. } else {
  92. if ($order && $order->order_origin == Order::ORDER_SOURCE_APP) {
  93. $from_type = WechatConfig::TYPE_CONFIG_APP;
  94. } else if ($order && $order->order_origin == Order::ORDER_SOURCE_MP) {
  95. $from_type = WechatConfig::TYPE_CONFIG_MP;
  96. } else if ($order && $order->order_origin == Order::ORDER_SOURCE_WEB) {
  97. $from_type = WechatConfig::TYPE_CONFIG_H5;
  98. } else {
  99. $from_type = WechatConfig::TYPE_CONFIG_MINI;
  100. }
  101. $is_platform = empty($order->is_platform) ? 0 : 1;
  102. WechatProfit::init($store_id, $is_platform, 0, $from_type);
  103. $wechat_pay = WechatProfit::$wechat_pay;
  104. }
  105. $this->notify($wechat_pay, $store_id);
  106. } elseif (!Store::hasIncoming($store_id) && \Yii::$app->isSaas()) {
  107. if ($isRG) {
  108. $order = ReOrder::findOne(['order_no' => $xml->out_trade_no]);
  109. } elseif ($isPT) {
  110. $order = PtActivityOrder::findOne(['order_no' => $xml->out_trade_no]);
  111. } else {
  112. $order = Order::findOne(['order_no' => $xml->out_trade_no]);
  113. }
  114. if ($order && intval($order->order_origin) == Order::ORDER_SOURCE_WEB || ($isRG && (int)$order->is_platform === 0)) {
  115. WechatProfit::init($store_id);
  116. $this->notify(WechatProfit::$wechat_pay, $store_id);
  117. } else {
  118. Wechat::init($store_id, Wechat::WECHAT_KIND_PAY);
  119. $this->notify(Wechat::$wechat_pay, $store_id);
  120. }
  121. }else {
  122. if (is_profit_pay('wx', $store_id) && !$isAP && !$isPR && $store_id > 0) {
  123. WechatProfit::init($store_id);
  124. WechatProfit::initSaas();
  125. $wxMini = WechatProfit::$wechat_mini;
  126. $this->notify(WechatProfit::$wechat_pay, $store_id);
  127. } else {
  128. $order = Order::findOne(['order_no' => $xml->out_trade_no]);
  129. if ($isRG) {
  130. $order = ReOrder::findOne(['order_no' => $xml->out_trade_no]);
  131. // TODO 临时处理, 充值订单表没有order_origin字段
  132. // $order_origin = Order::ORDER_SOURCE_MINI;
  133. }
  134. if ($isLv) {
  135. $order = LevelOrder::findOne(['order_no' => $xml->out_trade_no]);
  136. // TODO 临时处理, 充值订单表没有order_origin字段
  137. // $order_origin = Order::ORDER_SOURCE_MINI;
  138. }
  139. $order_origin = $order->order_origin;
  140. if (empty($store_id)) {
  141. $store_id = $order->store_id;
  142. }
  143. //20240816 独立部署存在的问题 查询订单号前是"IR"的查询不到 导致没有$order 后面的Wechat::$wechat_pay为空的问题
  144. $from = WechatConfig::TYPE_CONFIG_MINI;
  145. if ($order) {
  146. if (isset($order_origin)) {
  147. if (intval($order_origin) === Order::ORDER_SOURCE_WEB) {
  148. $from = WechatConfig::TYPE_CONFIG_MP;
  149. }
  150. if (intval($order_origin) === Order::ORDER_SOURCE_APP) {
  151. $from = WechatConfig::TYPE_CONFIG_APP;
  152. }
  153. if (intval($order_origin) === Order::ORDER_SOURCE_MP) {
  154. $from = WechatConfig::TYPE_CONFIG_MP;
  155. }
  156. }
  157. }
  158. Wechat::init($store_id, Wechat::WECHAT_KIND_PAY, $from);
  159. $this->notify(Wechat::$wechat_pay, $store_id);
  160. }
  161. }
  162. // try {
  163. // if(!$wxMini){
  164. // Wechat::init($store_id);
  165. // $wxMini = Wechat::$wechat_mini;
  166. // }
  167. // $this->wxUploadShippingInfo($store_id, $wxMini, $xml);
  168. // } catch (\Exception $e) {
  169. // \Yii::error($e);
  170. // }
  171. }
  172. /**
  173. * @param \EasyWeChat\Payment\Application $app
  174. * @param $store_id
  175. * 回调逻辑处理
  176. */
  177. private function notify($app, $store_id) {
  178. if($this->b2b){
  179. return $this->notifyB2b($app, $store_id);
  180. }
  181. try {
  182. $response = $app->handlePaidNotify(function($message, $fail) use ($store_id) {
  183. // return_code 通信状态
  184. if ($message['return_code'] != 'SUCCESS') {
  185. // 触发fail稍后再次继续通知
  186. return $fail('失败');
  187. }
  188. if ($message['result_code'] != 'SUCCESS') {
  189. // 交易状态,就算result_code=FAIL,也要通知微信处理完成
  190. return true;
  191. }
  192. if (isset($message['total_fee']) && isset($message['time_end'])) {
  193. $store = Store::findOne($store_id);
  194. if ($store && !empty($store->device_name)) {
  195. $total_fee = round($message['total_fee'] / 100, 2);
  196. IotCloudHelper::sendMessage($store_id, '{"cmd":"voice","msg":"微信收款'.$total_fee.'元","msgid":"'.$message['time_end'].'"}');
  197. }
  198. }
  199. $this->notifyForm($message);
  200. return true; // 返回处理完成
  201. });
  202. $response->send();// return $response;
  203. } catch (\Exception $e) {
  204. \Yii::warning(['WECHAT NOTIFY EXCEPTION <==========> ', $e->getMessage()]);
  205. }
  206. }
  207. public function notifyB2b($app, $store_id) {
  208. try {
  209. $message = (array)$this->xml;
  210. if ($message['pay_status'] != 'ORDER_PAY_SUCC') {
  211. throw new \Exception('pay_status:' . $message['pay_status']);
  212. }
  213. if (isset($message['amount']) && isset($message['amount']['order_amount'])) {
  214. $store = Store::findOne($store_id);
  215. if ($store && !empty($store->device_name)) {
  216. $total_fee = round($message['amount']['order_amount'] / 100, 2);
  217. IotCloudHelper::sendMessage($store_id, '{"cmd":"voice","msg":"微信收款'.$total_fee.'元","msgid":"'.$message['pay_time'].'"}');
  218. }
  219. }
  220. $this->notifyForm($message, Order::PAY_TYPE_WX_B2B);
  221. return true; // 返回处理完成
  222. } catch (\Exception $e) {
  223. \Yii::warning(['WECHAT NOTIFY EXCEPTION <==========> ', $e->getMessage()]);
  224. echo 'fail';
  225. }
  226. }
  227. public function notifyForm($message, $pay_type = null) {
  228. $orderNoHead = substr($message['out_trade_no'], 0, 2);
  229. $notify = new NotifyForm();
  230. if($pay_type){
  231. $notify->pay_type = $pay_type;
  232. }
  233. switch ($orderNoHead) {
  234. case 'UN':
  235. // 合并支付的订单
  236. $notify->UnionOrderNotify($message);
  237. break;
  238. case 'RG':
  239. // 充值订单
  240. $notify->RechargeOrderNotify($message);
  241. case OrderNo::INTEGRAL_RECHARGE:
  242. // 充值订单
  243. $notify->IntegralRechargeOrderNotify($message);
  244. break;
  245. case 'LV':
  246. // 会员购买
  247. $notify->LevelOrderNotify($message);
  248. break;
  249. case 'ML':
  250. // 商城订单
  251. $notify->MallOrderNotify($message);
  252. break;
  253. case 'SC':
  254. // 当面付订单
  255. $notify->ScanOrderNotify($message);
  256. break;
  257. case 'PC':
  258. // 联盟佣金提现订单
  259. $notify->saasProfitCash($message);
  260. break;
  261. case 'FO':
  262. // 点餐订单
  263. $notify->FoodNotify($message);
  264. break;
  265. case 'SG':
  266. // 店铺充值订单
  267. $notify->StoreReOrderNotify($message);
  268. break;
  269. case 'AM':
  270. //商盟会员卡订单
  271. $notify->BusinessMemberOrderNotify($message);
  272. break;
  273. case 'AL':
  274. //商盟会员购物订单
  275. $notify->BusinessNotify($message);
  276. break;
  277. case 'AD':
  278. // 认养费用
  279. $notify->AdoptCostOrderNotify($message);
  280. break;
  281. case 'AP':
  282. // 采购员订单
  283. $notify->APOrderNotify($message);
  284. break;
  285. case 'PR':
  286. \Yii::error([__METHOD__, 'PROrderNotify', $message]);
  287. // 采购员充值
  288. $notify->PROrderNotify($message);
  289. break;
  290. case 'LP':
  291. // 联盟券充值
  292. $notify->LPorderNotify($message);
  293. break;
  294. case 'PT':
  295. // 充值订单
  296. $notify->ptActivityOrderNotify($message);
  297. break;
  298. default:
  299. break;
  300. }
  301. }
  302. }