Notify.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. "order_sn" => $orderBuy['order_sn'],
  122. "type" => 2,
  123. "remark" => "充值",
  124. "balance" => $orderBuy['total_money']
  125. ];
  126. if (!empty($orderBuy['orders']))
  127. {
  128. $orderMoney = $orderBuy['orders']['money'];
  129. $logData[1] = [
  130. "openid" => $orderBuy['openid'],
  131. "card_no" => $orderBuy['card_no'],
  132. "shop_id" => $orderBuy['shop_id'],
  133. "order_sn" => $orderBuy['order_sn'],
  134. "money" => $orderMoney,
  135. "type" => 1,
  136. "remark" => "订单付款",
  137. "balance" => $orderBuy['total_money'] - $orderMoney
  138. ];
  139. };
  140. $orderBuy->status = 1;
  141. $orderBuy->pay_at = getDateFull();
  142. $orderBuy->transaction_id = $data['transaction_id']??'';
  143. $orderBuy->save();
  144. $shop = (new SaasShop)->where("shop_id",$orderBuy['shop_id'])->with(['wx' => function($query){
  145. $query->field("shop_id,openid,is_msg");
  146. }])->findOrEmpty();
  147. if ($shop->isEmpty()) return $this->getNotifySuccessReply();
  148. // 开通vip账户
  149. $card = (new SaasUser)->where("card_no",$orderBuy['card_no'])->findOrEmpty();
  150. $balanceMoney = $orderBuy['total_money'] - $orderMoney;
  151. if ($card->isEmpty()) {
  152. $card->insertGetId([
  153. "openid" => $orderBuy['openid'],
  154. "shop_id" => $orderBuy['shop_id'],
  155. "card_no" => $orderBuy['card_no'],
  156. "balance" => $balanceMoney, // 余额
  157. "total_balance" => $orderBuy['money'], // 累计充值
  158. "total_consume" => $orderMoney // 累计消费
  159. ]);
  160. } else {
  161. $card->total_consume = Db::raw("total_consume+{$orderMoney}");
  162. $card->total_balance = Db::raw("total_balance+{$orderBuy['money']}");
  163. $card->balance = Db::raw("balance+{$balanceMoney}");
  164. $card->save();
  165. }
  166. if (!empty($orderBuy['orders']))
  167. {
  168. $order = (new SaasOrder)->where("order_sn",$orderBuy['order_sn'])->findOrEmpty();
  169. if ($order->isEmpty()) return $this->getNotifySuccessReply();
  170. if ($order['status'] <> 0) return $this->getNotifySuccessReply(); // 已支付或者是其他状态
  171. $order->status = 1;
  172. $order->pay_at = getDateFull();
  173. $order->transaction_id = $data['transaction_id']??'';
  174. $order->notify_status = 1;
  175. $order->pay_type = 2;
  176. $order->save();
  177. events("create-order",['shop' => $order['shop_id'],'openid' => $order['openid'],"order" => $order['order_sn']]);
  178. }
  179. $shop->balance = Db::raw("balance+".$orderBuy['money']);
  180. $shop->total_balance = Db::raw("total_balance+".$orderBuy['money']);
  181. $shop->save();
  182. (new SaasShopLog)->insertGetId([
  183. "shop_id" => $orderBuy['shop_id'],
  184. "money" => $orderBuy['money'],
  185. "balance" => $shop->balance,
  186. "type" => 1,
  187. "remark" => "会员卡充值",
  188. "status" => 1
  189. ]);
  190. // 推送消息-公众号
  191. if (!empty($shop['wx'])) {
  192. $obj = \We::WeChatTemplate([
  193. "appid" => sConf("wechat.appid"),
  194. "appsecret" => sConf("wechat.secret"),
  195. "token" => sConf("wechat.token"),
  196. "encodingaeskey" => sConf("wechat.aeskey")
  197. ]);
  198. foreach ($shop['wx'] as $val) {
  199. if ($val['is_msg'] == 1) {
  200. $obj->send([
  201. "touser" => $val['openid'],
  202. "template_id" => "v1TVHflG9h5BPRLb2hH10r8oOV5OFMKD2cmqIw1nH-w",
  203. "data" => [
  204. "thing1" => ["value" => '微信用户'.$orderBuy['openid']],
  205. "thing6" => ["value" => $val['shop_name']],
  206. "amount3" => ["value" => format_money($orderBuy['money'] / 100)],
  207. "character_string9" => ["value" => $orderBuy['order_sn']],
  208. "time4" => ["time5" => date('Y-m-d H:i')]
  209. ]
  210. ]);
  211. }
  212. }
  213. }
  214. (new SaasUserLog)->insertAll($logData);
  215. return $this->getNotifySuccessReply();
  216. } catch (\Throwable $throwable) {
  217. return error($throwable->getMessage());
  218. }
  219. }
  220. /**
  221. * 会员卡充值
  222. */
  223. #[Route(path: "recharges",methods: "post")]
  224. public function notifyDataRecharge(Request $request)
  225. {
  226. try {
  227. echo getDateFull()."===>会员卡支付返回\n";
  228. $payResp = $request->rawBody();
  229. $data = $this->payReturn($payResp);
  230. } catch (\Throwable $throwable) {
  231. return error($throwable->getMessage());
  232. }
  233. }
  234. protected function payReturn($payResp)
  235. {
  236. $wechat = new \WeChat\Pay($this->getWxConfig());
  237. if (empty($payResp)) return $this->getNotifySuccessReply();
  238. $data = Tools::xml2arr($payResp);
  239. if (empty($data)) return $this->getNotifySuccessReply();
  240. return $data;
  241. }
  242. /**
  243. * 获取微信支付通知成功回复 XML
  244. * @return string
  245. */
  246. protected function getNotifySuccessReply(): string
  247. {
  248. return Tools::arr2xml(['return_code' => 'SUCCESS', 'return_msg' => 'OK']);
  249. }
  250. /**
  251. * 小程序配置
  252. * @return array
  253. */
  254. protected function getWxConfig(): array
  255. {
  256. return [
  257. 'token' => 'test',
  258. 'appid' => sConf("wechat.mini_appid"),
  259. 'appsecret' => sConf("wechat.mini_secret"),
  260. 'encodingaeskey' => 'BJIUzE0gqlWy0GxfPp4J1oPTBmOrNDIGPNav1YFH5Z5',
  261. // 配置商户支付参数(可选,在使用支付功能时需要)
  262. 'mch_id' => sConf("wechat.mch_id"),
  263. 'mch_key' => sConf("wechat.mch_key")
  264. ];
  265. }
  266. }