Notify.php 5.9 KB

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