我正在致力于实现一种名为 Swish 的支付服务,并使用本指南对其进行测试(尽管文件名是瑞典语,但指南是英文的)。
https://www.getswish.se/content/uploads/2015/06/Guide-Testverktyg_20151210.zip https://www.getswish.se/content/uploads/2015/06/Guide-Testverktyg_20151210.zip
如您所见,测试项目包含两个证书,.p12
and .pem
文件。我尝试用两者验证请求。
然而,即使尝试解决这个问题,我仍然收到错误。
按照建议here https://stackoverflow.com/a/12327881/3850405我实现了日志记录和this https://pastebin.com/WGHK04s1是结果。由于字符限制,Pastebin 链接。我没有看到任何指向StatusCode=401
在日志中,所以我不认为是这样HTTP 401 Unauthorized
.
public class PaymentRequest
{
[JsonProperty("payeePaymentReference")]
public string PayeePaymentReference { get; set; }
[JsonProperty("callbackUrl")]
public string CallbackUrl { get; set; }
[JsonProperty("payerAlias")]
public string PayerAlias { get; set; }
[JsonProperty("payeeAlias")]
public string PayeeAlias { get; set; }
[JsonProperty("amount")]
public string Amount { get; set; }
[JsonProperty("currency")]
public string Currency { get; set; }
[JsonProperty("message")]
public string Message { get; set; }
}
class Program
{
static void Main(string[] args)
{
var service = new SwishService();
var paymentRequest = new PaymentRequest();
paymentRequest.PayeePaymentReference = "0123456789";
paymentRequest.CallbackUrl = "https://example.com/api/swishcb/paymentrequests";
paymentRequest.PayerAlias = "4671234768";
paymentRequest.PayeeAlias = "1231181189";
paymentRequest.Amount = "100";
paymentRequest.Currency = "SEK";
paymentRequest.Message = "Kingston USB Flash Drive 8 GB";
service.SendPaymentRequest(paymentRequest);
}
}
public class SwishService
{
private HttpClient client;
public SwishService()
{
var baseAddress = new Uri("https://mss.swicpc.bankgirot.se");
var certHandler = new WebRequestHandler();
certHandler.ClientCertificateOptions = ClientCertificateOption.Manual;
certHandler.UseDefaultCredentials = false;
//Same result with Swish Merchant Test Certificate 1231181189.p12 file
var certPath = $"C:\\Users\\<Path>\\Testcertifikat\\Test Swish Root CA v1 Test.pem";
var certificate = new X509Certificate2(certPath, "swish");
certHandler.ClientCertificates.Add(certificate);
client = new HttpClient(new LoggingHandler(certHandler));
client.BaseAddress = baseAddress;
client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/json");
}
public void SendPaymentRequest(PaymentRequest paymentRequest)
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls
| SecurityProtocolType.Tls11
| SecurityProtocolType.Tls12
| SecurityProtocolType.Ssl3;
//Code throws exception before this callback is called
ServicePointManager.ServerCertificateValidationCallback = delegate (object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
return true;
};
var content = new StringContent(JsonConvert.SerializeObject(paymentRequest), Encoding.UTF8, "application/json");
var response = client.PostAsync("/swish-cpcapi/api/v1/paymentrequests/", content).Result;
var resultStream = response.Content.ReadAsStreamAsync().Result;
var result = string.Empty;
using (var streamReader = new StreamReader(resultStream))
{
result = streamReader.ReadToEnd();
}
}
}
Update:
在 Chrome 中使用 Postman 我可以在首先在 Chrome 中导入证书然后通过mmc
在商店Trusted Root Certification Authorities
。我还没有设法在代码中找到解决方案。 PS:应用程序的托管要求从文件加载证书,而不是从证书存储中获取证书。
更新2:
带有堆栈跟踪的系统诊断日志:
https://pastebin.com/qMz6X6yJ https://pastebin.com/qMz6X6yJ