Recharge.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\api\service;
  13. use app\api\model\Payment as PaymentModel;
  14. use app\api\model\Setting as SettingModel;
  15. use app\api\model\recharge\Plan as PlanModel;
  16. use app\api\service\User as UserService;
  17. use app\api\service\recharge\Payment as PaymentService;
  18. use app\common\service\BaseService;
  19. use cores\exception\BaseException;
  20. /**
  21. * 用户余额充值服务
  22. * Class Recharge
  23. * @package app\api\controller
  24. */
  25. class Recharge extends BaseService
  26. {
  27. // 提示信息
  28. private string $message = '';
  29. // 支付方式 (微信支付、支付宝)
  30. private string $method;
  31. // 下单的客户端
  32. private string $client;
  33. /**
  34. * 设置当前支付方式
  35. * @param string $method 支付方式
  36. * @return $this
  37. */
  38. public function setMethod(string $method): Recharge
  39. {
  40. $this->method = $method;
  41. return $this;
  42. }
  43. /**
  44. * 设置下单的客户端
  45. * @param string $client 客户端
  46. * @return $this
  47. */
  48. public function setClient(string $client): Recharge
  49. {
  50. $this->client = $client;
  51. return $this;
  52. }
  53. /**
  54. * 充值中心页面数据
  55. * @param string $client 当前客户端
  56. * @return array
  57. * @throws BaseException
  58. * @throws \think\db\exception\DataNotFoundException
  59. * @throws \think\db\exception\DbException
  60. * @throws \think\db\exception\ModelNotFoundException
  61. */
  62. public function center(string $client): array
  63. {
  64. // 当期用户信息
  65. $userInfo = UserService::getCurrentLoginUser(true);
  66. // 获取充值方案列表
  67. $PlanModel = new PlanModel;
  68. $planList = $PlanModel->getList();
  69. // 根据指定客户端获取可用的支付方式
  70. $PaymentModel = new PaymentModel;
  71. $methods = $PaymentModel->getMethodsByClient($client, false);
  72. // 充值设置
  73. $setting = SettingModel::getRecharge();
  74. // 返回数据
  75. return [
  76. 'setting' => $setting,
  77. 'personal' => $userInfo,
  78. 'planList' => $planList,
  79. 'paymentMethods' => $methods
  80. ];
  81. }
  82. /**
  83. * 确认订单支付事件
  84. * @param int|null $planId 方案ID
  85. * @param string|null $customMoney 自定义金额
  86. * @param array $extra 附加数据
  87. * @return array[]
  88. * @throws BaseException
  89. * @throws \think\db\exception\DataNotFoundException
  90. * @throws \think\db\exception\DbException
  91. * @throws \think\db\exception\ModelNotFoundException
  92. */
  93. public function orderPay(?int $planId = null, string $customMoney = null, array $extra = []): array
  94. {
  95. $PaymentService = new PaymentService;
  96. $result = $PaymentService->setMethod($this->method)
  97. ->setClient($this->client)
  98. ->orderPay($planId, $customMoney, $extra);
  99. $this->message = $PaymentService->getMessage();
  100. return $result;
  101. }
  102. /**
  103. * 交易查询
  104. * 查询第三方支付订单是否付款成功
  105. * @param string $outTradeNo 商户订单号
  106. * @return bool
  107. * @throws BaseException
  108. * @throws \think\db\exception\DataNotFoundException
  109. * @throws \think\db\exception\DbException
  110. * @throws \think\db\exception\ModelNotFoundException
  111. */
  112. public function tradeQuery(string $outTradeNo): bool
  113. {
  114. $PaymentService = new PaymentService;
  115. return $PaymentService->setMethod($this->method)->setClient($this->client)->tradeQuery($outTradeNo);
  116. }
  117. /**
  118. * 返回消息提示
  119. * @return string
  120. */
  121. public function getMessage(): string
  122. {
  123. return $this->message;
  124. }
  125. }