Notify.php 12 KB

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