Esempio n. 1
0
        public RedpackResponse SendRedpack(IWxApp official, string payKey, Redpack redpack)
        {
            var url  = "https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack";
            var sign = string.Concat(redpack.PrepareSign(), $"&key={payKey}").MakeSign(WxPayData.SIGN_TYPE_MD5, payKey);

            redpack.SetSign(sign);

            return(url.GetUriContentDirectly((http) => {
                http.Method = "POST";
                http.ContentType = "text/xml";
                var store = new X509Store(StoreName.Root, StoreLocation.LocalMachine);
                store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly | OpenFlags.MaxAllowed);
                var path = Path.Combine(Environment.CurrentDirectory, @"cert\apiclient_cert.p12");
                var cert = new X509Certificate2(path, "1520961881");
                //var cert = store.Certificates.Find(X509FindType.FindBySerialNumber, "7d4daceb0866305aed424a175e86c005e6b80ee3", false);
                http.ClientCertificates.Add(cert);
                using (var stream = http.GetRequestStream()) {
                    var body = redpack.SerializeToXml();
                    var buffers = UTF8Encoding.UTF8.GetBytes(body);
                    stream.Write(buffers, 0, buffers.Length);
                    stream.Flush();
                }
                return http;
            }).DeserializeFromXml <RedpackResponse>());
        }
Esempio n. 2
0
        public NormalWxResponse SendWeChatMessage(IWxApp official, string openid, string text)
        {
            var data = new CustomMessageContext()
            {
                MsgType = "text",
                Text    = new CustomMessageContent()
                {
                    Content = text
                },
                ToUser = openid
            };
            var url = $"https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={this.GetToken(official.AppId, official.Secret)}";

            return(url.GetUriJsonContent <NormalWxResponse>((http) => {
                http.Method = "POST";
                http.ContentType = "application/json; encoding=utf-8";
                using (var stream = http.GetRequestStream()) {
                    var body = data.SerializeToJson();
                    var buffers = UTF8Encoding.UTF8.GetBytes(body);
                    stream.Write(buffers, 0, buffers.Length);
                    stream.Flush();
                }
                return http;
            }));
        }
Esempio n. 3
0
        public QueryWxUserCardResponse QueryWxUserMCards(
            IWxApp app,
            IWxUserOpenId wxuser,
            IWxMCardId mcard = null)
        {
            var url = string.Format("https://api.weixin.qq.com/card/user/getcardlist?access_token={0}"
                                    , GetToken(app.AppId, app.Secret));

            return(url.GetUriJsonContent <QueryWxUserCardResponse>((http) => {
                http.Method = "POST";
                http.ContentType = "application/json; encoding=utf-8";
                var data = new {
                    openid = wxuser.OpenId,
                    card_id = (mcard == null || string.IsNullOrWhiteSpace(mcard.CardId))
                                        ? null
                                        : mcard.CardId
                };
                using (var stream = http.GetRequestStream()) {
                    var body = data.SerializeToJson();
                    var buffers = UTF8Encoding.UTF8.GetBytes(body);
                    stream.Write(buffers, 0, buffers.Length);
                    stream.Flush();
                }
                return http;
            }));
        }
Esempio n. 4
0
        public QueryCardCouponWxResponse QueryMCard(IWxApp official)
        {
            var url = string.Format("https://api.weixin.qq.com/card/batchget?access_token={0}", GetToken(official.AppId, official.Secret));

            return(url.GetUriJsonContent <QueryCardCouponWxResponse>((http) => {
                http.Method = "POST";
                http.ContentType = "application/json; encoding=utf-8";
                var data = new {
                    offset = 0,
                    count = 10,
                    //status_list

                    /*
                     * 支持开发者拉出指定状态的卡券列表
                     * “CARD_STATUS_NOT_VERIFY”, 待审核 ;
                     * “CARD_STATUS_VERIFY_FAIL”, 审核失败;
                     * “CARD_STATUS_VERIFY_OK”, 通过审核;
                     * “CARD_STATUS_DELETE”,  卡券被商户删除;
                     * “CARD_STATUS_DISPATCH” 在公众平台投放过的卡券;
                     */
                    status_list = new string[] { "CARD_STATUS_VERIFY_OK", "CARD_STATUS_DISPATCH" }
                };
                using (var stream = http.GetRequestStream()) {
                    var body = data.SerializeToJson();
                    var buffers = UTF8Encoding.UTF8.GetBytes(body);
                    stream.Write(buffers, 0, buffers.Length);
                    stream.Flush();
                }
                return http;
            }));
        }
