-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathsnapAdvancedExample.js
More file actions
168 lines (157 loc) · 5.29 KB
/
Copy pathsnapAdvancedExample.js
File metadata and controls
168 lines (157 loc) · 5.29 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
const midtransClient = require('./../../index.js');
// 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.
// Please refer to this docs for snap popup:
// https://docs.midtrans.com/en/snap/integration-guide?id=integration-steps-overview
// Please refer to this docs for snap-redirect:
// https://docs.midtrans.com/en/snap/integration-guide?id=alternative-way-to-display-snap-payment-page-via-redirect
// Initialize snap client object
// You can find it in Merchant Portal -> Settings -> Access keys
let snap = new midtransClient.Snap({
isProduction : false,
serverKey : 'YOUR_SERVER_KEY',
clientKey : 'YOUR_CLIENT_KEY'
});
// Alternative way to initialize snap client object:
// let snap = new midtransClient.Snap();
// snap.apiConfig.set({
// isProduction : false,
// serverKey : 'YOUR_SERVER_KEY',
// clientKey : 'YOUR_CLIENT_KEY'
// })
// Another alternative way to initialize snap client object:
// let snap = new midtransClient.Snap();
// snap.apiConfig.isProduction = false;
// snap.apiConfig.serverKey = 'YOUR_SERVER_KEY';
// snap.apiConfig.clientKey = 'YOUR_CLIENT_KEY';
// prepare Snap API parameter ( refer to: https://snap-docs.midtrans.com ) minimum parameter example:
let parameter = {
"transaction_details": {
"order_id": "test-transaction-1234",
"gross_amount": 10000
},
"item_details": [{
"id": "ITEM1",
"price": 10000,
"quantity": 1,
"name": "Midtrans Bear",
"brand": "Midtrans",
"category": "Toys",
"merchant_name": "Midtrans"
}],
"customer_details": {
"first_name": "John",
"last_name": "Watson",
"email": "test@example.com",
"phone": "+628123456",
"billing_address": {
"first_name": "John",
"last_name": "Watson",
"email": "test@example.com",
"phone": "081 2233 44-55",
"address": "Sudirman",
"city": "Jakarta",
"postal_code": "12190",
"country_code": "IDN"
},
"shipping_address": {
"first_name": "John",
"last_name": "Watson",
"email": "test@example.com",
"phone": "0 8128-75 7-9338",
"address": "Sudirman",
"city": "Jakarta",
"postal_code": "12190",
"country_code": "IDN"
}
},
"enabled_payments": ["credit_card", "mandiri_clickpay", "cimb_clicks","bca_klikbca", "bca_klikpay", "bri_epay", "echannel", "indosat_dompetku","mandiri_ecash", "permata_va", "bca_va", "bni_va", "other_va", "gopay","kioson", "indomaret", "gci", "danamon_online"],
"credit_card": {
"secure": true,
"bank": "bca",
"installment": {
"required": false,
"terms": {
"bni": [3, 6, 12],
"mandiri": [3, 6, 12],
"cimb": [3],
"bca": [3, 6, 12],
"offline": [6, 12]
}
},
"whitelist_bins": [
"48111111",
"41111111"
]
},
"bca_va": {
"va_number": "12345678911",
"free_text": {
"inquiry": [
{
"en": "text in English",
"id": "text in Bahasa Indonesia"
}
],
"payment": [
{
"en": "text in English",
"id": "text in Bahasa Indonesia"
}
]
}
},
"bni_va": {
"va_number": "12345678"
},
"permata_va": {
"va_number": "1234567890",
"recipient_name": "SUDARSONO"
},
"callbacks": {
"finish": "https://demo.midtrans.com"
},
"expiry": {
"start_time": "2025-12-20 18:11:08 +0700",
"unit": "minute",
"duration": 9000
},
"custom_field1": "custom field 1 content",
"custom_field2": "custom field 2 content",
"custom_field3": "custom field 3 content"
};
// create transaction
snap.createTransaction(parameter)
.then((transaction)=>{
// transaction token
let transactionToken = transaction.token;
console.log('transactionToken:',transactionToken);
// transaction redirect url
let transactionRedirectUrl = transaction.redirect_url;
console.log('transactionRedirectUrl:',transactionRedirectUrl);
})
.catch((e)=>{
console.log('Error occured:',e.message);
});
// alternative way to create transactionToken
snap.createTransactionToken(parameter)
.then((transactionToken)=>{
console.log('transactionToken:',transactionToken);
})
.catch((e)=>{
console.log('Error occured:',e.message);
});
// alternative way to create transactionRedirectUrl
snap.createTransactionRedirectUrl(parameter)
.then((transactionRedirectUrl)=>{
console.log('transactionRedirectUrl:',transactionRedirectUrl);
})
.catch((e)=>{
console.log('Error occured:',e.message);
});
// Aync / await way
async function snapAsync(){
let transactionToken = await snap.createTransactionToken(parameter);
console.log('transactionToken:',transactionToken);
}
snapAsync();