Notify.php 6.5 KB

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