Skip to content

Commit

Permalink
优化微信支付接口。
Browse files Browse the repository at this point in the history
  • Loading branch information
peisy committed Mar 10, 2016
1 parent 9d5e471 commit ad644df
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 124 deletions.
19 changes: 18 additions & 1 deletion examples/tutorial-wx/index2.html
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,25 @@ <h4>第七步</h4>
* @return {Object}
*/
$("#pay").click(function(){
sgt.WxCentralService.getPayOrder(body,total_fee,playerId,function(result,res){
sgt.WxCentralService.getPayOrder(body,total_fee,playerId,function(result,order){
if(result){
//微信支付
wx.chooseWXPay({
timestamp: order.time_start, // 支付签名时间戳,注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
nonceStr: order.nonce_str, // 支付签名随机串,不长于 32 位
package: 'prepay_id=' + order.prepay_id, // 统一支付接口返回的prepay_id参数值,提交格式如:prepay_id=***)
signType: 'MD5', // 签名方式,默认为'SHA1',使用新版支付需传入'MD5'
paySign: order.paySign, // 支付签名
success: function (res) {
// 支付成功后的回调函数验证是否支付成功
SgtApi.DelegateDidService.queryByDid(res.did, function (result1, data) {
console.log(data);
})
},
fail: function (res) {
console.log(data);
}
});
// 支付成功后的回调函数
}else{
// 支付失败后的回调函数
Expand Down
229 changes: 123 additions & 106 deletions examples/wxpay/index.html
Original file line number Diff line number Diff line change
@@ -1,136 +1,153 @@
<html>

<head>
<title>微信支付</title>
<title>微信支付0.0.42</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="./css/bootstrap.min.css">
<script type="text/javascript" src="./js/sgtcloud-html5-sdk.2.1.3.js"></script>
<script type="text/javascript" src="http://www.sgtcloud.cn/dist/sgtcloud-html5-sdk.2.1.3.min.js"></script>
<script type="text/javascript" src="./js/jquery.1.11.1.min.js"></script>
<script type="text/javascript" src="./js/bootstrap.min.js"></script>
<script type="text/javascript" src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
<script type="text/javascript" src="https://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
<script src="http://jsconsole.com/remote.js?163D17C0-4F49-47F8-ABD0-2FDCFE7AFA25"></script>
<script type="text/javascript">

$(function() {
//签名
var signature = null;
//订单
var order = null;

var playerid = '2222';
//初始化sdk
sgt.init({
appId: 'xmj_sgp',
appGateway: 'http://test.sgtcloud.cn/gateway',
async:false
});
$(function () {
//签名
var signature = null;
//订单
var order = null;

//登录用户
// SgtApi.UserService.login('Ak61E175', 'yoedge2014', function(result, data) {console.log(data);});
//SgtApi.PlayerService.getOneByUserId(sgt.context.user.userid,function(result,data){
//alert(data);
//playerid = data.id;
//});
sgt.WxCentralService.getSignature(function(result, data) {
console.log(data);
signature = data;
});
var playerid = '2222';
//初始化sdk
sgt.init({
appId: 'h5game',
// appGateway: 'http://test.sgtcloud.cn/gateway',
async: false
});

wx.config({
debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
appId: signature.result.wxAppId, // 必填,公众号的唯一标识
timestamp: signature.result.timestamp, // 必填,生成签名的时间戳
nonceStr: signature.result.noncestr, // 必填,生成签名的随机串
signature: signature.result.signature, // 必填,签名,见附录1
jsApiList: ['chooseWXPay'] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
});
wx.ready(function() {
// config信息验证后会执行ready方法,所有接口调用都必须在config接口获得结果之后,config是一个客户端的异步操作,所以如果需要在页面加载时就调用相关接口,则须把相关接口放在ready函数中调用来确保正确执行。对于用户触发时才调用的接口,则可以直接调用,不需要放在ready函数中。
wx.checkJsApi({
jsApiList: ['chooseWXPay'], // 需要检测的JS接口列表,所有JS接口列表见附录2,
success: function(res) {
// 以键值对的形式返回,可用的api值true,不可用为false
// 如:{"checkResult":{"chooseImage":true},"errMsg":"checkJsApi:ok"}
}
//登录用户
// SgtApi.UserService.login('Ak61E175', 'yoedge2014', function(result, data) {console.log(data);});
//SgtApi.PlayerService.getOneByUserId(sgt.context.user.userid,function(result,data){
//alert(data);
//playerid = data.id;
//});
sgt.WxCentralService.getSignature(function (result, data) {
console.log(data);
signature = data;
});
wx.error(function(res) {
// config信息验证失败会执行error函数,如签名过期导致验证失败,具体错误信息可以打开config的debug模式查看,也可以在返回的res参数中查看,对于SPA可以在这里更新签名。

wx.config({
debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
appId: signature.result.wxAppId, // 必填,公众号的唯一标识
timestamp: signature.result.timestamp, // 必填,生成签名的时间戳
nonceStr: signature.result.noncestr, // 必填,生成签名的随机串
signature: signature.result.signature, // 必填,签名,见附录1
jsApiList: ['chooseWXPay'] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
});
wx.ready(function () {
// config信息验证后会执行ready方法,所有接口调用都必须在config接口获得结果之后,config是一个客户端的异步操作,所以如果需要在页面加载时就调用相关接口,则须把相关接口放在ready函数中调用来确保正确执行。对于用户触发时才调用的接口,则可以直接调用,不需要放在ready函数中。
wx.checkJsApi({
jsApiList: ['chooseWXPay'], // 需要检测的JS接口列表,所有JS接口列表见附录2,
success: function (res) {
// 以键值对的形式返回,可用的api值true,不可用为false
// 如:{"checkResult":{"chooseImage":true},"errMsg":"checkJsApi:ok"}
}
});
wx.error(function (res) {
// config信息验证失败会执行error函数,如签名过期导致验证失败,具体错误信息可以打开config的debug模式查看,也可以在返回的res参数中查看,对于SPA可以在这里更新签名。
});
});
});


//获取url中的参数
function getUrlParam(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
var r = window.location.search.substr(1).match(reg); //匹配目标参数
if (r !== null) return unescape(r[2]);
return null; //返回参数值
}
//获取url中的参数
function getUrlParam(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
var r = window.location.search.substr(1).match(reg); //匹配目标参数
if (r !== null) return unescape(r[2]);
return null; //返回参数值
}


//认证逻辑
$('#auth').click(function() {
sgt.WxCentralService.auth('wx9e1986fc11ecbd89', 'snsapi_userinfo');
// , 'snsapi_userinfo');
});
//认证后逻辑
if (getUrlParam('code')) {
//支付的业务逻辑
$('#other').css('display', 'block');
$('#auth').css('display', 'none');
}

$('#re').click(function(){
$('#other').css('display', 'none');
$('#auth').css('display', 'block');
});
//认证逻辑
$('#auth').click(function () {
sgt.WxCentralService.auth('wx9e1986fc11ecbd89', 'snsapi_userinfo');
// , 'snsapi_userinfo');
});
//认证后逻辑
if (getUrlParam('code')) {
//支付的业务逻辑
$('#other').css('display', 'block');
$('#auth').css('display', 'none');
}

$('#userinfo').click(function() {
// location.href = 'https://api.weixin.qq.com/sns/userinfo?access_token=' + sgt.context.access_token + '&openid=' + sgt.context.openid + '&lang=zh_CN';
sgt.WxCentralService.getUserInfo(function(result, data) {
alert(JSON.stringify(data));
$('#re').click(function () {
$('#other').css('display', 'none');
$('#auth').css('display', 'block');
});
});
if (SgtApi.context.openid) {
$('#other').css('display', 'block');
$('#auth').css('display', 'none');
SgtApi.UserService.login3rd(SgtApi.User.WECHAT_MP,function(result,data){
if (!result) {
sgt.WxCentralService.getUserInfo(function(result, data) {
if(result){
var user = new SgtApi.User();
user.userName = data.openid;
user.nickName = data.nickname;
user.registryType = SgtApi.User.WECHAT_MP;//注册类型
SgtApi.UserService.regist(user, function (result, data) {
console.log(data);
});
}else{
//重现授权
sgt.WxCentralService.auth('wx9e1986fc11ecbd89', 'snsapi_userinfo');
}
});
}

$('#userinfo').click(function () {
// location.href = 'https://api.weixin.qq.com/sns/userinfo?access_token=' + sgt.context.access_token + '&openid=' + sgt.context.openid + '&lang=zh_CN';
sgt.WxCentralService.getUserInfo(function (result, data) {
alert(JSON.stringify(data));
});
});
}

//支付逻辑
$('#pay').click(function() {
sgt.WxCentralService.getPayOrder($('#good').val(),$('#money').val() * 100,playerid,function(result,res){
if(result){
// 支付成功后的回调函数
}else{
// 支付失败后的回调函数
}
if (SgtApi.context.openid) {
$('#other').css('display', 'block');
$('#auth').css('display', 'none');
SgtApi.UserService.login3rd(SgtApi.User.WECHAT_MP, function (result, data) {
if (!result) {
sgt.WxCentralService.getUserInfo(function (result, data) {
if (result) {
var user = new SgtApi.User();
user.userName = data.openid;
user.nickName = data.nickname;
user.registryType = SgtApi.User.WECHAT_MP;//注册类型
SgtApi.UserService.regist(user, function (result, data) {
console.log(data);
});
} else {
//重现授权
sgt.WxCentralService.auth('wx9e1986fc11ecbd89', 'snsapi_userinfo');
}
});
}
});
}

//支付逻辑
$('#pay').click(function () {
sgt.WxCentralService.getPayOrder($('#good').val(), $('#money').val() * 100, playerid, function (result, order) {
console.log(order);
if(result){
//微信支付
wx.chooseWXPay({
timestamp: order.time_start, // 支付签名时间戳,注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
nonceStr: order.nonce_str, // 支付签名随机串,不长于 32 位
package: 'prepay_id=' + order.prepay_id, // 统一支付接口返回的prepay_id参数值,提交格式如:prepay_id=***)
signType: 'MD5', // 签名方式,默认为'SHA1',使用新版支付需传入'MD5'
paySign: order.paySign, // 支付签名
success: function (res) {
// 支付成功后的回调函数
SgtApi.DelegateDidService.queryByDid(res.did, function (result1, data) {
console.log(data);
})
},
fail: function (res) {
console.log(data);
}
});

}
});
});
});
});


</script>
</head>

<body>
<body>
<div class="container">
<h1>微信支付</h1>
<p>请使用微信客户端打开此页面, 否则功能不会生效</p>
Expand Down
18 changes: 1 addition & 17 deletions src/sgtcloud-html5-sdk.js
Original file line number Diff line number Diff line change
Expand Up @@ -7046,23 +7046,7 @@
playerId: playerId,
userId: SgtApi.context.user.userid
}];
SgtApi.doRPC(name, data, _url, function (result, order) {
//微信支付
wx.chooseWXPay({
timestamp: order.time_start, // 支付签名时间戳,注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
nonceStr: order.nonce_str, // 支付签名随机串,不长于 32 位
package: 'prepay_id=' + order.prepay_id, // 统一支付接口返回的prepay_id参数值,提交格式如:prepay_id=***)
signType: 'MD5', // 签名方式,默认为'SHA1',使用新版支付需传入'MD5'
paySign: order.paySign, // 支付签名
success: function (res) {
// 支付成功后的回调函数
callback(true, order);
},
fail: function (res) {
callback(false, res);
}
});
});
SgtApi.doRPC(name, data, _url, callback);
},

/**
Expand Down

0 comments on commit ad644df

Please sign in to comment.