UserRechargeController.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. namespace app\api\controller\user;
  3. use app\models\store\StoreOrder;
  4. use app\models\system\SystemGroupData;
  5. use app\models\user\UserRecharge;
  6. use app\Request;
  7. use crmeb\repositories\OrderRepository;
  8. use crmeb\services\GroupDataService;
  9. use crmeb\services\SystemConfigService;
  10. use crmeb\services\UtilService;
  11. /**
  12. * 充值类
  13. * Class UserRechargeController
  14. * @package app\api\controller\user
  15. */
  16. class UserRechargeController
  17. {
  18. /**
  19. * 小程序充值
  20. *
  21. * @param Request $request
  22. * @return mixed
  23. */
  24. public function routine(Request $request)
  25. {
  26. list($price, $recharId, $type) = UtilService::postMore([['price', 0], ['rechar_id', 0], ['type', 0]], $request, true);
  27. if (!$price || $price <= 0) return app('json')->fail('参数错误');
  28. $storeMinRecharge = sys_config('store_user_min_recharge');
  29. if ($price < $storeMinRecharge) return app('json')->fail('充值金额不能低于' . $storeMinRecharge);
  30. switch ((int)$type) {
  31. case 0: //支付充值余额
  32. $paid_price = 0;
  33. if ($recharId) {
  34. $data = SystemGroupData::getDateValue($recharId);
  35. if ($data === false) {
  36. return app('json')->fail('您选择的充值方式已下架!');
  37. } else {
  38. $paid_price = $data['give_money'] ?? 0;
  39. }
  40. }
  41. $rechargeOrder = UserRecharge::addRecharge($request->uid(), $price, 'routine', $paid_price);
  42. if (!$rechargeOrder) return app('json')->fail('充值订单生成失败!');
  43. try {
  44. return app('json')->successful(UserRecharge::jsPay($rechargeOrder));
  45. } catch (\Exception $e) {
  46. return app('json')->fail($e->getMessage());
  47. }
  48. break;
  49. case 1: //佣金转入余额
  50. if (UserRecharge::importNowMoney($request->uid(), $price))
  51. return app('json')->successful('转入余额成功');
  52. else
  53. return app('json')->fail(UserRecharge::getErrorInfo());
  54. break;
  55. default:
  56. return app('json')->fail('缺少参数');
  57. break;
  58. }
  59. }
  60. /**
  61. * 公众号充值
  62. *
  63. * @param Request $request
  64. * @return mixed
  65. */
  66. public function wechat(Request $request)
  67. {
  68. list($price, $recharId, $from, $type) = UtilService::postMore([['price', 0], ['rechar_id', 0], ['from', 'weixin'], ['type', 0]], $request, true);
  69. if (!$price || $price <= 0) return app('json')->fail('参数错误');
  70. $storeMinRecharge = sys_config('store_user_min_recharge');
  71. if ($price < $storeMinRecharge) return app('json')->fail('充值金额不能低于' . $storeMinRecharge);
  72. switch ((int)$type) {
  73. case 0: //支付充值余额
  74. $paid_price = 0;
  75. if ($recharId) {
  76. $data = SystemGroupData::getDateValue($recharId);
  77. if ($data === false) {
  78. return app('json')->fail('您选择的充值方式已下架!');
  79. } else {
  80. $paid_price = $data['give_money'] ?? 0;
  81. }
  82. }
  83. $rechargeOrder = UserRecharge::addRecharge($request->uid(), $price, 'weixin', $paid_price);
  84. if (!$rechargeOrder) return app('json')->fail('充值订单生成失败!');
  85. try {
  86. if ($from == 'weixinh5') {
  87. $recharge = UserRecharge::wxH5Pay($rechargeOrder);
  88. } else if ($from == 'app') {
  89. $jsConfig = OrderRepository::appPayZf($rechargeOrder);
  90. }else {
  91. $recharge = UserRecharge::wxPay($rechargeOrder);
  92. }
  93. } catch (\Exception $e) {
  94. return app('json')->fail($e->getMessage());
  95. }
  96. return app('json')->successful(['type' => $from, 'data' => $recharge]);
  97. break;
  98. case 1: //佣金转入余额
  99. if (UserRecharge::importNowMoney($request->uid(), $price))
  100. return app('json')->successful('转入余额成功');
  101. else
  102. return app('json')->fail(UserRecharge::getErrorInfo());
  103. break;
  104. case 2: //支付宝
  105. $paid_price = 0;
  106. if ($recharId) {
  107. $data = SystemGroupData::getDateValue($recharId);
  108. if ($data === false) {
  109. return app('json')->fail('您选择的充值方式已下架!');
  110. } else {
  111. $paid_price = $data['give_money'] ?? 0;
  112. }
  113. }
  114. $rechargeOrder = UserRecharge::addRecharge($request->uid(), $price, 'alipay', $paid_price);
  115. if (!$rechargeOrder) return app('json')->fail('充值订单生成失败!');
  116. //支付金额为0
  117. if (bcsub((float)$rechargeOrder['price'], 0, 2) <= 0) {
  118. //创建订单jspay支付
  119. $payPriceStatus = StoreOrder::jsPayPrice($rechargeOrder['order_id'], $request->uid());
  120. if ($payPriceStatus)//0元支付成功
  121. return app('json')->status('success', '支付成功');
  122. else
  123. return app('json')->status('pay_error', StoreOrder::getErrorInfo());
  124. } else {
  125. try {
  126. $jsConfig = OrderRepository::aliPay($rechargeOrder); //创建订单jspay
  127. } catch (\Exception $e) {
  128. return app('json')->status('pay_error', $e->getMessage());
  129. }
  130. $info['jsConfig'] = $jsConfig;
  131. return app('json')->status('ali_pay', '订单创建成功', $info);
  132. }
  133. break;
  134. default:
  135. return app('json')->fail('缺少参数');
  136. break;
  137. }
  138. }
  139. /**
  140. * 充值额度选择
  141. * @return mixed
  142. */
  143. public function index()
  144. {
  145. $rechargeQuota = sys_data('user_recharge_quota') ?? [];
  146. $data['recharge_quota'] = $rechargeQuota;
  147. $recharge_attention = sys_config('recharge_attention');
  148. $recharge_attention = explode("\n", $recharge_attention);
  149. $data['recharge_attention'] = $recharge_attention;
  150. return app('json')->successful($data);
  151. }
  152. }