public function consume_coupons() { $code = $this->_post('code', 'trim'); $code = str_replace("-", "", $code); $where = array('token' => $this->token, 'cancel_code' => $code); $info = M('Member_card_coupon_record')->where($where)->find(); $info['is_wx'] = M('Member_card_coupon')->where(array('token' => $this->token, 'id' => $info['coupon_id']))->getField('is_weixin'); $result = array(); if ($info['is_use'] == 1) { $result['err'] = 1; $result['msg'] = '此券已经核销,请不要重新核销'; } else { if ($info['is_wx']) { $coupons = new WechatCoupons($this->wxuser); $res = $coupons->consumeCoupons($info['card_id'], $info['cancel_code']); if ($res['errcode'] > 0) { $result['err'] = 2; $result['msg'] = $res['errmsg']; } } $rwhere = array('token' => $this->token, 'cancel_code' => $code, 'is_use' => '0'); if (empty($result) && M('Member_card_coupon_record')->where($rwhere)->save(array('use_time' => time(), 'is_use' => '1', 'staff_id' => -2))) { $result['err'] = 0; $result['msg'] = '核销成功'; M('Member_card_coupon')->where(array('token' => $this->token, 'id' => $info['coupon_id']))->setInc('usetime', 1); } } echo json_encode($result); }
public function afterPay($id,$third_id='',$transaction_id='') { $thisOrder=$this->beforePay($id); if(empty($thisOrder)){ exit('订单不存在!'); }else if($thisOrder['paid']){ exit('此订单已付款,请勿重复操作!'); } $wecha_id = $thisOrder['wecha_id']; file_put_contents($_SERVER['DOCUMENT_ROOT'] . '/DataPig/conf/4' . $this->token . '.txt', json_encode($thisOrder)); if (($this->payType != 'daofu') && ($this->payType != 'dianfu')) { if ($cpr = D('Coupon_pay_record')->where(array('orderid' => $id, 'token' => $this->token, 'wechat_id' => $thisOrder['wecha_id'], 'from' => $this->from))->find()) { $tprice = number_format($thisOrder['price'] + $cpr['reduce_cost'], 2, '.', ''); $obj = new Member_card_coupon_recordModel(); $coupon = $obj->check_coupon($cpr['coupon_id'], $thisOrder['wecha_id'], $this->token, $tprice); if ($coupon['error']) { D('Userinfo')->where(array('wecha_id' => $thisOrder['wecha_id'], 'token' => $this->token))->setInc('balance', $thisOrder['price']); exit($coupon['msg'] . ',您支付的金额已经充值到您的 账号里'); exit(); } else { $coupon = $coupon['data']; $obj->use_coupon($cpr['coupon_id'], $thisOrder['wecha_id'], $this->token, $tprice); D('Coupon_pay_record')->where(array('orderid' => $id, 'token' => $this->token, 'wechat_id' => $thisOrder['wecha_id'], 'from' => $this->from))->save(array('dateline' => time())); if ($coupon['is_weixin']) { $thisWxUser = M('Wxuser')->where(array('token' => $this->token))->find(); $coupons = new WechatCoupons($thisWxUser); $res = $coupons->consumeCoupons($coupon['card_id'], $coupon['cancel_code']); } } } $member_card_create_db = M('Member_card_create'); $userCard = $member_card_create_db->where(array('token' => $this->token, 'wecha_id' => $wecha_id))->find(); $userinfo_db = M('Userinfo'); if ($userCard && ($this->from != 'Card')) { $member_card_set_db = M('Member_card_set'); $thisCard = $member_card_set_db->where(array('id' => intval($userCard['cardid'])))->find(); if ($thisCard) { $set_exchange = M('Member_card_exchange')->where(array('cardid' => intval($thisCard['id'])))->find(); $arr['token'] = $this->token; $arr['wecha_id'] = $wecha_id; $arr['expense'] = $thisOrder['price']; $arr['time'] = time(); $arr['cat'] = 99; $arr['staffid'] = 0; $arr['score'] = intval($set_exchange['reward']) * $arr['expense']; if (isset($_GET['redirect'])) { $infoArr = explode('|', $_GET['redirect']); $param = explode(',', $infoArr[1]); if ($param) { foreach ($param as $pa) { $pas = explode(':', $pa); if ($pas[0] == 'itemid') { $arr['itemid'] = $pas[1]; } } } } M('Member_card_use_record')->add($arr); $thisUser = $userinfo_db->where(array('token'=>$thisCard['token'],'wecha_id'=>$arr['wecha_id']))->find(); $userArr=array(); $userArr['total_score']=$thisUser['total_score']+$arr['score']; $userArr['expensetotal']=$thisUser['expensetotal']+$arr['expense']; $userinfo_db->where(array('token'=>$this->token,'wecha_id'=>$arr['wecha_id']))->save($userArr); } } $data_order['paid'] = 1; $data_order['havepaid'] = $thisOrder['havepaid'] + $arr['expense']; //自行增加 支付成功后,写入当前支付总额 } // $order_model=$this->db; $data_order['paytype'] = $this->payType; //file_put_contents($_SERVER['DOCUMENT_ROOT'].'/DataPig/conf/3'.$this->token.'.txt',json_encode($thisOrder)); if (key_exists('third_id',$thisOrder)){ $data_order['third_id'] = $third_id; }else { $data_order['transactionid']=$third_id; } //$order_model->where(array('orderid'=>$id))->setField('paid',1); $where_arr=array('orderid'=>$id); if (strtolower($this->from)=='repast'){ $where_arr=array('tmporderid'=>$id); } $order_model->where($where_arr)->data($data_order)->save(); // 微店 if ('Micrstore' == $this->from) { $this->apiMicrstorePayNofity(array('order_no'=>$id, 'third_id'=>$third_id, 'payment_method'=>$this->payType, 'pay_money'=>$data_order['price'])); } if (strtolower($this->getFrom())=='groupon'){ $order_model->where(array('orderid'=>$thisOrder['orderid']))->save(array('transactionid'=>$transaction_id,'paytype'=>$this->payType)); } if (isset($_GET['pl']) && ($_GET['pl'] == 1)) { $database_platform_pay = D('Platform_pay'); if(!$database_platform_pay->where(array('from'=>$this->from,'orderid'=>$thisOrder['orderid']))->find()){ $data_platform_pay['orderid'] = $thisOrder['orderid']; $data_platform_pay['price'] = $thisOrder['price']; $data_platform_pay['wecha_id'] = $thisOrder['wecha_id']; $data_platform_pay['token'] = $thisOrder['token']; $data_platform_pay['from'] = $this->from; $data_platform_pay['time'] = $_SERVER['REQUEST_TIME']; $database_platform_pay->data($data_platform_pay)->add(); } } if ($this->payType == 'weixin') { $plat_type = (isset($_GET['pl']) ? intval($_GET['pl']) : 0); $database_weixin_bill = D('Weixin_bill'); if (!$database_weixin_bill->where(array('from' => $this->from, 'orderid' => $thisOrder['orderid']))->find()) { if ($plat_type != 1) { $payConfig = M('Alipay_config')->where(array('token' => $this->token))->find(); $payConfigInfo = unserialize($payConfig['info']); $appid = (isset($payConfigInfo['weixin']['new_appid']) ? $payConfigInfo['weixin']['new_appid'] : $payConfigInfo['weixin']['appid']); $mchid = (isset($payConfigInfo['weixin']['mchid']) ? $payConfigInfo['weixin']['mchid'] : ''); } else { $appid = C('appid'); $mchid = C('platform_weixin_mchid'); } $data_system_pay['orderid'] = $thisOrder['orderid']; $data_system_pay['price'] = $thisOrder['price']; $data_system_pay['wecha_id'] = $thisOrder['wecha_id']; $data_system_pay['token'] = $thisOrder['token']; $data_system_pay['from'] = $this->from; $data_system_pay['time'] = $_SERVER['REQUEST_TIME']; $data_system_pay['third_id'] = $third_id; $data_system_pay['plat_type'] = $plat_type; $data_system_pay['appid'] = $appid; $data_system_pay['mchid'] = $mchid; $database_weixin_bill->data($data_system_pay)->add(); } } return $thisOrder; }
public function actionConsume(){ if(IS_POST){ $now = time(); $cardid = $this->_get('cardid','intval'); $cancel_code = $this->_post('cancel_code','trim'); $price = $this->_post('price','floatval'); $consume_id = $this->_post('consume_id','intval'); $pay_type = $this->_post('pay_type','intval'); $card_pwd = $this->_post('card_pwd','trim'); $com_pwd = $this->_post('com_pwd','trim'); $company_id = $this->_post('company_id','intval'); $from = $this->_post('from','intval'); $rwhere = array('token'=>$this->token,'wecha_id'=>$this->wecha_id,'id'=>$consume_id); $r_record = M('Member_card_coupon_record')->where($rwhere)->find(); if($pay_type == 1){ if($from == 'local' || $from == 'default'){ if($r_record['is_use'] == 1){ $this->error('优惠券已使用'); exit; } }else if($from == 'weixin' || $from == 'local_weixin'){ $coupons = new WechatCoupons($this->wxuser); $res = $coupons->consumeCoupons($r_record['card_id'],$r_record['cancel_code']); if($res['errcode'] > 0){ $this->error('核销失败,优惠券可能已经被使用'); exit; } }else{ $this->error('无效的卡券来源'); } $paypass = $this->fans['paypass']; $arr['itemid'] = $r_record['coupon_id']; $arr['wecha_id'] = $this->wecha_id; $arr['expense'] = $price; $arr['time'] = $now; $arr['token'] = $this->token; $arr['cat'] = 1; $arr['staffid'] = 0; $arr['usecount'] = 1; $set_exchange = M('Member_card_exchange')->where(array('cardid'=>$cardid))->find(); $arr['score']=intval($set_exchange['reward'])*$arr['expense']; $single_orderid = date('YmdHis',time()).mt_rand(1000,9999); $record['orderid'] = $single_orderid; $record['ordername'] = $consume_id == 0?'会员卡现金支付':'现金支付除优惠劵外的款项'; $record['paytype'] = 'CardPay'; $record['createtime'] = time(); $record['paid'] = 0; $record['price'] = $arr['expense']; $record['token'] = $this->token; $record['wecha_id'] = $this->wecha_id; $record['company_id'] = $company_id; $record['type'] = 0; $record['cardid'] = $cardid; M('Member_card_coupon')->where(array('id'=>$r_record['coupon_id']))->setInc('usetime',1); $result = M('Member_card_pay_record')->add($record); M('Member_card_coupon_record')->where($rwhere)->setField('company_id', $company_id); // 2015-08-13 修改会员卡支付 //M('Member_card_coupon_record')->where($rwhere)->save(array('use_time'=>time(),'is_use'=>'1','staff_id'=>-1)); $this->redirect(U('CardPay/pay',array('from'=>'Card', 'consume_id'=>$consume_id, 'token'=>$this->token,'wecha_id'=>$this->wecha_id,'price'=>$arr['expense'],'single_orderid'=>$single_orderid,'orderName'=>'支付除优惠劵外多余款项','redirect'=>'Card/payReturn|itemid:'.$r_record['coupon_id'].',usecount:'.$arr['usecount'].',score:'.$arr['score'].',type:coupon,cardid:'.$cardid))); exit; }else{ $staff_db=M('Company_staff'); $staff_where = array('username'=>$this->_post('username'),'token'=>$this->token,'companyid'=>$company_id); $thisStaff = $staff_db->where($staff_where)->find(); if(empty($thisStaff)){ $this->error('商家用户名不存在'); exit; } if (md5($this->_post('password')) == $thisStaff['password']){ if($from == 'local' || $from == 'default'){ if($r_record['is_use'] == 1){ $this->error('优惠券已使用'); exit; } }else if($from == 'weixin' || $from == 'local_weixin'){ $coupons = new WechatCoupons($this->wxuser); $res = $coupons->consumeCoupons($r_record['card_id'],$r_record['cancel_code']); if($res['errcode'] > 0){ $this->error('核销失败,优惠券可能已经被使用'); exit; } }else{ $this->error('无效的卡券来源'); } $arr=array(); $arr['itemid'] = $r_record['coupon_id']; $arr['wecha_id'] = $this->wecha_id; $arr['expense'] = $price?$price:0; $arr['time'] = $now; $arr['token'] = $this->token; $arr['cat'] = 0; $arr['notes'] = $this->_post('notes','trim'); $arr['staffid'] = $thisStaff['id']; $arr['usecount'] = 1; $arr['company_id'] = $company_id; $arr['cardid'] = $cardid; $arr['record_id'] = $consume_id; $arr['staff_id'] = $thisStaff['id']; $set_exchange = M('Member_card_exchange')->where(array('cardid'=>$cardid))->find(); $arr['score'] = intval($set_exchange['reward'])*$arr['expense']; if($set_exchange['reward'] > 0){ M('Member_card_use_record')->add($arr); M('Userinfo')->where(array('token'=>$this->token,'wecha_id'=>$this->wecha_id))->setInc('total_score',$arr['score']); M('Userinfo')->where(array('token'=>$this->token,'wecha_id'=>$this->wecha_id))->setInc('expensetotal',$arr['expense']); } M('Member_card_coupon')->where(array('id'=>$r_record['coupon_id']))->setInc('usetime',1); M('Member_card_coupon_record')->where($rwhere)->save(array('use_time'=>time(),'is_use'=>'1','staff_id'=>-1)); $this->success('支付成功'); exit; }else{ $this->error('商家密码错误!'); exit; } } } }
public function actionConsume() { if (IS_POST) { $now = time(); $cardid = $this->_get("cardid", "intval"); $cancel_code = $this->_post("cancel_code", "trim"); $price = $this->_post("price", "floatval"); $consume_id = $this->_post("consume_id", "intval"); $pay_type = $this->_post("pay_type", "intval"); $card_pwd = $this->_post("card_pwd", "trim"); $com_pwd = $this->_post("com_pwd", "trim"); $company_id = $this->_post("company_id", "intval"); $from = $this->_post("from", "intval"); $rwhere = array("token" => $this->token, "wecha_id" => $this->wecha_id, "id" => $consume_id); $r_record = M("Member_card_coupon_record")->where($rwhere)->find(); if ($pay_type == 1) { if ($from == "local" || $from == "default") { if ($r_record["is_use"] == 1) { $this->error("优惠券已使用"); exit; } } else { if ($from == "weixin" || $from == "local_weixin") { $coupons = new WechatCoupons($this->wxuser); $res = $coupons->consumeCoupons($r_record["card_id"], $r_record["cancel_code"]); if (0 < $res["errcode"]) { $this->error("核销失败,优惠券可能已经被使用"); exit; } } else { $this->error("无效的卡券来源"); } } $paypass = $this->fans["paypass"]; $arr["itemid"] = $r_record["coupon_id"]; $arr["wecha_id"] = $this->wecha_id; $arr["expense"] = $price; $arr["time"] = $now; $arr["token"] = $this->token; $arr["cat"] = 1; $arr["staffid"] = 0; $arr["usecount"] = 1; $set_exchange = M("Member_card_exchange")->where(array("cardid" => $cardid))->find(); $arr["score"] = intval($set_exchange["reward"]) * $arr["expense"]; $single_orderid = date("YmdHis", time()) . mt_rand(1000, 9999); $record["orderid"] = $single_orderid; $record["ordername"] = $consume_id == 0 ? "会员卡现金支付" : "现金支付除优惠劵外的款项"; $record["paytype"] = "CardPay"; $record["createtime"] = time(); $record["paid"] = 0; $record["price"] = $arr["expense"]; $record["token"] = $this->token; $record["wecha_id"] = $this->wecha_id; $record["company_id"] = $company_id; $record["type"] = 0; $record["cardid"] = $cardid; M("Member_card_coupon")->where(array("id" => $r_record["coupon_id"]))->setInc("usetime", 1); $result = M("Member_card_pay_record")->add($record); M("Member_card_coupon_record")->where($rwhere)->save(array("use_time" => time(), "is_use" => "1", "staff_id" => -1)); $this->redirect(U("CardPay/pay", array("from" => "Card", "token" => $this->token, "wecha_id" => $this->wecha_id, "price" => $arr["expense"], "single_orderid" => $single_orderid, "orderName" => "支付除优惠劵外多余款项", "redirect" => "Card/payReturn|itemid:" . $r_record["coupon_id"] . ",usecount:" . $arr["usecount"] . ",score:" . $arr["score"] . ",type:coupon,cardid:" . $cardid))); exit; } else { $staff_db = M("Company_staff"); $staff_where = array("username" => $this->_post("username"), "token" => $this->token, "companyid" => $company_id); $thisStaff = $staff_db->where($staff_where)->find(); if (empty($thisStaff)) { $this->error("商家用户名不存在"); exit; } if (md5($this->_post("password")) == $thisStaff["password"]) { if ($from == "local" || $from == "default") { if ($r_record["is_use"] == 1) { $this->error("优惠券已使用"); exit; } } else { if ($from == "weixin" || $from == "local_weixin") { $coupons = new WechatCoupons($this->wxuser); $res = $coupons->consumeCoupons($r_record["card_id"], $r_record["cancel_code"]); if (0 < $res["errcode"]) { $this->error("核销失败,优惠券可能已经被使用"); exit; } } else { $this->error("无效的卡券来源"); } } $arr = array(); $arr["itemid"] = $r_record["coupon_id"]; $arr["wecha_id"] = $this->wecha_id; $arr["expense"] = $price ? $price : 0; $arr["time"] = $now; $arr["token"] = $this->token; $arr["cat"] = 0; $arr["notes"] = $this->_post("notes", "trim"); $arr["staffid"] = $thisStaff["id"]; $arr["usecount"] = 1; $arr["company_id"] = $company_id; $arr["cardid"] = $cardid; $arr["record_id"] = $consume_id; $arr["staff_id"] = $thisStaff["id"]; $set_exchange = M("Member_card_exchange")->where(array("cardid" => $cardid))->find(); $arr["score"] = intval($set_exchange["reward"]) * $arr["expense"]; if (0 < $set_exchange["reward"]) { M("Member_card_use_record")->add($arr); M("Userinfo")->where(array("token" => $this->token, "wecha_id" => $this->wecha_id))->setInc("total_score", $arr["score"]); M("Userinfo")->where(array("token" => $this->token, "wecha_id" => $this->wecha_id))->setInc("expensetotal", $arr["expense"]); } M("Member_card_coupon")->where(array("id" => $r_record["coupon_id"]))->setInc("usetime", 1); M("Member_card_coupon_record")->where($rwhere)->save(array("use_time" => time(), "is_use" => "1", "staff_id" => -1)); $this->success("支付成功"); exit; } else { $this->error("商家密码错误!"); exit; } } } }