Created
February 28, 2023 05:43
-
-
Save chege-kimaru/5c7583bbc3b2ab547c992bf4c02ff7da to your computer and use it in GitHub Desktop.
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 crypto from 'crypto'; | |
import { v4 as uuidv4 } from 'uuid'; | |
import axios from 'axios'; | |
const SECRET_KEY = ""; | |
const ACCESS_KEY = ""; | |
const PROFILE_ID = ""; | |
const sign = (data: string) => crypto.createHmac('sha256', SECRET_KEY).update(data).digest('base64'); | |
// params is the payment form json data | |
// params.signed_field_names is a comma separated string | |
// eg 'access_key,profile_id,transaction_uuid,...' | |
const buildDataToSign = (params: any) => { | |
const signedFieldNamesArray = params.signed_field_names.split(','); | |
const dataToSign = []; | |
for(const field of signedFieldNamesArray) { | |
dataToSign.push(`${field}=${params[field]}`); | |
} | |
return dataToSign.join(','); | |
}; | |
const makePayment = async (data: any) => { | |
try { | |
const response = await axios.post('https://testsecureacceptance.cybersource.com/pay', data); | |
console.log(response.data); | |
} catch(e) { | |
console.error(e.response.data); | |
throw e; | |
} | |
} | |
const main = () => { | |
const params = { | |
access_key: ACCESS_KEY, | |
profile_id: PROFILE_ID, | |
transaction_uuid: uuidv4(), | |
signed_field_names: 'access_key,profile_id,transaction_uuid,signed_field_names,unsigned_field_names,signed_date_time,locale,transaction_type,reference_number,amount,currency', | |
unsigned_field_names: 'card_type,card_number,card_expiry_date', | |
signed_date_time: new Date(), | |
locale: 'en', | |
transaction_type: 'authorization', | |
reference_number: new Date().getTime(), // Use UUIDV4 | |
amount: 100, | |
currency: 'KES', | |
card_type: '001', | |
card_number: '4242424242424242', | |
card_expiry_date: '11-2020', | |
signature: '' | |
}; | |
params.signature = sign(buildDataToSign(params)); | |
console.log(params); | |
makePayment(params); | |
}; | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment