Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import fetch from 'node-fetch';
- import uuid from 'uuid/v4';
- import crypto from 'crypto';
- export default class Modular{
- async createAccount() {
- const body = JSON.stringify({
- type: 'INDIVIDUAL',
- tcsVersion: 0,
- expectedMonthlySpend: 0
- });
- const hmac = this.hmac();
- var res = await fetch(process.env.modularApiUrl+'/customers', {
- method:"POST",
- headers: {
- 'Date': hmac.date,
- 'x-mod-nonce': hmac.nonce,
- 'Authorization': hmac.authorization,
- 'Content-Type': 'application/json',
- },
- body
- });
- if(res.status === 200)
- return await res.json();
- else
- console.error(await res.text())
- return null;
- }
- hmac(){
- const date = (new Date()).toUTCString();
- const nonce = uuid();
- const signatureString = `date: ${date}\nx-mod-nonce: ${nonce}`;
- console.log("Signature string: "+signatureString);
- console.log("Secret: "+ process.env.modulrApiHmacSecret)
- const signature = crypto.createHmac('sha1', process.env.modulrApiHmacSecret
- ).update(signatureString).digest('base64');
- console.log("Signature: "+ JSON.stringify(signature));
- const base64Signnature = signature.toString('base64');
- console.log("Base64 signature: " + base64Signnature);
- const encodedSignature = encodeURIComponent(base64Signnature);
- console.log("Encoded signature: " + encodedSignature);
- const authorization = `Signature keyId="${process.env.modulrApiKey}",algorithm="hmac-sha1",headers="date x-mod-nonce",signature="${encodedSignature}"`
- console.log("Authorization header: "+authorization);
- return {
- date,
- nonce,
- authorization
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement