Pay.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\model\Wallet;
  5. use app\utils\CurlUtil;
  6. use think\Db;
  7. use addons\epay\library\Service;
  8. /**
  9. * 充值配置与充值订单
  10. */
  11. class Pay extends Api
  12. {
  13. protected $noNeedLogin = [];
  14. protected $noNeedRight = ['*'];
  15. //vip用的
  16. public function vip_config()
  17. {
  18. $list = Db::name('payvip_config')->order('weigh asc,id asc')->select();
  19. $data['vipconfig'] = $list;
  20. $data['vip_endtime'] = model('wallet')->getWallet($this->auth->id, 'vip_endtime');
  21. $data['is_vip'] = $data['vip_endtime'] > time() ? 1 : 0;
  22. $data['avatar'] = localpath_to_netpath($this->auth->avatar);
  23. $this->success('success', $data);
  24. }
  25. //vip用的,创建订单
  26. public function vip_recharge()
  27. {
  28. $rc_id = input('rc_id', 0);
  29. $pay_type = input('pay_type', 'wechat');
  30. $platform = 'app';
  31. $uid = $this->auth->id;
  32. if (!$rc_id) {
  33. $this->error('请选择会员套餐');
  34. }
  35. if (!$this->user_auth_limit()) {
  36. $this->error('请先完成实名认证');
  37. }
  38. //赋值money
  39. $recharge_config = Db::name('payvip_config')->where('id', $rc_id)->find();
  40. $money = $recharge_config['money'];
  41. if ($money <= 0) {
  42. $this->error('支付金额必须大于0');
  43. }
  44. if ($money > 10000) {
  45. $this->error('支付金额太大');
  46. }
  47. //创建订单
  48. $data = [];
  49. $data['user_id'] = $uid;
  50. $data['out_trade_no'] = createUniqueNo('P', $uid); // 数据库订单号加密
  51. $data['order_amount'] = $money;
  52. $data['createtime'] = time();
  53. $data['pay_type'] = $pay_type;
  54. $data['platform'] = $platform;
  55. $data['order_status'] = 0;
  56. $data['table_name'] = 'vip_recharge';
  57. $data['table_id'] = 0;
  58. $data['args'] = json_encode(['days' => $recharge_config['days']]);
  59. $orderid = Db::name('pay_order')->insertGetId($data);
  60. // $openid = $this->auth->mini_openid;
  61. //下单
  62. $params = [
  63. 'type' => $pay_type,
  64. 'orderid' => $data['out_trade_no'],
  65. 'title' => '支付订单',
  66. 'amount' => $data['order_amount'],
  67. 'method' => $platform,
  68. // 'openid' => $openid,
  69. 'notifyurl' => config('pay_notify_url') . '/api/notify/vip_notify_base/paytype/' . $pay_type,
  70. 'returnurl' => '',
  71. ];
  72. $res = Service::submitOrder($params);
  73. if ($pay_type == 'wechat') {
  74. $this->success('success', json_decode($res, true));
  75. } else {
  76. $this->success('success', $res);
  77. }
  78. }
  79. //人民币充值
  80. public function money_config()
  81. {
  82. $wallet = (new Wallet())->getWallet($this->auth->id);
  83. $data['wallet'] = $wallet;
  84. $data['recharge_explain'] = config('site.recharge_explain');
  85. $list = Db::name('paymoney_config')->order('weigh asc,id asc')->where('is_show', 1)->select();
  86. $data['config'] = $list;
  87. $this->success('success', $data);
  88. }
  89. //人民币充值 创建订单
  90. public function money_recharge()
  91. {
  92. $rc_id = input('rc_id', 0);
  93. $pay_type = input('pay_type', 'wechat');
  94. $platform = input('platform', 'app');
  95. $free_money = input('free_money', 0);
  96. $user_id = $this->auth->id;
  97. if (!$rc_id && !$free_money) {
  98. $this->error('请选择或填写充值金额');
  99. }
  100. if ($rc_id) {
  101. //赋值money
  102. $recharge_config = Db::name('paymoney_config')->where('id', $rc_id)->find();
  103. $money = $recharge_config['money'] ?: 0;
  104. } else {
  105. //自由输入覆盖
  106. $money = floatval($free_money);
  107. }
  108. if ($money <= 0) {
  109. $this->error('支付金额必须大于0');
  110. }
  111. if ($money > 10000) {
  112. $this->error('支付金额太大');
  113. }
  114. // 创建支付订单
  115. $remark = '充值订单';
  116. $orderData = [
  117. 'user_id' => $user_id,
  118. 'out_trade_no' => createUniqueNo('M', $user_id),
  119. 'order_amount' => $money,
  120. 'pay_type' => $pay_type,
  121. 'platform' => $platform,
  122. 'table_name' => 'money_recharge',
  123. 'table_id' => 0,
  124. 'createtime' => time(),
  125. 'args' => json_encode([
  126. 'table_id' => 0,
  127. 'money' => $money,
  128. 'remark' => $remark
  129. ], JSON_UNESCAPED_UNICODE),
  130. ];
  131. if (!Db::name('pay_order')->insert($orderData)) {
  132. return $this->error('订单创建失败');
  133. }
  134. // 第三方支付下单
  135. $params = [
  136. 'type' => $orderData['pay_type'],
  137. 'orderid' => $orderData['out_trade_no'],
  138. 'title' => $remark,
  139. 'amount' => $orderData['order_amount'],
  140. 'method' => $orderData['platform'],
  141. 'notifyurl' => CurlUtil::getHttp("/api/notify/recharge/pay_type/{$pay_type}"),
  142. 'returnurl' => '',
  143. ];
  144. // 如果是小程序则需要添加 openid
  145. if ($pay_type == 'wechat' && $platform == 'miniapp') {
  146. $params['openid'] = $this->auth->mini_openid;
  147. }
  148. $res = Service::submitOrder($params);
  149. if ($pay_type == 'wechat') {
  150. $this->success('success', json_decode($res, true));
  151. } else {
  152. $this->success('success', $res);
  153. }
  154. }
  155. }