123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298 |
- <?php
- namespace app\api\controller;
- use app\common\model\User;
- use app\common\model\UserLevel;
- use app\common\model\LevelOrder as LOM;
- use app\common\model\UserLevelRank;
- use think\Db;
- class LevelOrder extends Base
- {
- public function initialize()
- {
- parent::initialize();
- parent::checkLogin();
- }
-
- public function createOrder(){
- $level_id = input('post.level_id');
- $level_key = input('post.level_key');
- $user_info = User::where('id',$this->user_id)->field('id,group_id,is_first,account_type')->find();
- if($user_info['account_type'] == 1 && $user_info['is_first'] == 0) $this->error('请联系公司管理员进行会员开通');
- $user_vip = UserLevelRank::getUserVipInfo($this->user_id);
- if(!empty($user_vip) && $user_vip['level_id'] > 1) $this->error('已是会员,请联系平台');
- $level_set = UserLevel::where('id', $level_id)->find()->toArray();
- if (!$level_set['price']) $this->error('会员价格设置有误');
- $level_price = json_decode($level_set['price'], true);
- if (!isset($level_price[$level_key])) $this->error('会员价格设置有误.');
- if(!empty($user_vip)) {
- $cal_time = strtotime('+'.$level_price[$level_key]['time'] .' month',$user_vip['end_time']);
- if($cal_time > strtotime('+3 years')) $this->error('会员续费期限最多3年');
- }
- $order_insert = [
- 'user_id' => $this->user_id,
- 'order_no' => get_order_sn(),
- 'level_id' => $level_id,
- 'level_key' => $level_key,
- 'month' => $level_price[$level_key]['time'],
- 'price_total' =>$level_price[$level_key]['price'],
- ];
- $order_info = LOM::create($order_insert);
- $this->success('ok', ['order_id' => $order_info->id]);
- }
-
- public function getOrderDetail()
- {
- $order_id = input('get.order_id');
- $detail = LOM::where('id',$order_id)->find()->toArray();
- $level_set = UserLevel::where('id', $detail['level_id'])->find()->toArray();
- $detail['level_name'] = $level_set['name'];
- $detail['level_set'] = json_decode($level_set['price'],true)[$detail['level_key']];
- $this->success('',['detail'=>$detail]);
- }
-
- public function payOrder()
- {
- $order_id = input('post.order_id');
- $pay_type = input('post.pay_type',1);
- $order_info = LOM::where('id',$order_id)->find()->toArray();
- if($order_info['status'] != 0) $this->error('订单状态错误');
- if($order_info['cancel_state'] != 0 || $order_info['is_deleted'] != 0) $this->error('订单异常');
- if($order_info['price_total'] <= 0) $this->error('订单金额错误');
- $user_vip = UserLevelRank::getUserVipInfo($this->user_id);
- if(!empty($user_vip)) {
- $cal_time = strtotime('+'.$order_info['month'] .' month',$user_vip['end_time']);
- if($cal_time > strtotime('+3 years')) $this->error('会员续费期限最多3年');
- }
- $pay_no = $order_info['pay_no'] ? $order_info['pay_no'] : get_order_sn();
- if(!$order_info['pay_no']) LOM::where('id',$order_id)->update(['pay_no'=>$pay_no]);
- $user_info = User::where('id',$this->user_id)->find()->toArray();
- $ret_data = ['pay_status'=>0,'config'=>[],'code_url'=>'','openid'=>$user_info['openid']];
- Db::startTrans();
- LOM::where('id',$order_id)->update(['pay_type'=>$pay_type]);
- try {
- switch ($pay_type){
- case 1:
- $notify_url = $this->request->root(true) . '/api/we_chat_pay/LevelOrderNotify';
- $pay_config = WeChatPay::wxPay('订单支付',$pay_no,$order_info['price_total'],$notify_url,'MWEB');
- if($pay_config['code'] != 200) $this->exception($pay_config['msg']);
- $pay_config['config']['mch_id'] = config('app.wx_pay')['mch_id'];
- $pay_config['config']['pay_no'] = $pay_no;
- $pay_config['config']['notify_url'] = $notify_url;
- $pay_config['config']['mweb_url'] = $pay_config['mweb_url'];
- $pay_config['config']['total_fee'] = $order_info['price_total'] * 100;
- $ret_data['config'] = $pay_config['config'];
- break;
- case 2 :
- $notify_url = $this->request->root(true) . '/api/Alipay/LevelOrderNotify';
- $return_url ='https://'.$_SERVER['HTTP_HOST']."/dist/#/my";
- $config = Alipay::ali_pay_pc('订单支付',$pay_no,$order_info['price_total'],$notify_url,$return_url);
- $ret_data['config']['ali_url'] = $config;
- $ret_data['ali_url'] = $config;
- break;
- case 3:
- $notify_url = $this->request->root(true) . '/api/we_chat_pay/LevelOrderNotify';
- $pay_config = WeChatPay::wxPay('订单支付',$pay_no,$order_info['price_total'],$notify_url,'APP');
- if($pay_config['code'] != 200) $this->exception($pay_config['msg']);
- $ret_data['config'] = $pay_config['config'];
- break;
- case 4:
- $notify_url = $this->request->root(true) . '/api/Alipay/LevelOrderNotify';
- $config = Alipay::ali_pay('订单支付',$pay_no,$order_info['price_total'],$notify_url);
- $ret_data['config']['ali_url'] = $config;
- $ret_data['ali_url'] = $config;
- break;
- case 5 :
- if(!$user_info['openid']) $this->exception('请绑定openid');
- $notify_url = $this->request->root(true) . '/api/we_chat_pay/LevelOrderNotify';
- $pay_config = WeChatPay::wxPay('订单支付',$pay_no,$order_info['price_total'],$notify_url,'JSAPI',$user_info['openid']);
- if($pay_config['code'] != 200) $this->exception($pay_config['msg']);
- $ret_data['config'] = $pay_config['config'];
- break;
- }
- Db::commit();
- }catch (\Exception $e){
- $this->is_commit = false;
- $this->ret_msg = $e->getMessage();
- Db::rollback();
- }
- $this->transReturn($ret_data);
- }
-
- public function payOrderNew()
- {
- $level_id = input('post.level_id');
- $level_key = input('post.level_key');
- $pay_type = input('post.pay_type',1);
- $user_info = User::where('id',$this->user_id)->find();
- if($user_info['account_type'] == 1 && $user_info['is_first'] == 0) $this->error('请联系公司管理员进行会员开通');
- $level_set = UserLevel::where('id', $level_id)->find()->toArray();
- if (!$level_set['price']) $this->error('会员价格设置有误');
- $level_price = json_decode($level_set['price'], true);
- if (!isset($level_price[$level_key])) $this->error('会员价格设置有误.');
- $order_insert = [
- 'user_id' => $this->user_id,
- 'order_no' => get_order_sn(),
- 'level_id' => $level_id,
- 'level_key' => $level_key,
- 'pay_type' => $pay_type,
- 'month' => $level_price[$level_key]['time'],
- 'price_total' =>$level_price[$level_key]['price'],
- 'pay_no' =>get_order_sn()
- ];
- $order_info = LOM::create($order_insert);
- $order_info = $order_info->toArray();
- $pay_no = $order_info['pay_no'];
- $ret_data = ['pay_status'=>0,'config'=>[],'code_url'=>'','openid'=>$user_info['openid']];
- Db::startTrans();
- try {
- switch ($pay_type){
- case 1:
- $notify_url = $this->request->root(true) . '/api/we_chat_pay/LevelOrderNotify';
- $pay_config = WeChatPay::wxPay('订单支付',$pay_no,$order_info['price_total'],$notify_url,'MWEB');
- if($pay_config['code'] != 200) $this->exception($pay_config['msg']);
- $pay_config['config']['mch_id'] = config('app.wx_pay')['mch_id'];
- $pay_config['config']['pay_no'] = $pay_no;
- $pay_config['config']['notify_url'] = $notify_url;
- $pay_config['config']['mweb_url'] = $pay_config['mweb_url'];
- $pay_config['config']['total_fee'] = $order_info['price_total'] * 100;
- $ret_data['config'] = $pay_config['config'];
- break;
- case 2 :
- $notify_url = $this->request->root(true) . '/api/Alipay/LevelOrderNotify';
- $return_url ='https://'.$_SERVER['HTTP_HOST']."/dist/#/my";
- $config = Alipay::ali_pay_pc('订单支付',$pay_no,$order_info['price_total'],$notify_url,$return_url);
- $ret_data['config']['ali_url'] = $config;
- $ret_data['ali_url'] = $config;
- break;
- case 3:
- $notify_url = $this->request->root(true) . '/api/we_chat_pay/LevelOrderNotify';
- $pay_config = WeChatPay::wxPay('订单支付',$pay_no,$order_info['price_total'],$notify_url,'APP');
- if($pay_config['code'] != 200) $this->exception($pay_config['msg']);
- $ret_data['config'] = $pay_config['config'];
- break;
- case 4:
- $notify_url = $this->request->root(true) . '/api/Alipay/LevelOrderNotify';
- $config = Alipay::ali_pay('订单支付',$pay_no,$order_info['price_total'],$notify_url);
- $ret_data['config']['ali_url'] = $config;
- $ret_data['ali_url'] = $config;
- break;
- case 5 :
- if(!$user_info['openid']) $this->exception('请绑定openid');
- $notify_url = $this->request->root(true) . '/api/we_chat_pay/LevelOrderNotify';
- $pay_config = WeChatPay::wxPay('订单支付',$pay_no,$order_info['price_total'],$notify_url,'JSAPI',$user_info['openid']);
- if($pay_config['code'] != 200) $this->exception($pay_config['msg']);
- $ret_data['config'] = $pay_config['config'];
- break;
- }
- Db::commit();
- }catch (\Exception $e){
- $this->is_commit = false;
- $this->ret_msg = $e->getMessage();
- Db::rollback();
- }
- $this->transReturn($ret_data);
- }
- }
|