Skip to content

Instantly share code, notes, and snippets.

@RickardPettersson
Last active May 5, 2021 15:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save RickardPettersson/c95f78402d2d59552732 to your computer and use it in GitHub Desktop.
Save RickardPettersson/c95f78402d2d59552732 to your computer and use it in GitHub Desktop.
C# hjälp class för Swish API för e-handel, fungerande
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();
}
}
}
}
@gmorken
Copy link

gmorken commented Apr 7, 2017

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?

@RickardPettersson
Copy link
Author

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..

@MrFredrikT
Copy link

The issue you have gmorken is that the certificate is installed in the wrong place.

@ArjunThakurDev
Copy link

ArjunThakurDev commented Aug 31, 2018

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.

@Yodapp
Copy link

Yodapp commented Oct 24, 2018

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/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment