Pay.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\model\PayOrderModel;
  5. use app\common\model\Wallet;
  6. use app\utils\CurlUtil;
  7. use think\Db;
  8. use addons\epay\library\Service;
  9. /**
  10. * 充值配置与充值订单
  11. */
  12. class Pay extends Api
  13. {
  14. protected $noNeedLogin = [];
  15. protected $noNeedRight = ['*'];
  16. // VIP套餐
  17. public function vip_config()
  18. {
  19. $user_id = $this->auth->id;
  20. $list = Db::name('vip_config')->field(['id','name','image','price','color','is_try','coupon_ids'])->where('status',1)->order('weigh desc,id asc')->select();
  21. foreach ($list as $key => $val) {
  22. $coupon_ids = explode(',',$val['coupon_ids']);
  23. $list[$key]['image'] = cdnurl($val['image']);
  24. // 如果是试用会员,则需要判断是否使用过了
  25. if ($val['is_try'] == 1){
  26. // 是否购买过,1已购买
  27. $list[$key]['is_buy'] = Db::name('vip_order')->where(['user_id' => $user_id,'vip_id' => $val['id'],'status'=>1])->value('id') > 0 ? 1 : 0;
  28. }
  29. // 查询套餐信息
  30. $coupon_list = Db::name('vip_coupon')->whereIn('id',$coupon_ids)->where('status',1)->select();
  31. foreach ($coupon_list as $k => $v) {
  32. $list[$key]['coupon_list'][$k] = [
  33. 'id' => $v['id'],
  34. 'image' => cdnurl($v['image']),
  35. 'content' => "【{$v['name']}】【{$v['end_days']}日内使用有效】 \n {$v['info']}",
  36. ];
  37. }
  38. }
  39. $this->success('success', $list);
  40. }
  41. //vip用的,创建订单
  42. public function vip_recharge()
  43. {
  44. $user_id = $this->auth->id;
  45. $params = $this->request->param();
  46. if (empty($params['vip_id'])) {
  47. return $this->error('参数缺失');
  48. }
  49. if (empty($params['pay_type']) || empty($params['platform'])) {
  50. return $this->error('请选择支付方式');
  51. }
  52. if (!$vip_config = Db::name('vip_config')->where('id', $params['vip_id'])->where('status',1)->find()){
  53. return $this->error('套餐不存在或已下架');
  54. }
  55. // 判断是否使用过
  56. if ($vip_config['is_try'] == 1 && Db::name('vip_order')->where(['user_id' => $user_id,'vip_id' => $params['vip_id'],'status'=>1])->value('id') > 0){
  57. return $this->error('您已经试用过了');
  58. }
  59. if ($vip_config['is_try'] != 1 && $vip_config['price'] <= 0){
  60. return $this->error('套餐价格不能为0');
  61. }
  62. // 查询套餐信息
  63. $coupon_ids = explode(',',$vip_config['coupon_ids']);
  64. $coupon_list = Db::name('vip_coupon')->whereIn('id',$coupon_ids)->where('status',1)->select();
  65. //赋值money
  66. $pay_amount = $vip_config['is_try'] != 1 ? $vip_config['price'] : '0.00';
  67. $nowTime = time();
  68. $data = [
  69. 'user_id' => $user_id,
  70. 'vip_id' => $params['vip_id'],
  71. 'order_no' => createUniqueNo('E', $user_id),
  72. 'pay_amount' => $pay_amount,
  73. 'pay_time' => $vip_config['is_try'] != 1 ? 0 : $nowTime,
  74. 'status' => $vip_config['is_try'] != 1 ? 0 : 1,// 如果是试用会员 则无需支付
  75. 'create_time' => $nowTime
  76. ];
  77. Db::startTrans();
  78. if (!$order_id = Db::name('vip_order')->insertGetId($data)) {
  79. Db::rollback();
  80. return $this->error('订单创建失败');
  81. }
  82. $user_vip_coupon = [];
  83. foreach ($coupon_list as $k => $v) {
  84. for ($i = 0;$i < $v['num'];$i++){
  85. $user_vip_coupon[] = [
  86. 'coupon_id' => $v['id'],
  87. 'user_id' => $user_id,
  88. 'order_id' => $order_id,
  89. 'coupon_no' => \app\utils\Common::createNo('C0',8),
  90. 'type' => $v['type'],
  91. 'name' => $v['name'],
  92. 'info' => $v['info'],
  93. 'end_time' => strtotime('+' . $v['end_days'] . ' day'),
  94. 'use_frequency_day' => $v['use_frequency_day'],
  95. 'use_frequency_times' => $v['use_frequency_times'],
  96. 'status' => $vip_config['is_try'] != 1 ? 0 : 1,
  97. 'create_time' => $nowTime,
  98. ];
  99. }
  100. }
  101. if (!Db::name('vip_coupon_user')->insertAll($user_vip_coupon)) {
  102. Db::rollback();
  103. return $this->error('订单创建失败');
  104. }
  105. Db::commit();
  106. if ($vip_config['is_try'] != 1){
  107. // 创建支付订单
  108. $remark = 'VIP套餐购买';
  109. $orderData = [
  110. 'user_id' => $user_id,
  111. 'out_trade_no' => $data['order_no'],
  112. 'order_amount' => $data['pay_amount'],
  113. 'pay_type' => $params['pay_type'],
  114. 'platform' => $params['platform'],
  115. 'table_name' => 'university_event_apply',
  116. 'table_id' => $order_id,
  117. 'createtime' => time(),
  118. 'args' => json_encode([
  119. 'table_id' => $order_id,
  120. 'remark' => $remark
  121. ], JSON_UNESCAPED_UNICODE),
  122. ];
  123. if (!Db::name('pay_order')->insert($orderData)) {
  124. return $this->error('订单创建失败');
  125. }
  126. // 拉起支付 余额支付
  127. if ($params['pay_type'] == 'wallet') {
  128. Db::startTrans();
  129. //钱包更新
  130. $walletService = new Wallet();
  131. if (!$walletService->change($user_id, -$orderData['order_amount'], 'money', 20, $remark, $orderData['table_name'], $orderData['table_id'])) {
  132. Db::rollback();
  133. return $this->error($walletService->getMessage());
  134. }
  135. // 支付成功,更改支付金额
  136. [$res,$msg] = PayOrderModel::vip($orderData['out_trade_no']);
  137. if (!$res){
  138. Db::rollback();
  139. return $this->error($msg);
  140. }
  141. Db::commit();
  142. return $this->success('支付成功');
  143. }
  144. // 第三方支付下单
  145. $params = [
  146. 'type' => $orderData['pay_type'],
  147. 'orderid' => $orderData['out_trade_no'],
  148. 'title' => $remark,
  149. 'amount' => $orderData['order_amount'],
  150. 'method' => $orderData['platform'],
  151. 'notifyurl' => CurlUtil::getHttp("/api/notify/university_event_{$params['pay_type']}"),
  152. 'returnurl' => '',
  153. ];
  154. // 如果是小程序则需要添加 openid
  155. if ($params['pay_type'] == 'wechat' && $params['platform'] == 'miniapp') {
  156. $params['openid'] = $this->auth->mini_openid;
  157. }
  158. $res = Service::submitOrder($params);
  159. if ($params['pay_type'] == 'wechat') {
  160. $this->success('下单成功', json_decode($res, true));
  161. } else {
  162. $this->success('下单成功', $res);
  163. }
  164. }
  165. return $this->success('试用成功');
  166. }
  167. //人民币充值
  168. public function money_config()
  169. {
  170. $data['balance'] = (new Wallet())->getWallet($this->auth->id);
  171. $data['recharge_explain'] = config('site.recharge_explain');
  172. $list = Db::name('paymoney_config')->order('weigh asc,id asc')->where('is_show', 1)->select();
  173. $data['config'] = $list;
  174. $this->success('success', $data);
  175. }
  176. //人民币充值 创建订单
  177. public function money_recharge()
  178. {
  179. $rc_id = input('rc_id', 0);
  180. $pay_type = input('pay_type', 'wechat');
  181. $platform = input('platform', 'app');
  182. $free_money = input('free_money', 0);
  183. $user_id = $this->auth->id;
  184. if (!$rc_id && !$free_money) {
  185. $this->error('请选择或填写充值金额');
  186. }
  187. if ($rc_id) {
  188. //赋值money
  189. $recharge_config = Db::name('paymoney_config')->where('id', $rc_id)->find();
  190. $money = $recharge_config['money'] ?: 0;
  191. } else {
  192. //自由输入覆盖
  193. $money = floatval($free_money);
  194. }
  195. if ($money <= 0) {
  196. $this->error('支付金额必须大于0');
  197. }
  198. if ($money > 10000) {
  199. $this->error('支付金额太大');
  200. }
  201. // 创建支付订单
  202. $remark = '充值订单';
  203. $orderData = [
  204. 'user_id' => $user_id,
  205. 'out_trade_no' => createUniqueNo('M', $user_id),
  206. 'order_amount' => $money,
  207. 'pay_type' => $pay_type,
  208. 'platform' => $platform,
  209. 'table_name' => 'money_recharge',
  210. 'table_id' => 0,
  211. 'createtime' => time(),
  212. 'args' => json_encode([
  213. 'table_id' => 0,
  214. 'money' => $money,
  215. 'remark' => $remark
  216. ], JSON_UNESCAPED_UNICODE),
  217. ];
  218. if (!Db::name('pay_order')->insert($orderData)) {
  219. return $this->error('订单创建失败');
  220. }
  221. // 第三方支付下单
  222. $params = [
  223. 'type' => $orderData['pay_type'],
  224. 'orderid' => $orderData['out_trade_no'],
  225. 'title' => $remark,
  226. 'amount' => $orderData['order_amount'],
  227. 'method' => $orderData['platform'],
  228. 'notifyurl' => CurlUtil::getHttp("/api/notify/recharge/pay_type/{$pay_type}"),
  229. 'returnurl' => '',
  230. ];
  231. // 如果是小程序则需要添加 openid
  232. if ($pay_type == 'wechat' && $platform == 'miniapp') {
  233. $params['openid'] = $this->auth->mini_openid;
  234. }
  235. $res = Service::submitOrder($params);
  236. if ($pay_type == 'wechat') {
  237. $this->success('success', json_decode($res, true));
  238. } else {
  239. $this->success('success', $res);
  240. }
  241. }
  242. }