Notify.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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->save();
  47. return json(['err_no' => 0,'err_tips' => "success"]);
  48. } catch (\Throwable $th) {
  49. echo $th->getMessage()."\n";
  50. echo $th->getFile()."\n";
  51. echo $th->getLine()."\n";
  52. return error($th->getMessage());
  53. }
  54. }
  55. #[PostMapping("price")]
  56. public function getOrderNotifyPrice(Request $request): Response
  57. {
  58. try {
  59. $param = $request->all();
  60. echo getDateFull()."===支付返回\n";
  61. $data = !empty($param['msg']) ? json_decode($param['msg'],true) : [];
  62. print_r($data);
  63. } catch (\Throwable $throwable) {
  64. return error($throwable->getMessage());
  65. }
  66. }
  67. #[PostMapping("mini")]
  68. public function getOrderNotifyMini(Request $request): Response
  69. {
  70. try {
  71. $param = $request->all();
  72. echo getDateFull()."===支付返回\n";
  73. $data = !empty($param['msg']) ? json_decode($param['msg'],true) : [];
  74. print_r($data);
  75. $orderSn = $data['cp_extra'];
  76. if ($data['status'] <> "SUCCESS") return json(['err_no' => 0,'err_tips' => "success"]);
  77. if (empty($data)) return json(['err_no' => 0,'err_tips' => "success"]);
  78. $order = (new SaasOrder)->where("order_sn",$orderSn)->findOrEmpty();
  79. if ($order->isEmpty()) return json(['err_no' => 0,'err_tips' => "success"]);
  80. if ($order['status'] <> 0) return json(['err_no' => 0,'err_tips' => "success"]);
  81. $order->status = 1;
  82. $order->pay_at = getDateFull();
  83. $order->pay_type = $data['way'];
  84. $order->pay_sn = $data['cp_orderno'];
  85. $order->pay_money = $data['total_amount'];
  86. $order->save();
  87. return json(['err_no' => 0,'err_tips' => "success"]);
  88. } catch (\Throwable $th) {
  89. return error($th->getMessage());
  90. }
  91. }
  92. #[PostMapping("refund")]
  93. public function getOrderRefund(Request $request): Response
  94. {
  95. try {
  96. $param = $request->all();
  97. echo getDateFull()."===退款返回\n";
  98. $data = !empty($param['msg']) ? json_decode($param['msg'],true) : [];
  99. print_r($data);
  100. $orderSn = $data['order_id'];
  101. if ($data['status'] <> "SUCCESS") return json(['err_no' => 0,'err_tips' => "success"]);
  102. if (empty($data)) return json(['err_no' => 0,'err_tips' => "success"]);
  103. $order = (new SaasOrder)->where("order_sn",$orderSn)->findOrEmpty();
  104. if ($order['status'] <> 4) return json(['err_no' => 0,'err_tips' => "success"]);
  105. $itemNum = (new SaasOrderItem)->where(["order_sn" => $order['order_sn'],'status' => 2])->count();
  106. $trueNum = count($data['refund_item_detail']['item_order_detail']);
  107. if (($trueNum+$itemNum) <> $order['number']) { // 部分退款
  108. $order->status = 6;
  109. } else { // 全部退款
  110. $order->status = 3;
  111. }
  112. if (!empty($data['refund_item_detail']['item_order_detail']))
  113. {
  114. foreach ($data['refund_item_detail']['item_order_detail'] as $val) {
  115. (new SaasOrderItem)->where("item_order_id",$val['item_order_id'])->update(['status' => 2]);
  116. }
  117. }
  118. $order->refund_at = getDateFull();
  119. $order->save();
  120. return json(['err_no' => 0,'err_tips' => "success"]);
  121. } catch (\Throwable $throwable) {
  122. return error($throwable->getMessage());
  123. }
  124. }
  125. #[PostMapping("refund/mini")]
  126. public function getOrderRefundMini(Request $request): Response
  127. {
  128. try {
  129. $param = $request->all();
  130. echo getDateFull()."===小程序退款返回\n";
  131. $data = !empty($param['msg']) ? json_decode($param['msg'],true) : [];
  132. print_r($data);
  133. $orderSn = $data['cp_extra'];
  134. if ($data['status'] <> "SUCCESS") return json(['err_no' => 0,'err_tips' => "success"]);
  135. if (empty($data)) return json(['err_no' => 0,'err_tips' => "success"]);
  136. $order = (new SaasOrder)->where("order_sn",$orderSn)->findOrEmpty();
  137. if ($order['status'] <> 4) return json(['err_no' => 0,'err_tips' => "success"]);
  138. $order->status = 3;
  139. $order->refund_at = getDateFull();
  140. $order->save();
  141. return json(['err_no' => 0,'err_tips' => "success"]);
  142. } catch (\Throwable $throwable) {
  143. return error($throwable->getMessage());
  144. }
  145. }
  146. }