UserRechargeController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. <?php
  2. namespace app\api\controller\user;
  3. use app\models\system\SystemGroupData;
  4. use app\models\user\UserRecharge;
  5. use app\Request;
  6. use crmeb\services\GroupDataService;
  7. use crmeb\services\SystemConfigService;
  8. use crmeb\services\UtilService;
  9. /**
  10. * 充值类
  11. * Class UserRechargeController
  12. * @package app\api\controller\user
  13. */
  14. class UserRechargeController
  15. {
  16. /**
  17. * 小程序充值
  18. *
  19. * @param Request $request
  20. * @return mixed
  21. */
  22. public function routine(Request $request)
  23. {
  24. list($price, $recharId, $type, $get_money_type) = UtilService::postMore([['price', 0], ['rechar_id', 0], ['type', 0], ['get_money_type', 'now_money']], $request, true);
  25. if (!$price || $price <= 0) return app('json')->fail('参数错误');
  26. $storeMinRecharge = sys_config('store_user_min_recharge');
  27. if ($price < $storeMinRecharge) return app('json')->fail('充值金额不能低于' . $storeMinRecharge);
  28. switch ((int)$type) {
  29. case 0: //支付充值余额
  30. $paid_price = 0;
  31. if ($recharId) {
  32. $data = SystemGroupData::getDateValue($recharId);
  33. if ($data === false) {
  34. return app('json')->fail('您选择的充值方式已下架!');
  35. } else {
  36. $paid_price = $data['give_money'] ?? 0;
  37. }
  38. }
  39. $get_money_num = 0;
  40. if ($get_money_type != 'now_money') {
  41. $paid_price = 0;
  42. $flag = false;
  43. $money_type = sys_data('money_type');
  44. $usdt_price = 0;
  45. foreach ($money_type as $v) {
  46. if ($v['code'] == $get_money_type) {
  47. if (!$v['charge']) return app('json')->fail('不可充值币种');
  48. if ($v['usdt_price'] == 0) {
  49. $usdt_price = get_huobi_price($v['code']);
  50. } else {
  51. $usdt_price = $v['usdt_price'];
  52. }
  53. if ($v['code'] == 'LALA') {
  54. $usdt_price = get_lala_ratio();
  55. }
  56. }
  57. $flag = true;
  58. }
  59. if (!$flag) {
  60. return app('json')->fail('您选择的充值币种不存在!');
  61. }
  62. $rmb_price = bcmul($usdt_price, us_price(), 8);
  63. if ($rmb_price <= 0) return app('json')->fail('不可充值币种');
  64. $get_money_num = bcdiv($price, $rmb_price, 8);
  65. }
  66. $rechargeOrder = UserRecharge::addRecharge($request->uid(), $price, 'weixin', $paid_price, 0, $get_money_type, $get_money_num);
  67. // $rechargeOrder = UserRecharge::addRecharge($request->uid(), $price, 'routine', $paid_price);
  68. if (!$rechargeOrder) return app('json')->fail('充值订单生成失败!');
  69. try {
  70. return app('json')->successful(UserRecharge::jsPay($rechargeOrder));
  71. } catch (\Exception $e) {
  72. return app('json')->fail($e->getMessage());
  73. }
  74. break;
  75. case 1: //佣金转入余额
  76. if (UserRecharge::importNowMoney($request->uid(), $price))
  77. return app('json')->successful('转入余额成功');
  78. else
  79. return app('json')->fail(UserRecharge::getErrorInfo());
  80. break;
  81. default:
  82. return app('json')->fail('缺少参数');
  83. break;
  84. }
  85. }
  86. /**
  87. * 支付宝充值
  88. *
  89. * @param Request $request
  90. * @return mixed
  91. */
  92. public function ali(Request $request)
  93. {
  94. list($price, $recharId, $type, $get_money_type) = UtilService::postMore([['price', 0], ['rechar_id', 0], ['type', 0], ['get_money_type', 'now_money']], $request, true);
  95. if (!$price || $price <= 0) return app('json')->fail('参数错误');
  96. $storeMinRecharge = sys_config('store_user_min_recharge');
  97. if ($price < $storeMinRecharge) return app('json')->fail('充值金额不能低于' . $storeMinRecharge);
  98. switch ((int)$type) {
  99. case 0: //支付充值余额
  100. $paid_price = 0;
  101. if ($recharId) {
  102. $data = SystemGroupData::getDateValue($recharId);
  103. if ($data === false) {
  104. return app('json')->fail('您选择的充值方式已下架!');
  105. } else {
  106. $paid_price = $data['give_money'] ?? 0;
  107. }
  108. }
  109. $get_money_num = 0;
  110. if ($get_money_type != 'now_money') {
  111. $paid_price = 0;
  112. $flag = false;
  113. $money_type = sys_data('money_type');
  114. $usdt_price = 0;
  115. foreach ($money_type as $v) {
  116. if ($v['code'] == $get_money_type) {
  117. if (!$v['charge']) return app('json')->fail('不可充值币种');
  118. if ($v['usdt_price'] == 0) {
  119. $usdt_price = get_huobi_price($v['code']);
  120. } else {
  121. $usdt_price = $v['usdt_price'];
  122. }
  123. if ($v['code'] == 'LALA') {
  124. $usdt_price = get_lala_ratio();
  125. }
  126. }
  127. $flag = true;
  128. }
  129. if (!$flag) {
  130. return app('json')->fail('您选择的充值币种不存在!');
  131. }
  132. $rmb_price = bcmul($usdt_price, us_price(), 8);
  133. if ($rmb_price <= 0) return app('json')->fail('不可充值币种');
  134. $get_money_num = bcdiv($price, $rmb_price, 8);
  135. }
  136. $rechargeOrder = UserRecharge::addRecharge($request->uid(), $price, 'ali', $paid_price, 0, $get_money_type, $get_money_num);
  137. // $rechargeOrder = UserRecharge::addRecharge($request->uid(), $price, 'routine', $paid_price);
  138. if (!$rechargeOrder) return app('json')->fail('充值订单生成失败!');
  139. try {
  140. return app('json')->successful(UserRecharge::aliPay($rechargeOrder));
  141. } catch (\Exception $e) {
  142. return app('json')->fail($e->getMessage());
  143. }
  144. break;
  145. case 1: //佣金转入余额
  146. if (UserRecharge::importNowMoney($request->uid(), $price))
  147. return app('json')->successful('转入余额成功');
  148. else
  149. return app('json')->fail(UserRecharge::getErrorInfo());
  150. break;
  151. default:
  152. return app('json')->fail('缺少参数');
  153. break;
  154. }
  155. }
  156. /**
  157. * 支付宝充值
  158. *
  159. * @param Request $request
  160. * @return mixed
  161. */
  162. public function app(Request $request)
  163. {
  164. list($price, $recharId, $type, $get_money_type) = UtilService::postMore([['price', 0], ['rechar_id', 0], ['type', 0], ['get_money_type', 'now_money']], $request, true);
  165. if (!$price || $price <= 0) return app('json')->fail('参数错误');
  166. $storeMinRecharge = sys_config('store_user_min_recharge');
  167. if ($price < $storeMinRecharge) return app('json')->fail('充值金额不能低于' . $storeMinRecharge);
  168. switch ((int)$type) {
  169. case 0: //支付充值余额
  170. $paid_price = 0;
  171. if ($recharId) {
  172. $data = SystemGroupData::getDateValue($recharId);
  173. if ($data === false) {
  174. return app('json')->fail('您选择的充值方式已下架!');
  175. } else {
  176. $paid_price = $data['give_money'] ?? 0;
  177. }
  178. }
  179. $get_money_num = 0;
  180. if ($get_money_type != 'now_money') {
  181. $paid_price = 0;
  182. $flag = false;
  183. $money_type = sys_data('money_type');
  184. $usdt_price = 0;
  185. foreach ($money_type as $v) {
  186. if ($v['code'] == $get_money_type) {
  187. if (!$v['charge']) return app('json')->fail('不可充值币种');
  188. if ($v['usdt_price'] == 0) {
  189. $usdt_price = get_huobi_price($v['code']);
  190. } else {
  191. $usdt_price = $v['usdt_price'];
  192. }
  193. if ($v['code'] == 'LALA') {
  194. $usdt_price = get_lala_ratio();
  195. }
  196. }
  197. $flag = true;
  198. }
  199. if (!$flag) {
  200. return app('json')->fail('您选择的充值币种不存在!');
  201. }
  202. $rmb_price = bcmul($usdt_price, us_price(), 8);
  203. if ($rmb_price <= 0) return app('json')->fail('不可充值币种');
  204. $get_money_num = bcdiv($price, $rmb_price, 8);
  205. }
  206. $rechargeOrder = UserRecharge::addRecharge($request->uid(), $price, 'app', $paid_price, 0, $get_money_type, $get_money_num);
  207. // $rechargeOrder = UserRecharge::addRecharge($request->uid(), $price, 'routine', $paid_price);
  208. if (!$rechargeOrder) return app('json')->fail('充值订单生成失败!');
  209. try {
  210. return app('json')->successful(UserRecharge::appPay($rechargeOrder));
  211. } catch (\Exception $e) {
  212. return app('json')->fail($e->getMessage());
  213. }
  214. break;
  215. case 1: //佣金转入余额
  216. if (UserRecharge::importNowMoney($request->uid(), $price))
  217. return app('json')->successful('转入余额成功');
  218. else
  219. return app('json')->fail(UserRecharge::getErrorInfo());
  220. break;
  221. default:
  222. return app('json')->fail('缺少参数');
  223. break;
  224. }
  225. }
  226. /**
  227. * 公众号充值
  228. *
  229. * @param Request $request
  230. * @return mixed
  231. */
  232. public function wechat(Request $request)
  233. {
  234. list($price, $recharId, $from, $type, $get_money_type) = UtilService::postMore([['price', 0], ['rechar_id', 0], ['from', 'weixin'], ['type', 0], ['get_money_type', 'now_money']], $request, true);
  235. if (!$price || $price <= 0) return app('json')->fail('参数错误');
  236. $storeMinRecharge = sys_config('store_user_min_recharge');
  237. if ($price < $storeMinRecharge) return app('json')->fail('充值金额不能低于' . $storeMinRecharge);
  238. switch ((int)$type) {
  239. case 0: //支付充值余额
  240. $paid_price = 0;
  241. if ($recharId) {
  242. $data = SystemGroupData::getDateValue($recharId);
  243. if ($data === false) {
  244. return app('json')->fail('您选择的充值方式已下架!');
  245. } else {
  246. $paid_price = $data['give_money'] ?? 0;
  247. }
  248. }
  249. $get_money_num = 0;
  250. if ($get_money_type != 'now_money') {
  251. $paid_price = 0;
  252. $flag = false;
  253. $money_type = sys_data('money_type');
  254. $usdt_price = 0;
  255. foreach ($money_type as $v) {
  256. if ($v['code'] == $get_money_type) {
  257. if (!$v['charge']) return app('json')->fail('不可充值币种');
  258. if ($v['usdt_price'] == 0) {
  259. $usdt_price = get_huobi_price($v['code']);
  260. } else {
  261. $usdt_price = $v['usdt_price'];
  262. }
  263. if ($v['code'] == 'LALA') {
  264. $usdt_price = get_lala_ratio();
  265. }
  266. $flag = true;
  267. }
  268. }
  269. if (!$flag) {
  270. return app('json')->fail('您选择的充值币种不存在!');
  271. }
  272. $rmb_price = bcmul($usdt_price, us_price(), 8);
  273. if ($rmb_price <= 0) return app('json')->fail('不可充值币种');
  274. $get_money_num = bcdiv($price, $rmb_price, 2);
  275. }
  276. $rechargeOrder = UserRecharge::addRecharge($request->uid(), $price, 'weixin', $paid_price, 0, $get_money_type, $get_money_num);
  277. if (!$rechargeOrder) return app('json')->fail('充值订单生成失败!');
  278. try {
  279. if ($from == 'weixinh5') {
  280. $recharge = UserRecharge::wxH5Pay($rechargeOrder);
  281. } else {
  282. $recharge = UserRecharge::wxPay($rechargeOrder);
  283. }
  284. } catch (\Exception $e) {
  285. return app('json')->fail($e->getMessage());
  286. }
  287. return app('json')->successful(['type' => $from, 'data' => $recharge]);
  288. break;
  289. case 1: //佣金转入余额
  290. if (UserRecharge::importNowMoney($request->uid(), $price))
  291. return app('json')->successful('转入余额成功');
  292. else
  293. return app('json')->fail(UserRecharge::getErrorInfo());
  294. break;
  295. default:
  296. return app('json')->fail('缺少参数');
  297. break;
  298. }
  299. }
  300. /**
  301. * 充值额度选择
  302. * @return mixed
  303. */
  304. public function index()
  305. {
  306. $rechargeQuota = sys_data('user_recharge_quota') ?? [];
  307. $data['recharge_quota'] = $rechargeQuota;
  308. $recharge_attention = sys_config('recharge_attention');
  309. $recharge_attention = explode("\n", $recharge_attention);
  310. $data['recharge_attention'] = $recharge_attention;
  311. return app('json')->successful($data);
  312. }
  313. }