Order.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. namespace app\controller\api;
  3. use app\extra\basic\Base;
  4. use app\extra\dyLife\Crypt;
  5. use app\extra\dyMini\Pay;
  6. use app\middleware\AuthMiddleware;
  7. use app\model\saas\SaasOrder;
  8. use app\model\saas\SaasOrderPrice;
  9. use app\service\saas\OrderService;
  10. use DI\Attribute\Inject;
  11. use LinFly\Annotation\Attributes\Route\Controller;
  12. use LinFly\Annotation\Attributes\Route\GetMapping;
  13. use LinFly\Annotation\Attributes\Route\Middleware;
  14. use LinFly\Annotation\Attributes\Route\PostMapping;
  15. use support\Request;
  16. use support\Response;
  17. #[Controller("/dy/order"),Middleware(AuthMiddleware::class)]
  18. class Order extends Base
  19. {
  20. #[Inject]
  21. protected SaasOrder $model;
  22. #[Inject]
  23. protected OrderService $service;
  24. #[GetMapping('list')]
  25. public function getDataList(Request $request): Response
  26. {
  27. try {
  28. $param = $request->all();
  29. if (!empty($param['size'])) {
  30. $param['pageSize'] = $param['size'];
  31. }
  32. if ($param['status'] > 0) {
  33. $param['status'] = $param['status'] - 1;
  34. }
  35. $param['openid'] = $request->user['openid'];
  36. $data = $this->service->setModel()->getList($param);
  37. return successTrans("success.data",pageFormat($data));
  38. } catch (\Throwable $th) {
  39. return error($th->getMessage());
  40. }
  41. }
  42. #[PostMapping("refund")]
  43. public function orderRefund(Request $request): Response
  44. {
  45. try {
  46. $param = $this->_valid([
  47. "order.require" => trans("empty.require"),
  48. "refund.require" => trans("empty.require"),
  49. ],$request->method());
  50. if (!is_array($param)) return error($param);
  51. $order = $this->model->where("order_sn",$param['order'])->with(['product','poi'])->findOrEmpty();
  52. if ($order->isEmpty()) return errorTrans("empty.data");
  53. if ($order['openid'] <> $request->user['openid']) return errorTrans("empty.data");
  54. if ($order['status'] <> 1) return error("当前订单不支持退款");
  55. $reason = explode(",",$param['refund']);
  56. $payParam = [
  57. "order_sn" => $order['order_sn'],
  58. "total" => $order['pay_money'],
  59. "reason" => $reason[0]??'不想要了',
  60. "out_refund_no" => $order['pay_sn'],
  61. "notify_url" => "https://tran.jsshuita.cn/notify/refund"
  62. ];
  63. $byteAuthorization = (new Pay)->config([
  64. "appid" => sConf("wechat.mini_appid"),
  65. "secret" => sConf("wechat.mini_secret"),
  66. "salt" => sConf("wechat.mch_salt"),
  67. ])->createRefund($payParam);
  68. if (!$byteAuthorization) return error("发起退款失败");
  69. $order->status = 4;
  70. $order->refund_apply = getDateFull();
  71. $state = $order->save();
  72. if (!$state) return errorTrans("error.data");
  73. return successTrans("提交申请成功");
  74. } catch (\Throwable $throwable) {
  75. return error($throwable->getMessage());
  76. }
  77. }
  78. #[GetMapping('detail')]
  79. public function getDataDetail(Request $request): Response
  80. {
  81. try {
  82. $param = $this->_valid([
  83. "order.require" => trans("empty.require")
  84. ],$request->method());
  85. if (!is_array($param)) return error($param);
  86. $order = $this->model->where("order_sn",$param['order'])->with(['product','poi'])->findOrEmpty();
  87. if ($order->isEmpty()) return errorTrans("empty.data");
  88. return $this->encode("ok",$order->toArray());
  89. } catch (\Throwable $th) {
  90. return error($th->getMessage());
  91. }
  92. }
  93. #[GetMapping("confirm")]
  94. public function confirmOrder(Request $request): Response
  95. {
  96. try {
  97. $param = $this->_valid([
  98. "order.require" => trans("empty.require")
  99. ],$request->method());
  100. if (!is_array($param)) return error($param);
  101. $order = $this->model->where("order_sn",$param['order'])->with(['product','poi'])->findOrEmpty();
  102. if ($order->isEmpty()) return errorTrans("empty.data");
  103. if ($order['status'] <> 0) return errorTrans("empty.data");
  104. return $this->encode("ok",$order->toArray());
  105. } catch (\Throwable $throwable) {
  106. return error($throwable->getMessage());
  107. }
  108. }
  109. /**
  110. * 差价支付
  111. * @param Request $request
  112. * @return Response
  113. */
  114. #[PostMapping("pricePay")]
  115. public function orderPricePay(Request $request): Response
  116. {
  117. try {
  118. $param = $this->_valid([
  119. "order.require" => trans("empty.require")
  120. ],$request->method());
  121. if (!is_array($param)) return error($param);
  122. $order = (new SaasOrderPrice)->where("order_sn",$param['order'])->findOrEmpty();
  123. $payParam = [
  124. "order_sn" => $order['order_sn'],
  125. "total" => $order['price'],
  126. "name" => $order['name'],
  127. "notify_url" => "https://tran.jsshuita.cn/notify/price"
  128. ];
  129. $byteAuthorization = (new Pay)->config([
  130. "appid" => sConf("wechat.mini_appid"),
  131. "secret" => sConf("wechat.mini_secret"),
  132. "salt" => sConf("wechat.mch_salt"),
  133. ])->createOrder($payParam);
  134. return success("ok",['pay' => $byteAuthorization]);
  135. } catch (\Throwable $throwable) {
  136. return error($throwable->getMessage());
  137. }
  138. }
  139. /**
  140. * 发起支付
  141. * @param Request $request
  142. * @return Response
  143. */
  144. #[PostMapping("toPay")]
  145. public function orderPay(Request $request): Response
  146. {
  147. try {
  148. $param = $this->_valid([
  149. "order.require" => trans("empty.require"),
  150. "mobile.default" => ""
  151. ],$request->method());
  152. if (!is_array($param)) return error($param);
  153. $order = $this->model->where("order_sn",$param['order'])->with(['product','poi'])->findOrEmpty();
  154. $payParam = [
  155. "order_sn" => $order['order_sn'],
  156. "total" => $order['price'],
  157. "name" => $order['product']['product_name'],
  158. "notify_url" => "https://tran.jsshuita.cn/notify/douyin"
  159. ];
  160. $byteAuthorization = (new Pay)->config([
  161. "appid" => sConf("wechat.mini_appid"),
  162. "secret" => sConf("wechat.mini_secret"),
  163. "salt" => sConf("wechat.mch_salt"),
  164. ])->createOrder($payParam);
  165. if (!empty($param['mobile'])) {
  166. $order->mobile = $param['mobile'];
  167. $order->save();
  168. }
  169. return success("ok",['pay' => $byteAuthorization]);
  170. } catch (\Throwable $throwable) {
  171. return error($throwable->getMessage());
  172. }
  173. }
  174. #[PostMapping("mobile")]
  175. public function getOrderMobile(Request $request)
  176. {
  177. try {
  178. $param = $request->post();
  179. $mobile = [];
  180. if (!empty($param['code'])) {
  181. $mobileStr = (new Crypt)->config($this->getDyConfig())->token()->getMobile($param['code']);
  182. if (!empty($mobileStr)) {
  183. $mobile = $this->decrypt2code(sConf('wechat.min_private_key'), $mobileStr);
  184. }
  185. }
  186. $mobileStr = "";
  187. if (!empty($mobile['purePhoneNumber'])) {
  188. $mobileStr = $mobile['purePhoneNumber'];
  189. }
  190. return success("ok",['mobile' => $mobileStr]);
  191. } catch (\Throwable $throwable) {
  192. return error($throwable->getMessage());
  193. }
  194. }
  195. protected function decrypt2code($private_key, $ciphertext_str) {
  196. // 解码 base64 密文
  197. $ciphertext = base64_decode($ciphertext_str);
  198. // 使用私钥解密
  199. openssl_private_decrypt($ciphertext, $plaintext, $private_key, OPENSSL_PKCS1_PADDING);
  200. if ($plaintext === false) {
  201. return [];
  202. }
  203. return json_decode($plaintext,true);
  204. }
  205. protected function decrypt($encrypted_data, $session_key, $iv) {
  206. $data = base64_decode($encrypted_data);
  207. $key = base64_decode($session_key);
  208. $iv_decoded = base64_decode($iv);
  209. // 使用 AES-256-CBC 模式解密
  210. $decrypted = openssl_decrypt($data, 'AES-256-CBC', $key, OPENSSL_RAW_DATA, $iv_decoded);
  211. return $decrypted;
  212. }
  213. }