123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276 |
- <?php
- namespace app\api\controller;
- use app\common\model\User;
- use app\common\model\UserLevel;
- use app\common\model\LevelOrder as LOM;
- use think\Db;
- /**
- * @title 会员等级订单
- * @controller LevelOrder
- * @group base
- */
- class LevelOrder extends Base
- {
- public function initialize()
- {
- parent::initialize();
- parent::checkLogin();
- }
- /**
- * @title 开通会员(续费)订单
- * @desc 开通会员(续费)订单
- * @author qc
- * @url /api/Level_order/createOrder
- * @method POST
- * @header name:Authorization require:1 desc:Token
- * @param name:level_id type:int require:1 default:0 desc:会员等级
- * @param name:level_key type:int require:1 default:0 desc:等级价格的key值
- * @return name:order_id type:int default:0 desc:订单id
- */
- 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('请联系公司管理员进行会员开通');
- $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,
- '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]);
- }
- /**
- * @title 获取订单详情
- * @desc 获取订单详情
- * @author qc
- * @method GET
- * @url /api/Level_order/getOrderDetail
- * @header name:Authorization require:1 desc:Token
- * @param name:order_id type:int default:-- desc:订单id
- * @return name:id type:int default:-- desc:订单id
- * @return name:order_no type:string default:-- desc:订单号
- * @return name:price_total type:float default:-- desc:待支付金额
- * @return name:level_id type:float default:-- desc:等级id
- * @return name:level_name type:float default:-- desc:等级名称
- * @return name:month type:float default:-- desc:时长(月)
- * @return name:pay_state type:int default:-- desc:支付状态(0未支付1已支付)
- * @return name:pay_type type:int default:-- desc:支付方式(1.h5微信,2.h5支付宝,3.app微信,4.app支付宝,5.h5微信内支付)
- * @return name:pay_no type:string default:-- desc:支付号
- * @return name:pay_at type:string default:-- desc:支付时间
- * @return name:cancel_state type:int default:-- desc:取消状态(0未取消1已取消)
- * @return name:cancel_at type:string default:-- desc:取消时间
- * @return name:cancel_desc type:string default:-- desc:取消原因
- * @return name:cancel_desc type:string default:-- desc:取消原因
- * @return name:level_set type:array default:-- desc:购买会员设置
- * @return name:level_set.title type:string default:-- desc:标题
- * @return name:level_set.price type:float default:-- desc:价格
- * @return name:level_set.time type:int default:-- desc:时间(月)
- */
- 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]);
- }
- /**
- * @title 订单支付
- * @desc 订单支付
- * @author qc
- * @url /api/Level_order/payOrder
- * @method POST
- * @header name:Authorization require:1 desc:Token
- * @param name:order_id type:int require:1 default:0 desc:订单id
- * @param name:pay_type type:int require:1 default:0 desc:支付方式(1.h5微信,2.h5支付宝,3.app微信,4.app支付宝)
- * @return name:config type:array default:0 desc:小程序支付配置
- * @return name:config.appId type:string default:-- desc:公众账号ID
- * @return name:config.signType type:string default:-- desc:签名类型
- * @return name:config.paySign type:string default:-- desc:签名
- * @return name:config.nonceStr type:string default:-- desc:随机字符串
- * @return name:config.timestamp type:string default:-- desc:时间戳
- * @return name:config.partnerid type:string default:-- desc:商户号
- * @return name:config.prepayid type:string default:-- desc:唯一支付号
- * @return name:config.package type:string default:-- desc:package
- * @return name:config.mch_id type:string default:-- desc:商户号(H5)
- * @return name:config.pay_no type:string default:-- desc:订单支付号(H5)
- * @return name:config.notify_url type:string default:-- desc:回调地址(H5)
- * @return name:config.total_fee type:int default:-- desc:支付金额(分!!!)
- * @return name:code_url type:string default:-- desc:付款码url
- * @return name:openid type:string default:-- desc:openid
- */
- 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('订单金额错误');
- $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;
- }
- Db::commit();
- }catch (\Exception $e){
- $this->is_commit = false;
- $this->ret_msg = $e->getMessage();
- Db::rollback();
- }
- $this->transReturn($ret_data);
- }
- /**
- * @title 开通会员
- * @desc 开通会员
- * @author qc
- * @url /api/Level_order/payOrderNew
- * @method POST
- * @header name:Authorization require:1 desc:Token
- * @param name:level_id type:int require:1 default:0 desc:会员等级
- * @param name:level_key type:int require:1 default:0 desc:等级价格的key值
- * @param name:pay_type type:int require:1 default:0 desc:支付方式(1.h5微信,2.h5支付宝,3.app微信,4.app支付宝,5.h5微信内支付)
- * @return name:config type:array default:0 desc:支付配置
- * @return name:config.appId type:string default:-- desc:公众账号ID
- * @return name:config.signType type:string default:-- desc:签名类型
- * @return name:config.paySign type:string default:-- desc:签名
- * @return name:config.nonceStr type:string default:-- desc:随机字符串
- * @return name:config.timestamp type:string default:-- desc:时间戳
- * @return name:config.partnerid type:string default:-- desc:商户号
- * @return name:config.prepayid type:string default:-- desc:唯一支付号
- * @return name:config.package type:string default:-- desc:package
- * @return name:config.mch_id type:string default:-- desc:商户号(H5)
- * @return name:config.pay_no type:string default:-- desc:订单支付号(H5)
- * @return name:config.notify_url type:string default:-- desc:回调地址(H5)
- * @return name:config.total_fee type:int default:-- desc:支付金额(分!!!)
- * @return name:code_url type:string default:-- desc:付款码url
- * @return name:openid type:string default:-- desc:openid
- */
- 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);
- }
- }
|