Notify.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. namespace app\controller\api;
  3. use app\extra\basic\Base;
  4. use app\extra\dyLife\data\OrderData;
  5. use app\extra\tools\LifeWeb;
  6. use app\model\saas\SaasGoods;
  7. use app\model\saas\SaasOrder;
  8. use app\model\saas\SaasOrderItem;
  9. use LinFly\Annotation\Attributes\Route\Controller;
  10. use LinFly\Annotation\Attributes\Route\PostMapping;
  11. use support\Request;
  12. use support\Response;
  13. use Webman\RedisQueue\Redis;
  14. #[Controller("/notify")]
  15. class Notify extends Base
  16. {
  17. #[PostMapping("douyin")]
  18. public function getOrderNotify(Request $request): Response
  19. {
  20. try {
  21. $param = $request->all();
  22. echo getDateFull()."===支付返回\n";
  23. $data = !empty($param['msg']) ? json_decode($param['msg'],true) : [];
  24. print_r($data);
  25. $orderSn = $data['out_order_no'];
  26. if ($data['status'] <> "SUCCESS") return json(['err_no' => 0,'err_tips' => "success"]);
  27. if (empty($data)) return json(['err_no' => 0,'err_tips' => "success"]);
  28. $order = (new SaasOrder)->where("out_order_no",$orderSn)->findOrEmpty();
  29. if ($order->isEmpty()) return json(['err_no' => 0,'err_tips' => "success"]);
  30. if ($order['status'] <> 0) return json(['err_no' => 0,'err_tips' => "success"]);
  31. (new SaasOrderItem)->where("order_sn",$order['order_sn'])->update(['status' => 1]);
  32. // 查询订单详情 - 延迟30秒读取
  33. Redis::send("sync-order-info",['order' => $order['order_sn']],30);
  34. // $orderData = (new OrderData)->config([
  35. // "appid" => sConf("wechat.appid"),
  36. // "secret" => sConf("wechat.secret"),
  37. // "account" => sConf("wechat.shop_id"),
  38. // ])->token()->getOrderData($order['order_sn']);
  39. // if (isset($orderData['orders'][0]['anchor_id'])) {
  40. // $order->douyin_uid = $orderData['orders'][0]['anchor_id'];
  41. // }
  42. (new SaasGoods)->where("goods_id",$order['life_goods_id'])->inc("sale_stock")->save();
  43. $order->status = 1;
  44. $order->pay_at = getDateFull();
  45. $order->pay_type = $data['pay_channel'];
  46. $order->pay_sn = $data['channel_pay_id'];
  47. $order->pay_money = $data['total_amount'];
  48. $order->is_show = 1;
  49. $order->save();
  50. return json(['err_no' => 0,'err_tips' => "success"]);
  51. } catch (\Throwable $th) {
  52. echo $th->getMessage()."\n";
  53. echo $th->getFile()."\n";
  54. echo $th->getLine()."\n";
  55. return error($th->getMessage());
  56. }
  57. }
  58. #[PostMapping("price")]
  59. public function getOrderNotifyPrice(Request $request): Response
  60. {
  61. try {
  62. $param = $request->all();
  63. echo getDateFull()."===支付返回\n";
  64. $data = !empty($param['msg']) ? json_decode($param['msg'],true) : [];
  65. print_r($data);
  66. } catch (\Throwable $throwable) {
  67. return error($throwable->getMessage());
  68. }
  69. }
  70. #[PostMapping("mini")]
  71. public function getOrderNotifyMini(Request $request): Response
  72. {
  73. try {
  74. $param = $request->all();
  75. echo getDateFull()."===支付返回\n";
  76. $data = !empty($param['msg']) ? json_decode($param['msg'],true) : [];
  77. print_r($data);
  78. $orderSn = $data['cp_extra'];
  79. if ($data['status'] <> "SUCCESS") return json(['err_no' => 0,'err_tips' => "success"]);
  80. if (empty($data)) return json(['err_no' => 0,'err_tips' => "success"]);
  81. $order = (new SaasOrder)->where("order_sn",$orderSn)->findOrEmpty();
  82. if ($order->isEmpty()) return json(['err_no' => 0,'err_tips' => "success"]);
  83. if ($order['status'] <> 0) return json(['err_no' => 0,'err_tips' => "success"]);
  84. $order->status = 1;
  85. $order->pay_at = getDateFull();
  86. $order->pay_type = $data['way'];
  87. $order->pay_sn = $data['cp_orderno'];
  88. $order->pay_money = $data['total_amount'];
  89. $order->save();
  90. return json(['err_no' => 0,'err_tips' => "success"]);
  91. } catch (\Throwable $th) {
  92. return error($th->getMessage());
  93. }
  94. }
  95. #[PostMapping("refund")]
  96. public function getOrderRefund(Request $request): Response
  97. {
  98. try {
  99. $param = $request->all();
  100. echo getDateFull()."===退款返回\n";
  101. $data = !empty($param['msg']) ? json_decode($param['msg'],true) : [];
  102. print_r($data);
  103. $orderSn = $data['order_id'];
  104. if ($data['status'] <> "SUCCESS") return json(['err_no' => 0,'err_tips' => "success"]);
  105. if (empty($data)) return json(['err_no' => 0,'err_tips' => "success"]);
  106. $order = (new SaasOrder)->where("order_sn",$orderSn)->findOrEmpty();
  107. if ($order['status'] <> 4) return json(['err_no' => 0,'err_tips' => "success"]);
  108. $itemNum = (new SaasOrderItem)->where(["order_sn" => $order['order_sn'],'status' => 2])->count();
  109. $trueNum = count($data['refund_item_detail']['item_order_detail']);
  110. if (($trueNum+$itemNum) <> $order['number']) { // 部分退款
  111. $order->status = 6;
  112. } else { // 全部退款
  113. $order->status = 3;
  114. }
  115. if (!empty($data['refund_item_detail']['item_order_detail']))
  116. {
  117. foreach ($data['refund_item_detail']['item_order_detail'] as $val) {
  118. (new SaasOrderItem)->where("item_order_id",$val['item_order_id'])->update(['status' => 2]);
  119. }
  120. }
  121. $order->refund_at = getDateFull();
  122. $order->save();
  123. return json(['err_no' => 0,'err_tips' => "success"]);
  124. } catch (\Throwable $throwable) {
  125. return error($throwable->getMessage());
  126. }
  127. }
  128. #[PostMapping("refund/mini")]
  129. public function getOrderRefundMini(Request $request): Response
  130. {
  131. try {
  132. $param = $request->all();
  133. echo getDateFull()."===小程序退款返回\n";
  134. $data = !empty($param['msg']) ? json_decode($param['msg'],true) : [];
  135. print_r($data);
  136. $orderSn = $data['cp_extra'];
  137. if ($data['status'] <> "SUCCESS") return json(['err_no' => 0,'err_tips' => "success"]);
  138. if (empty($data)) return json(['err_no' => 0,'err_tips' => "success"]);
  139. $order = (new SaasOrder)->where("order_sn",$orderSn)->findOrEmpty();
  140. if ($order['status'] <> 4) return json(['err_no' => 0,'err_tips' => "success"]);
  141. $order->status = 3;
  142. $order->refund_at = getDateFull();
  143. $order->save();
  144. return json(['err_no' => 0,'err_tips' => "success"]);
  145. } catch (\Throwable $throwable) {
  146. return error($throwable->getMessage());
  147. }
  148. }
  149. }