Esempio n. 5
0
        //public static string SerializeToXml(this object data)
        //{
        //    var outString = string.Empty;

        //    using (MemoryStream ms = new MemoryStream())
        //    {
        //        var serializer = new XmlSerializer(data.GetType());
        //        serializer.Serialize(ms, data);
        //        byte[] arr = ms.ToArray();
        //        outString = System.Text.Encoding.UTF8.GetString(arr, 0, arr.Length);
        //        ms.Close();
        //    }
        //    return outString;
        //}
        //public static string SerializeToXml(this WxPayData data)
        //{
        //    var strBld = new StringBuilder();
        //    strBld.AppendLine("<xml>\r\n");
        //    foreach (var elm in data.GetType().GetProperties().Select((ctx) =>
        //    {
        //        var elm = ctx.GetCustomAttributes<XmlElementAttribute>().FirstOrDefault();
        //        var v = ctx.GetValue(data);
        //        if (v == null) return null;
        //        return new
        //        {
        //            name = elm.ElementName,
        //            value = v
        //        };
        //    })
        //    .Where(o => o != null)
        //    .OrderBy(o => o.name)
        //    .Select(o => o))
        //    {
        //        strBld.AppendLine(string.Format("<{0}>{1}</{0}>", elm.name, elm.value));
        //    }
        //    strBld.AppendLine("</xml>");
        //    return strBld.ToString();
        //}
        /// <summary>
        /// 生成基础授权URL 只能获取 openid
        /// </summary>
        /// <param name="config"></param>
        /// <returns></returns>
        public static string GenerateBasicAuthorizeUrl(this IWxApp app)
        {
            //scope:应用授权作用域,snsapi_base (不弹出授权页面,直接跳转,只能获取用户openid),
            //snsapi_userinfo (弹出授权页面,可通过openid拿到昵称、性别、所在地。并且, 即使在未关注的情况下,只要用户授权,也能获取其信息 )
            var redirect_uri = HttpUtility.UrlEncode("https://www.yourc.club/wap/pay");
            var @params      = new string[] {
                string.Format("appid={0}", app.AppId),
                string.Format("redirect_uri={0}", redirect_uri),
                string.Format("scope={0}", "snsapi_base"),
                string.Format("state={0}", "Joey#wechat_redirect"),
                string.Format("response_type={0}", "code")
            };

            return(string.Format("https://open.weixin.qq.com/connect/oauth2/authorize?{0}",
                                 string.Join("&", @params)));
        }
Esempio n. 6
0
        public NormalWxResponse DeleteCardCoupon(IWxApp official, IWxMCardId cardId)
        {
            var url = string.Format("https://api.weixin.qq.com/card/delete?access_token={0}", GetToken(official.AppId, official.Secret));

            return(url.GetUriJsonContent <NormalWxResponse>((http) => {
                var data = new { card_id = cardId.CardId };
                http.Method = "POST";
                http.ContentType = "application/json; encoding=utf-8";
                using (var stream = http.GetRequestStream()) {
                    var body = data.SerializeToJson();
                    var buffers = UTF8Encoding.UTF8.GetBytes(body);
                    stream.Write(buffers, 0, buffers.Length);
                    stream.Flush();
                }
                return http;
            }));
        }
