首頁  >  文章  >  web前端  >  微信小程式支付之c#後台實作方法實例詳解

微信小程式支付之c#後台實作方法實例詳解

小云云
小云云原創
2018-01-09 13:02:324400瀏覽

本文主要介紹了微信小程式支付之c#後台實現方法的相關資料,希望透過本文能幫助到大家,讓大家實現這樣的功能,需要的朋友可以參考下,希望能幫助到大家。

微信小程式支付c#後台實作

今天為大家帶來比較簡單的支付後台處理

首先下載官方的c#範本(WxPayAPI),將範本(WxPayAPI)加入伺服器上,然後在WxPayAPI專案目錄中新增兩個「一般處理程序」(改名為GetOpenid.ashx、pay.ashx)

之後開啟business目錄下的JsApiPay.cs,在JsApiPay.cs中修改如下兩處

然後在GetOpenid.ashx中加入程式碼如下:


#
public class GetOpenid : IHttpHandler 
  { 
    public string openid { get; set; } 
 
    public void ProcessRequest(HttpContext context) 
    { 
       
      string code = HttpContext.Current.Request.QueryString["code"]; 
      WxPayData data = new WxPayData(); 
      data.SetValue("appid", WxPayConfig.APPID); 
      data.SetValue("secret", WxPayConfig.APPSECRET); 
      data.SetValue("code", code); 
      data.SetValue("grant_type", "authorization_code"); 
      string url = "https://api.weixin.qq.com/sns/oauth2/access_token?" + data.ToUrl(); 
 
      //请求url以获取数据 
      string result = HttpService.Get(url); 
 
      Log.Debug(this.GetType().ToString(), "GetOpenidAndAccessTokenFromCode response : " + result); 
 
      //保存access_token,用于收货地址获取 
      JsonData jd = JsonMapper.ToObject(result); 
      //access_token = (string)jd["access_token"]; 
 
      //获取用户openid 
      openid = (string)jd["openid"]; 
      context.Response.Write(openid);//获取H5调起JS API参数 
      
    }

在pay.ashx中加入程式碼如下:


public class pay : IHttpHandler 
  { 
    
    public void ProcessRequest(HttpContext context) 
    {        
      context.Response.ContentType = "text/plain"; 
      
      string openid = HttpContext.Current.Request.QueryString["openid"]; 
      string total_fee = HttpContext.Current.Request.QueryString["total_fee"]; 
      JsApiPay jsApiPay = new JsApiPay(context); 
      jsApiPay.openid = openid; 
      jsApiPay.total_fee = int.Parse(total_fee); 
      WxPayData unifiedOrderResult = jsApiPay.GetUnifiedOrderResult(); 
      context.Response.Write(jsApiPay.GetJsApiParameters());//获取H5调起JS API参数 
    }

 然後發布就可以了(記得將相關的資訊appid等填好)

 微信小程式的程式碼如下:


wxpay: function () { 
  var that = this 
  //登陆获取code 
  wx.login({ 
   success: function (res) { 
    console.log(res.code) 
    //获取openid 
    that.getOpenId(res.code) 
   } 
  }); 
 }, 
 getOpenId: function (code) { 
//获取openID 
   
  var that = this; 
  wx.request({ 
   url: 'http://*******/WxPayAPI/GetOpenid.ashx?code='+ code , //改为自己的域名
   data: {}, 
  // method: 'GET', 
   success: function (res) { 
   var a12=res.data 
   that.generateOrder(a12) 
   //console.log(a12) 
   }, 
   fail: function () { 
    // fail 
   }, 
   complete: function () { 
    // complete 
   } 
  }) 
 }, 
/**生成商户订单 */ 
 generateOrder: function (openid) { 
  var that = this; 
  //console.log(openid) 
  //统一支付 
  wx.request({ 
   url: 'http://*******/WxPayAPI/pay.ashx', //改为自己的域名
   //method: 'GET', 
   data: { 
    total_fee: 1,//1分 
    openid: openid, 
   }, 
   header: { 
    'content-type': 'application/json' 
   }, 
  
   success: function (res) { 
  
    var pay = res.data 
    //发起支付 
      
    var timeStamp = pay.timeStamp; 
    var packages = pay.package; 
    var paySign = pay.paySign; 
    var nonceStr = pay.nonceStr; 
    var param = { "timeStamp": timeStamp, "package": packages, "paySign": paySign, "signType": "MD5", "nonceStr": nonceStr }; 
     
    that.pay(param) 
   }, 
  }) 
 }, 
  
 /* 支付  */ 
 pay: function (param) { 
  
  wx.requestPayment({ 
   timeStamp: param.timeStamp, 
   nonceStr: param.nonceStr, 
   package: param.package, 
   signType: param.signType, 
   paySign: param.paySign, 
   success: function (res) { 
    // success 
     
    wx.navigateBack({ 
     delta: 1, // 回退前 delta(默认为1) 页面 
     success: function (res1) { 
      wx.showToast({ 
       title: '支付成功', 
       icon: 'success', 
       duration: 2000 
      }); 
       
     }, 
     fail: function () { 
      // fail 
        
     }, 
     complete: function () { 
        
     } 
    }) 
   }, 
   fail: function (res) { 
    // fail 
   }, 
   complete: function () { 
    // complete 
   } 
  }) 
 },

相關推薦:

微信小程式付款及退款流程實例分享

php如何實現微信小程式支付及退款

#詳解微信小程式支付功能開發錯誤總結

#

以上是微信小程式支付之c#後台實作方法實例詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn