Recharge.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. /**
  3. *
  4. * @copyright ©2020 点小铺
  5. * @author hanj
  6. * @link: https://dyuit.com
  7. * Created by VSCode
  8. */
  9. namespace app\api\controller;
  10. use app\api\model\Setting as SettingModel;
  11. use app\api\model\recharge\Plan as PlanModel;
  12. use app\api\model\recharge\Order as OrderModel;
  13. use app\api\service\Payment as PaymentService;
  14. use app\common\enum\OrderType as OrderTypeEnum;
  15. /**
  16. * 用户充值管理
  17. * Class Recharge
  18. * @package app\api\controller
  19. */
  20. class Recharge extends Controller
  21. {
  22. /**
  23. * 充值中心
  24. * @return array
  25. * @throws \app\common\exception\BaseException
  26. * @throws \think\db\exception\DataNotFoundException
  27. * @throws \think\db\exception\ModelNotFoundException
  28. * @throws \think\exception\DbException
  29. */
  30. public function index()
  31. {
  32. // 用户信息
  33. $userInfo = $this->getUser();
  34. // 充值套餐列表
  35. $planList = (new PlanModel)->getList();
  36. // 充值设置
  37. $setting = SettingModel::getItem('recharge');
  38. return $this->renderSuccess(compact('userInfo', 'planList', 'setting'));
  39. }
  40. /**
  41. * 确认充值
  42. * @param null $planId
  43. * @param int $customMoney
  44. * @return array
  45. * @throws \app\common\exception\BaseException
  46. * @throws \think\exception\DbException
  47. */
  48. public function submit($planId = null, $customMoney = 0)
  49. {
  50. // 用户信息
  51. $userInfo = $this->getUser();
  52. // 生成充值订单
  53. $model = new OrderModel;
  54. if (!$model->createOrder($userInfo, $planId, $customMoney)) {
  55. return $this->renderError($model->getError() ?: '充值失败');
  56. }
  57. // 构建微信支付
  58. $payment = PaymentService::wechat(
  59. $userInfo,
  60. $model['order_id'],
  61. $model['order_no'],
  62. $model['pay_price'],
  63. OrderTypeEnum::RECHARGE
  64. );
  65. // 充值状态提醒
  66. $message = ['success' => '充值成功', 'error' => '订单未支付'];
  67. return $this->renderSuccess(compact('payment', 'message'), $message);
  68. }
  69. }