zoukankan      html  css  js  c++  java
  • 信小程序支付(C#后台+前台)

    今天为大家带来比较简单的支付后台处理

    首先下载官方的c#模板(WxPayAPI),将模板(WxPayAPI)添加到服务器上,然后在WxPayAPI项目目录中添加两个“一般处理程序” (改名为GetOpenid.ashx、pay.ashx)

    之后打开business目录下的JsApiPay.cs,在JsApiPay.cs中修改如下两处

    然后在GetOpenid.ashx中加入代码如下:

     1 public class GetOpenid : IHttpHandler  
     2     {  
     3         public string openid { get; set; }  
     4   
     5         public void ProcessRequest(HttpContext context)  
     6         {  
     7               
     8             string code = HttpContext.Current.Request.QueryString["code"];  
     9             WxPayData data = new WxPayData();  
    10             data.SetValue("appid", WxPayConfig.APPID);  
    11             data.SetValue("secret", WxPayConfig.APPSECRET);  
    12             data.SetValue("code", code);  
    13             data.SetValue("grant_type", "authorization_code");  
    14             string url = "https://api.weixin.qq.com/sns/oauth2/access_token?" + data.ToUrl();  
    15   
    16             //请求url以获取数据  
    17             string result = HttpService.Get(url);  
    18   
    19             Log.Debug(this.GetType().ToString(), "GetOpenidAndAccessTokenFromCode response : " + result);  
    20   
    21             //保存access_token,用于收货地址获取  
    22             JsonData jd = JsonMapper.ToObject(result);  
    23             //access_token = (string)jd["access_token"];  
    24   
    25             //获取用户openid  
    26             openid = (string)jd["openid"];  
    27             context.Response.Write(openid);//获取H5调起JS API参数  
    28             
    29         }

    在pay.ashx中加入代码如下:

     1 public class pay : IHttpHandler  
     2    {  
     3         
     4        public void ProcessRequest(HttpContext context)  
     5        {                
     6            context.Response.ContentType = "text/plain";  
     7             
     8            string openid = HttpContext.Current.Request.QueryString["openid"];  
     9            string total_fee = HttpContext.Current.Request.QueryString["total_fee"];  
    10            JsApiPay jsApiPay = new JsApiPay(context);  
    11            jsApiPay.openid = openid;  
    12            jsApiPay.total_fee = int.Parse(total_fee);  
    13            WxPayData unifiedOrderResult = jsApiPay.GetUnifiedOrderResult();  
    14            context.Response.Write(jsApiPay.GetJsApiParameters());//获取H5调起JS API参数  
    15        }

    然后发布就可以了(记得将相关的信息appid等填好)

    微信小程序的代码如下:

      1 wxpay: function () { 
      2     var that = this 
      3     //登陆获取code 
      4     wx.login({ 
      5       success: function (res) { 
      6         console.log(res.code) 
      7         //获取openid 
      8         that.getOpenId(res.code) 
      9       } 
     10     }); 
     11   }, 
     12   getOpenId: function (code) { 
     13 //获取openID 
     14       
     15     var that = this; 
     16     wx.request({ 
     17       url: 'http://*******/WxPayAPI/GetOpenid.ashx?code='+ code ,  //改为自己的域名
     18       data: {}, 
     19     //  method: 'GET', 
     20       success: function (res) { 
     21       var a12=res.data 
     22       that.generateOrder(a12) 
     23       //console.log(a12) 
     24       }, 
     25       fail: function () { 
     26         // fail 
     27       }, 
     28       complete: function () { 
     29         // complete 
     30       } 
     31     }) 
     32   }, 
     33 /**生成商户订单 */ 
     34   generateOrder: function (openid) { 
     35     var that = this; 
     36     //console.log(openid) 
     37     //统一支付 
     38     wx.request({ 
     39       url: 'http://*******/WxPayAPI/pay.ashx',  //改为自己的域名
     40       //method: 'GET', 
     41       data: { 
     42         total_fee: 1,//1分 
     43         openid: openid, 
     44       }, 
     45       header: { 
     46         'content-type': 'application/json' 
     47       }, 
     48    
     49       success: function (res) { 
     50    
     51         var pay = res.data 
     52         //发起支付 
     53            
     54         var timeStamp = pay.timeStamp; 
     55         var packages = pay.package; 
     56         var paySign = pay.paySign; 
     57         var nonceStr = pay.nonceStr; 
     58         var param = { "timeStamp": timeStamp, "package": packages, "paySign": paySign, "signType": "MD5", "nonceStr": nonceStr }; 
     59          
     60         that.pay(param) 
     61       }, 
     62     }) 
     63   }, 
     64    
     65   /* 支付   */ 
     66   pay: function (param) { 
     67    
     68     wx.requestPayment({ 
     69       timeStamp: param.timeStamp, 
     70       nonceStr: param.nonceStr, 
     71       package: param.package, 
     72       signType: param.signType, 
     73       paySign: param.paySign, 
     74       success: function (res) { 
     75         // success 
     76          
     77         wx.navigateBack({ 
     78           delta: 1, // 回退前 delta(默认为1) 页面 
     79           success: function (res1) { 
     80             wx.showToast({ 
     81               title: '支付成功', 
     82               icon: 'success', 
     83               duration: 2000 
     84             }); 
     85              
     86           }, 
     87           fail: function () { 
     88             // fail 
     89                
     90           }, 
     91           complete: function () { 
     92                
     93           } 
     94         }) 
     95       }, 
     96       fail: function (res) { 
     97         // fail 
     98       }, 
     99       complete: function () { 
    100         // complete 
    101       } 
    102     }) 
    103   }, 
  • 相关阅读:
    CriticalSection关键代码区(临界区)
    互斥对象(Mutex)、事件对象(Event) 与 关键代码段(CriticalSection) 的比较(互斥量Mutex 保证只有一个实例运行)
    VC popup Menu,弹出菜单
    C++ virtual inherit, 虚基类
    互斥量Mutex
    sprintf,swprintf,_stprintf
    图灵图书列表
    IDirect3DDevice9::EvictManagedResources
    过年去了,消失好久
    《C++编程规范》中文版上市
  • 原文地址:https://www.cnblogs.com/softwyy/p/9084200.html
Copyright © 2011-2022 走看看