Recharge.php 1.9 KB

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