-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathcoreApiCreditCardExample.js
More file actions
85 lines (77 loc) · 3.23 KB
/
Copy pathcoreApiCreditCardExample.js
File metadata and controls
85 lines (77 loc) · 3.23 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
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.
// Initialize core api client object
// You can find it in Merchant Portal -> Settings -> Access keys
let core = new midtransClient.CoreApi({
isProduction : false,
serverKey : 'YOUR_SERVER_KEY',
clientKey : 'YOUR_CLIENT_KEY'
});
// Alternative way to initialize core api client object:
// let core = new midtransClient.CoreApi();
// core.apiConfig.set({
// isProduction : false,
// serverKey : 'YOUR_SERVER_KEY',
// clientKey : 'YOUR_CLIENT_KEY'
// })
// Another alternative way to initialize core api client object:
// let core = new midtransClient.CoreApi();
// core.apiConfig.isProduction = false;
// core.apiConfig.serverKey = 'YOUR_SERVER_KEY';
// core.apiConfig.clientKey = 'YOUR_CLIENT_KEY';
// IMPORTANT NOTE: You should do credit card get token via frontend using `midtrans-new-3ds.min.js`, to avoid card data breach risks on your backend
// ( refer to: https://docs.midtrans.com/en/core-api/credit-card?id=_1-getting-the-card-token )
// For full example on Credit Card 3DS transaction refer to:
// (/examples/expressApp) that implement Snap & Core Api
// prepare CORE API parameter to get credit card token
// another sample of card number can refer to https://docs.midtrans.com/en/technical-reference/sandbox-test?id=card-payments
let parameter = {
'card_number': '5264 2210 3887 4659',
'card_exp_month': '12',
'card_exp_year': '2025',
'card_cvv': '123',
'client_key': core.apiConfig.clientKey,
};
core.cardToken(parameter)
.then((cardTokenResponse)=>{
// prepare CORE API parameter to charge credit card ( refer to: https://docs.midtrans.com/en/core-api/credit-card?id=_2-sending-transaction-data-to-charge-api )
let cardToken = cardTokenResponse.token_id;
let parameter = {
"payment_type": "credit_card",
"transaction_details": {
"gross_amount": 12145,
"order_id": "test-transaction-54321",
},
"credit_card":{
"token_id": cardToken
}
};
return core.charge(parameter);
})
.then((chargeResponse)=>{
console.log('chargeResponse:',JSON.stringify(chargeResponse));
})
.catch((e)=>{
console.log('Error occured:',e.message);
});;
// charge_response is object representation of API JSON response
// sample:
// {
// 'approval_code': '1540370521462',
// 'bank': 'bni',
// 'card_type': 'debit',
// 'channel_response_code': '00',
// 'channel_response_message': 'Approved',
// 'currency': 'IDR',
// 'fraud_status': 'accept',
// 'gross_amount': '12145.00',
// 'masked_card': '526422-4659',
// 'order_id': 'test-transaction-54321',
// 'payment_type': 'credit_card',
// 'status_code': '200',
// 'status_message': 'Success, Credit Card transaction is successful',
// 'transaction_id': '2bc57149-b52b-46ff-b901-86418ad1abcc',
// 'transaction_status': 'capture',
// 'transaction_time': '2018-10-24 15:42:01'
// }