Created
March 17, 2020 13:44
-
-
Save cayter/da135ea2f686aef47bbdc93d7110534f to your computer and use it in GitHub Desktop.
Rapyd API - NodeJS 13.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
import https from "https"; | |
import crypto from "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