小程序微信支付问题
这里说的支付是其程序方说明的JSAPI方式,即按支付按钮后,会出现输入支付密码的样式。
js中支付按钮函数(bindtap)可以是:
pay: function (options) {
var orderCode = this.getorderid();//可以定义一个获取订单号的函数;
var that=this;
var selfsession = wx.getStorageSync("selfsession");//前述selfsession的使用方法.文章-小程序获取session方法
wx.login({
success: function (res) {
if (res.code) {
wx.request({
url: "https://www.test.cn/test.ashx",
data: {
code: res.code,
order_code: orderCode,//相应参数在此规划好
total_fee: "",
body:"",
},
method: 'POST',
header: {'content-type': 'application/x-www-form-urlencoded'},
success: function(res) {
console.log(res.data);
wx.requestPayment({
timeStamp:res.data.timeStamp, nonceStr:res.data.nonceStr,
package:res.data.package,
signType:res.data.signType,
paySign:res.data.paySign,
console.log(res);
wx.request({//更新付款状态及录入订单号、订单金额等。
method: "POST",
header: { "Content-Type": "application/x-www-form-urlencoded" },
fail: function (res) {
})
fail: function(res) {
console.log("支付失败:",res);
}
后方函数,需要一系列函数支撑,需要定义一个微信支付类WXPay。
后方在接到命令后,处理函数如下,s是需要返回给前方的字符串。
{
try
string code = context.Request.Form["code"];
string order_code = context.Request.Form["order_code"];
string body = context.Request.Form["body"];
string total_fee = context.Request.Form["total_fee"];
WXPay.PayRequesEntity msg = (WXPay.PayRequesEntity)WXPay.Pay(code, order_code,total_fee,body);
if (msg != null)
s = "{\"state\":\"OK\",\"Error_code\":\"0\",\"Success\":\"" + true + "\",\"Message\":\"支付结果!\",\"Total_count\":\""+total_fee+"\",\"timeStamp\":\"" + msg.timeStamp + "\",\"nonceStr\":\"" + msg.nonceStr + "\",\"package\":\"" + msg.package + "\",\"signType\":\"" + msg.signType + "\",\"paySign\":\"" + msg.paySign + "\"}";
else
s = "{\"state\":\"none\"}";
catch (Exception e)
s = "{\"state\":\"none\",\"Error_code\":\"1\",\"Success\":\"" + false + "\",\"Message\":\"发起支付失败!\",\"Error_desc\":\"" + e.Message + "\"}";
友情链接
搜狐、网易、新浪、赶集