yuqian 7 年 前
コミット
ac2a199b03

+ 63 - 37
ServiceWechat/ServiceWechat/Controllers/WechatApis/PaymentController.cs

@@ -7,6 +7,7 @@ using Microsoft.Extensions.Configuration;
7 7
 using Microsoft.Extensions.Logging;
8 8
 using NLog;
9 9
 using Senparc.Weixin.MP.TenPayLibV3;
10
+using ServiceWechat.Utility;
10 11
 
11 12
 // For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
12 13
 
@@ -28,13 +29,13 @@ namespace ServiceWechat.Api.Controllers.WechatApis
28 29
             AppId = configuration["WechatStatic:AppId"];
29 30
             MchId = configuration["WechatStatic:MchId"];
30 31
             TenPayV3Notify = configuration["WechatStatic:TenPayV3Notify"];
31
-            Key = configuration["Key"];
32
+            Key = configuration["WechatStatic:Key"];
32 33
         }
33
-        public string MakeOrder()
34
+        public JsonResult MakeOrder()
34 35
         {
35 36
             #region 生成商户订单(系统内订单)TODO
36 37
 
37
-            #endregion 
38
+            #endregion
38 39
 
39 40
 
40 41
             #region 调用微信统一下单API,生成预付单,获取预付单信息
@@ -45,43 +46,63 @@ namespace ServiceWechat.Api.Controllers.WechatApis
45 46
             var orderNum = DateTime.Now.ToString("yyyyMMddHHmmss");
46 47
             var total_fee = "1";
47 48
 
49
+
48 50
             RequestHandler requestHandler = new RequestHandler(HttpContext);
49
-            requestHandler.SetParameter("appid", this.AppId);                       //公众账号ID
50
-            requestHandler.SetParameter("mch_id", this.MchId);                      //商户号
51
-            //requestHandler.SetParameterWhenNotNull("sub_appid", this.SubAppId);     //子商户公众账号ID
52
-            //requestHandler.SetParameterWhenNotNull("sub_mch_id", this.SubMchId);    //子商户号
53
-            //requestHandler.SetParameterWhenNotNull("device_info", this.DeviceInfo); //自定义参数
54
-            requestHandler.SetParameter("nonce_str", TenPayV3Util.GetNoncestr());                //随机字符串
55
-            requestHandler.SetParameterWhenNotNull("sign_type", "MD5");     //签名类型,默认为MD5
56
-            requestHandler.SetParameter("body", body);                         //商品信息
57
-            //requestHandler.SetParameterWhenNotNull("detail", this.Detail);          //商品详细列表
58
-            requestHandler.SetParameterWhenNotNull("attach", attach);          //附加数据
59
-            requestHandler.SetParameter("out_trade_no", orderNum);           //商家订单号
60
-            requestHandler.SetParameterWhenNotNull("fee_type", "CNY");       //符合ISO 4217标准的三位字母代码,默认人民币:CNY
61
-            requestHandler.SetParameter("total_fee", total_fee);     //商品金额,以分为单位(money * 100).ToString()
62
-            requestHandler.SetParameter("spbill_create_ip", "");   //用户的公网ip,不是商户服务器IP
63
-            requestHandler.SetParameterWhenNotNull("time_start", DateTime.Now.ToString());   //订单生成时间
64
-            requestHandler.SetParameterWhenNotNull("time_expire", DateTime.Now.AddDays(1).ToString());  //订单失效时间
65
-            //requestHandler.SetParameterWhenNotNull("goods_tag", this.GoodsTag);     //商品标记
66
-            requestHandler.SetParameter("notify_url", TenPayV3Notify);              //接收财付通通知的URL
67
-            requestHandler.SetParameter("trade_type", "JSAPI");   //交易类型
68
-            //requestHandler.SetParameterWhenNotNull("product_id", this.ProductId);   //trade_type=NATIVE时(即扫码支付),此参数必传。
69
-            //requestHandler.SetParameterWhenNotNull("limit_pay", this.LimitPay);     //上传此参数no_credit--可限制用户不能使用信用卡支付
70
-            requestHandler.SetParameterWhenNotNull("openid", openId);                     //用户的openId,trade_type=JSAPI时(即公众号支付),此参数必传
71
-            //requestHandler.SetParameterWhenNotNull("sub_openid", this.SubOpenid);              //用户子标识
72
-            //if (SceneInfo != null)
73
-            //{
74
-            //    requestHandler.SetParameter("scene_info", SceneInfo.ToString());   //场景信息
75
-            //}
76
-
77
-            var sign = requestHandler.CreateMd5Sign("key", this.Key);
78
-
79
-            requestHandler.SetParameter("sign", sign);                              //签名
80
-
81
-            var xml = requestHandler.ParseXML();
51
+            //微信分配的公众账号ID(企业号corpid即为此appId)
52
+            requestHandler.SetParameter("appid", AppId);
53
+            //附加数据,在查询API和支付通知中原样返回,该字段主要用于商户携带订单的自定义数据
54
+            requestHandler.SetParameter("attach", "attach");
55
+            //商品或支付单简要描述
56
+            requestHandler.SetParameter("body", "body");
57
+            //微信支付分配的商户号
58
+            requestHandler.SetParameter("mch_id", MchId);
59
+            //微信支付分配的商户号
60
+            // requestHandler.SetParameter("detail", "detail");
61
+            //随机字符串,不长于32位。
62
+            requestHandler.SetParameter("nonce_str", TenPayV3Util.GetNoncestr());
63
+            //接收微信支付异步通知回调地址,通知url必须为直接可访问的url,不能携带参数。
64
+            requestHandler.SetParameter("notify_url", TenPayV3Notify);
65
+            //trade_type=JSAPI,此参数必传,用户在商户公众号appid下的唯一标识。
66
+            requestHandler.SetParameter("openid", openId);
67
+            //商户系统内部的订单号,32个字符内、可包含字母,自己生成
68
+            requestHandler.SetParameter("out_trade_no", orderNum);
69
+            //APP和网页支付提交用户端ip,Native支付填调用微信支付API的机器IP。
70
+            requestHandler.SetParameter("spbill_create_ip", "127.0.0.1");
71
+            //订单总金额,单位为分,做过银联支付的朋友应该知道,代表金额为12位,末位分分
72
+            requestHandler.SetParameter("total_fee", total_fee);
73
+            //取值如下:JSAPI,NATIVE,APP,我们这里使用JSAPI
74
+            requestHandler.SetParameter("trade_type", "JSAPI");
75
+            requestHandler.SetParameter("sign", requestHandler.CreateMd5Sign("key", Key));
76
+
77
+            string xml = requestHandler.ParseXML();
78
+            //获取并返回预支付XML信息
82 79
             var result = TenPayV3.Unifiedorder(xml);
80
+            XmlHelper xmlHelper = new XmlHelper();
81
+            xmlHelper.LoadFromXmlString(result);
82
+
83
+            Logger.Info($"result={result}");
84
+
85
+            Logger.Info($"return_code={xmlHelper.GetValue("return_code")}");
86
+            Logger.Info($"return_msg={xmlHelper.GetValue("return_msg")}");
87
+
88
+            if (xmlHelper.GetValue("return_code") == "SUCCESS" && xmlHelper.GetValue("return_msg") == "OK")
89
+            {
90
+                return Json(new
91
+                {
92
+                    state = true,
93
+                    appid = AppId,
94
+                    package = xmlHelper.GetValue("prepay_id"),
95
+                    sign = xmlHelper.GetValue("sign"),
96
+                    noncestr = xmlHelper.GetValue("nonce_str"),
97
+                    timeStamp = DateTime.UtcNow.Ticks
98
+                });
99
+            }
100
+            return Json(new
101
+            {
102
+                state = false
103
+            });
83 104
             //获取并返回预支付XML信息
84
-            return $"xml= {xml} ||||||  result={TenPayV3.Unifiedorder(result)}";
105
+
85 106
             #endregion
86 107
         }
