第一篇:湖北元润科技有限公司 微信支付开发 内部
湖北元润科技有限公司 微信支付开发 内部
更新内容:自动获取openid,只在微信浏览器下 获取openid。到这里大家还不能搞定那实在是没天赋了。
/**
* 获取当前页面完整URL地址
*/
functionget_url(){ $sys_protocal= isset($_SERVER['SERVER_PORT'])&&$_SERVER['SERVER_PORT'] == '443'? 'https://': 'http://';$php_self= $_SERVER['PHP_SELF'] ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME'];$path_info= isset($_SERVER['PATH_INFO'])? $_SERVER['PATH_INFO'] : '';$relate_url= isset($_SERVER['REQUEST_URI'])? $_SERVER['REQUEST_URI'] : $php_self.(isset($_SERVER['QUERY_STRING'])? '?'.$_SERVER['QUERY_STRING'] : $path_info);return$sys_protocal.(isset($_SERVER['HTTP_HOST'])? $_SERVER['HTTP_HOST'] : '').$relate_url;}
//include_once(ROOT_PATH.'includes/modules/payment/wxpay.php');
$openid=@$_COOKIE['sopenid'];
if(!$openid&&strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger')!== false){
//使用jsapi接口
//$jsApi = new JsApi_pub();
if(!isset($_GET['code'])){ //=========步骤1:网页授权获取用户openid============
//通过code获得openid
//触发微信返回code码
$backurl= get_url();//$url = $jsApi->createOauthUrlForCode($backurl);$url= “https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=”.urlencode($backurl).“&response_type=code&scope=snsapi_base&state=123#wechat_redirect”;//echo $url;
Header(“Location: $url”);}else { //获取code码,以获取openid
$code= $_GET['code'];
$url= “https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SCRETID&code=”.$code.“&grant_type=authorization_code”;$re= file_get_contents($url);$rearr= json_decode($re,true);$openid= $rearr['openid'];//var_dump($code);
//$jsApi->setCode($code);
//$openid = $jsApi->getOpenid();
setcookie(“sopenid”,$openid,time()+864000,'/');} } //var_dump($openid);
——————————–
没有研究过ecshop插件,也不知道ecshop插件的制作流程。我就用现有插件修改了一番,让ecshop手机端支持微信支付。
1.安装:覆盖到网站根目录。注意备份原来的网站
2.配置:后台开启微信支付功能,填入相应的参数 appi,mchid , key, appsecret,支付通知url,支付成功跳转url 3.说明:
2个back 文件为备份文件不用理会,备份之前需要修改配置文件才可以成功支付,现在只要在后台安装配置参数即可开通微信支付V3。其中 mobileincludes 文件夹下的init.php文件是为了获取到微信用户openid,如果你已经获取到openid就不用覆盖整个文件。然后修改includesmodulespaymentwxpay.php大 概第168行 @$openid=$_COOKIE['sopenid'];改成你得到的openid.4.关于测试:
目前微世界商城可以正常下单支付。1.需要在微世界服务号内打开商城链接。2.需要在公众号后台添加支付人员测试白名单。满足以上两个条件才可以发起支付,支付成功后 wxpaydemonotify_url.php 会接受到通知,并改变订单状态。————————-附:获取当前页面完整URL地址
/**
* 获取当前页面完整URL地址
*/
functionget_url(){ $sys_protocal= isset($_SERVER['SERVER_PORT'])&&$_SERVER['SERVER_PORT'] == '443'? 'https://': 'http://';$php_self= $_SERVER['PHP_SELF'] ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME'];$path_info= isset($_SERVER['PATH_INFO'])? $_SERVER['PATH_INFO'] : '';$relate_url= isset($_SERVER['REQUEST_URI'])? $_SERVER['REQUEST_URI'] : $php_self.(isset($_SERVER['QUERY_STRING'])? '?'.$_SERVER['QUERY_STRING'] : $path_info);return$sys_protocal.(isset($_SERVER['HTTP_HOST'])? $_SERVER['HTTP_HOST'] : '').$relate_url;}
微信获取昵称自动登录
经过反复几次验证,发现我这个方法有缺陷: 微信内 未关注进入网站,无法获得昵称。关注后用我这个方法可以获得昵称。
是否是因为第一次生成openid 所以还未生成昵称?待测试。
/**
* 获取当前页面完整URL地址 */
functionget_url(){ $sys_protocal= isset($_SERVER['SERVER_PORT'])&&$_SERVER['SERVER_PORT'] == '443'? 'https://': 'http://';$php_self= $_SERVER['PHP_SELF'] ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME'];
$path_info= isset($_SERVER['PATH_INFO'])? $_SERVER['PATH_INFO'] : '';$relate_url= isset($_SERVER['REQUEST_URI'])? $_SERVER['REQUEST_URI'] : $php_self.(isset($_SERVER['QUERY_STRING'])? '?'.$_SERVER['QUERY_STRING'] : $path_info);return$sys_protocal.(isset($_SERVER['HTTP_HOST'])? $_SERVER['HTTP_HOST'] : '').$relate_url;}
$wxch_config= $db-> getRow(“SELECT * FROM `ecs_weixin_config` WHERE `id` = 1”);$appid= $wxch_config['appid'];$appsecret= $wxch_config['appsecret'];
$APPID= $appid;$SCRETID=$appsecret;
if(!$_SESSION['user_id'] &&strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger')!== false){
if(!isset($_GET['code'])){
$backurl= get_url();//$url = $jsApi->createOauthUrlForCode($backurl);$url= “https://open.weixin.qq.com/connect/oauth2/authorize?appid=”.$APPID.“&redirect_uri=”.urlencode($backurl).“&response_type=code&scope=snsapi_base&state=123#wechat_redirect”;//echo $url;
Header(“Location: $url”);}else { //获取code码,以获取openid $code= $_GET['code'];
$url= “https://api.weixin.qq.com/sns/oauth2/access_token?appid=”.$APPID.“&secret=”.$SCRETID.“&code=”.$code.“&grant_type=authorization_code”;$re= curl_get_contents1($url);$rearr= json_decode($re,true);$openid= $rearr['openid'];
//var_dump($rearr);
//$jsApi->setCode($code);
//$openid = $jsApi->getOpenid();
$user_name= $db->getOne(“select uname from ecs_weixin_user where wxid = '{$openid}'”);
if($openid&&!$user_name){ //注册进入
$passw= md5('shanmao.me'.rand(1,***));$wxch_user_sql= “INSERT INTO `ecs_weixin_user`(`wxid`,`setp`)VALUES('$openid','3')”;$db-> query($wxch_user_sql);$ecs_user_id= $db-> insert_id();if($ecs_user_id<=0){ exit('error get insert_id');} $url3= “https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=”.$APPID.“&secret=”.$SCRETID;$re3= curl_get_contents1($url3);$re3arr= json_decode($re3,true);$token= $re3arr['access_token'];$url2= “https://api.weixin.qq.com/cgi-bin/user/info?access_token=”.$token.“&openid=”.$openid.“&lang=zh_CN”;$re2= curl_get_contents1($url2);$rearr2= json_decode($re2,true);
$uc_username= $rearr2['nickname']?$rearr2['nickname']: 'doubag'.$ecs_user_id;$time= gmtime();$user_sql= “INSERT INTO `ecs_users`(`user_name`,`password`,`reg_time`)VALUES('$uc_username','$passw','$time')”;$db-> query($user_sql);$uc_update= “UPDATE ecs_weixin_user SET `uname` = '$uc_username' WHERE `uid` = '$ecs_user_id'”;$db-> query($uc_update);$user->set_session($uc_username);$user->set_cookie($uc_username,1);update_user_info();/*
$up_uid = get_affiliate();if($up_uid>0){
$sql = 'UPDATE ecs_users SET parent_id = '.$up_uid.' WHERE user_id = '.$ecs_user_id;$db->query($sql);
header('Location: user.php?newuser=1');}*/
}else{ $user->set_session($user_name);$user->set_cookie($user_name,1);update_user_info();}
//setcookie(“sopenid”,$openid,time()+864000,'/');} } //var_dump($openid);
functioncurl_get_contents1($url){ $ch= curl_init();curl_setopt($ch, CURLOPT_URL, $url);curl_setopt($ch, CURLOPT_TIMEOUT, 2);curl_setopt($ch, CURLOPT_USERAGENT, “IE 6.0”);curl_setopt($ch, CURLOPT_REFERER, "");curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);$r= curl_exec($ch);curl_close($ch);return$r;}
第二篇:微信服务号 微信支付开发
微信服务号 微信支付开发
1.发起支付,都是通过h5发起的,首先获取prepay_id
发起支付,需要统一下单的prepay_id
SortedMap
parameters.put(“appid”, ConfigUtil.APPID);
parameters.put(“mch_id”, ConfigUtil.MCH_ID);
parameters.put(“nonce_str”, PayCommonUtil.CreateNoncestr());
parameters.put(“body”, bodyorder);
parameters.put(“out_trade_no”, outTradeNo);// 用户订单号
parameters.put(“total_fee”, totalfee);// 标价金额(分)
// parameters.put(“spbill_create_ip”,IpAddressUtil.getIpAddr(request));
parameters.put(“spbill_create_ip”, “192.168.1.1”);// 电脑测试
// parameters.put(“spbill_create_ip”,NetworkUtil.getIpAddress(request));
parameters.put(“notify_url”, ConfigUtil.NOTIFY_URL);
parameters.put(“trade_type”, “JSAPI”);
parameters.put(“openid”, openId);
String sign = PayCommonUtil.createSign(“UTF-8”, parameters);
parameters.put(“sign”, sign);
String requestXML = PayCommonUtil.getRequestXml(parameters);
String result = CommonUtil.httpsRequestR2s(ConfigUtil.UNIFIED_ORDER_URL, “POST”, requestXML);
System.out.println(“第一弹数据” + result + “ip地址” + NetworkUtil.getIpAddress(request));
数据为
第一弹数据
2.拿到prepay_id后,就可以发起支付
支付,需要设置测试路径和真实路径,发起支付,将参数转为json,在放到respond里,然后h5页面从request里取
SortedMap
params.put(“appId”, ConfigUtil.APPID);
params.put(“timeStamp”, Long.toString(new Date().getTime()));
params.put(“nonceStr”, PayCommonUtil.CreateNoncestr());
System.out.println(“订单id” + map.get(“prepay_id”));
params.put(“package”, “prepay_id=” + map.get(“prepay_id”));
params.put(“signType”, ConfigUtil.SIGN_TYPE);
String paySign = PayCommonUtil.createSign(“UTF-8”, params);
// params.put(“packageValue”, “prepay_id=”+map.get(“prepay_id”));
// //这里用packageValue是预防package是关键字在js获取值出错
params.put(“paySign”, paySign);// paySign的生成规则和Sign的生成规则一致
String json = JSONObject.fromObject(params).toString();
System.out.println(“第二弹数据” + json);
try {
response.setContentType(“text/html” + “;charset=UTF-8”);
response.setHeader(“Pragma”, “No-cache”);
response.setHeader(“Cache-Control”, “no-cache”);
response.setDateHeader(“Expires”, 0);
response.getWriter().write(json);
response.getWriter().flush();
} catch(IOException e){
e.printStackTrace();
} }
上传,支付的html页面
苏牌皮草
库存18件
原价:8888元
现价:5000
文档为doc格式
声明:本文内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:645879355@qq.com 进行举报,并提供相关证据,工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。