-
-
Save RickardPettersson/c95f78402d2d59552732 to your computer and use it in GitHub Desktop.
using System; | |
using System.IO; | |
using System.Net; | |
using System.Security.Cryptography.X509Certificates; | |
using System.Text; | |
using System.Web; | |
namespace WebShop.Helpers | |
{ | |
public class SwishAPIHelper | |
{ | |
public static string StartSwishPayment(string phonenumber, int amount, string message) | |
{ | |
try | |
{ | |
string DataToPost = "{ \"payeePaymentReference\": \"0123456789\", " + | |
"\"callbackUrl\": \"https://datorbutik.se/Swish/Callback/\", " + | |
"\"payerAlias\": \"" + phonenumber + "\", " + | |
"\"payeeAlias\": \"1231181189\", " + | |
"\"amount\": \"" + amount + "\", " + | |
"\"currency\": \"SEK\", " + | |
"\"message\": \"" + message + "\" }"; | |
string URL = "https://mss.swicpc.bankgirot.se/swish-cpcapi/api/v1/paymentrequests/"; | |
//ServicePointManager.Expect100Continue = true; | |
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11; | |
//Cert Challenge URL | |
Uri requestURI = new Uri(URL); | |
//Create the Request Object | |
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(requestURI); | |
//Set the Request Object parameters | |
req.ContentType = "application/json; charset=UTF-8"; | |
req.Method = "POST"; | |
req.ProtocolVersion = HttpVersion.Version10; | |
// turn our request string into a byte stream | |
byte[] postBytes = Encoding.UTF8.GetBytes(DataToPost); | |
req.ContentLength = postBytes.Length; | |
string path = HttpContext.Current.Server.MapPath("~/App_Data/SwishMerchantTestCertificate1231181189.p12"); | |
X509Certificate cert = new X509Certificate2(path, "swish", X509KeyStorageFlags.Exportable | X509KeyStorageFlags.PersistKeySet); | |
req.ClientCertificates.Add(cert); | |
Stream requestStream = req.GetRequestStream(); | |
// now send it | |
requestStream.Write(postBytes, 0, postBytes.Length); | |
requestStream.Close(); | |
try | |
{ | |
WebResponse response = req.GetResponse(); | |
String result = ""; | |
using (StreamReader rdr = new StreamReader(response.GetResponseStream())) | |
{ | |
result = rdr.ReadToEnd(); | |
} | |
HttpWebResponse httpResponse = (HttpWebResponse)response; | |
return "Status code: " + httpResponse.StatusCode + " - Content: " + result; | |
} | |
catch (WebException e) | |
{ | |
using (WebResponse response = e.Response) | |
{ | |
HttpWebResponse httpResponse = (HttpWebResponse)response; | |
//Log.Information("Error code: {0}", httpResponse.StatusCode); | |
using (Stream data = response.GetResponseStream()) | |
{ | |
string text = new StreamReader(data).ReadToEnd(); | |
return "Error code: " + httpResponse.StatusCode + " - Content: " + text; | |
} | |
} | |
} | |
} | |
catch (Exception ex) | |
{ | |
return "Exception: " + ex.ToString(); | |
} | |
} | |
} | |
} |
Have today released http://www.tabetaltmedswish.se/ that are a C# ASP.NET MVC test projekt for Swish for handel you can fork or clone/download from github..
The issue you have gmorken is that the certificate is installed in the wrong place.
I was trying this code. Is it self-sustained or we need to change some URLs in the config to make it connect to right API endpoints
At this moment I am just getting "The remote server returned an error: (502) Bad Gateway." if I run this.
I was trying this code. Is it self-sustained or we need to change some URLs in the config to make it connect to right API endpoints
At this moment I am just getting "The remote server returned an error: (502) Bad Gateway." if I run this.
Swish has changed their URLs.
See documentation here, https://developer.getswish.se/
Hello!
Nice code, but I got an error "The request was canceled: Unable to establish secure SSL / TLS channel." when running Stream requestStream = req.GetRequestStream();
Do you know what theproblem is?