PaymentLogic.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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\model\agricultural_machinery\UserService;
  22. use app\common\service\pay\WeChatPayService;
  23. /**
  24. * 支付逻辑
  25. * Class PaymentLogic
  26. * @package app\common\logic
  27. */
  28. class PaymentLogic extends BaseLogic
  29. {
  30. /**
  31. * @notes 支付方式
  32. * @param $userId
  33. * @param $terminal
  34. * @param $params
  35. * @return array|false
  36. * @author 段誉
  37. * @date 2023/2/24 17:53
  38. */
  39. public static function getPayWay($userId, $terminal, $params)
  40. {
  41. try {
  42. if ($params['from'] == 'recharge') {
  43. // 充值
  44. $order = RechargeOrder::findOrEmpty($params['order_id'])->toArray();
  45. }
  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. * @author 段誉
  127. * @date 2023/2/27 15:19
  128. */
  129. public static function getPayOrderInfo($params)
  130. {
  131. try {
  132. switch ($params['from']) {
  133. case 'recharge':
  134. $order = RechargeOrder::findOrEmpty($params['order_id']);
  135. if ($order->isEmpty()) {
  136. throw new \Exception('充值订单不存在');
  137. }
  138. break;
  139. case 'service':
  140. $order = RechargeOrder::findOrEmpty($params['order_id']);
  141. if ($order->isEmpty()) {
  142. throw new \Exception('服务订单不存在');
  143. }
  144. break;
  145. }
  146. if ($order['pay_status'] == PayEnum::ISPAID) {
  147. throw new \Exception('订单已支付');
  148. }
  149. $desc = '';
  150. $user_service = UserService::where(['order_id'=>$params['order_id']])->find();
  151. if($user_service){
  152. switch ($user_service['type']){
  153. case 1:
  154. $desc = '入驻农机手服务费用';
  155. break;
  156. case 2:
  157. $desc = '入驻烘干服务费用';
  158. break;
  159. case 3:
  160. $desc = '入驻飞防服务费用';
  161. break;
  162. }
  163. }
  164. $order['desc'] = $desc;
  165. return $order;
  166. } catch (\Exception $e) {
  167. self::$error = $e->getMessage();
  168. return false;
  169. }
  170. }
  171. /**
  172. * @notes 支付
  173. * @param $payWay
  174. * @param $from
  175. * @param $order
  176. * @param $terminal
  177. * @param $redirectUrl
  178. * @return array|false|mixed|string|string[]
  179. * @throws \Exception
  180. * @author mjf
  181. * @date 2024/3/18 16:49
  182. */
  183. public static function pay($payWay, $from, $order, $terminal, $redirectUrl)
  184. {
  185. // 支付编号-仅为微信支付预置(同一商户号下不同客户端支付需使用唯一订单号)
  186. $paySn = $order['sn'];
  187. if ($payWay == PayEnum::WECHAT_PAY) {
  188. $paySn = self::formatOrderSn($order['sn'], $terminal);
  189. }
  190. //更新支付方式
  191. switch ($from) {
  192. case 'recharge':
  193. RechargeOrder::update(['pay_way' => $payWay, 'pay_sn' => $paySn], ['id' => $order['id']]);
  194. break;
  195. case 'service':
  196. RechargeOrder::update(['pay_way' => $payWay, 'pay_sn' => $paySn], ['id' => $order['id']]);
  197. break;
  198. }
  199. if ($order['order_amount'] == 0) {
  200. PayNotifyLogic::handle($from, $order['sn']);
  201. return ['pay_way' => PayEnum::BALANCE_PAY];
  202. }
  203. $payService = null;
  204. switch ($payWay) {
  205. case PayEnum::WECHAT_PAY:
  206. $payService = (new WeChatPayService($terminal, $order['user_id'] ?? null));
  207. $order['pay_sn'] = $paySn;
  208. $order['redirect_url'] = $redirectUrl;
  209. $result = $payService->pay($from, $order);
  210. break;
  211. case PayEnum::ALI_PAY:
  212. $payService = (new AliPayService($terminal));
  213. $order['redirect_url'] = $redirectUrl;
  214. $result = $payService->pay($from, $order);
  215. break;
  216. default:
  217. self::$error = '订单异常';
  218. $result = false;
  219. }
  220. if (false === $result && !self::hasError()) {
  221. self::setError($payService->getError());
  222. }
  223. return $result;
  224. }
  225. /**
  226. * @notes 设置订单号 支付回调时截取前面的单号 18个
  227. * @param $orderSn
  228. * @param $terminal
  229. * @return string
  230. * @author 段誉
  231. * @date 2023/3/1 16:31
  232. * @remark 回调时使用了不同的回调地址,导致跨客户端支付时(例如小程序,公众号)可能出现201,商户订单号重复错误
  233. */
  234. public static function formatOrderSn($orderSn, $terminal)
  235. {
  236. $suffix = mb_substr(time(), -4);
  237. return $orderSn . $terminal . $suffix;
  238. }
  239. }