Created
March 17, 2020 13:46
-
-
Save cayter/faab2812a419e79bae7c498a6072f72a to your computer and use it in GitHub Desktop.
Rapyd API - NodeJS 12.x Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const https = require("https"); | |
const crypto = require("crypto"); | |
const accessKey = "<YOUR_RAPYD_ACCESS_KEY>"; | |
const secretKey = "<YOUR_RAPYD_SECRET_KEY>"; | |
const salt = crypto.randomBytes(12).toString("hex"); | |
const timestamp = (Math.floor(new Date().getTime() / 1000) - 10).toString(); | |
const method = "post"; | |
const path = "/v1/payments"; | |
const body = JSON.stringify({ | |
"amount": 10, | |
"currency": "sgd", | |
"payment_method": { | |
"type": "sg_paynow_bank" | |
} | |
}); | |
const toSign = `${method}${path}${salt}${timestamp}${accessKey}${secretKey}${body}`; | |
let signature = crypto.createHmac("sha256", secretKey).update(toSign, "utf8").digest("hex"); | |
signature = Buffer.from(signature, "utf8").toString("base64"); | |
const req = https | |
.request({ | |
hostname: "sandboxapi.rapyd.net", | |
port: 443, | |
path, | |
method, | |
headers: { | |
"Content-Type": "application/json", | |
"Content-Length": body.length, | |
"access_key": accessKey, | |
"salt": salt, | |
"timestamp": timestamp, | |
"signature": signature | |
} | |
}, res => { | |
res.on('data', d => { | |
console.log(d.toString()); | |
}); | |
}); | |
req.on('error', error => { | |
console.error(error) | |
}); | |
req.write(body); | |
req.end(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment