PaymentLogic.php 13 KB

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