From 7d455b045cacbfb74e1655cdc77f2ce29cf10a09 Mon Sep 17 00:00:00 2001 From: lccsw <805383944@qq.com> Date: Thu, 20 Oct 2022 19:54:15 +0800 Subject: [PATCH] add-sylive-act_pay --- common/helpers/order_helper.php | 3 + .../Market_sylive_activity_orders_model.php | 17 +++ home/controllers/h5/market/sylive/Act.php | 45 +----- home/controllers/h5/market/sylive/Biz.php | 131 +++++++++++++++++- home/controllers/h5/market/sylive/Common.php | 42 +++++- home/controllers/h5/market/sylive/Notify.php | 59 ++++++-- home/views/h5/market/sylive/act/index.php | 3 +- home/views/h5/market/sylive/act/item.php | 3 +- home/views/h5/market/sylive/act/share.php | 3 +- home/views/h5/market/sylive/act/ucenter.php | 3 +- home/views/h5/market/sylive/biz/brands.php | 5 +- home/views/h5/market/sylive/biz/index.php | 10 +- home/views/h5/market/sylive/biz/lists.php | 5 +- home/views/h5/market/sylive/biz/pay.php | 75 ++++++++++ home/views/h5/market/sylive/header.php | 2 +- home/views/h5/market/sylive/login.php | 5 +- home/views/h5/market/sylive/stic/area.php | 3 +- home/views/h5/market/sylive/stic/index.php | 5 +- home/views/h5/market/sylive/stic/rank.php | 5 +- home/views/h5/market/sylive/stic/users.php | 5 +- www/home/css/h5/market/sylive/h5.css | 2 +- 21 files changed, 338 insertions(+), 93 deletions(-) create mode 100644 common/models/market/Market_sylive_activity_orders_model.php create mode 100644 home/views/h5/market/sylive/biz/pay.php diff --git a/common/helpers/order_helper.php b/common/helpers/order_helper.php index 90232e19..17d522b1 100644 --- a/common/helpers/order_helper.php +++ b/common/helpers/order_helper.php @@ -28,6 +28,9 @@ if ( ! function_exists('create_order_no')) case 'licheb': $source_id = sprintf("%02d", 2); break; + case 'market': + $source_id = sprintf("%02d", 51); + break; default: $source_id = sprintf("%02d", 0); } diff --git a/common/models/market/Market_sylive_activity_orders_model.php b/common/models/market/Market_sylive_activity_orders_model.php new file mode 100644 index 00000000..3bc9aaa0 --- /dev/null +++ b/common/models/market/Market_sylive_activity_orders_model.php @@ -0,0 +1,17 @@ +table_name, 'default'); + } + +} \ No newline at end of file diff --git a/home/controllers/h5/market/sylive/Act.php b/home/controllers/h5/market/sylive/Act.php index 63df2732..2238a494 100644 --- a/home/controllers/h5/market/sylive/Act.php +++ b/home/controllers/h5/market/sylive/Act.php @@ -305,7 +305,7 @@ class Act extends Wx { if(!$unpay){ $this->load->helper('order'); $unpay = [ - 'sid' => create_order_no(), + 'sid' => create_order_no('350200','market'), 'uid' => $this->uid, 'item_id' => $a_id, 'item_title' => $item['title'], @@ -322,7 +322,7 @@ class Act extends Wx { } $user = $this->user_model->get(['userId'=>$this->uid]); $notify_url = http_host_com('home').'/h5/market/sylive/notify'; - $result = $this->pay($unpay['sid'],$unpay['total_price'],$user['openid'],$unpay['item_title'],$notify_url,$unpay['expire_time']); + $result = $this->unorder($unpay['sid'],$unpay['total_price'],$user['openid'],$unpay['item_title'],$notify_url,$unpay['expire_time']); if(!$result['code']){ $this->show_json('',400,$result['msg']); } @@ -402,47 +402,6 @@ class Act extends Wx { $this->show_view('h5/market/sylive/act/ucenter'); } - /** - * 支付 - * @param $trade_no - * @param $price - * @param $openid - * @param $body - * @param $notify_url 异步通知 - * @param $expire_time 过期时间 - * @param $attach - * @return bool|json数据,可直接填入js函数作为参数|mixed - * @throws WxPayException - */ - private function pay($trade_no,$price,$openid,$body,$notify_url,$expire_time,$attach=''){ - - if(!$body){return false;} - require_once APPPATH."../api/third_party/WXconfig/hdy_WxPay.Config.php"; - require_once APPPATH."../api/third_party/WXpay/WxPay.Api.php"; - $config = new WxPayConfig(); - $wxpay = new WxPayUnifiedOrder(); - $wxpay->SetVersion('1.0'); - $wxpay->SetBody($body); //简单描述 - $attach && $wxpay->SetAttach($attach); //附加信息 - $wxpay->SetNotify_url($notify_url); - $wxpay->SetOut_trade_no($trade_no); //订单号 - $wxpay->SetTotal_fee($price * 100); //支付价格 - $wxpay->SetTime_start(date("YmdHis")); //交易起始时间 - $wxpay->SetTime_expire(date('YmdHis',$expire_time)); //交易结束时间 - $wxpay->SetTrade_type("JSAPI"); //设置交易类型 - $wxpay->SetOpenid($openid); //openid - $return = WxPayApi::unifiedOrder($config, $wxpay); //统一支付 - if($return['result_code'] == 'SUCCESS') { - $wxpay_api = new WxPayJsApiPay(); - $jsApiParameters = WxPayApi::GetJsApiParameters($return, $config, $wxpay_api); - $jsApiParameters = json_decode($jsApiParameters, true); - return ['code'=>1,'data'=>$jsApiParameters,'msg'=>'下单成功']; - }else{ - $msg = $return['return_msg'] ? $return['return_msg'].$return['err_code_des'] : $return['return_msg']; - return ['code'=>0,'data'=>[],'msg'=>$msg]; - } - } - //获取微信用户信息 public function userinfo(){ $ret = $this->set_auth('',1); diff --git a/home/controllers/h5/market/sylive/Biz.php b/home/controllers/h5/market/sylive/Biz.php index a3e8b01b..9f226e67 100644 --- a/home/controllers/h5/market/sylive/Biz.php +++ b/home/controllers/h5/market/sylive/Biz.php @@ -12,6 +12,7 @@ class Biz extends Admin { $this->load->model('market/market_sylive_organization_model'); $this->load->model('market/market_sylive_activity_model'); $this->load->model('market/market_sylive_activity_kpidata_model','mdSytActivityKpiData'); + $this->load->model('market/market_sylive_activity_orders_model','mdSytActivityOrders'); $this->load->library('market/sylive_entity'); $this->load->library('MyEncryption'); $this->group_id = $this->sylive_entity->get_level($this->session['org_id']); @@ -255,8 +256,9 @@ class Biz extends Admin { $total = $this->market_sylive_activity_model->count($where); $lists = []; if($total){ - $rows = $this->market_sylive_activity_model->select($where,'timeStart desc',$page,$size,'activityId,title,timeStart,timeEnd'); + $rows = $this->market_sylive_activity_model->select($where,'timeStart desc',$page,$size,'activityId,title,timeStart,timeEnd,jsondata'); foreach ($rows as $val) { + $jsondata = json_decode($val['jsondata'],true); $s_time = strtotime($val['timeStart']); $e_time = strtotime($val['timeEnd']); $n_time = time(); @@ -267,13 +269,47 @@ class Biz extends Admin { }else{ $state = ['title'=>'活动已结束','class'=>'bg-fccba0']; } + //判断是否支付 + $pay = $jsondata['pay'] ? $jsondata['pay'] : []; + $way = $pay['way'] ? intval($pay['way']) : 1; //1厂商支付 2 经销商支付 $skey = $this->myencryption->base64url_encode("a_id=" . $val['activityId']); + $pay_status = 1; + $url = '/h5/market/sylive/act?skey='.$skey; + $stat_url = '/h5/market/sylive/stic?skey='.$skey; + $text_cn = ''; + if($way==2 && in_array($this->group_id,[2,3])){ + $where = [ + 'activityId' => $val['activityId'], + 'status' => 1 + ]; + if($this->group_id==3){ //顾问 + $biz_row = $this->market_sylive_organization_model->get(['organizationId'=>$this->session['org_id']],'parentId'); + $biz_id = $biz_row['parentId']; + }else{ + $biz_id = $this->session['org_id']; + } + $where['bizId'] = $biz_id; + $pay_row = $this->mdSytActivityOrders->count($where); + if(!$pay_row){ + $pay_status = 0; + if($this->group_id==3){ + $url = $stat_url = ''; + $text_cn = '请联系店长'; + }else{ + $url = $stat_url = '/h5/market/sylive/biz/pay?skey='.$skey; + $text_cn = '开通'; + } + } + } + $temp = [ 'title' => $val['title'], 'time' => date('Y-m-d H:i',$s_time).'~'.date('Y-m-d H:i',$e_time), - 'url' => '/h5/market/sylive/act?skey='.$skey, - 'stat_url' => '/h5/market/sylive/stic?skey='.$skey, + 'url' => $url, + 'stat_url' => $stat_url, 'state' => $state, + 'pay_status' => $pay_status, + 'text_cn' => $text_cn ]; $lists[] = $temp; } @@ -283,5 +319,94 @@ class Biz extends Admin { $this->show_json($data,200); } + //活动支付页面 + public function pay(){ + $skey = $this->input->get('skey'); + $param = $this->myencryption->base64url_decode($skey); + $a_id = intval($param['a_id']);//活动id + $row = $this->market_sylive_activity_model->get(['activityId'=>$a_id]); + if(!$row){ + throw new Hd_exception('参数错误',400); + } + if($this->group_id!=2){ + throw new Hd_exception('权限不足',400); + } + $jsondata = json_decode($row['jsondata'],true); + //判断是否支付 + $pay = $jsondata['pay'] ? $jsondata['pay'] : []; + $way = $pay['way'] ? intval($pay['way']) : 1; //1厂商支付 2 经销商支付 + $img = $pay['img'] ? $pay['img'] : $row['bgImg']; + $btntx = '已支付'; + $pay_status = 1; + $where = [ + 'activityId' => $a_id, + 'bizId' => $this->session['org_id'], + 'status' => 1, + ]; + $pay_row = $this->mdSytActivityOrders->count($where); + if($way==2 && !$pay_row){ + $pay_status = 0; + $btntx = "{$pay['price']}元立即支付"; + } + $info = [ + 'status' => $pay_status, + 'img' => build_qiniu_image_url($img), + 'btntx' => $btntx, + 'skey' => $skey, + ]; + $this->data['info'] = $info; + $wx_info = $this->share_info(); + $this->data['sign_package'] = $wx_info['sign_package']; + $this->show_view('h5/market/sylive/biz/pay'); + } + //创建支付订单 + public function c_order(){ + if($this->group_id!=2){ + $this->show_json('',400,'权限不足'); + } + $skey = $this->input->post('skey'); + $param = $this->myencryption->base64url_decode($skey); + $a_id = intval($param['a_id']);//活动id + $row = $this->market_sylive_activity_model->get(['activityId'=>$a_id]); + if(!$row){ + $this->show_json('',400,'参数错误'); + } + $jsondata = json_decode($row['jsondata'],true); + $pay = $jsondata['pay'] ? $jsondata['pay'] : []; + //判断是否已支付 + $pay_row = $this->mdSytActivityOrders->count(['activityId'=>$a_id,'status'=>1,'bizId'=>$this->session['org_id']]); + if($pay_row){ + $this->show_json('',400,'已支付,请勿重复支付'); + } + $pay_price = $pay['price']; + if($pay_price<=0){ + $this->show_json('',400,'无需支付'); + } + $unpay = $this->mdSytActivityOrders->get(['activityId'=>$a_id,'status'=>0,'bizId'=>$this->session['org_id'],'expireTime>'=>time()]); + if(!$unpay){ + $this->load->helper('order'); + $unpay = [ + 'sid' => create_order_no('350200','market'), + 'activityId' => $a_id, + 'userId' => $this->uid, + 'areaId' => $a_id, + 'bizId' => $this->session['org_id'], + 'totalPrice' => $pay_price, + 'expireTime' => time()+24*60*60,//订单过期时间 + 'createTime' => date('Y-m-d H:i:s') + ]; + $oid = $this->mdSytActivityOrders->add($unpay); + if(!is_numeric($oid)){ + $this->show_json('',400,'创建订单失败'); + } + } + $user = $this->user_model->get(['userId'=>$this->uid]); + $notify_url = http_host_com('home').'/h5/market/sylive/notify/biz'; + $result = $this->unorder($unpay['sid'],$unpay['totalPrice'],$user['openid'],$row['title'],$notify_url,$unpay['expireTime']); + if(!$result['code']){ + $this->show_json('',400,$result['msg']); + } + $this->show_json($result['data'],200,'操作成功'); + } } \ No newline at end of file diff --git a/home/controllers/h5/market/sylive/Common.php b/home/controllers/h5/market/sylive/Common.php index 2c1d7982..c46631d5 100644 --- a/home/controllers/h5/market/sylive/Common.php +++ b/home/controllers/h5/market/sylive/Common.php @@ -95,7 +95,7 @@ abstract class Common extends CI_Controller{ } } //微信分享信息 - public function share_info($act_row=[]){ + protected function share_info($act_row=[]){ $a_id = $act_row['activityId']; $share_skey = "a_id=" . $a_id . "&cf_uid=" . $this->uid . "&cf_share=" . $this->myencryption->random_string(6); $this->load->library('Jssdk'); @@ -115,6 +115,46 @@ abstract class Common extends CI_Controller{ } return ['sign_package' => $sign_package ,'share' => $share]; } + + /** + * 支付 + * @param $trade_no + * @param $price + * @param $openid + * @param $body + * @param $notify_url 异步通知 + * @param $expire_time 过期时间 + * @param $attach + * @return bool|json数据,可直接填入js函数作为参数|mixed + * @throws WxPayException + */ + protected function unorder($trade_no,$price,$openid,$body,$notify_url,$expire_time,$attach=''){ + if(!$body){return false;} + require_once APPPATH."../api/third_party/WXconfig/hdy_WxPay.Config.php"; + require_once APPPATH."../api/third_party/WXpay/WxPay.Api.php"; + $config = new WxPayConfig(); + $wxpay = new WxPayUnifiedOrder(); + $wxpay->SetVersion('1.0'); + $wxpay->SetBody($body); //简单描述 + $attach && $wxpay->SetAttach($attach); //附加信息 + $wxpay->SetNotify_url($notify_url); + $wxpay->SetOut_trade_no($trade_no); //订单号 + $wxpay->SetTotal_fee($price * 100); //支付价格 + $wxpay->SetTime_start(date("YmdHis")); //交易起始时间 + $wxpay->SetTime_expire(date('YmdHis',$expire_time)); //交易结束时间 + $wxpay->SetTrade_type("JSAPI"); //设置交易类型 + $wxpay->SetOpenid($openid); //openid + $return = WxPayApi::unifiedOrder($config, $wxpay); //统一支付 + if($return['result_code'] == 'SUCCESS') { + $wxpay_api = new WxPayJsApiPay(); + $jsApiParameters = WxPayApi::GetJsApiParameters($return, $config, $wxpay_api); + $jsApiParameters = json_decode($jsApiParameters, true); + return ['code'=>1,'data'=>$jsApiParameters,'msg'=>'下单成功']; + }else{ + $msg = $return['return_msg'] ? $return['return_msg'].$return['err_code_des'] : $return['return_msg']; + return ['code'=>0,'data'=>[],'msg'=>$msg]; + } + } } class Admin extends Common{ diff --git a/home/controllers/h5/market/sylive/Notify.php b/home/controllers/h5/market/sylive/Notify.php index 14fb8392..6d5af5fb 100644 --- a/home/controllers/h5/market/sylive/Notify.php +++ b/home/controllers/h5/market/sylive/Notify.php @@ -13,18 +13,21 @@ class Notify extends CI_Controller { parent::__construct(); $this->load->model('market/market_sylive_activity_user_model', 'act_user_model'); $this->load->model('market/market_sylive_order_model','order_model'); + $this->load->model('market/market_sylive_activity_model'); + $this->load->model('market/market_sylive_activity_orders_model','mdSytActivityOrders'); $this->load->library('market/sylive_entity'); - try{ - //如果返回成功则验证签名 - $config = new WxPayConfig(); - $input = file_get_contents("php://input"); - $result = WxPayNotifyResults::Init($config, $input); - $this->notify = $result->GetValues(); - debug_log("[info] ". __FUNCTION__ . "# notify:" . json_encode($this->notify,JSON_UNESCAPED_UNICODE), $this->log_file,$this->log_dir); - }catch (WxPayException $e){ - debug_log("[error] ". __FUNCTION__ . ":".$e->getMessage(), $this->log_file,$this->log_dir); - exit(); - } +// try{ +// //如果返回成功则验证签名 +// $config = new WxPayConfig(); +// $input = file_get_contents("php://input"); +// $result = WxPayNotifyResults::Init($config, $input); +// $this->notify = $result->GetValues(); +// debug_log("[info] ". __FUNCTION__ . "# notify:" . json_encode($this->notify,JSON_UNESCAPED_UNICODE), $this->log_file,$this->log_dir); +// }catch (WxPayException $e){ +// debug_log("[error] ". __FUNCTION__ . ":".$e->getMessage(), $this->log_file,$this->log_dir); +// exit(); +// } + $this->notify = $this->input->get(); } //微信支付异步通知 @@ -64,4 +67,38 @@ class Notify extends CI_Controller { echo ''; } + //活动支付回调 + public function biz(){ + try{ + $sid = $this->notify['out_trade_no']; + if($sid){ + debug_log("[start] ". __FUNCTION__ . ": out_trade_no:".$this->notify['out_trade_no'], $this->log_file,$this->log_dir); + $order = $this->mdSytActivityOrders->get(array('sid'=>$sid)); + if(!$order){ + throw new Exception("{$sid}_订单不存在"); + } + if($order['status']){ + throw new Exception("{$sid}_订单已支付"); + } + if($this->notify['result_code'] != 'SUCCESS'){ //支付失败 + throw new Exception("支付失败,sid={$sid}"); + }else{ //支付成功 + $res = $this->mdSytActivityOrders->update(['status'=>1,'payTime'=>date('Y-m-d H:i:s')],['id'=>$order['id']]); + if($res){ + debug_log("[success] ". __FUNCTION__ . ":操作成功", $this->log_file,$this->log_dir); + } + } + } + }catch (Exception $e){ + $msg = $e->getMessage(); + debug_log("[error] ". __FUNCTION__ . ":{$msg}", $this->log_file,$this->log_dir); + } + if($sid){ + debug_log("[finish] ". __FUNCTION__ . ": out_trade_no-".$this->notify['out_trade_no'], $this->log_file,$this->log_dir); + }else{ + debug_log("[finish] ". __FUNCTION__ . ": 参数错误-".json_encode($this->notify,JSON_UNESCAPED_UNICODE), $this->log_file,$this->log_dir); + } + echo ''; + } + } diff --git a/home/views/h5/market/sylive/act/index.php b/home/views/h5/market/sylive/act/index.php index ecd1dd85..00d0c47b 100644 --- a/home/views/h5/market/sylive/act/index.php +++ b/home/views/h5/market/sylive/act/index.php @@ -1,8 +1,7 @@ -
-
+
diff --git a/home/views/h5/market/sylive/act/item.php b/home/views/h5/market/sylive/act/item.php index 60fc5fa3..9c9ad931 100644 --- a/home/views/h5/market/sylive/act/item.php +++ b/home/views/h5/market/sylive/act/item.php @@ -1,6 +1,5 @@ -
-
+
diff --git a/home/views/h5/market/sylive/act/share.php b/home/views/h5/market/sylive/act/share.php index fbce29aa..d19b87e4 100644 --- a/home/views/h5/market/sylive/act/share.php +++ b/home/views/h5/market/sylive/act/share.php @@ -1,7 +1,6 @@ -
-
+
diff --git a/home/views/h5/market/sylive/act/ucenter.php b/home/views/h5/market/sylive/act/ucenter.php index d696a74e..ed0999c2 100644 --- a/home/views/h5/market/sylive/act/ucenter.php +++ b/home/views/h5/market/sylive/act/ucenter.php @@ -1,6 +1,5 @@ -
-
+
diff --git a/home/views/h5/market/sylive/biz/brands.php b/home/views/h5/market/sylive/biz/brands.php index 2063c581..3b77798e 100644 --- a/home/views/h5/market/sylive/biz/brands.php +++ b/home/views/h5/market/sylive/biz/brands.php @@ -1,6 +1,5 @@ -
-
+
@@ -66,7 +65,7 @@
- +
暂无数据
请稍等...
我们是有底线的
diff --git a/home/views/h5/market/sylive/biz/index.php b/home/views/h5/market/sylive/biz/index.php index b280e34c..1a668f0f 100644 --- a/home/views/h5/market/sylive/biz/index.php +++ b/home/views/h5/market/sylive/biz/index.php @@ -1,6 +1,5 @@ -
-
+
@@ -21,7 +20,7 @@
{{info.title}}
- - +
暂无数据
请稍等...
我们是有底线的
diff --git a/home/views/h5/market/sylive/biz/lists.php b/home/views/h5/market/sylive/biz/lists.php index 519cc350..e34c42c3 100644 --- a/home/views/h5/market/sylive/biz/lists.php +++ b/home/views/h5/market/sylive/biz/lists.php @@ -1,6 +1,5 @@ -
-
+
@@ -73,7 +72,7 @@
- +
暂无数据
请稍等...
我们是有底线的
diff --git a/home/views/h5/market/sylive/biz/pay.php b/home/views/h5/market/sylive/biz/pay.php new file mode 100644 index 00000000..c69c17f6 --- /dev/null +++ b/home/views/h5/market/sylive/biz/pay.php @@ -0,0 +1,75 @@ + + + + +load->view('h5/market/sylive/hidden_wx_share')?> + \ No newline at end of file diff --git a/home/views/h5/market/sylive/header.php b/home/views/h5/market/sylive/header.php index 221c39dd..5b865d76 100644 --- a/home/views/h5/market/sylive/header.php +++ b/home/views/h5/market/sylive/header.php @@ -7,7 +7,7 @@ content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no"> <?=$_title?> - + diff --git a/home/views/h5/market/sylive/login.php b/home/views/h5/market/sylive/login.php index 484dfeed..78785b42 100644 --- a/home/views/h5/market/sylive/login.php +++ b/home/views/h5/market/sylive/login.php @@ -7,7 +7,7 @@ content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no"> <?=$_title?> - + @@ -15,8 +15,7 @@ -
-