/// <summary> /// /// </summary> /// <param name="tradeSource"></param> /// <param name="paymentDetail"></param> /// <returns></returns> public Tuple <bool, IDictionary <string, string> > VerifyPaymentResult(TradeJournal tradeSource, IDictionary <string, string> paymentDetail) { if (null == paymentDetail || false == paymentDetail.ContainsKey("PaymentResult")) { return(Tuple.Create(false, paymentDetail)); } var orderParams = new Dictionary <string, string>(); orderParams.Add("receipt-data", paymentDetail["PaymentResult"]); if (false == string.IsNullOrEmpty(this.VerifyPassword)) { orderParams.Add("password", this.VerifyPassword); } var verifyResponse = new HttpDecorator().HttpPostJson(this.ProductVerifyApi, orderParams.ToJson(), null); var verifyJson = verifyResponse.Content; if (string.IsNullOrEmpty(verifyJson) && null != verifyResponse.Stream && verifyResponse.Stream.Length > 0) { verifyJson = Encoding.UTF8.GetString(verifyResponse.Stream); } var verifyResult = verifyJson.ConvertEntity <VerifyResult>(); if (null != verifyResult && verifyResult.Status == "21007") { //测试环境下,请求沙盒验证 paymentDetail["Sandbox"] = "true"; verifyResponse = new HttpDecorator().HttpPostJson(this.SandboxVerifyApi, orderParams.ToJson(), null); verifyJson = verifyResponse.Content; if (string.IsNullOrEmpty(verifyJson) && null != verifyResponse.Stream && verifyResponse.Stream.Length > 0) { verifyJson = Encoding.UTF8.GetString(verifyResponse.Stream); } verifyResult = verifyJson.ConvertEntity <VerifyResult>(); } paymentDetail.Clear(); var verifySuccess = false; if (null != verifyResult) { verifySuccess = verifyResult.Status == "0"; var verifyDetail = verifyResult.GetPropertyValues(); foreach (var item in verifyDetail) { if (null != item.Value) { paymentDetail.Add(item.Key, item.Value.ToString()); } } } LogManager.GetLogger().Info("==>verifyResult: {0}", verifyJson); return(Tuple.Create(verifySuccess, paymentDetail)); }
/// <summary> /// /// </summary> /// <param name="payRouteName"></param> /// <param name="authCode"></param> /// <returns></returns> private AuthorizationInfo LoadAuthorizationInfo(string payRouteName, string authCode) { var payRoute = this.PayRoutes[payRouteName]; var tokenParams = new List <KeyValuePair <string, object> >(); tokenParams.Add("appid", payRoute.AppId); tokenParams.Add("secret", payRoute.AppSecret); tokenParams.Add("code", authCode); tokenParams.Add("grant_type", "authorization_code"); var httpResult = new HttpDecorator().HttpPost(API_OAuthAccessTokenURI, tokenParams); var authInfo = httpResult.Content.ConvertEntity <AuthorizationInfo>(); return(authInfo); }
private HttpDecorator.HttpResponseResult HttpJson(HttpDecorator.HttpMethod httpMethod, string url, object data) { var tokenHeaders = new Dictionary <string, string>(2); if (this.IsEffectived) { tokenHeaders.Add("Authorization", string.Concat("Bearer ", this.AccessToken)); } var result = new HttpDecorator().HttpJson(httpMethod, url, JsonConvert.SerializeObject(data), tokenHeaders); if (result.StatusCode != System.Net.HttpStatusCode.OK) { LogManager.GetLogger().Error("Easemob-API: {0} <= {1}", (int)result.StatusCode, url); } return(result); }
private string LoadQRCodeContent(TradeJournal tradeSource, IDictionary <string, string> signedParams) { var signedParamsQuery = BuildUrlQuery(signedParams).ToString(); var requestUrl = string.Format("{0}?{1}", API_URI, signedParamsQuery); var responseText = new HttpDecorator().HttpGet(requestUrl, null).Content; if (false == string.IsNullOrEmpty(responseText)) { var paymentDetail = this.ParsePaymentResult(tradeSource, responseText); if (paymentDetail.ContainsKey(API_TradePayResult)) { var alipayResponse = paymentDetail[API_TradePayResult].ConvertEntity <AlipayProvider.AlipayResult.AlipayResponse>(); return(alipayResponse.QRCode); } } return(null); }
/// <summary> /// /// </summary> /// <param name="tradeJournal"></param> public virtual BizProcessResult BizProcess(TradeJournal tradeJournal) { this.BizProcessApi.AssertNotNull("BizProcess"); tradeJournal.AssertNotNull("tradeJournal"); var data = tradeJournal.ToJson(); try { var sw = System.Diagnostics.Stopwatch.StartNew(); var response = new HttpDecorator().HttpPostJson(this.BizProcessApi, data, null); sw.Stop(); LogManager.GetLogger().Info("BizProcess [{0}-{1}] {2}", response.StatusCode, sw.Elapsed, this.BizProcessApi); if (response.StatusCode == System.Net.HttpStatusCode.OK) { if (null != response.Content) { LogManager.GetLogger().Info("BizProcess [{0}] <<===== {1}", response.Content, tradeJournal.ToJson()); var result = response.Content.ConvertEntity <BizProcessResult>(); return(result); } } var errorInfo = new Dictionary <string, object>(); errorInfo.Add("tradeJournal", tradeJournal.ToJson()); errorInfo.Add("BizProcessApi", this.BizProcessApi); errorInfo.Add("HttpStatusCode", response.StatusCode); if (response.StatusCode == System.Net.HttpStatusCode.OK) { errorInfo.Add("HttpResult", response.Content); } LogManager.GetLogger().Error(errorInfo); return(BizProcessResult.CreateErrorResult(this.GetType().Name, string.Format("the request failed[{0}].", response.StatusCode))); } catch (Exception ex) { var info = new Dictionary <string, object>(); info.Add("tradeJournal", tradeJournal.ToJson()); ex.HandleException(info); return(BizProcessResult.CreateErrorResult(this.GetType().Name, ex.Message)); } }
/// <summary> /// /// </summary> /// <param name="paymentParams"></param> /// <returns></returns> public virtual bool Preprocess(Payment paymentParams) { this.BizProcessApi.AssertNotNull("BizProcessApi"); paymentParams.AssertNotNull("paymentParams"); var data = paymentParams.ToJson(); try { var sw = System.Diagnostics.Stopwatch.StartNew(); var response = new HttpDecorator().HttpPostJson(this.PreprocessApi, data, null); sw.Stop(); LogManager.GetLogger().Info("Preprocess [{0}-{1}] {2}", response.StatusCode, sw.Elapsed, this.PreprocessApi); if (response.StatusCode == System.Net.HttpStatusCode.OK) { LogManager.GetLogger().Info("Preprocess [{0}] {1}", response.Content, paymentParams.ToJson()); if (null != response.Content && response.Content.ToString().ToLower() == "true") { return(true); } } var errotInfo = new Dictionary <string, object>(); errotInfo.Add("tradeJournal", paymentParams.ToJson()); errotInfo.Add("BizProcessApi", this.PreprocessApi); errotInfo.Add("HttpStatusCode", response.StatusCode); if (response.StatusCode == System.Net.HttpStatusCode.OK) { errotInfo.Add("HttpResult", response.Content); } LogManager.GetLogger().Error(errotInfo); return(false); } catch (Exception ex) { var info = new Dictionary <string, object>(); info.Add("paymentParams", paymentParams.ToJson()); ex.HandleException(info); return(false); } }
private Tuple <bool, IDictionary <string, string> > QueryPaidStatusFromAlipayGateway(TradeJournal tradeSource, IDictionary <string, string> paymentDetail) { var bizParams = new Dictionary <string, string>(); bizParams["out_trade_no"] = tradeSource.TradeCode; var signedParams = this.BuilderSignParams(API_Query, bizParams); var signedParamsQuery = BuildUrlQuery(signedParams).ToString(); var requestUrl = string.Format("{0}?{1}", API_URI, signedParamsQuery); var responseText = new HttpDecorator().HttpGet(requestUrl, null).Content; if (false == string.IsNullOrEmpty(responseText)) { LogManager.GetLogger().Info("==>paymentResult[QueryPaidStatusFromAlipayGateway]: {0}=>{1}", requestUrl, responseText); paymentDetail = this.ParsePaymentResult(tradeSource, responseText); var isVerified = this.VerifySignature(tradeSource, paymentDetail); return(Tuple.Create(isVerified, paymentDetail)); } return(Tuple.Create(false, paymentDetail)); }
private IDictionary <string, string> UnifiedOrder(TradeJournal tradeJournal, string openId = null) { var payRoute = this.PayRoutes[tradeJournal.PayRoute]; var signParams = new Dictionary <string, string>(); signParams["appid"] = payRoute.AppId; signParams["mch_id"] = payRoute.Partner; signParams["notify_url"] = payRoute.NotifyUrl; signParams["trade_type"] = payRoute.TradeType; signParams["nonce_str"] = Guid.NewGuid().ToString("N"); signParams["body"] = tradeJournal.CommoditySubject; signParams["out_trade_no"] = tradeJournal.TradeCode; signParams["total_fee"] = ((int)(Math.Abs(tradeJournal.TotalFee) * 100)).ToString(); signParams["spbill_create_ip"] = tradeJournal.ClientIP; if (tradeJournal.PayRoute == PayWays.Route_H5) { signParams["device_info"] = "WEB"; signParams["openid"] = openId; } else if (tradeJournal.PayRoute == PayWays.Route_QRCODE) { signParams["product_id"] = tradeJournal.TradeCode; } signParams["sign"] = Sign(signParams, payRoute.PartnerSecret); var responseText = new HttpDecorator().HttpPostXml(API_UnifiedOrder, ToXmlParams(signParams), null).Content; var orderParams = ToDictionary(responseText); if (null == orderParams || false == orderParams.ContainsKey("result_code") || orderParams["result_code"] != "SUCCESS") { return(null); } if (false == orderParams.ContainsKey("prepay_id")) { return(null); } return(orderParams); }
/// <summary> /// /// </summary> /// <param name="tradeJournal"></param> /// <returns></returns> public Tuple <bool, string> Refund(TradeJournal tradeJournal) { tradeJournal.AssertNotNull("tradeJournal"); var bizParams = new Dictionary <string, string>(); bizParams["out_trade_no"] = tradeJournal.ParentTradeCode; bizParams["refund_amount"] = Math.Abs(tradeJournal.TotalFee).ToString("0.00"); bizParams["operator_id"] = "system"; var signedParams = this.BuilderSignParams(API_Refund, bizParams); var signedParamsQuery = BuildUrlQuery(signedParams).ToString(); var requestUrl = string.Format("{0}?{1}", API_URI, signedParamsQuery); var responseText = new HttpDecorator().HttpGet(requestUrl, null).Content; if (false == string.IsNullOrEmpty(responseText)) { LogManager.GetLogger().Info("==>paymentResult[Refund]: {0}=>{1}", requestUrl, responseText); var paymentDetail = this.ParsePaymentResult(tradeJournal, responseText); var isRefunded = this.VerifySignature(tradeJournal, paymentDetail); return(Tuple.Create <bool, string>(isRefunded, paymentDetail.ToJson())); } return(Tuple.Create <bool, string>(false, null)); }