PaymentLogic.php 7.6 KB

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