PaymentLogic.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeadmin快速开发前后端分离管理后台(PHP版)
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
  8. // | github下载:https://github.com/likeshop-github/likeadmin
  9. // | 访问官网:https://www.likeadmin.cn
  10. // | likeadmin团队 版权所有 拥有最终解释权
  11. // +----------------------------------------------------------------------
  12. // | author: likeadminTeam
  13. // +----------------------------------------------------------------------
  14. namespace app\common\logic;
  15. use app\common\cache\MasterWokerTokenCache;
  16. use app\common\enum\PayEnum;
  17. use app\common\enum\YesNoEnum;
  18. use app\common\model\effective\EffectiveCategory;
  19. use app\common\model\group_activity\GroupOrder;
  20. use app\common\model\master_worker\MappingMworkerSession;
  21. use app\common\model\pay\PayWay;
  22. use app\common\model\recharge\RechargeOrder;
  23. use app\common\model\shops\ShopOrders;
  24. use app\common\model\group_activity\GroupUserOrder;
  25. use app\common\model\user\User;
  26. use app\common\service\pay\AliPayService;
  27. use app\common\service\pay\WeChatPayService;
  28. use app\common\service\pay\WorkerWeChatPayService;
  29. use think\Exception;
  30. /**
  31. * 支付逻辑
  32. * Class PaymentLogic
  33. * @package app\common\logic
  34. */
  35. class PaymentLogic extends BaseLogic
  36. {
  37. /**
  38. * @notes 支付方式
  39. * @param $userId
  40. * @param $terminal
  41. * @param $params
  42. * @return array|false
  43. * @author 段誉
  44. * @date 2023/2/24 17:53
  45. */
  46. public static function getPayWay($userId, $terminal, $params)
  47. {
  48. try {
  49. $order = RechargeOrder::findOrEmpty($params['order_id'])->toArray();
  50. if (empty($order)) {
  51. throw new \Exception('待支付订单不存在');
  52. }
  53. //获取支付场景
  54. $pay_way = PayWay::alias('pw')
  55. ->join('dev_pay_config dp', 'pw.pay_config_id = dp.id')
  56. ->where(['pw.scene' => $terminal, 'pw.status' => YesNoEnum::YES])
  57. ->field('dp.id,dp.name,dp.pay_way,dp.icon,dp.sort,dp.remark,pw.is_default')
  58. ->order('pw.is_default desc,dp.sort desc,id asc')
  59. ->select()
  60. ->toArray();
  61. foreach ($pay_way as $k => &$item) {
  62. if ($item['pay_way'] == PayEnum::WECHAT_PAY) {
  63. $item['extra'] = '微信快捷支付';
  64. }
  65. if ($item['pay_way'] == PayEnum::ALI_PAY) {
  66. $item['extra'] = '支付宝快捷支付';
  67. }
  68. if ($item['pay_way'] == PayEnum::BALANCE_PAY) {
  69. $user_money = User::where(['id' => $userId])->value('user_money');
  70. $item['extra'] = '可用余额:' . $user_money;
  71. }
  72. // 充值时去除余额支付
  73. if ($params['from'] == 'recharge' && $item['pay_way'] == PayEnum::BALANCE_PAY) {
  74. unset($pay_way[$k]);
  75. }
  76. }
  77. return [
  78. 'lists' => array_values($pay_way),
  79. 'order_amount' => $order['order_amount'],
  80. ];
  81. } catch (\Exception $e) {
  82. self::setError($e->getMessage());
  83. return false;
  84. }
  85. }
  86. /**
  87. * @notes 获取支付状态
  88. * @param $params
  89. * @return array|false
  90. * @author 段誉
  91. * @date 2023/3/1 16:23
  92. */
  93. public static function getPayStatus($params)
  94. {
  95. try {
  96. switch ($params['from']) {
  97. case 'recharge':
  98. $order = RechargeOrder::where(['user_id' => $params['user_id'], 'id' => $params['order_id']])
  99. ->findOrEmpty();
  100. break;
  101. case 'goods':
  102. $order = RechargeOrder::where(['user_id' => $params['user_id'], 'id' => $params['order_id']])
  103. ->findOrEmpty();
  104. break;
  105. case 'group':
  106. $order = GroupUserOrder::where(['user_id' => $params['user_id'], 'id' => $params['order_id']])
  107. ->findOrEmpty();
  108. break;
  109. default:
  110. throw new \Exception('订单不存在');
  111. break;
  112. }
  113. $payTime = empty($order['pay_time']) ? '' : date('Y-m-d H:i:s', strtotime($order['pay_time']));
  114. $orderInfo = [
  115. 'order_id' => $order['id'],
  116. 'order_sn' => $order['sn'],
  117. 'order_amount' => $order['order_amount'],
  118. 'pay_way' => PayEnum::getPayDesc($order['pay_way']),
  119. 'pay_status' => PayEnum::getPayStatusDesc($order['pay_status']),
  120. 'pay_time' => $payTime,
  121. ];
  122. if (empty($order)) {
  123. throw new \Exception('订单不存在');
  124. }
  125. return [
  126. 'pay_status' => $order['pay_status'],
  127. 'pay_way' => $order['pay_way'],
  128. 'order' => $orderInfo
  129. ];
  130. } catch (\Exception $e) {
  131. self::setError($e->getMessage());
  132. return false;
  133. }
  134. }
  135. /**
  136. * @notes 获取预支付订单信息
  137. * @param $params
  138. * @return RechargeOrder|array|false|\think\Model
  139. */
  140. public static function getPayOrderInfo($params)
  141. {
  142. try {
  143. $order = RechargeOrder::findOrEmpty($params['order_id']);
  144. if ($order->isEmpty()) {
  145. throw new Exception('订单不存在');
  146. }
  147. //判断订单类型.服务订单尾款处理
  148. if($order['order_type'] == 0 and $order['pay_status'] == PayEnum::ISPAID)//服务工单
  149. {
  150. $order = RechargeOrder::where(['work_id'=>$order['work_id'],'pay_status'=>0])->findOrEmpty();
  151. if($order->isEmpty()){
  152. throw new Exception('订单已支付');
  153. }
  154. }
  155. if ($order['pay_status'] == PayEnum::ISPAID) {
  156. throw new Exception('订单已支付');
  157. }
  158. return $order;
  159. } catch (\Exception $e) {
  160. self::$error = $e->getMessage();
  161. return false;
  162. }
  163. }
  164. /**
  165. * @notes 获取电子商城预支付订单信息
  166. * @param $params
  167. * @return RechargeOrder|array|false|\think\Model
  168. */
  169. public static function getPayShopOrderInfo($params)
  170. {
  171. try {
  172. $order = ShopOrders::findOrEmpty($params['order_id']);
  173. if ($order->isEmpty()) {
  174. throw new Exception('订单不存在');
  175. }
  176. if ($order['pay_status'] == PayEnum::ISPAID) {
  177. throw new Exception('订单已支付');
  178. }
  179. return $order;
  180. } catch (\Exception $e) {
  181. self::$error = $e->getMessage();
  182. return false;
  183. }
  184. }
  185. /**
  186. * @notes 获取拼团预支付订单信息
  187. * @param $params
  188. * @return RechargeOrder|array|false|\think\Model
  189. */
  190. public static function getPayGroupOrderInfo($params)
  191. {
  192. try {
  193. $order = GroupUserOrder::findOrEmpty($params['order_id']);
  194. if ($order->isEmpty()) {
  195. throw new Exception('订单不存在');
  196. }
  197. if ($order['pay_status'] == PayEnum::ISPAID) {
  198. throw new Exception('订单已支付');
  199. }
  200. $group_order = GroupOrder::where(['id'=>$order['group_order_id']])->findOrEmpty();
  201. if ($group_order['num'] >= 100) {
  202. throw new Exception('拼团人数已满!');
  203. }
  204. if ($group_order['status'] == 1 ) {
  205. throw new Exception('订单已支付!');
  206. }
  207. if ($group_order['status'] >= 1 ) {
  208. throw new Exception('订单已取消!');
  209. }
  210. if ($group_order['end_time'] < time()) {
  211. throw new Exception('拼团活动已结束!');
  212. }
  213. return $order;
  214. } catch (\Exception $e) {
  215. self::$error = $e->getMessage();
  216. return false;
  217. }
  218. }
  219. /**
  220. * @notes 支付
  221. * @param $payWay
  222. * @param $from
  223. * @param $order
  224. * @param $terminal
  225. * @param $redirectUrl
  226. * @return array|false|mixed|string|string[]
  227. * @throws \Exception
  228. * @author mjf
  229. * @date 2024/3/18 16:49
  230. */
  231. public static function pay($payWay, $from, $order, $terminal, $redirectUrl)
  232. {
  233. // 支付编号-仅为微信支付预置(同一商户号下不同客户端支付需使用唯一订单号)
  234. $paySn = $order['sn'];
  235. if ($payWay == PayEnum::WECHAT_PAY) {
  236. $paySn = self::formatOrderSn($order['sn'], $terminal);
  237. }
  238. //更新支付方式
  239. switch ($from) {
  240. case 'recharge':
  241. RechargeOrder::update(['pay_way' => $payWay, 'pay_sn' => $paySn], ['id' => $order['id']]);
  242. break;
  243. case 'goods':
  244. RechargeOrder::update(['pay_way' => $payWay, 'pay_sn' => $paySn], ['id' => $order['id']]);
  245. break;
  246. case 'group':
  247. GroupUserOrder::update(['pay_way' => $payWay, 'pay_sn' => $paySn], ['id' => $order['id']]);
  248. break;
  249. }
  250. if ($order['order_amount'] == 0) {
  251. PayNotifyLogic::handle($from, $order['sn']);
  252. return ['sn' => $order['sn'],'need_pay'=>0];
  253. }
  254. //todo 测试订单
  255. // if($order['id']==11){
  256. // PayNotifyLogic::handle($from, $order['sn']);
  257. // return ['sn' => $order['sn'],'need_pay'=>0];
  258. // }
  259. $payService = null;
  260. switch ($payWay) {
  261. case PayEnum::WECHAT_PAY:
  262. $payService = (new WeChatPayService($terminal, $order['user_id'] ?? null));
  263. $order['pay_sn'] = $paySn;
  264. $order['redirect_url'] = $redirectUrl;
  265. $result = $payService->pay($from, $order);
  266. break;
  267. case PayEnum::ALI_PAY:
  268. $payService = (new AliPayService($terminal));
  269. $order['redirect_url'] = $redirectUrl;
  270. $result = $payService->pay($from, $order);
  271. break;
  272. default:
  273. self::$error = '订单异常';
  274. $result = false;
  275. }
  276. if (false === $result && !self::hasError()) {
  277. self::setError($payService->getError());
  278. }
  279. $result['need_pay'] = 1;
  280. return $result;
  281. }
  282. /**
  283. * @notes 支付
  284. * @param $payWay
  285. * @param $from
  286. * @param $order
  287. * @param $terminal
  288. * @param $redirectUrl
  289. * @return array|false|mixed|string|string[]
  290. * @throws \Exception
  291. * @author mjf
  292. * @date 2024/3/18 16:49
  293. */
  294. public static function workerPay($payWay, $from, $order, $userInfo, $redirectUrl)
  295. {
  296. $terminal = $userInfo['terminal'];
  297. // 支付编号-仅为微信支付预置(同一商户号下不同客户端支付需使用唯一订单号)
  298. $paySn = $order['sn'];
  299. if ($payWay == PayEnum::WECHAT_PAY) {
  300. $paySn = self::formatOrderSn($order['sn'], $terminal);
  301. }
  302. //更新支付方式
  303. switch ($from) {
  304. case 'recharge':
  305. RechargeOrder::update(['pay_way' => $payWay, 'pay_sn' => $paySn], ['id' => $order['id']]);
  306. break;
  307. case 'goods':
  308. RechargeOrder::update(['pay_way' => $payWay, 'pay_sn' => $paySn], ['id' => $order['id']]);
  309. break;
  310. }
  311. if ($order['order_amount'] == 0) {
  312. PayNotifyLogic::handle($from, $order['sn']);
  313. return ['sn' => $order['sn'],'need_pay'=>0];
  314. }
  315. $mapSession = MappingMworkerSession::where([['type', '=', 1],['map_token', '=', $userInfo['token']], ['expire_time', '>', time()]])->findOrEmpty();
  316. if(!$mapSession->isEmpty()) {
  317. $token = '';
  318. $mapSession->token && $token = $mapSession->token;
  319. $token && $userInfo = (new MasterWokerTokenCache())->getUserInfo($token);
  320. }
  321. $payService = null;
  322. switch ($payWay) {
  323. case PayEnum::WECHAT_PAY:
  324. $payService = (new WorkerWeChatPayService($terminal, $userInfo['user_id'] ?? null));
  325. $order['pay_sn'] = $paySn;
  326. $order['redirect_url'] = $redirectUrl;
  327. $result = $payService->pay($from, $order);
  328. break;
  329. default:
  330. self::$error = '订单异常';
  331. $result = false;
  332. }
  333. if (false === $result && !self::hasError()) {
  334. self::setError($payService->getError());
  335. }
  336. $result['need_pay'] = 1;
  337. return $result;
  338. }
  339. /**
  340. * @notes 电子商城支付
  341. * @param $payWay
  342. * @param $from
  343. * @param $order
  344. * @param $terminal
  345. * @param $redirectUrl
  346. * @return array|false|mixed|string|string[]
  347. * @throws \Exception
  348. */
  349. public static function shopPay($payWay, $from, $order, $userInfo, $redirectUrl)
  350. {
  351. $terminal = $userInfo['terminal'];
  352. // 支付编号-仅为微信支付预置(同一商户号下不同客户端支付需使用唯一订单号)
  353. $paySn = $order['sn'];
  354. if ($payWay == PayEnum::WECHAT_PAY) {
  355. $paySn = self::formatOrderSn($order['sn'], $terminal);
  356. }
  357. //更新支付方式
  358. switch ($from) {
  359. case 'shop_goods':
  360. ShopOrders::update(['pay_way' => $payWay, 'pay_sn' => $paySn], ['id' => $order['id']]);
  361. break;
  362. }
  363. if ($order['order_amount'] == 0) {
  364. PayNotifyLogic::handle($from, $order['sn']);
  365. return ['sn' => $order['sn'],'need_pay'=>0];
  366. }
  367. $payService = null;
  368. switch ($payWay) {
  369. case PayEnum::WECHAT_PAY:
  370. $payService = (new WorkerWeChatPayService($terminal, $userInfo['user_id'] ?? null));
  371. $order['pay_sn'] = $paySn;
  372. $order['redirect_url'] = $redirectUrl;
  373. $result = $payService->pay($from, $order);
  374. break;
  375. default:
  376. self::$error = '订单异常';
  377. $result = false;
  378. }
  379. if (false === $result && !self::hasError()) {
  380. self::setError($payService->getError());
  381. }
  382. // $result['need_pay'] = 1;
  383. return $result;
  384. }
  385. /**
  386. * @notes 设置订单号 支付回调时截取前面的单号 18个
  387. * @param $orderSn
  388. * @param $terminal
  389. * @return string
  390. * @author 段誉
  391. * @date 2023/3/1 16:31
  392. * @remark 回调时使用了不同的回调地址,导致跨客户端支付时(例如小程序,公众号)可能出现201,商户订单号重复错误
  393. */
  394. public static function formatOrderSn($orderSn, $terminal)
  395. {
  396. $suffix = mb_substr(time(), -4);
  397. return $orderSn . $terminal . $suffix;
  398. }
  399. }