Recharge.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 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\controller;
  13. use app\api\model\recharge\Order as OrderModel;
  14. use app\api\service\Payment as PaymentService;
  15. use app\common\enum\OrderType as OrderTypeEnum;
  16. use app\common\exception\BaseException;
  17. /**
  18. * 用户充值管理
  19. * Class Recharge
  20. * @package app\api\controller
  21. */
  22. class Recharge extends Controller
  23. {
  24. /**
  25. * 确认充值
  26. * @param int|null $planId 方案ID
  27. * @param float|string|null $customMoney 自定义金额
  28. * @return array|\think\response\Json
  29. * @throws BaseException
  30. * @throws \think\db\exception\DataNotFoundException
  31. * @throws \think\db\exception\DbException
  32. * @throws \think\db\exception\ModelNotFoundException
  33. */
  34. public function submit(int $planId = null, $customMoney = null)
  35. {
  36. if (getPlatform() !== 'MP-WEIXIN') {
  37. return $this->renderError('很抱歉,余额充值暂时仅支持微信小程序端');
  38. }
  39. // 生成充值订单
  40. $model = new OrderModel;
  41. if (!$model->createOrder($planId, (float)$customMoney)) {
  42. return $this->renderError($model->getError() ?: '充值失败');
  43. }
  44. // 构建微信支付
  45. $payment = PaymentService::wechat(
  46. $model['order_id'],
  47. $model['order_no'],
  48. $model['pay_price'],
  49. OrderTypeEnum::RECHARGE
  50. );
  51. // 充值状态提醒
  52. $message = ['success' => '充值成功', 'error' => '订单未支付'];
  53. return $this->renderSuccess(compact('payment', 'message'));
  54. }
  55. }