public class BankCardInfoProvider : IBankCardInfoProvider { private string _key; private string _url; private string _header; public BankCardInfoProvider(IOptions<BankCardInfoConfig> _config) { var config = _config.Value; if (config == null) throw new ArgumentNullException(nameof(config)); if (string.IsNullOrEmpty(config.Key)) throw new ArgumentNullException(nameof(config.Key)); if (string.IsNullOrEmpty(config.Url)) throw new ArgumentNullException(nameof(config.Url)); if (string.IsNullOrEmpty(config.Header)) throw new ArgumentNullException(nameof(config.Header)); _key = config.Key; _url = config.Url; _header = config.Header; } public async Task<BankCardInfoResult> GetBankCardInfoAsync(string bankCardNumber) { if (string.IsNullOrEmpty(bankCardNumber)) throw new ArgumentNullException(nameof(bankCardNumber)); CardInfo cardInfo = null; string url = $"{_url}{bankCardNumber}"; var http = new HttpClient(); var header = _header.Split(':'); http.DefaultRequestHeaders.Add(header[0], header[1] + _key); var response = await http.GetAsync(url); if (response.IsSuccessStatusCode) { string contentStr = await response.Content.ReadAsStringAsync(); cardInfo = JsonConvert.DeserializeObject<CardInfo>(contentStr); } if (cardInfo == null || cardInfo.Code != 0) { throw new SinoException(ErrorCode.E007001, nameof(ErrorCode.E007001).GetCode()); } else { string bankName = cardInfo.Data?.BankName; bankName = bankName == "中国银行" ? "中国银行" : bankName.IndexOf("中国") != -1 ? bankName.Replace("中国", "") : bankName; return new BankCardInfoResult { Code = cardInfo.Code, Msg = cardInfo.Msg, Province = cardInfo.Data?.Province, City = cardInfo.Data?.City, CardType = cardInfo.Data?.CardType, CardPreFixNum = cardInfo.Data?.CardPreFixNum, BankNum = cardInfo.Data?.BankNum, LogoUrl = cardInfo.Data?.LogoUrl, CardName = cardInfo.Data?.CardName, BankName = bankName, ServicePhone = cardInfo.Data?.ServicePhone, BankUrl = cardInfo.Data?.BankUrl }; } } }
{ "BankCardInfoConfig": { "Key": "", "Url": "http://api43.market.alicloudapi.com/api/c43?apiversion=2.0.5&bankcard=", "Header": "Authorization:APPCODE " } }
using Newtonsoft.Json; namespace Sino.TMSystem.CommonService { /// <summary> /// 卡信息 /// </summary> public class CardInfo { /// <summary> /// 状态码(0表示请求成功,其他表示查询失败) /// </summary> [JsonProperty("error_code")] public int Code { get; set; } /// <summary> /// 对应code的信息 /// </summary> [JsonProperty("reason")] public string Msg { get; set; } /// <summary> /// Luhn校验值 /// </summary> [JsonProperty("ordersign")] public string Ordersign { get; set; } /// <summary> /// 卡主体信息 /// </summary> [JsonProperty("result")] public CardInfoBody Data { get; set; } } /// <summary> /// 卡信息主体 /// </summary> public class CardInfoBody { /// <summary> /// 卡种 /// </summary> [JsonProperty("cardtype")] public string CardType { get; set; } /// <summary> /// 卡片类型 /// </summary> [JsonProperty("cardname")] public string CardName { get; set; } /// <summary> /// 开户银行名称 /// </summary> [JsonProperty("bankname")] public string BankName { get; set; } /// <summary> /// 保留(开户行编码) /// </summary> [JsonProperty("banknum")] public string BankNum { get; set; } /// <summary> /// 开户省份 /// </summary> [JsonProperty("province")] public string Province { get; set; } /// <summary> /// 开户城市 /// </summary> [JsonProperty("city")] public string City { get; set; } /// <summary> /// 银行客服电话 /// </summary> [JsonProperty("servicephone")] public string ServicePhone { get; set; } /// <summary> /// 银行官网 /// </summary> [JsonProperty("bankurl")] public string BankUrl { get; set; } /// <summary> /// 卡的前缀(银行卡bin号) /// </summary> [JsonProperty("cardprefixnum")] public string CardPreFixNum { get; set; } /// <summary> /// 银行小Logo /// </summary> [JsonProperty("bankimage")] public string LogoUrl { get; set; } } }