-
Notifications
You must be signed in to change notification settings - Fork 58
/
SnapBiQrisPayment.js
108 lines (95 loc) · 3.36 KB
/
SnapBiQrisPayment.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
const midtransClient = require('midtrans-client'); // use this if installed via NPM
// This is just for very basic implementation reference, in production, you should validate the incoming requests and implement your backend more securely.
const { randomUUID } = require('crypto');
/**
* Setup your credentials here to try the example code.
*/
const clientId = "Zabcdefg-MIDTRANS-CLIENT-SNAP";
// Ensure to include newlines properly in the private key as shown below
const private_key = `-----BEGIN PRIVATE KEY-----
BCDEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC7Zk6kJjqamLddaN1lK03XJW3vi5zOSA7V+5eSiYeM9tCOGouJewN/Py58wgvRh7OMAMm1IbSZpAbcZbBa1=
-----END PRIVATE KEY-----`;
const clientSecret = "ABcdefghiSrLJPgKRXqdjaSAuj5WDAbeaXAX8Vn7CWGHuBCfFgABCDVqRLvNZf8BaqPGKaksMjrZDrZqzZEbaA1AYFwBewIWCqLZr4PuvuLBqfTmYIzAbCakHKejABCa";
const partnerId = "partner-id";
const merchantId = "M001234";
const channelId = "12345";
const externalId = randomUUID();
midtransClient.SnapBiConfig.snapBiPrivateKey = private_key;
midtransClient.SnapBiConfig.snapBiClientSecret = clientSecret;
midtransClient.SnapBiConfig.snapBiPartnerId = partnerId;
midtransClient.SnapBiConfig.snapBiChannelId = channelId;
midtransClient.SnapBiConfig.snapBiClientId = clientId
midtransClient.SnapBiConfig.enableLogging = true;
const externalId = randomUUID();
let additionalHeader = {
"X-device-id": "your device id",
"debug-id": "your debug id"
}
let qrisRequestBody = {
"partnerReferenceNo": externalId,
"merchantId": merchantId,
"amount": {
"value": "1500.00",
"currency": "IDR"
},
"validityPeriod": "2030-07-03T12:08:56-07:00",
"additionalInfo": {
"acquirer": "gopay",
"customerDetails": {
"firstName": "Merchant",
"lastName": "Operation",
"email": "[email protected]",
"phone": "+6281932358123"
},
"items": [
{
"id": "8143fc4f-ec05-4c55-92fb-620c212f401e",
"price": {
"value": "1500.00",
"currency": "IDR"
},
"quantity": 1,
"name": "test item name",
"brand": "test item brand",
"category": "test item category",
"merchantName": "Merchant Operation"
}
],
"countryCode": "ID",
"locale": "id_ID"
}
}
/**
* Example code for Qris using Snap Bi.
* Below are example code to create payment.
*/
/**
* Basic example
*/
midtransClient.SnapBi.qris()
.withBody(qrisRequestBody)
.createPayment(externalId)
.then(r =>
console.log("Snap Bi result: " + JSON.stringify(r, null, 2))
)
/**
* Example of using existing access token to create payment
*/
midtransClient.SnapBi.qris()
.withBody(qrisRequestBody)
.withAccessToken("your access token")
.createPayment(externalId)
.then(r =>
console.log("Snap Bi result: " + JSON.stringify(r, null, 2))
)
/**
* Adding custom header during both access token & transaction process request by using .withAccessTokenHeader() .withTransactionHeader()
*/
midtransClient.SnapBi.qris()
.withBody(qrisRequestBody)
.withAccessTokenHeader(additionalHeader)
.withTransactionHeader(additionalHeader)
.createPayment(externalId)
.then(r =>
console.log("Snap Bi result: " + JSON.stringify(r, null, 2))
)