Last active
January 24, 2025 02:53
-
-
Save bogordesaincom/b9a772303272cc8c21364551a2b80a75 to your computer and use it in GitHub Desktop.
Sample
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 'dotenv/config'; | |
| import axios from 'redaxios'; | |
| const danaapi = axios.create({ | |
| baseURL: process.env.DANA_SANBOX_URL, | |
| }); | |
| export { danaapi }; |
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
| async createBearerToken({ dataOrder }) { | |
| const pathName = '/v1.0/payment-gateway/payment.htm'; | |
| const { signature, timestamp } = await signRequest({ | |
| pathName, | |
| dataOrder, | |
| }); | |
| // console.log(dataOrder); | |
| const payload = JSON.stringify(dataOrder); | |
| const tokenResponse = await danaapi.post(pathName, payload, { | |
| headers: { | |
| 'X-SIGNATURE': signature, | |
| 'X-TIMESTAMP': timestamp, | |
| 'X-EXTERNAL-ID': dataOrder.partnerReferenceNo, | |
| 'CHANNEL-ID': process.env.DANA_CLIENT_ID, | |
| 'X-PARTNER-ID': process.env.DANA_CLIENT_ID, | |
| 'Content-Type': 'application/json', | |
| }, | |
| }); | |
| // console.log('token response', tokenResponse); | |
| return tokenResponse; | |
| } |
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 paymentService = new DanaService(); | |
| const orderID = uuid4(); | |
| const dataOrder = { | |
| merchantId: process.env.DANA_MERCHANT_ID, | |
| partnerReferenceNo: orderID, | |
| amount: { | |
| value: '10000', | |
| currency: 'IDR', | |
| }, | |
| urlParams: [ | |
| { | |
| url: `https://3fd8-103-171-182-9.ngrok-free.app/v1/payments/webhook/${orderID}`, | |
| type: 'NOTIFICATION', | |
| }, | |
| { | |
| url: 'https:/www.google.com', | |
| type: 'PAY_RETURN', | |
| }, | |
| ], | |
| payOptionDetails: [ | |
| { | |
| payMethod: 'VIRTUAL_ACCOUNT', | |
| payOption: 'VIRTUAL_ACCOUNT_CIMB', | |
| transAmount: { | |
| value: '10000', | |
| currency: 'IDR', | |
| }, | |
| additionalInfo: { | |
| phoneNumber: '0817345545', | |
| }, | |
| }, | |
| ], | |
| additionalInfo: { | |
| order: { | |
| orderTitle: 'Demo', | |
| scenario: 'REDIRECT', | |
| }, | |
| envInfo: { | |
| sourcePlatform: 'IPG', | |
| terminalType: 'SYSTEM', | |
| orderTerminalType: 'APP', | |
| }, | |
| mcc: '5945', | |
| }, | |
| }; | |
| s | |
| const datareturn = await paymentService.createBearerToken({ dataOrder }); | |
| console.log('data retur', datareturn); | |
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 'dotenv/config'; | |
| import dayjs from 'dayjs'; | |
| import crypto from 'node:crypto'; | |
| import utc from 'dayjs/plugin/utc'; | |
| import timezone from 'dayjs/plugin/timezone'; | |
| dayjs.extend(utc); | |
| dayjs.extend(timezone); | |
| const hashData = (string) => { | |
| return crypto.createHash('sha256').update(string).digest('hex'); | |
| }; | |
| const signContent = (content, privateKey, encoding) => { | |
| const sign = crypto.createSign('SHA256'); | |
| sign.write(content, encoding); | |
| sign.end(); | |
| return sign.sign(privateKey, 'base64'); | |
| }; | |
| const splitStringIntoChunks = (input, chunkSize) => { | |
| const chunkCount = Math.ceil(input.length / chunkSize); | |
| return Array.from({ length: chunkCount }).map((v, chunkIndex) => | |
| input.substr(chunkIndex * chunkSize, chunkSize) | |
| ); | |
| }; | |
| const base64KeyToPEM = (base64Key, keyType) => { | |
| return [ | |
| `-----BEGIN ${keyType} KEY-----`, | |
| ...splitStringIntoChunks(base64Key, 64), | |
| `-----END ${keyType} KEY-----`, | |
| ].join('\n'); | |
| }; | |
| const signRequest = async ({ pathName, dataOrder }) => { | |
| const dataOrderStringfy = JSON.stringify(dataOrder); | |
| const timestamp = dayjs().tz('Asia/Jakarta').format('YYYY-MM-DDTHH:mm:ssZ'); | |
| const privateKey = process.env.DANA_PRIVATE_KEY; | |
| const content = 'POST:' + pathName + ':' + hashData(dataOrderStringfy) + ':' + timestamp; | |
| console.log(content); | |
| const signature = signContent(content, base64KeyToPEM(privateKey, 'PRIVATE'), 'utf8'); | |
| return { signature, timestamp }; | |
| }; | |
| export default signRequest; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment