-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathcreditCardSubscriptionExample.js
139 lines (127 loc) · 4.71 KB
/
creditCardSubscriptionExample.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
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
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'
});
// To use API subscription for credit card, you should first obtain the 1 click token
// Refer to this docs: https://docs.midtrans.com/en/core-api/advanced-features?id=recurring-transaction-with-subscriptions-api
// You will receive saved_token_id as part of the response when the initial card payment is accepted (will also available in the HTTP notification's JSON)
// Refer to this docs: https://docs.midtrans.com/en/core-api/advanced-features?id=sample-3ds-authenticate-json-response-for-the-first-transaction
// {
// ..
// "status_code": "200",
// "signature_key": "99bc6101abcda11c17a8d401747d1fdfb6085af88daf94c575aeaab79487b4b5534a7b5cc898314e5386c0def86f6f7676079bdb656362c5ccd2c9906b42e5a3",
// "saved_token_id_expired_at": "2021-12-31 07:00:00",
// "saved_token_id": "521111gmWqMegyejqCQmmopnCFRs1117",
// "payment_type": "credit_card"
// ..
// }
// Sample saved token id for testing purpose
let savedTokenId = '521111gmWqMegyejqCQmmopnCFRs1117'
// prepare CORE API parameter ( refer to: https://api-docs.midtrans.com/#create-subscription ) create subscription parameter example
let parameter = {
"name": "MONTHLY_2021",
"amount": "14000",
"currency": "IDR",
"payment_type": "credit_card",
"token": savedTokenId,
"schedule": {
"interval": 1,
"interval_unit": "month",
"max_interval": 12,
"start_time": "2021-11-25 07:25:01 +0700"
},
"metadata": {
"description": "Recurring payment for A"
},
"customer_details": {
"first_name": "John",
"last_name": "Doe",
"email": "[email protected]",
"phone": "+62812345678"
}
};
core.createSubscription(parameter)
.then((Response) => {
console.log('createSubscription Response:', JSON.stringify(Response));
})
.catch((e) => {
console.log('createSubscription Error message:', e.message);
});
// sample response is dictionary representation of API JSON response
// {
// "id": "537d04c6-eef7-4c07-a65c-11c55f92c7ed",
// "name": "MONTHLY_2021",
// "amount": "14000",
// "currency": "IDR",
// "created_at": "2021-11-18 12:14:09",
// "schedule": {
// "interval": 1,
// "current_interval": 0,
// "max_interval": 12,
// "interval_unit": "month",
// "start_time": "2021-11-25 07:25:01",
// "next_execution_at": "2021-11-25 07:25:01"
// },
// "status": "active",
// "token": "521111gmWqMegyejqCQmmopnCFRs1117",
// "payment_type": "credit_card",
// "transaction_ids": [],
// "metadata": {
// "description": "Recurring payment for A"
// },
// "customer_details": {
// "email": "[email protected]",
// "first_name": "John",
// "last_name": "Doe",
// "phone": "+62812345678"
// }
// }
// Sample active subscription id for testing purpose
let subscriptionId = "537d04c6-eef7-4c07-a65c-11c55f92c7ed"
// get subscription by subscriptionId
core.getSubscription(subscriptionId)
.then((Response) => {
console.log('getSubscription Response:', JSON.stringify(Response));
})
.catch((e) => {
console.log('getSubscription Error message:', e.message);
});
// enable subscription by subscriptionId
core.enableSubscription(subscriptionId)
.then((Response) => {
console.log('enableSubscription Response:', JSON.stringify(Response));
})
.catch((e) => {
console.log('enableSubscription Error message:', e.message);
});
// update subscription by subscriptionId and Param
let updateSubscriptionParam = {
"name": "MONTHLY_2021",
"amount": "300000",
"currency": "IDR",
"token": savedTokenId,
"schedule": {
"interval": 1
}
}
core.updateSubscription(subscriptionId, updateSubscriptionParam)
.then((Response) => {
console.log('updateSubscription Response:', JSON.stringify(Response));
})
.catch((e) => {
console.log('updateSubscription Error message:', e.message);
});
// disable subscription by subscriptionId
core.disableSubscription(subscriptionId)
.then((Response) => {
console.log('disableSubscription Response:', JSON.stringify(Response));
})
.catch((e) => {
console.log('disableSubscription Error message:', e.message);
});