Notify.php 6.5 KB

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