UserRechargeController.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. include_once dirname(__FILE__). "/../AdapaySdk/init.php";
  10. include_once dirname(__FILE__). "/../AdapaySdk/config.php";
  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 {
  89. $recharge = UserRecharge::wxPay($rechargeOrder);
  90. }
  91. } catch (\Exception $e) {
  92. return app('json')->fail($e->getMessage());
  93. }
  94. return app('json')->successful(['type' => $from, 'data' => $recharge]);
  95. break;
  96. case 1: //佣金转入余额
  97. if (UserRecharge::importNowMoney($request->uid(), $price))
  98. return app('json')->successful('转入余额成功');
  99. else
  100. return app('json')->fail(UserRecharge::getErrorInfo());
  101. break;
  102. case 2:
  103. $rechargeOrder = UserRecharge::addRecharge($request->uid(), $price, 'alipay');
  104. $payment = new \AdaPaySdk\Payment();
  105. # 支付设置
  106. $payment_params = array(
  107. 'app_id'=> 'app_3dc215b5-ade5-47d8-8461-ddb796bd5e06',
  108. 'order_no'=> $rechargeOrder['order_id'].'-'.rand(100000, 999999),
  109. 'pay_channel'=> 'alipay_wap',
  110. //'time_expire'=> date("YmdHis", time()+86400),
  111. 'pay_amt'=> sprintf("%01.2f",$rechargeOrder['price']),
  112. 'goods_title'=> '环球优购',
  113. 'goods_desc'=> '环球优购',
  114. 'description'=> 'alipay',
  115. 'device_info'=> ['device_p'=> $_SERVER["REMOTE_ADDR"]],
  116. 'callback_url' => 'http://xqyg.frp.liuniu946.com/index',
  117. 'notify_url' => 'http://xqyg.frp.liuniu946.com/api/adapay/cz_notify'
  118. );
  119. $payment->create($payment_params);
  120. if ($payment->isError()){
  121. //失败处理
  122. return app('json')->fail('支付失败');
  123. } else {
  124. //成功处理
  125. return app('json')->status('alipay', ['jsConfig' => $payment->result, 'orderId' => $rechargeOrder['order_id']]);
  126. }
  127. break;
  128. default:
  129. return app('json')->fail('缺少参数');
  130. break;
  131. }
  132. }
  133. /**
  134. * 充值额度选择
  135. * @return mixed
  136. */
  137. public function index()
  138. {
  139. $rechargeQuota = sys_data('user_recharge_quota') ?? [];
  140. $data['recharge_quota'] = $rechargeQuota;
  141. $recharge_attention = sys_config('recharge_attention');
  142. $recharge_attention = explode("\n", $recharge_attention);
  143. $data['recharge_attention'] = $recharge_attention;
  144. return app('json')->successful($data);
  145. }
  146. }