PaymentLogic.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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\enum\PayEnum;
  16. use app\common\enum\YesNoEnum;
  17. use app\common\model\effective\EffectiveCategory;
  18. use app\common\model\pay\PayWay;
  19. use app\common\model\recharge\RechargeOrder;
  20. use app\common\model\user\User;
  21. use app\common\service\pay\AliPayService;
  22. use app\common\service\pay\WeChatPayService;
  23. use app\common\service\pay\WorkerWeChatPayService;
  24. use think\Exception;
  25. /**
  26. * 支付逻辑
  27. * Class PaymentLogic
  28. * @package app\common\logic
  29. */
  30. class PaymentLogic extends BaseLogic
  31. {
  32. /**
  33. * @notes 支付方式
  34. * @param $userId
  35. * @param $terminal
  36. * @param $params
  37. * @return array|false
  38. * @author 段誉
  39. * @date 2023/2/24 17:53
  40. */
  41. public static function getPayWay($userId, $terminal, $params)
  42. {
  43. try {
  44. $order = RechargeOrder::findOrEmpty($params['order_id'])->toArray();
  45. if (empty($order)) {
  46. throw new \Exception('待支付订单不存在');
  47. }
  48. //获取支付场景
  49. $pay_way = PayWay::alias('pw')
  50. ->join('dev_pay_config dp', 'pw.pay_config_id = dp.id')
  51. ->where(['pw.scene' => $terminal, 'pw.status' => YesNoEnum::YES])
  52. ->field('dp.id,dp.name,dp.pay_way,dp.icon,dp.sort,dp.remark,pw.is_default')
  53. ->order('pw.is_default desc,dp.sort desc,id asc')
  54. ->select()
  55. ->toArray();
  56. foreach ($pay_way as $k => &$item) {
  57. if ($item['pay_way'] == PayEnum::WECHAT_PAY) {
  58. $item['extra'] = '微信快捷支付';
  59. }
  60. if ($item['pay_way'] == PayEnum::ALI_PAY) {
  61. $item['extra'] = '支付宝快捷支付';
  62. }
  63. if ($item['pay_way'] == PayEnum::BALANCE_PAY) {
  64. $user_money = User::where(['id' => $userId])->value('user_money');
  65. $item['extra'] = '可用余额:' . $user_money;
  66. }
  67. // 充值时去除余额支付
  68. if ($params['from'] == 'recharge' && $item['pay_way'] == PayEnum::BALANCE_PAY) {
  69. unset($pay_way[$k]);
  70. }
  71. }
  72. return [
  73. 'lists' => array_values($pay_way),
  74. 'order_amount' => $order['order_amount'],
  75. ];
  76. } catch (\Exception $e) {
  77. self::setError($e->getMessage());
  78. return false;
  79. }
  80. }
  81. /**
  82. * @notes 获取支付状态
  83. * @param $params
  84. * @return array|false
  85. * @author 段誉
  86. * @date 2023/3/1 16:23
  87. */
  88. public static function getPayStatus($params)
  89. {
  90. try {
  91. $order = [];
  92. $orderInfo = [];
  93. switch ($params['from']) {
  94. case 'recharge':
  95. $order = RechargeOrder::where(['user_id' => $params['user_id'], 'id' => $params['order_id']])
  96. ->findOrEmpty();
  97. $payTime = empty($order['pay_time']) ? '' : date('Y-m-d H:i:s', $order['pay_time']);
  98. $orderInfo = [
  99. 'order_id' => $order['id'],
  100. 'order_sn' => $order['sn'],
  101. 'order_amount' => $order['order_amount'],
  102. 'pay_way' => PayEnum::getPayDesc($order['pay_way']),
  103. 'pay_status' => PayEnum::getPayStatusDesc($order['pay_status']),
  104. 'pay_time' => $payTime,
  105. ];
  106. break;
  107. }
  108. if (empty($order)) {
  109. throw new \Exception('订单不存在');
  110. }
  111. return [
  112. 'pay_status' => $order['pay_status'],
  113. 'pay_way' => $order['pay_way'],
  114. 'order' => $orderInfo
  115. ];
  116. } catch (\Exception $e) {
  117. self::setError($e->getMessage());
  118. return false;
  119. }
  120. }
  121. /**
  122. * @notes 获取预支付订单信息
  123. * @param $params
  124. * @return RechargeOrder|array|false|\think\Model
  125. * @author 段誉
  126. * @date 2023/2/27 15:19
  127. */
  128. public static function getPayOrderInfo($params)
  129. {
  130. try {
  131. $order = RechargeOrder::findOrEmpty($params['order_id']);
  132. if ($order->isEmpty()) {
  133. throw new Exception('订单不存在');
  134. }
  135. //判断订单类型.服务订单尾款处理
  136. if($order['order_type'] == 0 and $order['pay_status'] == PayEnum::ISPAID)//服务工单
  137. {
  138. $order = RechargeOrder::where(['work_id'=>$order['work_id'],'pay_status'=>0])->findOrEmpty();
  139. if($order->isEmpty()){
  140. throw new Exception('订单已支付');
  141. }
  142. }
  143. if ($order['pay_status'] == PayEnum::ISPAID) {
  144. throw new Exception('订单已支付');
  145. }
  146. return $order;
  147. } catch (\Exception $e) {
  148. self::$error = $e->getMessage();
  149. return false;
  150. }
  151. }
  152. /**
  153. * @notes 支付
  154. * @param $payWay
  155. * @param $from
  156. * @param $order
  157. * @param $terminal
  158. * @param $redirectUrl
  159. * @return array|false|mixed|string|string[]
  160. * @throws \Exception
  161. * @author mjf
  162. * @date 2024/3/18 16:49
  163. */
  164. public static function pay($payWay, $from, $order, $terminal, $redirectUrl)
  165. {
  166. // 支付编号-仅为微信支付预置(同一商户号下不同客户端支付需使用唯一订单号)
  167. $paySn = $order['sn'];
  168. if ($payWay == PayEnum::WECHAT_PAY) {
  169. $paySn = self::formatOrderSn($order['sn'], $terminal);
  170. }
  171. //更新支付方式
  172. switch ($from) {
  173. case 'recharge':
  174. RechargeOrder::update(['pay_way' => $payWay, 'pay_sn' => $paySn], ['id' => $order['id']]);
  175. break;
  176. case 'goods':
  177. RechargeOrder::update(['pay_way' => $payWay, 'pay_sn' => $paySn], ['id' => $order['id']]);
  178. break;
  179. }
  180. if ($order['order_amount'] == 0) {
  181. PayNotifyLogic::handle($from, $order['sn']);
  182. return ['sn' => $order['sn'],'need_pay'=>0];
  183. }
  184. //todo 测试订单
  185. // if($order['id']==11){
  186. // PayNotifyLogic::handle($from, $order['sn']);
  187. // return ['sn' => $order['sn'],'need_pay'=>0];
  188. // }
  189. $payService = null;
  190. switch ($payWay) {
  191. case PayEnum::WECHAT_PAY:
  192. $payService = (new WeChatPayService($terminal, $order['user_id'] ?? null));
  193. $order['pay_sn'] = $paySn;
  194. $order['redirect_url'] = $redirectUrl;
  195. $result = $payService->pay($from, $order);
  196. break;
  197. case PayEnum::ALI_PAY:
  198. $payService = (new AliPayService($terminal));
  199. $order['redirect_url'] = $redirectUrl;
  200. $result = $payService->pay($from, $order);
  201. break;
  202. default:
  203. self::$error = '订单异常';
  204. $result = false;
  205. }
  206. if (false === $result && !self::hasError()) {
  207. self::setError($payService->getError());
  208. }
  209. $result['need_pay'] = 1;
  210. return $result;
  211. }
  212. /**
  213. * @notes 支付
  214. * @param $payWay
  215. * @param $from
  216. * @param $order
  217. * @param $terminal
  218. * @param $redirectUrl
  219. * @return array|false|mixed|string|string[]
  220. * @throws \Exception
  221. * @author mjf
  222. * @date 2024/3/18 16:49
  223. */
  224. public static function workerPay($payWay, $from, $order, $userInfo, $redirectUrl)
  225. {
  226. $terminal = $userInfo['terminal'];
  227. // 支付编号-仅为微信支付预置(同一商户号下不同客户端支付需使用唯一订单号)
  228. $paySn = $order['sn'];
  229. if ($payWay == PayEnum::WECHAT_PAY) {
  230. $paySn = self::formatOrderSn($order['sn'], $terminal);
  231. }
  232. //更新支付方式
  233. switch ($from) {
  234. case 'recharge':
  235. RechargeOrder::update(['pay_way' => $payWay, 'pay_sn' => $paySn], ['id' => $order['id']]);
  236. break;
  237. case 'goods':
  238. RechargeOrder::update(['pay_way' => $payWay, 'pay_sn' => $paySn], ['id' => $order['id']]);
  239. break;
  240. }
  241. if ($order['order_amount'] == 0) {
  242. PayNotifyLogic::handle($from, $order['sn']);
  243. return ['sn' => $order['sn'],'need_pay'=>0];
  244. }
  245. $payService = null;
  246. switch ($payWay) {
  247. case PayEnum::WECHAT_PAY:
  248. $payService = (new WorkerWeChatPayService($terminal, $userInfo['user_id'] ?? null));
  249. $order['pay_sn'] = $paySn;
  250. $order['redirect_url'] = $redirectUrl;
  251. $result = $payService->pay($from, $order);
  252. break;
  253. default:
  254. self::$error = '订单异常';
  255. $result = false;
  256. }
  257. if (false === $result && !self::hasError()) {
  258. self::setError($payService->getError());
  259. }
  260. $result['need_pay'] = 1;
  261. return $result;
  262. }
  263. /**
  264. * @notes 设置订单号 支付回调时截取前面的单号 18个
  265. * @param $orderSn
  266. * @param $terminal
  267. * @return string
  268. * @author 段誉
  269. * @date 2023/3/1 16:31
  270. * @remark 回调时使用了不同的回调地址,导致跨客户端支付时(例如小程序,公众号)可能出现201,商户订单号重复错误
  271. */
  272. public static function formatOrderSn($orderSn, $terminal)
  273. {
  274. $suffix = mb_substr(time(), -4);
  275. return $orderSn . $terminal . $suffix;
  276. }
  277. }