Notify.php 6.7 KB

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