/// <summary> /// 发送模板消息。 /// </summary> /// <param name="openId">用户OpendId。</param> /// <param name="templateId">模板Id。</param> /// <param name="url">链接地址。</param> /// <param name="parameters">模板参数。</param> /// <returns>返回消息Id。</returns> public async Task <string> SendTemplateMessage(IOpenId openId, string templateId, string url, object parameters) { Dictionary <string, object> parameterData = new Dictionary <string, object>(); foreach (var p in parameters.GetType().GetProperties()) { object value = p.GetValue(parameters); if (value is string) { parameterData.Add(p.Name, new { value = value }); } else { parameterData.Add(p.Name, value); } } byte[] jsonData = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(new { touser = openId.OpenId, template_id = templateId, url = url, data = parameterData })); await CheckServiceToken(); var result = await HttpHelper.PostHttp(new Uri(SendTemplateMessageUrl + "?access_token=" + ServiceToken.Token), jsonData, "text/json", Encoding.UTF8); HandleJsonError(result); return(JsonConvert.DeserializeXNode(result, "xml").Element("xml").Element("msgid").Value); }
/// <summary> /// 微信内置JsAPI支付。 /// </summary> /// <param name="order">订单信息。</param> /// <param name="openId">用户OpenId。</param> /// <param name="notifyUrl">回调通知地址。</param> /// <returns></returns> public virtual async Task <IJsPayment> CreatePayment(IPaymentOrder order, IOpenId openId, string notifyUrl) { if (order == null) { throw new ArgumentNullException("order"); } if (openId == null) { throw new ArgumentNullException("openId"); } if (notifyUrl == null) { throw new ArgumentNullException("notifyUrl"); } ValidateOrderInfo(order); var payData = OrderInfoToDictionary(order); payData.Add("trade_type", "JSAPI"); payData.Add("openid", openId.OpenId); payData.Add("appid", AppId); //公众账号ID payData.Add("mch_id", ShopId); //商户号 payData.Add("nonce_str", Guid.NewGuid().ToString().Replace("-", "")); //随机字符串 payData.Add("notify_url", notifyUrl); payData.Add("sign", GetSignature(payData, ShopKey)); string backData = await HttpHelper.PostHttp(new Uri(CreatePayUrl), Encoding.UTF8.GetBytes(GetXml(payData)), "application/x-www-form-urlencoded", Encoding.UTF8); XElement root = XDocument.Parse(backData).Element("xml"); if (root.Element("return_code").Value == "FAIL") { string errMsg = root.Element("return_msg").Value; throw new WechatException(errMsg); } if (root.Element("result_code").Value == "FAIL") { string errMsg = root.Element("err_code").Value; throw new WechatException(errMsg); } JsPayment payment = new JsPayment(); payment.PrepayId = root.Element("prepay_id").Value; payment.Nonce = Guid.NewGuid().ToString().Replace("-", ""); payment.TradeType = root.Element("trade_type").Value; payment.TimeStamp = GetTimestamp(); payment.Signature = GetSignature(new { appId = AppId, timeStamp = payment.TimeStamp, nonceStr = payment.Nonce, package = "prepay_id=" + payment.PrepayId, signType = "MD5" }, ShopKey); return(payment); }
/// <summary> /// 获取用户信息。 /// </summary> /// <param name="openId">用户OpenId。</param> /// <returns>返回用户信息。</returns> public virtual async Task <IUserInfo> GetUserInfo(IOpenId openId) { await CheckServiceToken(); string result = await HttpHelper.GetHttp(UserInfoUrl, new { access_token = ServiceToken.Token, openid = openId.OpenId }); HandleJsonError(result); var item = JsonConvert.DeserializeObject <UserInfo>(result); return(item); }
/** * Set the delegate implementation. * * @param _delegate The delegate implementing platform specific functions. */ public void SetDelegate(IOpenId _delegate) { this._delegate = _delegate; }
/** * Constructor with delegate. * * @param _delegate The delegate implementing platform specific functions. */ public OpenIdBridge(IOpenId _delegate) : base() { this._delegate = _delegate; }