Notify.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. <?php
  2. namespace app\controller\api;
  3. use app\extra\basic\Base;
  4. use app\model\saas\SaasCart;
  5. use app\model\saas\SaasOrder;
  6. use app\model\saas\SaasOrderDetail;
  7. use app\model\saas\SaasShop;
  8. use app\model\saas\SaasShopLog;
  9. use app\model\saas\SaasUser;
  10. use app\model\saas\SaasUserBuy;
  11. use app\model\saas\SaasUserLog;
  12. use LinFly\Annotation\Route\Controller;
  13. use LinFly\Annotation\Route\Route;
  14. use support\Request;
  15. use support\Response;
  16. use think\facade\Db;
  17. use WeChat\Contracts\Tools;
  18. #[Controller(prefix: "/notify")]
  19. class Notify extends Base
  20. {
  21. /**
  22. * 退款回调
  23. * @param Request $request
  24. */
  25. #[Route(path: "refund",methods: "post")]
  26. public function notifyRefund(Request $request)
  27. {
  28. try {
  29. echo getDateFull() . "===>退款申请返回\n";
  30. $payResp = $request->rawBody();
  31. $wechat = new \WePay\Refund($this->getWxConfig());
  32. if (empty($payResp)) return $wechat->getNotifySuccessReply();
  33. $data = $wechat->getNotify($payResp);
  34. if (empty($data)) return $wechat->getNotifySuccessReply();
  35. $orderSn = explode("-",$data['result']['out_refund_no']);
  36. $order = (new SaasOrder)->where("order_sn",$orderSn[0])->with(['shop' => function($query){
  37. $query->field('shop_id,shop_name,shop_address');
  38. }])->findOrEmpty();
  39. if ($order->isEmpty()) return $wechat->getNotifySuccessReply();
  40. if ($order['status'] <> 4) return $wechat->getNotifySuccessReply();
  41. $order->status = 6;
  42. $order->save();
  43. (new SaasOrderDetail)->where("order_sn",$order['order_sn'])->update(['status' => 6]);
  44. // 商家账户额度退款
  45. $shop = (new SaasShop)->where("shop_id",$order['shop_id'])->with(['wx' => function($query){
  46. $query->field("shop_id,openid,is_msg");
  47. }])->findOrEmpty();
  48. if ($shop->isEmpty()) return $this->getNotifySuccessReply();
  49. $shop->balance = Db::raw("balance-".$order['discount']);
  50. $shop->total_balance = Db::raw("total_balance-".$order['discount']);
  51. $shop->save();
  52. (new SaasShopLog)->insertGetId([
  53. "shop_id" => $order['shop_id'],
  54. "money" => $order['discount'],
  55. "balance" => $shop->balance,
  56. "remark" => "订单退款【{$order['order_sn']}】",
  57. "type" => 2
  58. ]);// 推送消息-公众号
  59. if (!empty($shop['wx'])) {
  60. $obj = \We::WeChatTemplate([
  61. "appid" => sConf("wechat.appid"),
  62. "appsecret" => sConf("wechat.secret"),
  63. "token" => sConf("wechat.token"),
  64. "encodingaeskey" => sConf("wechat.aeskey")
  65. ]);
  66. foreach ($shop['wx'] as $val) {
  67. if ($val['is_msg'] == 1) {
  68. $obj->send([
  69. "touser" => $val['openid'],
  70. "template_id" => "WboxN-32rdHORBQHzaVvQb9kPN7rLaXAPS_BL2yfb5w",
  71. "data" => [
  72. "amount3" => ["value" => format_money($order['money']/100)],
  73. "character_string1" => ["value" => $order['old_order']],
  74. "time4" => ["value" => date('Y-m-d H:i')]
  75. ]
  76. ]);
  77. }
  78. }
  79. }
  80. } catch (\Throwable $throwable) {
  81. return error($throwable->getMessage());
  82. }
  83. }
  84. /**
  85. * 订单支付
  86. */
  87. #[Route(path: "wx",methods: "post")]
  88. public function notifyWx(Request $request)
  89. {
  90. try {
  91. echo getDateFull()."===>支付返回\n";
  92. $payResp = $request->rawBody();
  93. $data = $this->payReturn($payResp);
  94. $order = (new SaasOrder)->where("order_sn",$data['attach'])->with(['shop' => function($query){
  95. $query->field('shop_id,shop_name,shop_address');
  96. }])->findOrEmpty();
  97. if ($order->isEmpty()) return $this->getNotifySuccessReply();
  98. if ($order['status'] <> 0) return $this->getNotifySuccessReply(); // 已支付或者是其他状态
  99. $order->status = 1;
  100. $order->pay_at = getDateFull();
  101. $order->transaction_id = $data['transaction_id']??'';
  102. $order->notify_status = 1;
  103. $order->pay_type = 1;
  104. $order->save();
  105. $shop = (new SaasShop)->where("shop_id",$order['shop_id'])->with(['wx' => function($query){
  106. $query->field("shop_id,openid,is_msg");
  107. }])->findOrEmpty();
  108. if ($shop->isEmpty()) return $this->getNotifySuccessReply();
  109. $shop->balance = Db::raw("balance+".$order['money']);
  110. $shop->total_balance = Db::raw("total_balance+".$order['money']);
  111. $shop->save();
  112. (new SaasShopLog)->insertGetId([
  113. "shop_id" => $order['shop_id'],
  114. "money" => $order['money'],
  115. "balance" => $shop->balance,
  116. "remark" => "新订单【{$order['order_sn']}】"
  117. ]);
  118. events("create-order",['shop' => $order['shop_id'],'openid' => $order['openid'],"order" => $order['order_sn']]);
  119. // 推送消息-公众号
  120. if (!empty($shop['wx'])) {
  121. $obj = \We::WeChatTemplate([
  122. "appid" => sConf("wechat.appid"),
  123. "appsecret" => sConf("wechat.secret"),
  124. "token" => sConf("wechat.token"),
  125. "encodingaeskey" => sConf("wechat.aeskey")
  126. ]);
  127. foreach ($shop['wx'] as $val) {
  128. if ($val['is_msg'] == 1) {
  129. $obj->send([
  130. "touser" => $val['openid'],
  131. "template_id" => "D20ZEWmUNmXMHLwiTOzaEateX5NvM9zoCbp2YwbIHsI",
  132. "data" => [
  133. "thing20" => ["value" => $val['shop_name']],
  134. "character_string1" => ["value" => $order['order_sn']],
  135. "amount16" => ["value" => format_money($order['money'] / 100)],
  136. "time2" => ["value" => date('Y-m-d H:i')]
  137. ],
  138. "url" => "https://inmei.yunenv.cn/weixin/order/detail?id=" . $order['id']
  139. ]);
  140. }
  141. }
  142. }
  143. return $this->getNotifySuccessReply();
  144. } catch (\Throwable $throwable) {
  145. return error($throwable->getMessage());
  146. }
  147. }
  148. /**
  149. * 充值并支付
  150. */
  151. #[Route(path: "recharge",methods: "post")]
  152. public function notifyPayRecharge(Request $request)
  153. {
  154. try {
  155. echo getDateFull()."===>充值并支付支付返回\n";
  156. $payResp = $request->rawBody();
  157. $data = $this->payReturn($payResp);
  158. $orderBuy = (new SaasUserBuy)->where("order_sn",$data['attach'])->with(['orders'])->findOrEmpty();
  159. if ($orderBuy->isEmpty()) return $this->getNotifySuccessReply();
  160. if ($orderBuy['status'] <> 0) return $this->getNotifySuccessReply(); // 已支付或者是其他状态
  161. $orderMoney = 0;
  162. $logData[0] = [
  163. "openid" => $orderBuy['openid'],
  164. "card_no" => $orderBuy['card_no'],
  165. "shop_id" => $orderBuy['shop_id'],
  166. "money" => $orderBuy['total_money'],
  167. "order_sn" => $orderBuy['order_sn'],
  168. "type" => 2,
  169. "remark" => "充值",
  170. "balance" => $orderBuy['total_money']
  171. ];
  172. if (!empty($orderBuy['orders']))
  173. {
  174. $orderMoney = $orderBuy['orders']['money'];
  175. $logData[1] = [
  176. "openid" => $orderBuy['openid'],
  177. "card_no" => $orderBuy['card_no'],
  178. "shop_id" => $orderBuy['shop_id'],
  179. "order_sn" => $orderBuy['order_sn'],
  180. "money" => $orderMoney,
  181. "type" => 1,
  182. "remark" => "订单付款",
  183. "balance" => $orderBuy['total_money'] - $orderMoney
  184. ];
  185. };
  186. $orderBuy->status = 1;
  187. $orderBuy->pay_at = getDateFull();
  188. $orderBuy->transaction_id = $data['transaction_id']??'';
  189. $orderBuy->save();
  190. $shop = (new SaasShop)->where("shop_id",$orderBuy['shop_id'])->with(['wx' => function($query){
  191. $query->field("shop_id,openid,is_msg");
  192. }])->findOrEmpty();
  193. if ($shop->isEmpty()) return $this->getNotifySuccessReply();
  194. // 开通vip账户
  195. $card = (new SaasUser)->where("card_no",$orderBuy['card_no'])->findOrEmpty();
  196. $balanceMoney = $orderBuy['total_money'] - $orderMoney;
  197. if ($card->isEmpty()) {
  198. $card->insertGetId([
  199. "openid" => $orderBuy['openid'],
  200. "shop_id" => $orderBuy['shop_id'],
  201. "card_no" => $orderBuy['card_no'],
  202. "balance" => $balanceMoney, // 余额
  203. "total_balance" => $orderBuy['money'], // 累计充值
  204. "total_consume" => $orderMoney // 累计消费
  205. ]);
  206. } else {
  207. $card->total_consume = Db::raw("total_consume+{$orderMoney}");
  208. $card->total_balance = Db::raw("total_balance+{$orderBuy['money']}");
  209. $card->balance = Db::raw("balance+{$balanceMoney}");
  210. $card->save();
  211. }
  212. if (!empty($orderBuy['orders']))
  213. {
  214. $order = (new SaasOrder)->where("order_sn",$orderBuy['order_sn'])->findOrEmpty();
  215. if ($order->isEmpty()) return $this->getNotifySuccessReply();
  216. if ($order['status'] <> 0) return $this->getNotifySuccessReply(); // 已支付或者是其他状态
  217. $order->status = 1;
  218. $order->pay_at = getDateFull();
  219. $order->transaction_id = $data['transaction_id']??'';
  220. $order->notify_status = 1;
  221. $order->pay_type = 2;
  222. $order->save();
  223. events("create-order",['shop' => $order['shop_id'],'openid' => $order['openid'],"order" => $order['order_sn']]);
  224. }
  225. $shop->balance = Db::raw("balance+".$orderBuy['money']);
  226. $shop->total_balance = Db::raw("total_balance+".$orderBuy['money']);
  227. $shop->save();
  228. (new SaasShopLog)->insertGetId([
  229. "shop_id" => $orderBuy['shop_id'],
  230. "money" => $orderBuy['money'],
  231. "balance" => $shop->balance,
  232. "type" => 1,
  233. "remark" => "会员卡充值",
  234. "status" => 1
  235. ]);
  236. // 推送消息-公众号
  237. if (!empty($shop['wx'])) {
  238. $obj = \We::WeChatTemplate([
  239. "appid" => sConf("wechat.appid"),
  240. "appsecret" => sConf("wechat.secret"),
  241. "token" => sConf("wechat.token"),
  242. "encodingaeskey" => sConf("wechat.aeskey")
  243. ]);
  244. foreach ($shop['wx'] as $val) {
  245. if ($val['is_msg'] == 1) {
  246. $obj->send([
  247. "touser" => $val['openid'],
  248. "template_id" => "v1TVHflG9h5BPRLb2hH10r8oOV5OFMKD2cmqIw1nH-w",
  249. "data" => [
  250. "thing1" => ["value" => '微信用户'.$orderBuy['openid']],
  251. "thing6" => ["value" => $val['shop_name']],
  252. "amount3" => ["value" => format_money($orderBuy['money'] / 100)],
  253. "character_string9" => ["value" => $orderBuy['order_sn']],
  254. "time4" => ["time5" => date('Y-m-d H:i')]
  255. ]
  256. ]);
  257. }
  258. }
  259. }
  260. (new SaasUserLog)->insertAll($logData);
  261. return $this->getNotifySuccessReply();
  262. } catch (\Throwable $throwable) {
  263. return error($throwable->getMessage());
  264. }
  265. }
  266. /**
  267. * 会员卡充值
  268. */
  269. #[Route(path: "recharges",methods: "post")]
  270. public function notifyDataRecharge(Request $request)
  271. {
  272. try {
  273. echo getDateFull()."===>会员卡支付返回\n";
  274. $payResp = $request->rawBody();
  275. $data = $this->payReturn($payResp);
  276. } catch (\Throwable $throwable) {
  277. return error($throwable->getMessage());
  278. }
  279. }
  280. protected function payReturn($payResp)
  281. {
  282. $wechat = new \WeChat\Pay($this->getWxConfig());
  283. if (empty($payResp)) return $this->getNotifySuccessReply();
  284. $data = Tools::xml2arr($payResp);
  285. if (empty($data)) return $this->getNotifySuccessReply();
  286. return $data;
  287. }
  288. /**
  289. * 获取微信支付通知成功回复 XML
  290. * @return string
  291. */
  292. protected function getNotifySuccessReply(): string
  293. {
  294. return Tools::arr2xml(['return_code' => 'SUCCESS', 'return_msg' => 'OK']);
  295. }
  296. /**
  297. * 小程序配置
  298. * @return array
  299. */
  300. protected function getWxConfig(): array
  301. {
  302. return [
  303. 'token' => 'test',
  304. 'appid' => sConf("wechat.mini_appid"),
  305. 'appsecret' => sConf("wechat.mini_secret"),
  306. 'encodingaeskey' => 'BJIUzE0gqlWy0GxfPp4J1oPTBmOrNDIGPNav1YFH5Z5',
  307. // 配置商户支付参数(可选,在使用支付功能时需要)
  308. 'mch_id' => sConf("wechat.mch_id"),
  309. 'mch_key' => sConf("wechat.mch_key")
  310. ];
  311. }
  312. #[Route(path: "test",methods: "get")]
  313. public function testData()
  314. {
  315. try {
  316. $data = $this->_valid([
  317. "openid.default" => "otdgy3fYZ0JBHW7mbnxmdKTQJRUU",
  318. "shop.default" => "326945519114632",
  319. "order.default" => "202604022640934408406"
  320. ]);
  321. $cart = (new SaasCart)->where("shop_id",$data['shop'])->where("openid",$data['openid'])->order("create_at desc")->select();
  322. if ($cart->isEmpty()) return success('ok',['cart' => []]);
  323. $orderDetail = [];
  324. foreach ($cart->toArray() as $key=>$val)
  325. {
  326. unset($val['id']);
  327. $orderDetail[$key] = $val;
  328. $orderDetail[$key]['order_sn'] = $data['order'];
  329. if ($val['extension'] == "pdf") {
  330. $orderDetail[$key]['status'] = 1;
  331. } else {
  332. $orderDetail[$key]['status'] = 0;
  333. }
  334. }
  335. print_r($orderDetail);
  336. $state = (new SaasOrderDetail)->strict(false)->insertAll($orderDetail);
  337. return success("ok");
  338. } catch (\Throwable $throwable) {
  339. return error($throwable->getMessage());
  340. }
  341. }
  342. }