Pay.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. $list = Db::name('paymoney_config')->order('weigh asc,id asc')->where('is_show', 1)->select();
  85. $data['config'] = $list;
  86. $this->success('success', $data);
  87. }
  88. //人民币充值 创建订单
  89. public function money_recharge()
  90. {
  91. $rc_id = input('rc_id', 0);
  92. $pay_type = input('pay_type', 'wechat');
  93. $platform = input('platform', 'app');
  94. $free_money = input('free_money', 0);
  95. $user_id = $this->auth->id;
  96. if (!$rc_id && !$free_money) {
  97. $this->error('请选择或填写充值金额');
  98. }
  99. if ($rc_id) {
  100. //赋值money
  101. $recharge_config = Db::name('paymoney_config')->where('id', $rc_id)->find();
  102. $money = $recharge_config['money'] ?: 0;
  103. } else {
  104. //自由输入覆盖
  105. $money = floatval($free_money);
  106. }
  107. if ($money <= 0) {
  108. $this->error('支付金额必须大于0');
  109. }
  110. if ($money > 10000) {
  111. $this->error('支付金额太大');
  112. }
  113. // 创建支付订单
  114. $remark = '充值订单';
  115. $orderData = [
  116. 'user_id' => $user_id,
  117. 'out_trade_no' => createUniqueNo('M', $user_id),
  118. 'order_amount' => $money,
  119. 'pay_type' => $pay_type,
  120. 'platform' => $platform,
  121. 'table_name' => 'money_recharge',
  122. 'table_id' => 0,
  123. 'createtime' => time(),
  124. 'args' => json_encode([
  125. 'table_id' => 0,
  126. 'money' => $money,
  127. 'remark' => $remark
  128. ], JSON_UNESCAPED_UNICODE),
  129. ];
  130. if (!Db::name('pay_order')->insert($orderData)) {
  131. return $this->error('订单创建失败');
  132. }
  133. // 第三方支付下单
  134. $params = [
  135. 'type' => $orderData['pay_type'],
  136. 'orderid' => $orderData['out_trade_no'],
  137. 'title' => $remark,
  138. 'amount' => $orderData['order_amount'],
  139. 'method' => $orderData['platform'],
  140. 'notifyurl' => CurlUtil::getHttp("/api/notify/recharge/pay_type/{$pay_type}"),
  141. 'returnurl' => '',
  142. ];
  143. // 如果是小程序则需要添加 openid
  144. if ($pay_type == 'wechat' && $platform == 'miniapp') {
  145. $params['openid'] = $this->auth->mini_openid;
  146. }
  147. $res = Service::submitOrder($params);
  148. if ($pay_type == 'wechat') {
  149. $this->success('success', json_decode($res, true));
  150. } else {
  151. $this->success('success', $res);
  152. }
  153. }
  154. }