-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathgopaySubscriptionExample.js
163 lines (151 loc) · 5.22 KB
/
gopaySubscriptionExample.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
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 gopay, you should first link your customer gopay account with gopay tokenization
// Refer to this docs: https://api-docs.midtrans.com/#gopay-tokenization
// You will receive gopay payment token using `get_payment_account` API call.
// You can see some Tokenization API examples here (examples/tokenization)
// {
// "status_code": "200",
// "payment_type": "gopay",
// "account_id": "aa211a53-3d39-426e-a191-0bb078923526",
// "account_status": "ENABLED",
// "metadata": {
// "payment_options": [{
// "name": "GOPAY_WALLET",
// "active": true,
// "balance": {
// "value": "8000000.00",
// "currency": "IDR"
// },
// "metadata": {},
// "token": "2fda0635-bac6-4a7a-bce8-91d87444ab80"
// },
// {
// "name": "PAY_LATER",
// "active": true,
// "balance": {
// "value": "8000000.00",
// "currency": "IDR"
// },
// "metadata": {},
// "token": "43ceb007-b0cf-4f11-8b9f-229cfc7474fc"
// }
// ]
// }
// }
// Sample gopay payment option token and gopay account id for testing purpose that has been already activated before
let gopayPaymentOptionToken = '2fda0635-bac6-4a7a-bce8-91d87444ab80'
let gopayAccountId = 'aa211a53-3d39-426e-a191-0bb078923526'
// prepare CORE API parameter ( refer to: https://api-docs.midtrans.com/#create-subscription ) create subscription parameter example
let parameter = {
"name": "SUBS-Gopay-2021",
"amount": "10000",
"currency": "IDR",
"payment_type": "gopay",
"token": gopayPaymentOptionToken,
"schedule": {
"interval": 1,
"interval_unit": "day",
"max_interval": 7
},
"metadata": {
"description": "Recurring payment for A"
},
"customer_details": {
"first_name": "John A",
"last_name": "Doe A",
"email": "[email protected]",
"phone": "+62812345678"
},
"gopay": {
"account_id": gopayAccountId
}
};
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": "ca67e11f-3c38-4026-9e04-53b1f277d1a0",
// "name": "SUBS-Gopay-2021",
// "amount": "10000",
// "currency": "IDR",
// "created_at": "2021-11-18 13:16:15",
// "schedule": {
// "interval": 1,
// "current_interval": 0,
// "max_interval": 7,
// "interval_unit": "day",
// "start_time": "2021-11-18 13:16:15",
// "previous_execution_at": "2021-11-18 13:16:15",
// "next_execution_at": "2021-11-19 13:16:15"
// },
// "status": "active",
// "token": "2fda0635-bac6-4a7a-bce8-91d87444ab80",
// "payment_type": "gopay",
// "transaction_ids": [],
// "metadata": {
// "description": "Recurring payment for A"
// },
// "customer_details": {
// "email": "[email protected]",
// "first_name": "John A",
// "last_name": "Doe A",
// "phone": "+62812345678"
// }
// }
// Sample active subscription id for testing purpose
let subscriptionId = "ca67e11f-3c38-4026-9e04-53b1f277d1a0"
// 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": gopayPaymentOptionToken,
"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);
});