How do you consume the Swish QR Code API using classic ASP?

654 Views Asked by At

The designers of the Swish QR Code API have chosen to use POST as the method for generating and fetching QR codes. I do not understand this choice, but that's how it is. Consuming the API would be trivial if they had opted to utilize the GET method instead. But as it is, you need to write your own proxy to translate a GET to POST if you need to generate Swish payment QR Codes on the fly.

This is how I would like to use the API:

<img src="qr.asp?payee=9006032&amount=200">

How do you accomplish this using Classic ASP?

Background information:

Swish is a Swedish mobile payment provider and the QR codes are used to initiate a payment. In the QR code, the payee (mobile phone number for private individuals), the amount, and a message to the payee (for example an order number) may be specified.

1

There are 1 best solutions below

0
On

Use the XMLHTTP component to connect to the Swish API server. Build the appropriate JSON request and stream the response to the client.

This example uses JScript as the scripting language.

<%@ codepage=65001 language="jscript" %>
<%

var payee       = Request.QueryString('payee');
var amount      = Request.QueryString('amount');
var message     = Request.QueryString('message');

var format      = 'png'; // Can also be 'jpg' or 'svg'
var size        = 400; // Between 300 and 2000
var border      = 2; // Between 0 and 4 (measured in blocks, not pixels)
var transparent = 'true'; // Must be 'false' for 'jpg'

var json = '{'
    + '"payee":{"value":"' + payee + '","editable":false},'
    + '"amount":{"value":' + amount + ',"editable":false},'
    + '"message":{"value":"' + message + '","editable":false},'
    + '"format":"' + format + '",'
    + '"size":' + size + ','
    + '"border":' + border + ','
    + '"transparent":' + transparent
    + '}';


var http = new ActiveXObject('MSXML2.Serverhttp');
http.open('POST', 'https://mpc.getswish.net/qrg-swish/api/v1/prefilled', false);
http.setRequestHeader('content-type', 'application/json');
http.send(json);

if (http.status != 200) {
    Response.ContentType = 'text/plain';
    Response.Write('HTTP ' + http.status + '\n' + http.statusText + '\n' + http.responseText);
} else {
    Response.ContentType = 'image/png';
    Response.BinaryWrite(http.responseBody);
    Response.End();
}

%>