截流自动化的商城平台

OrderRenewLogic.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. <?php
  2. /*
  3. * @Author: xiaohai zmhwork@qq.com
  4. * @Date: 2025-03-14 17:38:50
  5. * @LastEditors: xiaohai zmhwork@qq.com
  6. * @LastEditTime: 2025-03-22 18:24:44
  7. * @FilePath: \opkpm\app\shop\logic\order\OrderRenewLogic.php
  8. * @Description: 续费订单逻辑处理
  9. */
  10. namespace app\shop\logic\order;
  11. use app\common\model\shop\OrderRenew;
  12. use app\common\model\shop\ShopGoodsRenew;
  13. use app\common\basics\Logic;
  14. use app\common\enum\OrderEnum;
  15. use app\common\model\shop\Shop;
  16. use app\common\model\shop\ShopHkLog;
  17. use app\common\server\AliPayServer;
  18. use app\common\server\YansongdaAliPayServer;
  19. use app\shop\validate\order\OrderRenewValidate;
  20. use think\facade\Db;
  21. use think\facade\Log;
  22. use Yansongda\Pay\Pay;
  23. class OrderRenewLogic extends Logic
  24. {
  25. public static function lists($get)
  26. {
  27. $page = $get['page'] ?? 1;
  28. $limit = $get['limit'] ?? 10;
  29. $where[] = ['del', '=', 0];
  30. $where[] = ['shop_id', '=', $get['shop_id']];
  31. $count = OrderRenew::where($where)->count();
  32. $lists = OrderRenew::where($where)
  33. ->page($page, $limit)
  34. ->order('id', 'desc')
  35. ->select()->toArray();
  36. foreach ($lists as $key => $value) {
  37. $lists[$key]['type_str'] = $value['renew_type_id'] == 0 ? '包月' : '按量付费';
  38. $lists[$key]['order_status_str'] = OrderEnum::getOrderStatus($value['order_status']);
  39. $lists[$key]['pay_status_str'] = OrderEnum::getPayStatus($value['pay_status']);
  40. $lists[$key]['pay_way_str'] = $value['pay_way'] == 1 ? '微信' : '支付宝';
  41. $lists[$key]['pay_time_str'] = $value['pay_time'] ? date('Y-m-d H:i:s', $value['pay_time']) : '';
  42. }
  43. return ['count' => $count, 'lists' => $lists];
  44. }
  45. public static function renewLists($get)
  46. {
  47. $page = $get['page'] ?? 1;
  48. $limit = $get['limit'] ?? 10;
  49. $where = [
  50. ['del', '=', 0],
  51. //['status', '=', 1]
  52. ];
  53. if (!empty($get['name']) && $get['name']) {
  54. $where[] = ['name', 'like', '%' . $get['name'] . '%'];
  55. }
  56. // var_dump($get, $where);
  57. $count = ShopGoodsRenew::where($where)->count();
  58. $lists = ShopGoodsRenew::where($where)
  59. ->page($page, $limit)
  60. ->order('id', 'desc')
  61. ->select()->toArray();
  62. foreach ($lists as $key => $value) {
  63. $lists[$key]['type_str'] = $value['type_id'] == 0 ? '包月' : '按量付费';
  64. $lists[$key]['status_str'] = $value['status'] == 0 ? '禁用' : '启用';
  65. }
  66. return ['count' => $count, 'lists' => $lists];
  67. }
  68. public static function buy($id)
  69. {
  70. $where = [
  71. ['del', '=', 0],
  72. ['status', '=', 1],
  73. ['id', '=', $id]
  74. ];
  75. $info = ShopGoodsRenew::where($where)->find();
  76. if (!$info) {
  77. static::$error = '数据不存在';
  78. return false;
  79. }
  80. return $info->toArray();
  81. }
  82. public static function cancel($post)
  83. {
  84. try {
  85. validate(OrderRenewValidate::class)->scene('cancel')->check($post);
  86. } catch (\Exception $e) {
  87. static::$error = $e->getMessage();
  88. return false;
  89. }
  90. $where = [
  91. ['del', '=', 0],
  92. ['id', '=', $post['id']],
  93. ['shop_id', '=', $post['shop_id']],
  94. ];
  95. $info = OrderRenew::where($where)->find();
  96. if (!$info) {
  97. self::$error = '数据不存在';
  98. return false;
  99. }
  100. if ($info->order_status > 0) {
  101. self::$error = '订单状态不可取消';
  102. return false;
  103. }
  104. $info->order_status = 4;
  105. $info->save();
  106. return true;
  107. }
  108. public static function add($post)
  109. {
  110. try {
  111. validate(OrderRenewValidate::class)->scene('add')->check($post);
  112. } catch (\Exception $e) {
  113. static::$error = $e->getMessage();
  114. return false;
  115. }
  116. $model = new OrderRenew();
  117. $name = $model->getName();
  118. $post['order_sn'] = createSn($name, 'order_sn');
  119. $post['renew_name'] = $post['name'];
  120. $post['renew_image'] = $post['image'] ?? "";
  121. $post['renew_desc'] = $post['desc'];
  122. $post['renew_type_id'] = $post['type_id'];
  123. $post['renew_price'] = $post['price'];
  124. $post['renew_op_count'] = $post['op_count'];
  125. $post['total_amount'] = $post['renew_price'] * $post['renew_num'];
  126. $post['total_num'] = $post['renew_num'];
  127. $post['order_amount'] = $post['total_amount'];
  128. $info = $model->create($post);
  129. return $info->toArray();
  130. }
  131. public static function payPage($get)
  132. {
  133. $where = [
  134. ['del', '=', 0],
  135. ['id', '=', $get['id']],
  136. ['shop_id', '=', $get['shop_id']],
  137. ];
  138. $info = OrderRenew::where($where)->find();
  139. if (!$info) {
  140. static::$error = '数据不存在';
  141. return false;
  142. }
  143. return $info->toArray();
  144. }
  145. private static function aliPay($info, $time_expire)
  146. {
  147. // 支付宝PC端支付
  148. $domain = request()->domain();
  149. $return_url = (string) url('shop/order.OrderRenew/lists', [], false, true);
  150. $notify_url = (string) url('shop/order.Pay/aliNotify', [], false, true);
  151. $ali_data = [
  152. 'out_trade_no' => $info['order_sn'],
  153. 'total_amount' => $info['order_amount'],
  154. 'subject' => '订单:' . $info['order_sn'],
  155. // 'return_url' => $domain . '/shop/order.OrderRenew/lists',
  156. 'return_url' => $return_url,
  157. 'time_expire' => date('Y-m-d H:i:s', $time_expire),
  158. ];
  159. $aliPayConf = YansongdaAliPayServer::config();
  160. $aliPayConf['notify_url'] = $notify_url;
  161. $aliPay = Pay::alipay($aliPayConf);
  162. // $ali_data['notify_url'] = $domain . '/shop/order.Pay/aliNotify';
  163. $ali_data['notify_url'] = $notify_url;
  164. return $aliPay->web($ali_data)->getContent();
  165. }
  166. public static function payWay($post)
  167. {
  168. try {
  169. validate(OrderRenewValidate::class)->scene('pay_way')->check($post);
  170. } catch (\Exception $e) {
  171. static::$error = $e->getMessage();
  172. return false;
  173. }
  174. $where = [
  175. ['del', '=', 0],
  176. ['id', '=', $post['id']],
  177. ['shop_id', '=', $post['shop_id']],
  178. ];
  179. $info = OrderRenew::where($where)->find();
  180. if (!$info) {
  181. static::$error = '数据不存在';
  182. return false;
  183. }
  184. if ($info['order_status'] > 0) {
  185. static::$error = '订单不处于可支付状态下';
  186. return false;
  187. }
  188. $time_expire = time() + 3600 * 2;
  189. $info->pay_way = $post['pay_way'];
  190. $info->time_expire = $time_expire - 600;
  191. $info->save();
  192. if ($post['pay_way'] == 2) {
  193. return self::aliPay($info, $time_expire);
  194. }
  195. self::$error = '暂不支持该支付方式';
  196. return false;
  197. }
  198. private static function changeHkCount($info, $time)
  199. {
  200. // 操作数量
  201. $op_count = $info['renew_op_count'] * $info['total_num'];
  202. $shop = Shop::find($info['shop_id']);
  203. // 添加用户套餐时间或者数量
  204. $hk = new ShopHkLog();
  205. $hk->shop_id = $shop['id'];
  206. $hk->source_id = $info->id;
  207. $hk->change_count = $op_count;
  208. // 包月
  209. if ($info['renew_type_id'] == 0) {
  210. $old_expire_time = $shop['expire_time'];
  211. if ($old_expire_time < $time) {
  212. $old_expire_time = $time;
  213. }
  214. $di = \DateInterval::createFromDateString(strval($op_count) . ' months');
  215. $dt = date_create_from_format('Y-m-d H:i:s', date('Y-m-d H:i:s', $old_expire_time))->add($di);
  216. $dt_str = $dt->format('Y-m-d 00:00:00');
  217. $shop->expire_time = strtotime($dt_str);
  218. $hk->source_type = 200;
  219. $hk->before_date = date('Y-m-d H:i:s', $old_expire_time);
  220. $hk->after_date = date('Y-m-d H:i:s', $shop->expire_time);
  221. $hk->remark = '[' . $info->renew_name . '],订单号:' . $info->order_sn . ',增加月份数:' . $op_count;
  222. $hk->save();
  223. } else {
  224. // 按量付费
  225. // 添加数量记录
  226. $old_hksy_count = $shop->hksy_count;
  227. $hk->left_count = $op_count + $old_hksy_count;
  228. $hk->remark = '[' . $info->renew_name . '],订单号:' . $info->order_sn . ',增加总数:' . $op_count;
  229. $hk->save();
  230. $shop->hksy_count += $op_count;
  231. }
  232. $shop->save();
  233. return true;
  234. }
  235. public static function renewLog($get)
  236. {
  237. $page = $get['page'] ?? 1;
  238. $limit = $get['limit'] ?? 10;
  239. $where = [];
  240. $where[] = ['shop_id', '=', $get['shop_id']];
  241. $type_id = $get['type_id'] ?? 1;
  242. if ($type_id == 1) {
  243. $where[] = ['source_type', '>=', 100];
  244. $where[] = ['source_type', '<', 200];
  245. } else {
  246. $where[] = ['source_type', '>=', 200];
  247. $where[] = ['source_type', '<', 300];
  248. }
  249. $count = ShopHkLog::where($where)->count();
  250. $lists = ShopHkLog::where($where)
  251. ->page($page, $limit)
  252. ->order('id', 'desc')
  253. ->select()->toArray();
  254. return ['count' => $count, 'lists' => $lists];
  255. }
  256. private static function aliPaySucc($order_sn, $result)
  257. {
  258. $time = time();
  259. Db::startTrans();
  260. try {
  261. $where = [
  262. ['del', '=', 0],
  263. ['order_sn', '=', $order_sn],
  264. ];
  265. $info = OrderRenew::where($where)->find();
  266. if (empty($info)) {
  267. static::$error = '数据不存在';
  268. return false;
  269. }
  270. $info->order_status = 3;
  271. $info->pay_status = 1;
  272. $info->pay_time = $time;
  273. $info->notify_content = $result->toJson();
  274. $info->buyer_id = $result->buyer_open_id;
  275. $info->trade_no = $result->trade_no;
  276. $info->save();
  277. self::changeHkCount($info, $time);
  278. Db::commit();
  279. } catch (\Exception $e) {
  280. Db::rollback();
  281. Log::error('aliPaySucc 支付宝回调错误:' . $e->getMessage());
  282. static::$error = $e->getMessage();
  283. return false;
  284. }
  285. return true;
  286. }
  287. public static function aliNotify($post)
  288. {
  289. try {
  290. $aliPayConf = YansongdaAliPayServer::config();
  291. $aliPay = Pay::alipay($aliPayConf);
  292. $result = $aliPay->verify($post);
  293. } catch (\Exception $e) {
  294. static::$error = $e->getMessage();
  295. return false;
  296. }
  297. // 支付成功
  298. if ($result->trade_status === 'TRADE_SUCCESS') {
  299. self::aliPaySucc($result->out_trade_no, $result);
  300. }
  301. return true;
  302. }
  303. }