Esempio n. 7
0
        public CreateCouponWxResponse SaveOrUpdateCardCoupon(IWxApp official, JObject jObject)
        {
            var url = string.Format(string.IsNullOrEmpty(jObject.ParseCardId())
                                ? "https://api.weixin.qq.com/card/create?access_token={0}"
                                : "https://api.weixin.qq.com/card/update?access_token={0}",
                                    GetToken(official.AppId, official.Secret));

            return(url.GetUriJsonContent <CreateCouponWxResponse>((http) => {
                http.Method = "POST";
                http.ContentType = "application/json;encoding=utf-8";
                using (var stream = http.GetRequestStream()) {
                    var buffers = UTF8Encoding.UTF8.GetBytes(jObject.ToString());
                    stream.Write(buffers, 0, buffers.Length);
                    stream.Flush();
                }
                return http;
            }));
        }
Esempio n. 8
0
        public DecryptCodeWxResponse DecryptMCardUserCode(IWxApp app, string encryptedData)
        {
            string url = string.Format("https://api.weixin.qq.com/card/code/decrypt?access_token={0}", GetToken(app.AppId, app.Secret));

            return(url.GetUriJsonContent <DecryptCodeWxResponse>((http) => {
                http.Method = "POST";
                http.ContentType = "application/json; encoding=utf-8";
                var data = new {
                    encrypt_code = encryptedData
                };
                using (var stream = http.GetRequestStream()) {
                    var body = data.SerializeToJson();
                    var buffers = UTF8Encoding.UTF8.GetBytes(body);
                    stream.Write(buffers, 0, buffers.Length);
                    stream.Flush();
                }
                return http;
            }));
        }
Esempio n. 9
0
        public JObject QueryMCardDetails(IWxApp official, IWxCardKey card)
        {
            var url = string.Format("https://api.weixin.qq.com/card/get?access_token={0}", GetToken(official.AppId, official.Secret));

            return(url.GetUriJsonContent <JObject>((http) => {
                http.Method = "POST";
                http.ContentType = "application/json; encoding=utf-8";
                var data = new {
                    card_id = card.CardId
                };
                using (var stream = http.GetRequestStream()) {
                    var body = data.SerializeToJson();
                    var buffers = UTF8Encoding.UTF8.GetBytes(body);
                    stream.Write(buffers, 0, buffers.Length);
                    stream.Flush();
                }
                return http;
            }));
        }
Esempio n. 10
0
        public void GenernateSharedMomentsPoster(Stream stream, IWxApp app, IWxUserKey sharedBy)
        {
            var uri = $"https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={GetToken(app.AppId, app.Secret)}";

            uri.SaveBinaryFile((http) => {
                http.Method      = "POST";
                http.ContentType = "application/json; encoding=utf-8";
                var data         = new {
                    path  = $"pages/welcome/index",
                    scene = sharedBy.OpenId,
                    width = 280
                };
                using (var xx = http.GetRequestStream()) {
                    var body    = data.SerializeToJson();
                    var buffers = UTF8Encoding.UTF8.GetBytes(body);
                    xx.Write(buffers, 0, buffers.Length);
                    xx.Flush();
                }
                return(http);
            }, stream);
        }
Esempio n. 11
0
        public string GenerateSignForApplyMCard(
            IWxApp official,
            IWxApp miniprogram,
            string cardid,
            long timestamp,
            string nonce_str)
        {
            //https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=cardsign
            //卡券签名算法  https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115
            var       api_ticket = GetApiTicket(miniprogram.AppId, this.GetToken(official.AppId, official.Secret));
            ArrayList AL         = new ArrayList();

            AL.Add(api_ticket);
            AL.Add(timestamp);
            AL.Add(nonce_str);
            AL.Add(cardid);
            AL.Sort(new DictionarySort());

            //var perpare = string.Format("{0}{1}{2}{3}", timestamp, nonce_str,api_ticket, cardid );
            var perpare = string.Join(string.Empty, AL.ToArray());

            return(perpare.GetSHA1Crypto());
        }