上篇說到生成繳費二維碼,下一步微信支付,程序寫的差不多了就跟威富通那邊要測試的商戶號跑一下,他們給了,一直都跑不通,我就納悶了,後來詳細的問他們這個微信Wap通道是否還可以用,他們說微信官方已經關閉了純手機瀏覽器支付通道權限。當時就想關閉了,爲啥威富通官方還顯示微信Wap文檔,這這該怎麼搞。。。這種行不通就換條道唄,用授權的方式也可以獲取到想要的用戶Openid,那就再研究唄,授權的前提是必須要微信公衆號appid和secret開發密碼。有了這個不就什麼都好搞了嘛,哈哈哈哈
大概流程:
掃碼顯示個體商戶登錄頁面,輸入賬號密碼,進入主頁面,上欄顯示賬號+個體商戶名稱,點擊個人信息可以看詳細信息。待繳記錄、自助繳費、繳費記錄。重點說一下待繳記錄,這裏使用的微信掃一掃進入的,所以只顯示微信支付通道,(判斷一下是微信瀏覽器還是支付寶瀏覽器是可以區分開的)
//配置授權地址就跳到這裏,第一次code不用賦值,rurl放授權過後跳轉的地址 public ActionResult Index(string code, string rurl) { bool flag = HJFController.config == null; if (flag) { this.Reload(); } //微信公衆號,開發密碼,公衆號網頁授權地址,放配置裏面 string appid = HJFController.config.appid; string secret = HJFController.config.secret; string redirecturl = HJFController.config.redirecturl; if (!string.IsNullOrWhiteSpace(rurl)) Session["xxxxxxxxxx"] = rurl; bool flag2 = string.IsNullOrEmpty(code); ActionResult result; if (flag2) { string url = string.Format("https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri={1}&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect", appid, HttpUtility.UrlEncode(redirecturl)); result = this.Redirect(url); } else { WebClient webClient = new WebClient(); webClient.Encoding = Encoding.UTF8; string address = string.Format("https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code", appid, secret, code); string input = webClient.DownloadString(address); JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer(); Dictionary<string, string> dictionary = javaScriptSerializer.Deserialize<Dictionary<string, string>>(input); string text; bool flag3 = !dictionary.TryGetValue("access_token", out text); if (flag3) { result = base.Content("Error"); } else { string arg = dictionary["openid"]; Session["WeiXinOpenId"] = arg; string x = string.Empty; var rr = Session["xxxxxxxxxx"]; if (rr != null) x = System.Web.HttpUtility.UrlEncode(rr.ToString()); result = this.Redirect(string.Format("{0}?openid={1}&url={2}", HJFController.config.finalurl, arg, x)); } } return result; }
這樣就獲取到了,跟客戶那邊說需要他們提供微信公衆號,他們不給提供,這一路都是坎坷啊,比較是銀行,不給就不給吧,可以理解,但是這個不給我最後還讓不讓繳費成功了,最後協商,讓去請求他們返給我一個用戶Openid,早說這個 我還搞那麼多道幹啥,話說還是能學到點東西得。1.支付前檢查訂單 2.微信支付必不可少得參數
/// <summary> /// 從手機端公從號支付 /// </summary> /// <param name="ddh">訂單號</param> /// <param name="allMoney">總金額</param> /// <param name="attach">附加信息</param> /// <param name="sub_openid">支付用戶的openid</param> /// <param name="callback_url">支付完成後的返回地址</param> /// <param name="f">是否原生態支付</param> /// <returns></returns> public Result getAppPayUrl(string ddh, string allMoney, string attach, string sub_openid, string callback_url, bool f) { var ipAddress = PayHelper.WeiXinHZ.PayHZ.GetHostAddress(); RequestHandler reqHandler = new RequestHandler(null); reqHandler.setParameter("service", "pay.weixin.jspay");//接口類型:pay.weixin.jspay reqHandler.setParameter("mch_id", MerchantId);//必填項,商戶號,由威富通分配 if (!string.IsNullOrEmpty(Sub_appid)) { reqHandler.setParameter("sub_appid", Sub_appid);//微信Sub_appid } reqHandler.setParameter("notify_url", notify_url);//通知地址,必填項,接收威富通通知的URL,需給絕對路徑,255字符內;此URL要保證外網能訪問 reqHandler.setParameter("callback_url", callback_url); reqHandler.setParameter("out_trade_no", ddh);//商戶訂單號 reqHandler.setParameter("body", ddh);//商品描述 reqHandler.setParameter("attach", attach);//附加信息 存放支付方式編碼,方便回寫庫 reqHandler.setParameter("total_fee", allMoney);//總金額 reqHandler.setParameter("mch_create_ip", ipAddress);//終端IP if (!string.IsNullOrWhiteSpace(MerchantId) && MerchantId != "7551000001")//測試商戶號 reqHandler.setParameter("sub_openid", sub_openid);//用戶openid //使用測試商戶號不需要傳用戶openid;切換正式的商戶號需獲取openid, //在請求參數裏增加sub_openid字段,並把獲取的openid值傳給sub_openid。 //在切換成正式商戶號傳sub_openid參數前,需提供 正式商戶號和公衆號(服務號)appid由我方配置, //如果沒有配置的話,會報sub_appid and sub_openid not match錯誤,導 致無法正常調用接口 if (isRsaKey()) { reqHandler.setParameter("sign_type", "RSA_1_256");//簽名類型, } else { reqHandler.setParameter("sign_type", "MD5");//簽名類型,取值:MD5 默認:MD5 } // reqHandler.setParameter("service", "pay.weixin.native");//接口類型:pay.weixin.native pay.alipay.native // reqHandler.setParameter("nonce_str", Utils.random()); //後已經賦值了 if (f) reqHandler.setParameter("is_raw", "1");//是否原生態 var strdata = Exe(reqHandler); if (isRsaKey()) MerchantKey = MerchantPublicKey; //如果是rsa 則返回結果 需要傳遞 公鑰 var result = new TokenResult(strdata, MerchantKey); if (result.Success) { if (f) { return new Result { Success = true, Message = result.pay_info }; } else { var msg = string.Format("https://pay.swiftpass.cn/pay/jspay?token_id={0}&showwxtitle=1", result.token_id); return new Result { Success = true, Message = msg }; } } return new Result { Success = false, Message = result.message ?? (result.err_code + result.err_msg) }; }