87 108
 
@@ -89,5 +110,10 @@ namespace ServiceWechat.Api.Controllers.WechatApis
89 110
         {
90 111
 
91 112
         }
113
+
114
+        private void GetPayInfo()
115
+        {
116
+
117
+        }
92 118
     }
93 119
 }

+ 2 - 3
ServiceWechat/ServiceWechat/Program.cs

@@ -20,9 +20,8 @@ namespace ServiceWechat.Api
20 20
         public static IWebHost BuildWebHost(string[] args) =>
21 21
             WebHost.CreateDefaultBuilder(args)
22 22
                 .UseStartup<Startup>()
23
-                .UseKestrel()
24
-                .UseUrls("http://meiwang.800100.net:80")
25
-                //.UseIISIntegration()
23
+                //.UseUrls("http://*:80")
24
+                .UseIISIntegration()
26 25
                 .Build();
27 26
     }
28 27
 }

+ 39 - 5
ServiceWechat/ServiceWechat/wwwroot/makeorder.html

@@ -1,12 +1,12 @@
1
-<!DOCTYPE html>
1
+<!DOCTYPE html>
2 2
 <html lang="en">
3 3
 <head>
4 4
     <meta charset="utf-8">
5
-    <title>订单页面</title>
5
+    <title>页面</title>
6 6
 </head>
7 7
 <body>
8
-    <h1>订单页面</h1>
9
-    <input id="btn" type="button" value="点击下订单" />
8
+    <h1>页面</h1>
9
+    <input id="btn" type="button" value="点击" />
10 10
     <script type="text/javascript" src="script/jquery.min.js"></script>
11 11
     <script>
12 12
         $(function () {
@@ -15,9 +15,43 @@
15 15
                     "url": "/wechatapi/payment/makeorder",
16 16
                     "type": "json",
17 17
                     "success": function (res) {
18
-                        alert(res);
18
+
19
+                        if (!res.state) {
20
+                            alert(false);
21
+                            return;
22
+                        }
23
+
24
+                        var appid = res.appid;
25
+                        var timestamp = res.timestamp;
26
+                        var noncestr = res.noncestr;
27
+                        var package = res.package;
28
+                        var paysign = res.paysign;
29
+
30
+                        WeixinJSBridge.invoke(
31
+                            'getBrandWCPayRequest', {
32
+                                "appId": appid,     //公众号名称,由商户传入
33
+                                "timeStamp": timestamp,         //时间戳,自1970年以来的秒数
34
+                                "nonceStr": noncestr, //随机串
35
+                                "package": package,
36
+                                "signType": "MD5",         //微信签名方式:
37
+                                "paySign": paysign //微信签名
38
+                            },
39
+                            function (res) {
40
+                                if (res.err_msg == "get_brand_wcpay_request:ok") {
41
+                                    //支付成功,后续自行处理
42
+                                    alert("支付成功");
43
+                                }
44
+                                else {
45
+                                    //支付取消,或者其他错误,自行处理
46
+                                    alert("支付取消或错误");
47
+                                }
48
+                            }
49
+                        );
19 50
                     }
20 51
                 });
52
+
53
+
54
+
21 55
             });
22 56
         });
23 57
     </script>