예제 #1
0
        public async Task <string> Upload1(string fileName, byte[] buffer)
        {
            var hashValueStr = string.Empty;

            using (var mySHA256 = SHA256.Create())
            {
                try
                {
                    var sha      = new SHA256Managed();
                    var checksum = sha.ComputeHash(buffer);
                    hashValueStr = BitConverter.ToString(checksum).Replace("-", String.Empty);
                }
                catch (IOException e)
                {
                    Console.WriteLine($"I/O Exception: {e.Message}");
                }
                catch (UnauthorizedAccessException e)
                {
                    Console.WriteLine($"Access Exception: {e.Message}");
                }
            }

            var boundary = $"{DateTime.Now.Ticks:x}";

            var meta = new
            {
                filename = fileName,
                sha256   = hashValueStr
            };

            var jsonStr = JsonSerializer.Serialize(meta);

            var mchid     = _configuration.GetValue <string>("WxPay:MchId");
            var serial_no = _configuration.GetValue <string>("WxPay:SerialNo");

            var nonce_str = Guid.NewGuid().ToString();

            var path = _hostEnvironment.ContentRootPath + _configuration.GetValue <string>("WxPay:CertPath");

            var httpHandler = new HttpHandler(mchid, serial_no, privateKey, jsonStr);
            var client      = new HttpClient(httpHandler);

            var request = new HttpRequestMessage(HttpMethod.Post, "https://api.mch.weixin.qq.com/v3/merchant/media/upload");

            var requestContent = new MultipartFormDataContent("--" + boundary);

            requestContent.Add(new StringContent(jsonStr, Encoding.UTF8, "application/json"), "\"meta\"");

            var byteArrayContent = new ByteArrayContent(buffer);

            byteArrayContent.Headers.ContentType = new MediaTypeHeaderValue("image/jpg");
            requestContent.Add(byteArrayContent, "\"file\"", "\"" + meta.filename + "\"");

            //client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("WECHATPAY2-SHA256-RSA2048", await BuildAuthAsync(request, mchid, serial_no, nonce_str, jsonStr));
            //client.DefaultRequestHeaders.Add("Wechatpay-Serial", _configuration.GetValue<string>("WxPay:SerialNo"));
            //client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36");
            //client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            var response = await client.PostAsync("https://api.mch.weixin.qq.com/v3/merchant/media/upload", requestContent);              // await client.SendAsync(request);

            var result = await response.Content.ReadAsStringAsync();

            if (response.StatusCode != HttpStatusCode.OK)
            {
                //logger.Error($"GetAsync End, url:{url}, HttpStatusCode:{response.StatusCode}, result:{result}");
                // return new T();
            }

            return(null);
        }
예제 #2
0
        public async Task <object> ApplyMent()
        {
            var nonce = GenerateNonce();

            #region  入数据

            var applyment = new ApplyMentDto();
            applyment.BusinessCode = "X00000000001";
            applyment.ContactInfo  = new ApplyMentContactInfoDto()
            {
                ContactName = "张三",
                // OpenId = "1312321",
                ContactIdNumber = "511111111111111111",
                MobilePhone     = "13333333333",
                ContactEmail    = "*****@*****.**",
            };
            applyment.SubjectInfo = new ApplyMentSubjectInfo()
            {
                SubjectType         = "SUBJECT_TYPE_INDIVIDUAL",
                BusinessLicenseInfo = new ApplyMentBusinessLicenseInfo()
                {
                    LicenseCopy   = "tV7icXfN8VX58X0D187-NV08cFGRlx6LPg261cpQDI-asaq-0MXen3N7OVE5lDbStaS8nBBbBlv6hL3er8bDK-djRp4PtWGArfr9Numqxsk",
                    LicenseNumber = "91440300MA5EYUKH2K",
                    MerchantName  = "张三餐饮店",
                    LegalPerson   = "张三"
                },
                IdentityInfo = new ApplyMentIdentityInfo()
                {
                    IdDocType  = "IDENTIFICATION_TYPE_IDCARD",
                    IdCardInfo = new ApplyMentIdCardInfo()
                    {
                        IdCardCopy      = "tV7icXfN8VX58X0D187-NV08cFGRlx6LPg261cpQDI-asaq-0MXen3N7OVE5lDbStaS8nBBbBlv6hL3er8bDK-djRp4PtWGArfr9Numqxsk",
                        IdCardNational  = "tV7icXfN8VX58X0D187-NV08cFGRlx6LPg261cpQDI-asaq-0MXen3N7OVE5lDbStaS8nBBbBlv6hL3er8bDK-djRp4PtWGArfr9Numqxsk",
                        IdCardName      = "张三",
                        IdCardNumber    = "511111111111111111",
                        CardPeriodBegin = "2010-01-01",
                        CardPeriodEnd   = "长期",
                    },
                    Owner = true
                },
            };
            applyment.BusinessInfo = new ApplyMentBusinessInfo()
            {
                MerchantShortName = "张三餐饮店",
                ServicePhone      = "13333333333",
                SalesInfo         = new ApplyMentSalesInfo()
                {
                    SalesScenesType = new List <string>()
                    {
                        "SALES_SCENES_STORE"
                    },
                    BizStorInfo = new ApplyMentBizStorInfo()
                    {
                        BizStoreName     = "张三餐饮店",
                        BizAddressCode   = "440305",
                        BizStoreAddress  = "南山区xx大厦x层xxxx室",
                        StoreEntrancePic = new List <string>()
                        {
                            "tV7icXfN8VX58X0D187-NV08cFGRlx6LPg261cpQDI-asaq-0MXen3N7OVE5lDbStaS8nBBbBlv6hL3er8bDK-djRp4PtWGArfr9Numqxsk"
                        },
                        IndoorPic = new List <string>()
                        {
                            "tV7icXfN8VX58X0D187-NV08cFGRlx6LPg261cpQDI-asaq-0MXen3N7OVE5lDbStaS8nBBbBlv6hL3er8bDK-djRp4PtWGArfr9Numqxsk"
                        }
                    }
                }
            };
            applyment.SettlementInfo = new ApplyMentSettlementInfo()
            {
                SettlementId      = "719",
                QualificationType = "餐饮",
                Qualifications    = new List <string>()
                {
                    "tV7icXfN8VX58X0D187-NV08cFGRlx6LPg261cpQDI-asaq-0MXen3N7OVE5lDbStaS8nBBbBlv6hL3er8bDK-djRp4PtWGArfr9Numqxsk"
                },
                ActivitiesAdditions = new List <string>(),
            };
            applyment.Encrypt(GetPublicCertificate().PublicKey.Key as RSA);

            #endregion  入数据

            var jsonContent = JsonConvert.SerializeObject(applyment);             //.Serialize(applyment);

            var httpHandler = new HttpHandler(_mchId, _serialNo, GetPublicCertificate().SerialNumber, GetPrivateCertificate(), GetMerchantCertificate());
            var client      = new HttpClient(httpHandler);

            var request = new HttpRequestMessage(HttpMethod.Post,
                                                 "https://api.mch.weixin.qq.com/v3/applyment4sub/applyment/")
            {
                Content = new StringContent(jsonContent, Encoding.UTF8, "application/json")
            };

            var response = await client.SendAsync(request);

            var result = await response.Content.ReadAsStringAsync();

            if (response.StatusCode != HttpStatusCode.OK)
            {
            }

            return(string.Empty);
        }