C#微信开发之旅(十):APP预支付及支付参数生成(V2)

App支付流程:

本篇随笔只实现红框内的两个功能:生成预支付Id,生成app支付参数

 1        /// <summary>
 2         /// App 预支付
 3         /// </summary>
 4         /// <returns></returns>
 5         public ActionResult AppPrePay()
 6         {
 7             string orderNo = ""; //订单编号,文档中的out_trade_no
 8             string description = ""; //订单详情
 9             string totalFee = "";//订单金额(单位:分)
10             string notifyUrl = ""; //支付通知Url
11             string createIp = "";//用户IP
12 
13             string traceId = string.Empty; //
14 
15             WxPayModel model = WxPayModel.Create(description, orderNo, totalFee, notifyUrl, createIp);
16             //预支付
17             var result = WeiXinHelper.PreWXPay(model.CreateAppPrePayPackage(traceId));
18 
19             if (!string.IsNullOrEmpty(result.PrePayId))
20             {
21                 Response.Write("预支付ID:" + result.PrePayId);
22             }
23             else
24             {
25                 Response.Write("预支付失败!");
26             }
27 
28             //生成app支付json
29             string appPayJson = model.CreateAppPayPackage(result.PrePayId);
30 
31             Response.Write("app支付Json:" + appPayJson);
32 
33             return View();
34         }

下面是生成预支付json 和 app支付json的方法(均通过实际测试,可放心使用):

 1         /// <summary>
 2         /// 生成app预支付请求json(已测试通过)
 3         /// </summary>
 4         /// <param name="traceid">交易号:自定义,可用于订单的查询和跟踪,建议根据支付用户信息生成此Id</param>
 5         /// <returns></returns>
 6         /// <remarks>
 7         ///* { "appid":"wwwwb4f85f3a797777", "traceid":"crestxu",
 8         ///* "noncestr":"111112222233333", "package":
 9         ///* "bank_type=WX&body=XXX&fee_type=1&input_charset=GBK&notify_url=http%3a%2f%2f
10         ///* www
11         ///* .qq.com&out_trade_no=16642817866003386000&partner=1900000109&spbill_create_ip
12         ///* =127.0.0.1&total_fee=1&sign=BEEF37AD19575D92E191C1E4B1474CA9",
13         ///* "timestamp":1381405298,
14         ///* "app_signature":"53cca9d47b883bd4a5c85a9300df3da0cb48565c",
15         ///* "sign_method":"sha1" }
16         /// </remarks>
17         public string CreateAppPrePayPackage(string traceid)
18         {
19             Dictionary<string, string> nativeObj = new Dictionary<string, string>();
20             if (CheckCftParameters() == false)
21             {
22                 throw new SDKRuntimeException("生成package参数缺失!");
23             }
24             nativeObj.Add("appid", AppId);
25             nativeObj.Add("package", GetCftPackage());
26             nativeObj.Add("timestamp", ((DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000).ToString());
27             nativeObj.Add("traceid", traceid);
28             nativeObj.Add("noncestr", CommonUtil.CreateNoncestr());
29             nativeObj.Add("app_signature", GetBizSign(nativeObj));
30             nativeObj.Add("sign_method", SignType);
31 
32             var entries = nativeObj.Select(d => string.Format("\"{0}\": \"{1}\"", d.Key, d.Value));
33 
34             return "{" + string.Join(",", entries.ToArray()) + "}";
35         }
36 
37         /// <summary>
38         /// 生成app支付请求json(已测试通过)
39         /// </summary>
40         /// <param name="prepayid">预支付id</param>
41         /// <returns></returns>
42         /// <remarks>
43         /// { 
44         /// "appid":"wwwwb4f85f3a797777",
45         /// "noncestr":"111112222233333", 
46         /// "package":"Sign=WXpay",
47         /// "partnerid":"1900000109",
48         /// "prepayid":"1101000000140429eb40476f4c9",
49         /// "sign":"53cca9d47b883bd4a5c85a9300df3da0cb48565c",
50         /// "timestamp":1381405298,
51         ///  }
52         /// </remarks>
53         public string CreateAppPayPackage(string prepayid)
54         {
55             Dictionary<string, string> nativeObj = new Dictionary<string, string>();
56             if (CheckCftParameters() == false)
57             {
58                 throw new SDKRuntimeException("生成package参数缺失!");
59             }
60             nativeObj.Add("appid", AppId);
61             nativeObj.Add("noncestr", CommonUtil.CreateNoncestr());
62             nativeObj.Add("package", "Sign=WXPay");
63             nativeObj.Add("partnerid", GetPartnerId());
64             nativeObj.Add("prepayid", prepayid);
65             nativeObj.Add("timestamp", ((DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000).ToString());
66             nativeObj.Add("sign", GetBizSign(nativeObj));
67 
68             var entries = nativeObj.Select(d => string.Format("\"{0}\": \"{1}\"", d.Key, d.Value));
69 
70             return "{" + string.Join(",", entries.ToArray()) + "}";
71         }

 

转载于:https://www.cnblogs.com/hetring/p/4065518.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值