{"id":31307,"date":"2024-11-25T14:31:56","date_gmt":"2024-11-25T06:31:56","guid":{"rendered":"https:\/\/fwq.ai\/blog\/31307\/"},"modified":"2024-11-25T14:31:56","modified_gmt":"2024-11-25T06:31:56","slug":"%e5%85%ac%e4%bc%97%e5%8f%b7%e6%94%af%e4%bb%98%e6%8e%a5%e5%8f%a3%e7%9a%84%e5%bc%80%e5%8f%91","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/31307\/","title":{"rendered":"\u516c\u4f17\u53f7\u652f\u4ed8\u63a5\u53e3\u7684\u5f00\u53d1"},"content":{"rendered":"<p style=\"text-align: left\">\u8fd9\u6b21\u7ed9\u5927\u5bb6\u5e26\u6765\u516c\u4f17\u53f7\u652f\u4ed8\u63a5\u53e3\u7684\u5f00\u53d1\uff0c\u516c\u4f17\u53f7\u652f\u4ed8\u63a5\u53e3\u5f00\u53d1\u7684\u6709\u54ea\u4e9b\uff0c\u4e0b\u9762\u5c31\u662f\u5b9e\u6218\u6848\u4f8b\uff0c\u4e00\u8d77\u6765\u770b\u4e00\u4e0b\u3002<\/p>\n<p style=\"text-align: left\">\u516c\u4f17\u53f7\u652f\u4ed8\u5c31\u662f\u5728\u5fae\u4fe1\u91cc\u9762\u7684H5\u9875\u9762\u5524\u8d77\u5fae\u4fe1\u652f\u4ed8\uff0c\u4e0d\u7528\u626b\u7801\u5373\u53ef\u4ed8\u6b3e\u7684\u529f\u80fd\u3002\u505a\u8fd9\u4e2a\u529f\u80fd\u9996\u5148\u8981\u660e\u786e\u7684\u5c31\u662f\uff0c\u53ea\u6709\u548c\u5546\u6237\u53f7mch_id\u5339\u914d\u7684appid\u624d\u80fd\u6210\u529f\u652f\u4ed8\u3002\u5546\u6237\u53f7\u5728\u6ce8\u518c\u6210\u529f\u7684\u65f6\u5019\u5c31\u4f1a\u5c06\u76f8\u5173\u4fe1\u606f\u53d1\u9001\u5230\u90ae\u7bb1\u91cc\u9762\u3002\u800c\u5524\u8d77\u652f\u4ed8\u7684\u4e00\u4e2a\u5173\u952e\u662f\u9760openid\u62ff\u5230\u7edf\u4e00\u4e0b\u5355\u3002\u800copenid\u662f\u548cappid\u4e00\u4e00\u5bf9\u5e94\u7684\u3002\u4e5f\u5c31\u662f\u8bf4\u5982\u679c\u4f60\u767b\u5f55\u4f7f\u7528\u7684appid\u4e0d\u662f\u516c\u4f17\u53f7\u7684appid\uff0c\u5f97\u5230\u7684openid\u5c31\u65e0\u6cd5\u5524\u8d77\u516c\u4f17\u53f7\u5185\u7684\u652f\u4ed8(\u4f1a\u51fa\u73b0appid\u548c\u5546\u6237\u53f7\u4e0d\u5339\u914d\u7684\u9519\u8bef)\u3002\u66fe\u7ecf\u5c31\u5728\u8fd9\u4e2a\u5730\u65b9\u7ed5\u4e86\u4e2a\u5f2f\uff0c\u56e0\u4e3a\u5fae\u4fe1\u7684\u5f00\u653e\u5e73\u53f0\u53ef\u4ee5\u521b\u5efa\u7f51\u7ad9\u5e94\u7528\uff0c\u4e5f\u6709\u4e00\u4e2aappid\u548cappsecreat\uff0c\u4e5f\u53ef\u4ee5\u5728\u5fae\u4fe1\u91cc\u9762\u4e00\u952e\u767b\u5f55\u3002<\/p>\n<h2>\u4e1a\u52a1\u6d41\u7a0b<\/h2>\n<p style=\"text-align: left\">\u4e0b\u9762\u662f\u5fae\u4fe1\u7684\u5b98\u65b9\u6d41\u7a0b\uff0c\u770b\u4f3c\u6709\u70b9\u590d\u6742\uff0c\u91cd\u70b9\u5c31\u662f\u8981\u62ff\u5230\u7edf\u4e00\u4e0b\u5355\u63a5\u53e3\u8fd4\u56de\u7684json\u4e32\uff0c\u5176\u4ed6\u6309\u7167\u5b98\u65b9demo\u57fa\u672c\u5c31\u80fd\u6b63\u786e\uff0c\u4e0b\u9762\u8bf4\u4e00\u4e0b\u51e0\u4e2a\u7ec6\u8282\u3002<\/p>\n<p style=\"text-align: left\"><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/061\/021\/b117daa05812bf41c67e07e59439bf05-0.png\" style=\"max-width:90%\" class=\"aligncenter\" title=\"\u516c\u4f17\u53f7\u652f\u4ed8\u63a5\u53e3\u7684\u5f00\u53d1\u63d2\u56fe\" alt=\"\u516c\u4f17\u53f7\u652f\u4ed8\u63a5\u53e3\u7684\u5f00\u53d1\u63d2\u56fe\" \/><\/p>\n<h3>\u521b\u5efa\u8ba2\u5355<\/h3>\n<p style=\"text-align: left\">\u5728\u8c03\u7528\u5fae\u4fe1\u516c\u4f17\u53f7\u652f\u4ed8\u4e4b\u524d\uff0c\u9996\u5148\u6211\u4eec\u81ea\u5df1\u8981\u628a\u8ba2\u5355\u521b\u5efa\u597d\u3002\u6bd4\u5982\u4e00\u4e2a\u5145\u503c\u7684\u8ba2\u5355\u3002\u4e3b\u8981\u662f\u5148\u786e\u5b9a\u4e0b\u91d1\u989d\u518d\u8fdb\u884c\u4e0b\u4e00\u6b65\u3002<\/p>\n<pre>  public JsonResult CreateRecharegOrder(decimal money)\r\n        {            if (money &lt; (decimal)0.01) return Json(new PaymentResult(\"\u5145\u503c\u91d1\u989d\u975e\u6cd5\uff01\"));            var user = _workContext.CurrentUser;            var order = _paymentService.CreateRechargeOrder(user.Id, money);            return Json(new PaymentResult(true) {OrderId = order.OrderNumber});\r\n        }<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h3>\u8c03\u7528\u7edf\u4e00\u4e0b\u5355<\/h3>\n<p style=\"text-align: left\">\u8ba2\u5355\u521b\u5efa\u6210\u529f\u4e4b\u540e\uff0c\u9875\u9762\u8df3\u8f6c\u5230\u652f\u4ed8\u9875\u9762\uff0c\u8fd9\u4e2a\u65f6\u5019\u5c31\u662f\u6309\u7167\u5b98\u65b9\u7684\u6d41\u7a0b\u53bb\u62ffprepay_id\u548cpaySign,\u5fae\u4fe1\u7684demo\u4e2d\u63d0\u4f9b\u4e86\u4e00\u4e2ajsApiPay\u7684\u5bf9\u8c61\u3002\u4f46\u8fd9\u4e2a\u5bf9\u8c61\u9700\u8981\u4e00\u4e2apage\u5bf9\u8c61\u521d\u59cb\u5316\u3002<\/p>\n<pre>       [LoginValid]        public ActionResult H5Pay(string orderNumber)\r\n        {            var user = _workContext.CurrentUser;            var order = _paymentService.GetOrderByOrderNumber(orderNumber);            \/\/\u5224\u65ad\u8ba2\u5355\u662f\u5426\u5b58\u5728            \/\/\u8ba2\u5355\u662f\u5426\u5df2\u7ecf\u652f\u4ed8\u4e86\r\n            var openid = user.OpenId;            var jsApipay = new JsApiPayMvc(this.ControllerContext.HttpContext);\r\n            jsApipay.openid = openid;\r\n            jsApipay.total_fee = (int)order.Amount * 100;\r\n            WxPayData unifiedOrderResult = jsApipay.GetUnifiedOrderResult();\r\n            ViewBag.wxJsApiParam = jsApipay.GetJsApiParameters();\/\/\u83b7\u53d6H5\u8c03\u8d77JS API\u53c2\u6570     \r\n            ViewBag.unifiedOrder = unifiedOrderResult.ToPrintStr();\r\n            ViewBag.OrderNumber = order.OrderNumber;            return View();\r\n        }<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p style=\"text-align: left\">\u5728MVC\u4e2d\u6211\u4eec\u7b80\u5355\u6539\u4e00\u4e0b\u5c31\u53ef\u4ee5\u4e86\u3002\u4e5f\u5c31\u662f\u628apage\u5bf9\u8c61\u6362\u6210httpContext\u5373\u53ef\u3002\u7136\u540e\u91cc\u9762\u7684\u65b9\u6cd5\u5c31\u53ef\u4ee5\u76f4\u63a5\u7528\u4e86\u3002<\/p>\n<p style=\"text-align: left\">JsApiPayMvc\uff1a<\/p>\n<pre>using System;using System.Collections.Generic;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Runtime.Serialization;using System.IO;using System.Text;using System.Net;using System.Web.Security;using LitJson;namespace WxPayAPI\r\n{    public class JsApiPayMvc\r\n    {        \/\/\/ &lt;summary&gt;\r\n        \/\/\/ \u4fdd\u5b58\u9875\u9762\u5bf9\u8c61\uff0c\u56e0\u4e3a\u8981\u5728\u7c7b\u7684\u65b9\u6cd5\u4e2d\u4f7f\u7528Page\u7684Request\u5bf9\u8c61        \/\/\/ &lt;\/summary&gt;\r\n        public HttpContextBase context { get; set; }        \/\/\/ &lt;summary&gt;\r\n        \/\/\/ openid\u7528\u4e8e\u8c03\u7528\u7edf\u4e00\u4e0b\u5355\u63a5\u53e3        \/\/\/ &lt;\/summary&gt;\r\n        public string openid { get; set; }        \/\/\/ &lt;summary&gt;\r\n        \/\/\/ access_token\u7528\u4e8e\u83b7\u53d6\u6536\u8d27\u5730\u5740js\u51fd\u6570\u5165\u53e3\u53c2\u6570        \/\/\/ &lt;\/summary&gt;\r\n        public string access_token { get; set; }        \/\/\/ &lt;summary&gt;\r\n        \/\/\/ \u5546\u54c1\u91d1\u989d\uff0c\u7528\u4e8e\u7edf\u4e00\u4e0b\u5355        \/\/\/ &lt;\/summary&gt;\r\n        public int total_fee { get; set; }        \/\/\/ &lt;summary&gt;\r\n        \/\/\/ \u7edf\u4e00\u4e0b\u5355\u63a5\u53e3\u8fd4\u56de\u7ed3\u679c        \/\/\/ &lt;\/summary&gt;\r\n        public WxPayData unifiedOrderResult { get; set; }        public JsApiPayMvc(HttpContextBase _context)\r\n        {\r\n            context = _context;\r\n        }        \/**\r\n        * \r\n        * \u7f51\u9875\u6388\u6743\u83b7\u53d6\u7528\u6237\u57fa\u672c\u4fe1\u606f\u7684\u5168\u90e8\u8fc7\u7a0b\r\n        * \u8be6\u60c5\u8bf7\u53c2\u770b\u7f51\u9875\u6388\u6743\u83b7\u53d6\u7528\u6237\u57fa\u672c\u4fe1\u606f\uff1ahttp:\/\/mp.weixin.qq.com\/wiki\/17\/c0f37d5704f0b64713d5d2c37b468d75.html\r\n        * \u7b2c\u4e00\u6b65\uff1a\u5229\u7528url\u8df3\u8f6c\u83b7\u53d6code\r\n        * \u7b2c\u4e8c\u6b65\uff1a\u5229\u7528code\u53bb\u83b7\u53d6openid\u548caccess_token\r\n        * \r\n        *\/\r\n        public void GetOpenidAndAccessToken(string code)\r\n        {            if (!string.IsNullOrEmpty(code))\r\n            {                \/\/\u83b7\u53d6code\u7801\uff0c\u4ee5\u83b7\u53d6openid\u548caccess_token\r\n                Log.Debug(this.GetType().ToString(), \"Get code : \" + code);\r\n                GetOpenidAndAccessTokenFromCode(code);\r\n            }            else\r\n            {                \/\/\u6784\u9020\u7f51\u9875\u6388\u6743\u83b7\u53d6code\u7684URL\r\n                string host = context.Request.Url.Host;                string path = context.Request.Path;                string redirect_uri = HttpUtility.UrlEncode(\"http:\/\/\" + host + path);\r\n                WxPayData data = new WxPayData();\r\n                data.SetValue(\"appid\", WxPayConfig.APPID);\r\n                data.SetValue(\"redirect_uri\", redirect_uri);\r\n                data.SetValue(\"response_type\", \"code\");\r\n                data.SetValue(\"scope\", \"snsapi_base\");\r\n                data.SetValue(\"state\", \"STATE\" + \"#wechat_redirect\");                string url = \"https:\/\/open.weixin.qq.com\/connect\/oauth2\/authorize?\" + data.ToUrl();\r\n                Log.Debug(this.GetType().ToString(), \"Will Redirect to URL : \" + url);                try\r\n                {                    \/\/\u89e6\u53d1\u5fae\u4fe1\u8fd4\u56decode\u7801         \r\n                    context.Response.Redirect(url);\/\/Redirect\u51fd\u6570\u4f1a\u629b\u51faThreadAbortException\u5f02\u5e38\uff0c\u4e0d\u7528\u5904\u7406\u8fd9\u4e2a\u5f02\u5e38                }                catch(System.Threading.ThreadAbortException ex)\r\n                {\r\n                }\r\n            }\r\n        }        \/**\r\n        * \r\n        * \u901a\u8fc7code\u6362\u53d6\u7f51\u9875\u6388\u6743access_token\u548copenid\u7684\u8fd4\u56de\u6570\u636e\uff0c\u6b63\u786e\u65f6\u8fd4\u56de\u7684JSON\u6570\u636e\u5305\u5982\u4e0b\uff1a\r\n        * {\r\n        *  \"access_token\":\"ACCESS_TOKEN\",\r\n        *  \"expires_in\":7200,\r\n        *  \"refresh_token\":\"REFRESH_TOKEN\",\r\n        *  \"openid\":\"OPENID\",\r\n        *  \"scope\":\"SCOPE\",\r\n        *  \"unionid\": \"o6_bmasdasdsad6_2sgVt7hMZOPfL\"\r\n        * }\r\n        * \u5176\u4e2daccess_token\u53ef\u7528\u4e8e\u83b7\u53d6\u5171\u4eab\u6536\u8d27\u5730\u5740\r\n        * openid\u662f\u5fae\u4fe1\u652f\u4ed8jsapi\u652f\u4ed8\u63a5\u53e3\u7edf\u4e00\u4e0b\u5355\u65f6\u5fc5\u987b\u7684\u53c2\u6570\r\n        * \u66f4\u8be6\u7ec6\u7684\u8bf4\u660e\u8bf7\u53c2\u8003\u7f51\u9875\u6388\u6743\u83b7\u53d6\u7528\u6237\u57fa\u672c\u4fe1\u606f\uff1ahttp:\/\/mp.weixin.qq.com\/wiki\/17\/c0f37d5704f0b64713d5d2c37b468d75.html\r\n        * @\u5931\u8d25\u65f6\u629b\u5f02\u5e38WxPayException        *\/\r\n        public void GetOpenidAndAccessTokenFromCode(string code)\r\n        {            try\r\n            {                \/\/\u6784\u9020\u83b7\u53d6openid\u53caaccess_token\u7684url\r\n                WxPayData data = new WxPayData();\r\n                data.SetValue(\"appid\", WxPayConfig.APPID);\r\n                data.SetValue(\"secret\", WxPayConfig.APPSECRET);\r\n                data.SetValue(\"code\", code);\r\n                data.SetValue(\"grant_type\", \"authorization_code\");                string url = \"https:\/\/api.weixin.qq.com\/sns\/oauth2\/access_token?\" + data.ToUrl();                \/\/\u8bf7\u6c42url\u4ee5\u83b7\u53d6\u6570\u636e\r\n                string result = HttpService.Get(url);\r\n                Log.Debug(this.GetType().ToString(), \"GetOpenidAndAccessTokenFromCode response : \" + result);                \/\/\u4fdd\u5b58access_token\uff0c\u7528\u4e8e\u6536\u8d27\u5730\u5740\u83b7\u53d6\r\n                JsonData jd = JsonMapper.ToObject(result);\r\n                access_token = (string)jd[\"access_token\"];                \/\/\u83b7\u53d6\u7528\u6237openid\r\n                openid = (string)jd[\"openid\"];\r\n                Log.Debug(this.GetType().ToString(), \"Get openid : \" + openid);\r\n                Log.Debug(this.GetType().ToString(), \"Get access_token : \" + access_token);\r\n            }            catch (Exception ex)\r\n            {\r\n                Log.Error(this.GetType().ToString(), ex.ToString());                throw new WxPayException(ex.ToString());\r\n            }\r\n        }        \/**\r\n         * \u8c03\u7528\u7edf\u4e00\u4e0b\u5355\uff0c\u83b7\u5f97\u4e0b\u5355\u7ed3\u679c\r\n         * @return \u7edf\u4e00\u4e0b\u5355\u7ed3\u679c\r\n         * @\u5931\u8d25\u65f6\u629b\u5f02\u5e38WxPayException         *\/\r\n        public WxPayData GetUnifiedOrderResult()\r\n        {            \/\/\u7edf\u4e00\u4e0b\u5355\r\n            WxPayData data = new WxPayData();\r\n            data.SetValue(\"body\", \"test\");\r\n            data.SetValue(\"attach\", \"test\");\r\n            data.SetValue(\"out_trade_no\", WxPayApi.GenerateOutTradeNo());\r\n            data.SetValue(\"total_fee\", total_fee);\r\n            data.SetValue(\"time_start\", DateTime.Now.ToString(\"yyyyMMddHHmmss\"));\r\n            data.SetValue(\"time_expire\", DateTime.Now.AddMinutes(10).ToString(\"yyyyMMddHHmmss\"));\r\n            data.SetValue(\"goods_tag\", \"test\");\r\n            data.SetValue(\"trade_type\", \"JSAPI\");\r\n            data.SetValue(\"openid\", openid);\r\n            WxPayData result = WxPayApi.UnifiedOrder(data);            if (!result.IsSet(\"appid\") || !result.IsSet(\"prepay_id\") || result.GetValue(\"prepay_id\").ToString() == \"\")\r\n            {\r\n                Log.Error(this.GetType().ToString(), \"UnifiedOrder response error!\");                throw new WxPayException(\"UnifiedOrder response error!\");\r\n            }\r\n            unifiedOrderResult = result;            return result;\r\n        }        \/**\r\n        *  \r\n        * \u4ece\u7edf\u4e00\u4e0b\u5355\u6210\u529f\u8fd4\u56de\u7684\u6570\u636e\u4e2d\u83b7\u53d6\u5fae\u4fe1\u6d4f\u89c8\u5668\u8c03\u8d77jsapi\u652f\u4ed8\u6240\u9700\u7684\u53c2\u6570\uff0c\r\n        * \u5fae\u4fe1\u6d4f\u89c8\u5668\u8c03\u8d77JSAPI\u65f6\u7684\u8f93\u5165\u53c2\u6570\u683c\u5f0f\u5982\u4e0b\uff1a\r\n        * {\r\n        *   \"appId\" : \"wx2421b1c4370ec43b\",     \/\/\u516c\u4f17\u53f7\u540d\u79f0\uff0c\u7531\u5546\u6237\u4f20\u5165     \r\n        *   \"timeStamp\":\" 1395712654\",         \/\/\u65f6\u95f4\u6233\uff0c\u81ea1970\u5e74\u4ee5\u6765\u7684\u79d2\u6570     \r\n        *   \"nonceStr\" : \"e61463f8efa94090b1f366cccfbbb444\", \/\/\u968f\u673a\u4e32     \r\n        *   \"package\" : \"prepay_id=u802345jgfjsdfgsdg888\",     \r\n        *   \"signType\" : \"MD5\",         \/\/\u5fae\u4fe1\u7b7e\u540d\u65b9\u5f0f:    \r\n        *   \"paySign\" : \"70EA570631E4BB79628FBCA90534C63FF7FADD89\" \/\/\u5fae\u4fe1\u7b7e\u540d \r\n        * }\r\n        * @return string \u5fae\u4fe1\u6d4f\u89c8\u5668\u8c03\u8d77JSAPI\u65f6\u7684\u8f93\u5165\u53c2\u6570\uff0cjson\u683c\u5f0f\u53ef\u4ee5\u76f4\u63a5\u505a\u53c2\u6570\u7528\r\n        * \u66f4\u8be6\u7ec6\u7684\u8bf4\u660e\u8bf7\u53c2\u8003\u7f51\u9875\u7aef\u8c03\u8d77\u652f\u4ed8API\uff1ahttp:\/\/pay.weixin.qq.com\/wiki\/doc\/api\/jsapi.php?chapter=7_7\r\n        * \r\n        *\/\r\n        public string GetJsApiParameters()\r\n        {\r\n            Log.Debug(this.GetType().ToString(), \"JsApiPay::GetJsApiParam is processing...\");\r\n            WxPayData jsApiParam = new WxPayData();\r\n            jsApiParam.SetValue(\"appId\", unifiedOrderResult.GetValue(\"appid\"));\r\n            jsApiParam.SetValue(\"timeStamp\", WxPayApi.GenerateTimeStamp());\r\n            jsApiParam.SetValue(\"nonceStr\", WxPayApi.GenerateNonceStr());\r\n            jsApiParam.SetValue(\"package\", \"prepay_id=\" + unifiedOrderResult.GetValue(\"prepay_id\"));\r\n            jsApiParam.SetValue(\"signType\", \"MD5\");\r\n            jsApiParam.SetValue(\"paySign\", jsApiParam.MakeSign());            string parameters = jsApiParam.ToJson();\r\n            Log.Debug(this.GetType().ToString(), \"Get jsApiParam : \" + parameters);            return parameters;\r\n        }        \/**\r\n        * \r\n        * \u83b7\u53d6\u6536\u8d27\u5730\u5740js\u51fd\u6570\u5165\u53e3\u53c2\u6570,\u8be6\u60c5\u8bf7\u53c2\u8003\u6536\u8d27\u5730\u5740\u5171\u4eab\u63a5\u53e3\uff1ahttp:\/\/pay.weixin.qq.com\/wiki\/doc\/api\/jsapi.php?chapter=7_9\r\n        * @return string \u5171\u4eab\u6536\u8d27\u5730\u5740js\u51fd\u6570\u9700\u8981\u7684\u53c2\u6570\uff0cjson\u683c\u5f0f\u53ef\u4ee5\u76f4\u63a5\u505a\u53c2\u6570\u4f7f\u7528        *\/\r\n        public string GetEditAddressParameters()\r\n        {            string parameter = \"\";            try\r\n            {                string host = context.Request.Url.Host;                string path = context.Request.Path;                string queryString = context.Request.Url.Query;                \/\/\u8fd9\u4e2a\u5730\u65b9\u8981\u6ce8\u610f\uff0c\u53c2\u4e0e\u7b7e\u540d\u7684\u662f\u7f51\u9875\u6388\u6743\u83b7\u53d6\u7528\u6237\u4fe1\u606f\u65f6\u5fae\u4fe1\u540e\u53f0\u56de\u4f20\u7684\u5b8c\u6574url\r\n                string url = \"http:\/\/\" + host + path + queryString;                \/\/\u6784\u9020\u9700\u8981\u7528SHA1\u7b97\u6cd5\u52a0\u5bc6\u7684\u6570\u636e\r\n                WxPayData signData = new WxPayData();\r\n                signData.SetValue(\"appid\",WxPayConfig.APPID);\r\n                signData.SetValue(\"url\", url);\r\n                signData.SetValue(\"timestamp\",WxPayApi.GenerateTimeStamp());\r\n                signData.SetValue(\"noncestr\",WxPayApi.GenerateNonceStr());\r\n                signData.SetValue(\"accesstoken\",access_token);                string param = signData.ToUrl();\r\n                Log.Debug(this.GetType().ToString(), \"SHA1 encrypt param : \" + param);                \/\/SHA1\u52a0\u5bc6\r\n                string addrSign = FormsAuthentication.HashPasswordForStoringInConfigFile(param, \"SHA1\");\r\n                Log.Debug(this.GetType().ToString(), \"SHA1 encrypt result : \" + addrSign);                \/\/\u83b7\u53d6\u6536\u8d27\u5730\u5740js\u51fd\u6570\u5165\u53e3\u53c2\u6570\r\n                WxPayData afterData = new WxPayData();\r\n                afterData.SetValue(\"appId\",WxPayConfig.APPID);\r\n                afterData.SetValue(\"scope\",\"jsapi_address\");\r\n                afterData.SetValue(\"signType\",\"sha1\");\r\n                afterData.SetValue(\"addrSign\",addrSign);\r\n                afterData.SetValue(\"timeStamp\",signData.GetValue(\"timestamp\"));\r\n                afterData.SetValue(\"nonceStr\",signData.GetValue(\"noncestr\"));                \/\/\u8f6c\u4e3ajson\u683c\u5f0f\r\n                parameter = afterData.ToJson();\r\n                Log.Debug(this.GetType().ToString(), \"Get EditAddressParam : \" + parameter);\r\n            }            catch (Exception ex)\r\n            {\r\n                Log.Error(this.GetType().ToString(), ex.ToString());                throw new WxPayException(ex.ToString());\r\n            }            return parameter;\r\n        }\r\n    }\r\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p style=\"text-align: left\">View Code<\/p>\n<p style=\"text-align: left\">\u8fd9\u4e2a\u9875\u9762\u53ef\u4ee5\u5728\u672c\u5730\u8c03\u8bd5\uff0c\u53ef\u4ee5\u6bd4\u8f83\u65b9\u4fbf\u7684\u786e\u8ba4\u53c2\u6570\u662f\u5426ok\u3002<\/p>\n<h3>\u5524\u8d77\u652f\u4ed8<\/h3>\n<p style=\"text-align: left\">\u5b98\u65b9\u9875\u9762\u7684\u793a\u4f8b\u5982\u4e0b\uff1ahttps:\/\/pay.weixin.qq.com\/wiki\/doc\/api\/jsapi.php?chapter=7_7&amp;index=6&nbsp;\u4f46\u4e3b\u8981\u7684\u53c2\u6570(mark\u90e8\u5206)\u662f\u7531\u540e\u53f0\u751f\u6210\u7684\uff0c\u4e5f\u5c31\u662f\u4e0a\u4e00\u4e2a\u6b65\u9aa4\u7684ViewBag.wxJsApiParam<\/p>\n<pre>function onBridgeReady(){\r\n   WeixinJSBridge.invoke(\r\n       'getBrandWCPayRequest', {           \"appId\" \uff1a \"wx2421b1c4370ec43b\",     \/\/\u516c\u4f17\u53f7\u540d\u79f0\uff0c\u7531\u5546\u6237\u4f20\u5165     \r\n           \"timeStamp\"\uff1a\" 1395712654\",         \/\/\u65f6\u95f4\u6233\uff0c\u81ea1970\u5e74\u4ee5\u6765\u7684\u79d2\u6570     \r\n           \"nonceStr\" \uff1a \"e61463f8efa94090b1f366cccfbbb444\", \/\/\u968f\u673a\u4e32     \r\n           \"package\" \uff1a \"prepay_id=u802345jgfjsdfgsdg888\",     \r\n           \"signType\" \uff1a \"MD5\",         \/\/\u5fae\u4fe1\u7b7e\u540d\u65b9\u5f0f\uff1a     \r\n           \"paySign\" \uff1a \"70EA570631E4BB79628FBCA90534C63FF7FADD89\" \/\/\u5fae\u4fe1\u7b7e\u540d \r\n       },\r\n       function(res){     \r\n           if(res.err_msg == \"get_brand_wcpay_request\uff1aok\" ) {}     \/\/ \u4f7f\u7528\u4ee5\u4e0a\u65b9\u5f0f\u5224\u65ad\u524d\u7aef\u8fd4\u56de,\u5fae\u4fe1\u56e2\u961f\u90d1\u91cd\u63d0\u793a\uff1ares.err_msg\u5c06\u5728\u7528\u6237\u652f\u4ed8\u6210\u529f\u540e\u8fd4\u56de    ok\uff0c\u4f46\u5e76\u4e0d\u4fdd\u8bc1\u5b83\u7edd\u5bf9\u53ef\u9760\u3002 \r\n       }\r\n   ); \r\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p style=\"text-align: left\">\u6240\u4ee5\u5728MVC\u4e2d\u8981\u8fd9\u6837\u5199\uff1a<\/p>\n<pre>@{\r\n    ViewBag.Title = \"\u5fae\u4fe1\u652f\u4ed8\";\r\n    Layout = \"~\/Views\/Shared\/_Layout.cshtml\";\r\n}&lt;p class=\"page\" id=\"Wxpayment\"&gt;\r\n    &lt;p class=\"content\"&gt;\r\n        &lt;p&gt;\u8ba2\u5355\u8be6\u60c5\uff1a@html.raw(viewbag.unifiedorder)&lt;\/p&gt;\r\n        &lt;button id=\"h5pay\" onclick=\"callpay()\"&gt;\u652f\u4ed8&lt;\/button&gt;\r\n    &lt;\/p&gt;\r\n    &lt;input type=\"hidden\" value=\"@ViewBag.OrderNumber\" id=\"ordernum\"\/&gt;&lt;\/p&gt;\r\n &lt;script type=\"text\/javascript\"&gt;\r\n    \/\/\u8c03\u7528\u5fae\u4fe1JS api \u652f\u4ed8\r\n    function jsApiCall() {\r\n        WeixinJSBridge.invoke(            'getBrandWCPayRequest',            @Html.Raw(ViewBag.wxJsApiParam),\/\/josn\u4e32\r\n            function (res)\r\n            {\r\n                WeixinJSBridge.log(res.err_msg);                \/\/alert(res.err_code + res.err_desc + res.err_msg);\r\n                if (res.err_msg == \"get_brand_wcpay_request:ok\") {                   var num = $(\"#ordernum\").val();\r\n                    $.post(\"\/payment\/WeiXinPaySuccess\", { ordernumber: num }, function(data) {                        if (data.IsSuccess === true) {\r\n                            alert(\"\u652f\u4ed8\u6210\u529f\");\r\n                            location.href = document.referrer;\r\n                        } else {\r\n                            \r\n                        }\r\n                    });\r\n                } \r\n                if (res.err_msg == 'get_brand_wcpay_request:cancel') {\r\n                      $('.button').removeAttr('submitting');\r\n                      alert('\u53d6\u6d88\u652f\u4ed8');\r\n                } \r\n            }\r\n        );\r\n    }    function callpay()\r\n    {        if (typeof WeixinJSBridge == \"undefined\")\r\n        {\r\n            alert(\"WeixinJSBridge =\");            if (document.addEventListener)\r\n            {\r\n                document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);\r\n            }            else if (document.attachEvent)\r\n            {\r\n                document.attachEvent('WeixinJSBridgeReady', jsApiCall);\r\n                document.attachEvent('onWeixinJSBridgeReady', jsApiCall);\r\n            }\r\n        }        else\r\n        {\r\n            jsApiCall();\r\n        }\r\n    }&lt;\/script&gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p style=\"text-align: left\">\u5fc5\u987b\u8981\u7528Html.Raw\uff0c\u4e0d\u7136json\u89e3\u6790\u4e0d\u5bf9\uff0c\u65e0\u6cd5\u652f\u4ed8\u3002\u8fd9\u4e2a\u65f6\u5019\u70b9\u51fb\u9875\u9762\uff0c\u4f1a\u51fa\u73b0\u5fae\u4fe1\u7684\u52a0\u8f7d\u6548\u679c\uff0c\u4f46\u522b\u9ad8\u5174\u7684\u592a\u65e9\uff0c\u8fd8\u662f\u4f1a\u51fa\u9519\uff0c\u51fa\u73b0\u4e00\u4e2a\u201c3\u5f53\u524d\u7684URL\u672a\u6ce8\u518c\u201d<\/p>\n<p style=\"text-align: left\"><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/061\/021\/de4559010336af65c86d541825ab1f0d-3.png\" class=\"aligncenter\" title=\"\u516c\u4f17\u53f7\u652f\u4ed8\u63a5\u53e3\u7684\u5f00\u53d1\u63d2\u56fe1\" alt=\"\u516c\u4f17\u53f7\u652f\u4ed8\u63a5\u53e3\u7684\u5f00\u53d1\u63d2\u56fe1\" \/><\/p>\n<p style=\"text-align: left\">\u539f\u56e0\u5c31\u5728\u4e8e\uff0c\u9700\u8981\u5728\u516c\u4f17\u53f7\u4e2d\u8bbe\u7f6e\u652f\u4ed8\u76ee\u5f55\u3002\u800c\u8fd9\u4e2a\u652f\u4ed8\u76ee\u5f55\u662f\u5927\u5c0f\u5199\u654f\u611f\u7684\uff0c\u6240\u4ee5\u4f60\u5f97\u591a\u8bd5\u51e0\u6b21\u3002\u76f4\u5230\u5f39\u51fa\u8f93\u5165\u5bc6\u7801\u7684\u7a97\u53e3\u624d\u662f\u771f\u7684\u6d41\u7a0b\u6b63\u786e\u4e86\u3002\u7136\u540e\u652f\u4ed8\u6210\u529f\u4e4b\u540e\u9a6c\u4e0a\u5c31\u53ef\u4ee5\u6536\u5230js\u4e2d\u7684\u56de\u8c03\uff0c\u8fd9\u4e2a\u65f6\u5019\u4f60\u53ef\u4ee5\u53bb\u5904\u7406\u4f60\u7684\u8ba2\u5355\u548c\u4e1a\u52a1\u903b\u8f91\u3002<\/p>\n<h2>\u5c0f\u7ed3&nbsp;<\/h2>\n<p style=\"text-align: left\">\u5982\u679c\u662f\u751f\u4ea7\u73af\u5883\uff0c\u6211\u4eec\u9700\u8981\u518d\u591a\u4e2a\u5730\u65b9\u8c03\u7528\uff0c\u9700\u8981\u518d\u5c01\u88c5\u4e00\u4e0b\u3002<\/p>\n<pre>function jsApiCall(json, success, fail) {\r\n    WeixinJSBridge.invoke(        'getBrandWCPayRequest',\r\n        json,\/\/josn\u4e32\r\n        function (res)\r\n        {\r\n            WeixinJSBridge.log(res.err_msg);            \/\/alert(res.err_code + res.err_desc + res.err_msg);\r\n            if (res.err_msg == \"get_brand_wcpay_request:ok\") {                \/\/\u5145\u503c\u8fdb\u53bb \u8981\u533a\u5206\u662f\u51fa\u9898\u5145\u503c \u8fd8\u662f\u8d2d\u4e70\u60ac\u8d4f \u524d\u8005\u51b2\u5230\u4ed6\u7684\u94b1\u5305\r\n                \/\/\u540e\u8005\u76f4\u63a5\u51b2\u5230\u7cfb\u7edf\u8d26\u6237\r\n                if (success) success();\r\n            } \r\n            if (res.err_msg == 'get_brand_wcpay_request:cancel') {                \/\/ alert('\u53d6\u6d88\u652f\u4ed8');\r\n                if (fail)fail();\r\n            } \r\n        }\r\n    );\r\n}function callpay(json,success,fail)\r\n{    if (typeof WeixinJSBridge == \"undefined\")\r\n    {\r\n        alert(\"\u8bf7\u5728\u5fae\u4fe1\u4e2d\u6253\u5f00!\");        if (document.addEventListener)\r\n        {\r\n            document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);\r\n        }        else if (document.attachEvent)\r\n        {\r\n            document.attachEvent('WeixinJSBridgeReady', jsApiCall);\r\n            document.attachEvent('onWeixinJSBridgeReady', jsApiCall);\r\n        }\r\n    }    else\r\n    {\r\n        jsApiCall(json, success, fail);\r\n    }\r\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p style=\"text-align: left\">View Code<\/p>\n<pre>  [LoginValid]        public ActionResult H5PayJson(string orederId)\r\n        {            var user = _workContext.CurrentUser;            var order = _paymentService.GetOrderByOrderNumber(orederId);            \/\/\u5224\u65ad\u8ba2\u5355\u662f\u5426\u5b58\u5728            \/\/\u8ba2\u5355\u662f\u5426\u5df2\u7ecf\u652f\u4ed8\u4e86\r\n            var openid = user.OpenId;            var jsApipay = new JsApiPayMvc(ControllerContext.HttpContext)\r\n            {\r\n                openid = openid,\r\n                total_fee = (int) order.Amount*100\r\n            };            try\r\n            {\r\n                jsApipay.GetUnifiedOrderResult();                return Json(jsApipay.GetJsApiParameters());\/\/\u5b9e\u9645\u8fd8\u662f\u5b57\u7b26\u4e32\r\n            }            catch (Exception e)\r\n            {                \/\/\u7edf\u4e00\u4e0b\u5355\u5931\u8d25\r\n                return Json(new PortalResult(false, e.Message));\r\n            }\r\n        }<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p style=\"text-align: left\">\u8c03\u7528\u7684\u65f6\u5019\u8fd9\u6837\u76f4\u63a5\u5524\u8d77\u652f\u4ed8\u4e86\u3002 \u4f46\u5982\u679c\u4f20\u5165\u7684json\u4e0d\u662fjson\u5bf9\u8c61\uff0c\u5fae\u4fe1\u52a0\u8f7d\u52a8\u753b\u4f1a\u4e00\u76f4\u5361\u5728\u54ea\u513f\u3002<\/p>\n<pre> $.post(\"\/Checkout\/H5PayJson\", { orederId: orderId }, function (jsondata) {                                var jdata = JSON.parse(jsondata);                                if (jdata.appId) {\r\n                                    callpay(jdata, function () {\r\n                                        $.post(\"\/payment\/WeiXinPaySuccess\", { ordernumber: orderId }, function (paymentdata) {                                            if (paymentdata.IsSuccess === true) {\r\n                                                submitQuestion();\r\n                                            } else {\r\n                                                $.alert(paymentdata.Message);\r\n                                            }\r\n                                        });\r\n                                    }, function () {\r\n                                        $.alert(\"\u4f60\u5df2\u53d6\u6d88\u652f\u4ed8!\");\r\n                                    });\r\n                                } else {\r\n                                    alert(\"\u7edf\u4e00\u4e0b\u5355\u5931\u8d25!\");\r\n                                }\r\n                            });<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u76f8\u4fe1\u770b\u4e86\u672c\u6587\u6848\u4f8b\u4f60\u5df2\u7ecf\u638c\u63e1\u4e86\u65b9\u6cd5\uff0c\u66f4\u591a\u7cbe\u5f69\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n<p>\u63a8\u8350\u9605\u8bfb\uff1a<\/p>\n<p style=\"text-align: left\"><\/p>\n<p style=\"text-align: left\"><\/p>\n<p style=\"text-align: left\"><\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u516c\u4f17\u53f7\u652f\u4ed8\u63a5\u53e3\u7684\u5f00\u53d1\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u6b21\u7ed9\u5927\u5bb6\u5e26\u6765\u516c\u4f17\u53f7\u652f\u4ed8\u63a5\u53e3\u7684\u5f00\u53d1\uff0c\u516c\u4f17\u53f7\u652f\u4ed8\u63a5\u53e3\u5f00\u53d1\u7684\u6709\u54ea\u4e9b\uff0c\u4e0b\u9762\u5c31\u662f\u5b9e\u6218\u6848\u4f8b\uff0c\u4e00\u8d77\u6765\u770b\u4e00\u4e0b\u3002 \u516c\u4f17\u53f7\u652f\u4ed8\u5c31\u662f\u5728\u5fae\u4fe1\u91cc\u9762\u7684H5\u9875\u9762\u5524\u8d77\u5fae\u4fe1\u652f\u4ed8\uff0c\u4e0d\u7528\u626b\u7801\u5373\u53ef\u4ed8\u6b3e\u7684\u529f\u80fd\u3002\u505a\u8fd9\u4e2a\u529f\u80fd\u9996\u5148\u8981\u660e\u786e\u7684\u5c31\u662f\uff0c\u53ea\u6709\u548c\u5546\u6237\u53f7mch_id\u5339\u914d\u7684appid\u624d\u80fd\u6210\u529f\u652f\u4ed8\u3002\u5546\u6237\u53f7\u5728\u6ce8\u518c\u6210\u529f\u7684\u65f6\u5019\u5c31\u4f1a\u5c06\u76f8\u5173\u4fe1\u606f\u53d1\u9001\u5230\u90ae\u7bb1\u91cc\u9762\u3002\u800c\u5524\u8d77\u652f\u4ed8\u7684\u4e00\u4e2a\u5173\u952e\u662f\u9760openid\u62ff\u5230\u7edf\u4e00\u4e0b\u5355\u3002\u800copenid\u662f\u548cappid\u4e00\u4e00\u5bf9\u5e94\u7684\u3002\u4e5f\u5c31\u662f\u8bf4\u5982\u679c\u4f60\u767b\u5f55\u4f7f\u7528\u7684appid\u4e0d\u662f\u516c\u4f17\u53f7\u7684appid\uff0c\u5f97\u5230\u7684openid\u5c31\u65e0\u6cd5\u5524\u8d77\u516c\u4f17\u53f7\u5185\u7684\u652f\u4ed8(\u4f1a\u51fa\u73b0appid\u548c\u5546\u6237\u53f7\u4e0d\u5339\u914d\u7684\u9519\u8bef)\u3002\u66fe\u7ecf\u5c31\u5728\u8fd9\u4e2a\u5730\u65b9\u7ed5\u4e86\u4e2a\u5f2f\uff0c\u56e0\u4e3a\u5fae\u4fe1\u7684\u5f00\u653e\u5e73\u53f0\u53ef\u4ee5\u521b\u5efa\u7f51\u7ad9\u5e94\u7528\uff0c\u4e5f\u6709\u4e00\u4e2aappid\u548cappsecreat\uff0c\u4e5f\u53ef\u4ee5\u5728\u5fae\u4fe1\u91cc\u9762\u4e00\u952e\u767b\u5f55\u3002 \u4e1a\u52a1\u6d41\u7a0b \u4e0b\u9762\u662f\u5fae\u4fe1\u7684\u5b98\u65b9\u6d41\u7a0b\uff0c\u770b\u4f3c\u6709\u70b9\u590d\u6742\uff0c\u91cd\u70b9\u5c31\u662f\u8981\u62ff\u5230\u7edf\u4e00\u4e0b\u5355\u63a5\u53e3\u8fd4\u56de\u7684json\u4e32\uff0c\u5176\u4ed6\u6309\u7167\u5b98\u65b9demo\u57fa\u672c\u5c31\u80fd\u6b63\u786e\uff0c\u4e0b\u9762\u8bf4\u4e00\u4e0b\u51e0\u4e2a\u7ec6\u8282\u3002 \u521b\u5efa\u8ba2\u5355 \u5728\u8c03\u7528\u5fae\u4fe1\u516c\u4f17\u53f7\u652f\u4ed8\u4e4b\u524d\uff0c\u9996\u5148\u6211\u4eec\u81ea\u5df1\u8981\u628a\u8ba2\u5355\u521b\u5efa\u597d\u3002\u6bd4\u5982\u4e00\u4e2a\u5145\u503c\u7684\u8ba2\u5355\u3002\u4e3b\u8981\u662f\u5148\u786e\u5b9a\u4e0b\u91d1\u989d\u518d\u8fdb\u884c\u4e0b\u4e00\u6b65\u3002 public JsonResult CreateRecharegOrder(decimal money) { if (money &lt; (decimal)0.01) return Json(new PaymentResult(&#8220;\u5145\u503c\u91d1\u989d\u975e\u6cd5\uff01&#8221;)); var user = _workContext.CurrentUser; var order = _paymentService.CreateRechargeOrder(user.Id, money); return Json(new PaymentResult(true) {OrderId = order.OrderNumber}); } \u767b\u5f55\u540e\u590d\u5236 \u8c03\u7528\u7edf\u4e00\u4e0b\u5355 \u8ba2\u5355\u521b\u5efa\u6210\u529f\u4e4b\u540e\uff0c\u9875\u9762\u8df3\u8f6c\u5230\u652f\u4ed8\u9875\u9762\uff0c\u8fd9\u4e2a\u65f6\u5019\u5c31\u662f\u6309\u7167\u5b98\u65b9\u7684\u6d41\u7a0b\u53bb\u62ffprepay_id\u548cpaySign,\u5fae\u4fe1\u7684demo\u4e2d\u63d0\u4f9b\u4e86\u4e00\u4e2ajsApiPay\u7684\u5bf9\u8c61\u3002\u4f46\u8fd9\u4e2a\u5bf9\u8c61\u9700\u8981\u4e00\u4e2apage\u5bf9\u8c61\u521d\u59cb\u5316\u3002 [LoginValid] public ActionResult H5Pay(string orderNumber) { var user = _workContext.CurrentUser; var order = _paymentService.GetOrderByOrderNumber(orderNumber); \/\/\u5224\u65ad\u8ba2\u5355\u662f\u5426\u5b58\u5728 \/\/\u8ba2\u5355\u662f\u5426\u5df2\u7ecf\u652f\u4ed8\u4e86 var openid [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"class_list":["post-31307","post","type-post","status-publish","format-standard","hentry","category-19"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/31307","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=31307"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/31307\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=31307"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=31307"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=31307"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}