UserRechargeController.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. namespace app\api\controller\user;
  3. use app\models\order\Level;
  4. use app\models\system\SystemGroupData;
  5. use app\models\user\UserRecharge;
  6. use app\Request;
  7. use crmeb\services\GroupDataService;
  8. use crmeb\services\SystemConfigService;
  9. use crmeb\services\UtilService;
  10. /**
  11. * 充值类
  12. * Class UserRechargeController
  13. * @package app\api\controller\user
  14. */
  15. class UserRechargeController
  16. {
  17. /**
  18. * 小程序充值
  19. *
  20. * @param Request $request
  21. * @return mixed
  22. */
  23. public function routine(Request $request)
  24. {
  25. list($price, $recharId, $type) = UtilService::postMore([
  26. [['price', 'f'], 0],
  27. [['rechar_id', 'd'], 0],
  28. ['type', 0]
  29. ], $request, true);
  30. if (!$price || $price <= 0) return app('json')->fail('参数错误');
  31. $storeMinRecharge = sys_config('store_user_min_recharge');
  32. if ($price < $storeMinRecharge) return app('json')->fail('充值金额不能低于' . $storeMinRecharge);
  33. switch ((int)$type) {
  34. case 0: //支付充值余额
  35. $paid_price = 0;
  36. if ($recharId) {
  37. $data = SystemGroupData::getDateValue($recharId);
  38. if ($data === false) {
  39. return app('json')->fail('您选择的充值方式已下架!');
  40. } else {
  41. $paid_price = $data['give_money'] ?? 0;
  42. }
  43. }
  44. $rechargeOrder = UserRecharge::addRecharge($request->uid(), $price, 'routine', $paid_price);
  45. if (!$rechargeOrder) return app('json')->fail('充值订单生成失败!');
  46. try {
  47. return app('json')->successful(UserRecharge::jsPay($rechargeOrder));
  48. } catch (\Exception $e) {
  49. return app('json')->fail($e->getMessage());
  50. }
  51. break;
  52. case 1: //佣金转入余额
  53. if (UserRecharge::importNowMoney($request->uid(), $price))
  54. return app('json')->successful('转入余额成功');
  55. else
  56. return app('json')->fail(UserRecharge::getErrorInfo());
  57. break;
  58. default:
  59. return app('json')->fail('缺少参数');
  60. break;
  61. }
  62. }
  63. /**
  64. * 公众号充值
  65. *
  66. * @param Request $request
  67. * @return mixed
  68. */
  69. public function wechat(Request $request)
  70. {
  71. list($price, $recharId, $from, $type) = UtilService::postMore([
  72. [['price', 'f'], 0],
  73. [['rechar_id', 'd'], 0],
  74. ['from', 'weixin'],
  75. ['type', 0]
  76. ], $request, true);
  77. if (!$price || $price <= 0) return app('json')->fail('参数错误');
  78. $storeMinRecharge = sys_config('store_user_min_recharge');
  79. if ($price < $storeMinRecharge) return app('json')->fail('充值金额不能低于' . $storeMinRecharge);
  80. switch ((int)$type) {
  81. case 0: //支付充值余额
  82. $paid_price = 0;
  83. if ($recharId) {
  84. $data = SystemGroupData::getDateValue($recharId);
  85. if ($data === false) {
  86. return app('json')->fail('您选择的充值方式已下架!');
  87. } else {
  88. $paid_price = $data['give_money'] ?? 0;
  89. }
  90. }
  91. $rechargeOrder = UserRecharge::addRecharge($request->uid(), $price, 'weixin', $paid_price);
  92. if (!$rechargeOrder) return app('json')->fail('充值订单生成失败!');
  93. try {
  94. if ($from == 'weixinh5') {
  95. $recharge = UserRecharge::wxH5Pay($rechargeOrder);
  96. } else {
  97. $recharge = UserRecharge::wxPay($rechargeOrder);
  98. }
  99. } catch (\Exception $e) {
  100. return app('json')->fail($e->getMessage());
  101. }
  102. return app('json')->successful(['type' => $from, 'data' => $recharge]);
  103. break;
  104. case 1: //佣金转入余额
  105. if (UserRecharge::importNowMoney($request->uid(), $price))
  106. return app('json')->successful('转入余额成功');
  107. else
  108. return app('json')->fail(UserRecharge::getErrorInfo());
  109. break;
  110. default:
  111. return app('json')->fail('缺少参数');
  112. break;
  113. }
  114. }
  115. /**
  116. * 充值额度选择
  117. * @return mixed
  118. */
  119. public function index()
  120. {
  121. $rechargeQuota = sys_data('user_recharge_quota') ?? [];
  122. $data['recharge_quota'] = $rechargeQuota;
  123. $recharge_attention = sys_config('recharge_attention');
  124. $recharge_attention = explode("\n", $recharge_attention);
  125. $data['recharge_attention'] = $recharge_attention;
  126. return app('json')->successful($data);
  127. }
  128. /**
  129. * 会员开通
  130. * @param Request $request
  131. * @return mixed
  132. */
  133. public function recharge(Request $request)
  134. {
  135. //@file_put_contents("level.txt",json_encode(input()));
  136. list($level_id, $from, $pay_type,$spread_uid,$store_id) = UtilService::postMore([['level_id', 1], ['from', 'weixin'], ['pay_type', 'weixin'],['spread',0],['store_id',0]], $request, true);
  137. if (!$level_id || $level_id <= 0) return app('json')->fail('参数错误');
  138. $rechargeOrder = Level::createLevelOrder($request->uid(), $level_id, $pay_type,$spread_uid,$store_id);
  139. if (!$rechargeOrder) return app('json')->fail('充值订单生成失败!');
  140. $Order = $rechargeOrder->toArray();
  141. try {
  142. switch ($pay_type) {
  143. case 'weixin':
  144. if ($from == 'routine') {
  145. $jsConfig = Level::jsPay($Order['order_id']); //订单列表发起支付
  146. if (isset($jsConfig['package']) && $jsConfig['package']) {
  147. $package = str_replace('prepay_id=', '', $jsConfig['package']);
  148. for ($i = 0; $i < 3; $i++) {
  149. RoutineFormId::SetFormId($package, $request->uid());
  150. }
  151. }
  152. } else if ($from == 'weixinh5') {
  153. $jsConfig = Level::h5Pay($Order['order_id']);
  154. } else {
  155. $jsConfig = Level::wxPay($Order['order_id']);
  156. }
  157. if ($from == 'weixinh5') {
  158. return app('json')->status('wechat_h5_pay', ['jsConfig' => $jsConfig, 'order_id' => $rechargeOrder['order_id']]);
  159. } else {
  160. return app('json')->status('wechat_pay', ['jsConfig' => $jsConfig, 'order_id' => $rechargeOrder['order_id']]);
  161. }
  162. break;
  163. case 'yue':
  164. if (Level::yuePay($Order['order_id'], $request->uid()))
  165. return app('json')->status('success', '余额支付成功');
  166. else {
  167. $error = Level::getErrorInfo();
  168. return app('json')->fail(is_array($error) && isset($error['msg']) ? $error['msg'] : $error);
  169. }
  170. break;
  171. case 'ali':
  172. $rechargeOrder = Level::alipay($Order, $request->uid());
  173. if (!$rechargeOrder) return app('json')->fail('开通订单生成失败!');
  174. return app('json')->successful(['type' => $from, 'data' => $rechargeOrder]);
  175. break;
  176. }
  177. } catch (\Exception $e) {
  178. return app('json')->fail($e->getMessage());
  179. }
  180. return app('json')->fail('支付方式错误');
  181. }
  182. }