midtrans-client
Advanced tools
Comparing version 1.2.5 to 1.3.0
@@ -79,4 +79,4 @@ 'use strict' | ||
// Static vars | ||
ApiConfig.CORE_SANDBOX_BASE_URL = 'https://api.sandbox.midtrans.com/v2'; | ||
ApiConfig.CORE_PRODUCTION_BASE_URL = 'https://api.midtrans.com/v2'; | ||
ApiConfig.CORE_SANDBOX_BASE_URL = 'https://api.sandbox.midtrans.com'; | ||
ApiConfig.CORE_PRODUCTION_BASE_URL = 'https://api.midtrans.com'; | ||
ApiConfig.SNAP_SANDBOX_BASE_URL = 'https://app.sandbox.midtrans.com/snap/v1'; | ||
@@ -83,0 +83,0 @@ ApiConfig.SNAP_PRODUCTION_BASE_URL = 'https://app.midtrans.com/snap/v1'; |
@@ -9,3 +9,3 @@ 'use strict' | ||
*/ | ||
class CoreApi{ | ||
class CoreApi { | ||
/** | ||
@@ -16,3 +16,3 @@ * Initiate with options | ||
*/ | ||
constructor(options={isProduction:false,serverKey:'',clientKey:''}){ | ||
constructor(options = {isProduction: false, serverKey: '', clientKey: ''}) { | ||
this.apiConfig = new ApiConfig(options); | ||
@@ -22,73 +22,199 @@ this.httpClient = new HttpClient(this); | ||
} | ||
/** | ||
* Do `/charge` API request to Core API | ||
* Do `/v2/charge` API request to Core API | ||
* @param {Object} parameter - object of Core API JSON body as parameter, will be converted to JSON (more params detail refer to: https://api-docs.midtrans.com) | ||
* @return {Promise} - Promise contains Object from JSON decoded response | ||
*/ | ||
charge(parameter={}){ | ||
let apiUrl = this.apiConfig.getCoreApiBaseUrl()+'/charge'; | ||
charge(parameter = {}) { | ||
let apiUrl = this.apiConfig.getCoreApiBaseUrl() + '/v2/charge'; | ||
let responsePromise = this.httpClient.request( | ||
'post', | ||
this.apiConfig.get().serverKey, | ||
apiUrl, | ||
parameter); | ||
'post', | ||
this.apiConfig.get().serverKey, | ||
apiUrl, | ||
parameter); | ||
return responsePromise; | ||
} | ||
/** | ||
* Do `/capture` API request to Core API | ||
* Do `/v2/capture` API request to Core API | ||
* @param {Object} parameter - object of Core API JSON body as parameter, will be converted to JSON (more params detail refer to: https://api-docs.midtrans.com) | ||
* @return {Promise} - Promise contains Object from JSON decoded response | ||
*/ | ||
capture(parameter={}){ | ||
let apiUrl = this.apiConfig.getCoreApiBaseUrl()+'/capture'; | ||
capture(parameter = {}) { | ||
let apiUrl = this.apiConfig.getCoreApiBaseUrl() + '/v2/capture'; | ||
let responsePromise = this.httpClient.request( | ||
'post', | ||
this.apiConfig.get().serverKey, | ||
apiUrl, | ||
parameter); | ||
'post', | ||
this.apiConfig.get().serverKey, | ||
apiUrl, | ||
parameter); | ||
return responsePromise; | ||
} | ||
/** | ||
* Do `/card/register` API request to Core API | ||
* Do `/v2/card/register` API request to Core API | ||
* @param {Object} parameter - object of Core API JSON body as parameter, will be converted to JSON (more params detail refer to: https://api-docs.midtrans.com) | ||
* @return {Promise} - Promise contains Object from JSON decoded response | ||
*/ | ||
cardRegister(parameter={}){ | ||
let apiUrl = this.apiConfig.getCoreApiBaseUrl()+'/card/register'; | ||
cardRegister(parameter = {}) { | ||
let apiUrl = this.apiConfig.getCoreApiBaseUrl() + '/v2/card/register'; | ||
let responsePromise = this.httpClient.request( | ||
'get', | ||
this.apiConfig.get().serverKey, | ||
apiUrl, | ||
parameter); | ||
'get', | ||
this.apiConfig.get().serverKey, | ||
apiUrl, | ||
parameter); | ||
return responsePromise; | ||
} | ||
/** | ||
* Do `/token` API request to Core API | ||
* Do `/v2/token` API request to Core API | ||
* @param {Object} parameter - object of Core API JSON body as parameter, will be converted to JSON (more params detail refer to: https://api-docs.midtrans.com) | ||
* @return {Promise} - Promise contains Object from JSON decoded response | ||
*/ | ||
cardToken(parameter={}){ | ||
let apiUrl = this.apiConfig.getCoreApiBaseUrl()+'/token'; | ||
cardToken(parameter = {}) { | ||
let apiUrl = this.apiConfig.getCoreApiBaseUrl() + '/v2/token'; | ||
let responsePromise = this.httpClient.request( | ||
'get', | ||
this.apiConfig.get().serverKey, | ||
apiUrl, | ||
parameter); | ||
'get', | ||
this.apiConfig.get().serverKey, | ||
apiUrl, | ||
parameter); | ||
return responsePromise; | ||
} | ||
/** | ||
* Do `/point_inquiry/<tokenId>` API request to Core API | ||
* Do `/v2/point_inquiry/<tokenId>` API request to Core API | ||
* @param {String} tokenId - tokenId of credit card (more params detail refer to: https://api-docs.midtrans.com) | ||
* @return {Promise} - Promise contains Object from JSON decoded response | ||
*/ | ||
cardPointInquiry(tokenId){ | ||
let apiUrl = this.apiConfig.getCoreApiBaseUrl()+'/point_inquiry/'+tokenId; | ||
cardPointInquiry(tokenId) { | ||
let apiUrl = this.apiConfig.getCoreApiBaseUrl() + '/v2/point_inquiry/' + tokenId; | ||
let responsePromise = this.httpClient.request( | ||
'get', | ||
this.apiConfig.get().serverKey, | ||
apiUrl, | ||
null); | ||
'get', | ||
this.apiConfig.get().serverKey, | ||
apiUrl, | ||
null); | ||
return responsePromise; | ||
} | ||
/** | ||
* Create `/v2/pay/account` API request to Core API | ||
* @param {Object} parameter - object of Core API JSON body as parameter, will be converted to JSON (more params detail refer to: https://api-docs.midtrans.com/#create-pay-account) | ||
* @return {Promise} - Promise contains Object from JSON decoded response | ||
*/ | ||
linkPaymentAccount(parameter = {}) { | ||
let apiUrl = this.apiConfig.getCoreApiBaseUrl() + '/v2/pay/account'; | ||
let responsePromise = this.httpClient.request( | ||
'post', | ||
this.apiConfig.get().serverKey, | ||
apiUrl, | ||
parameter); | ||
return responsePromise; | ||
} | ||
/** | ||
* Do `/v2/pay/account/<accountId>` API request to Core API | ||
* @param {String} accountId - accountId for specific payment channel (more params detail refer to: https://api-docs.midtrans.com/#get-pay-account) | ||
* @return {Promise} - Promise contains Object from JSON decoded response | ||
*/ | ||
getPaymentAccount(accountId) { | ||
let apiUrl = this.apiConfig.getCoreApiBaseUrl() + '/v2/pay/account/' + accountId; | ||
let responsePromise = this.httpClient.request( | ||
'get', | ||
this.apiConfig.get().serverKey, | ||
apiUrl, | ||
null); | ||
return responsePromise; | ||
} | ||
/** | ||
* Unbind `/v2/pay/account/<accountId>/unbind` API request to Core API | ||
* @param {String} accountId - accountId for specific payment channel (more params detail refer to: https://api-docs.midtrans.com/#unbind-pay-account) | ||
* @return {Promise} - Promise contains Object from JSON decoded response | ||
*/ | ||
unlinkPaymentAccount(accountId) { | ||
let apiUrl = this.apiConfig.getCoreApiBaseUrl() + '/v2/pay/account/' + accountId + '/unbind'; | ||
let responsePromise = this.httpClient.request( | ||
'post', | ||
this.apiConfig.get().serverKey, | ||
apiUrl, | ||
null); | ||
return responsePromise; | ||
} | ||
/** | ||
* Create `/v1/subscription` API request to Core API | ||
* @param {Object} parameter - object of Core API JSON body as parameter, will be converted to JSON (more params detail refer to: https://api-docs.midtrans.com/#create-subscription) | ||
* @return {Promise} - Promise contains Object from JSON decoded response | ||
*/ | ||
createSubscription(parameter = {}) { | ||
let apiUrl = this.apiConfig.getCoreApiBaseUrl() + '/v1/subscriptions'; | ||
let responsePromise = this.httpClient.request( | ||
'post', | ||
this.apiConfig.get().serverKey, | ||
apiUrl, | ||
parameter); | ||
return responsePromise; | ||
} | ||
/** | ||
* Do `/v1/subscription/<subscriptionId>` API request to Core API | ||
* @param {String} subscriptionId - subscriptionId given by Midtrans (more params detail refer to: https://api-docs.midtrans.com/#get-subscription) | ||
* @return {Promise} - Promise contains Object from JSON decoded response | ||
*/ | ||
getSubscription(subscriptionId) { | ||
let apiUrl = this.apiConfig.getCoreApiBaseUrl() + '/v1/subscriptions/' + subscriptionId; | ||
let responsePromise = this.httpClient.request( | ||
'get', | ||
this.apiConfig.get().serverKey, | ||
apiUrl, | ||
null); | ||
return responsePromise; | ||
} | ||
/** | ||
* Do `/v1/subscription/<subscriptionId>/disable` API request to Core API | ||
* @param {String} subscriptionId - subscriptionId given by Midtrans (more params detail refer to: https://api-docs.midtrans.com/#disable-subscription) | ||
* @return {Promise} - Promise contains Object from JSON decoded response | ||
*/ | ||
disableSubscription(subscriptionId) { | ||
let apiUrl = this.apiConfig.getCoreApiBaseUrl() + '/v1/subscriptions/' + subscriptionId + '/disable'; | ||
let responsePromise = this.httpClient.request( | ||
'post', | ||
this.apiConfig.get().serverKey, | ||
apiUrl, | ||
null); | ||
return responsePromise; | ||
} | ||
/** | ||
* Do `/v1/subscription/<subscriptionId>/enable` API request to Core API | ||
* @param {String} subscriptionId - subscriptionId given by Midtrans (more params detail refer to: https://api-docs.midtrans.com/#enable-subscription) | ||
* @return {Promise} - Promise contains Object from JSON decoded response | ||
*/ | ||
enableSubscription(subscriptionId) { | ||
let apiUrl = this.apiConfig.getCoreApiBaseUrl() + '/v1/subscriptions/' + subscriptionId + '/enable'; | ||
let responsePromise = this.httpClient.request( | ||
'post', | ||
this.apiConfig.get().serverKey, | ||
apiUrl, | ||
null); | ||
return responsePromise; | ||
} | ||
/** | ||
* Do update subscription `/v1/subscription/<subscriptionId>` API request to Core API | ||
* @param {String} subscriptionId - subscriptionId given by Midtrans (more params detail refer to: https://api-docs.midtrans.com/#update-subscription) | ||
* @return {Promise} - Promise contains Object from JSON decoded response | ||
*/ | ||
updateSubscription(subscriptionId, parameter = {} ) { | ||
let apiUrl = this.apiConfig.getCoreApiBaseUrl() + '/v1/subscriptions/' + subscriptionId; | ||
let responsePromise = this.httpClient.request( | ||
'patch', | ||
this.apiConfig.get().serverKey, | ||
apiUrl, | ||
parameter); | ||
return responsePromise; | ||
} | ||
} | ||
module.exports = CoreApi; |
@@ -20,3 +20,3 @@ 'use strict' | ||
'accept': 'application/json', | ||
'user-agent': 'midtransclient-nodejs/1.2.5' | ||
'user-agent': 'midtransclient-nodejs/1.3.0' | ||
}; | ||
@@ -23,0 +23,0 @@ |
@@ -16,3 +16,3 @@ 'use strict' | ||
status(transactionId=''){ | ||
let apiUrl = this.parent.apiConfig.getCoreApiBaseUrl()+'/'+transactionId+'/status'; | ||
let apiUrl = this.parent.apiConfig.getCoreApiBaseUrl()+'/v2/'+transactionId+'/status'; | ||
let responsePromise = this.parent.httpClient.request( | ||
@@ -26,3 +26,3 @@ 'get', | ||
statusb2b(transactionId=''){ | ||
let apiUrl = this.parent.apiConfig.getCoreApiBaseUrl()+'/'+transactionId+'/status/b2b'; | ||
let apiUrl = this.parent.apiConfig.getCoreApiBaseUrl()+'/v2/'+transactionId+'/status/b2b'; | ||
let responsePromise = this.parent.httpClient.request( | ||
@@ -36,3 +36,3 @@ 'get', | ||
approve(transactionId=''){ | ||
let apiUrl = this.parent.apiConfig.getCoreApiBaseUrl()+'/'+transactionId+'/approve'; | ||
let apiUrl = this.parent.apiConfig.getCoreApiBaseUrl()+'/v2/'+transactionId+'/approve'; | ||
let responsePromise = this.parent.httpClient.request( | ||
@@ -46,3 +46,3 @@ 'post', | ||
deny(transactionId=''){ | ||
let apiUrl = this.parent.apiConfig.getCoreApiBaseUrl()+'/'+transactionId+'/deny'; | ||
let apiUrl = this.parent.apiConfig.getCoreApiBaseUrl()+'/v2/'+transactionId+'/deny'; | ||
let responsePromise = this.parent.httpClient.request( | ||
@@ -56,3 +56,3 @@ 'post', | ||
cancel(transactionId=''){ | ||
let apiUrl = this.parent.apiConfig.getCoreApiBaseUrl()+'/'+transactionId+'/cancel'; | ||
let apiUrl = this.parent.apiConfig.getCoreApiBaseUrl()+'/v2/'+transactionId+'/cancel'; | ||
let responsePromise = this.parent.httpClient.request( | ||
@@ -66,3 +66,3 @@ 'post', | ||
expire(transactionId=''){ | ||
let apiUrl = this.parent.apiConfig.getCoreApiBaseUrl()+'/'+transactionId+'/expire'; | ||
let apiUrl = this.parent.apiConfig.getCoreApiBaseUrl()+'/v2/'+transactionId+'/expire'; | ||
let responsePromise = this.parent.httpClient.request( | ||
@@ -76,3 +76,3 @@ 'post', | ||
refund(transactionId='',parameter={}){ | ||
let apiUrl = this.parent.apiConfig.getCoreApiBaseUrl()+'/'+transactionId+'/refund'; | ||
let apiUrl = this.parent.apiConfig.getCoreApiBaseUrl()+'/v2/'+transactionId+'/refund'; | ||
let responsePromise = this.parent.httpClient.request( | ||
@@ -86,3 +86,3 @@ 'post', | ||
refundDirect(transactionId='',parameter={}){ | ||
let apiUrl = this.parent.apiConfig.getCoreApiBaseUrl()+'/'+transactionId+'/refund/online/direct'; | ||
let apiUrl = this.parent.apiConfig.getCoreApiBaseUrl()+'/v2/'+transactionId+'/refund/online/direct'; | ||
let responsePromise = this.parent.httpClient.request( | ||
@@ -89,0 +89,0 @@ 'post', |
{ | ||
"name": "midtrans-client", | ||
"version": "1.2.5", | ||
"version": "1.3.0", | ||
"description": "Official Midtrans Payment API Client for Node JS", | ||
@@ -32,4 +32,4 @@ "main": "index.js", | ||
"chai": "^4.3.4", | ||
"mocha": "^9.1.1" | ||
"mocha": "^9.1.3" | ||
} | ||
} |
134
README.md
@@ -317,2 +317,126 @@ Midtrans Client - Node JS | ||
### 2.2.D Subscription API | ||
You can see some Subscription API examples [here](examples/subscription), [Subscription API Docs](https://api-docs.midtrans.com/#subscription-api) | ||
#### Subscription API for Credit Card | ||
To use subscription API for credit card, you should first obtain the 1-click saved 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) | ||
```javascript | ||
const midtransClient = require('midtrans-client'); | ||
// Create Core API / Snap instance (both have shared `transactions` methods) | ||
let core = new midtransClient.CoreAPi({ | ||
isProduction : false, | ||
serverKey : 'YOUR_SERVER_KEY', | ||
clientKey : 'YOUR_CLIENT_KEY' | ||
}); | ||
// prepare parameter | ||
let parameter = { | ||
"name": "MONTHLY_2021", | ||
"amount": "14000", | ||
"currency": "IDR", | ||
"payment_type": "credit_card", | ||
"token": "521111gmWqMegyejqCQmmopnCFRs1117", | ||
"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": "johndoe@email.com", | ||
"phone": "+62812345678" | ||
} | ||
}; | ||
core.createSubscription(parameter) | ||
.then((response)=>{ | ||
// do something to `response` object | ||
}); | ||
// get subscription by subscriptionId | ||
core.getSubscription(subscriptionId) | ||
.then((response)=>{ | ||
// do something to `response` object | ||
}); | ||
// enable subscription by subscriptionId | ||
core.enableSubscription(subscriptionId) | ||
.then((response)=>{ | ||
// do something to `response` object | ||
}); | ||
// update subscription by subscriptionId and updateSubscriptionParam | ||
let updateSubscriptionParam = { | ||
"name": "MONTHLY_2021", | ||
"amount": "300000", | ||
"currency": "IDR", | ||
"token": savedTokenId, | ||
"schedule": { | ||
"interval": 1 | ||
} | ||
} | ||
core.updateSubscription(subscriptionId, updateSubscriptionParam) | ||
.then((response)=>{ | ||
// do something to `response` object | ||
}); | ||
``` | ||
#### Subscription API for Gopay | ||
To use subscription API for gopay, you should first link your customer gopay account with gopay tokenization API, [refer to this section](#22e-tokenization-api) | ||
You will receive gopay payment token using `getPaymentAccount` API call | ||
You can see some Subscription API examples [here](examples/subscription) | ||
### 2.2.E Tokenization API | ||
You can see some Tokenization API examples [here](examples/tokenization), [Tokenization API Docs](https://api-docs.midtrans.com/#gopay-tokenization) | ||
```javascript | ||
const midtransClient = require('midtrans-client'); | ||
// Create Core API / Snap instance (both have shared `transactions` methods) | ||
let core = new midtransClient.CoreApi({ | ||
isProduction : false, | ||
serverKey : 'YOUR_SERVER_KEY', | ||
clientKey : 'YOUR_CLIENT_KEY' | ||
}); | ||
// prepare parameter | ||
let parameter = { | ||
"payment_type": "gopay", | ||
"gopay_partner": { | ||
"phone_number": "81212345678", | ||
"country_code": "62", | ||
"redirect_url": "https://www.gojek.com" | ||
} | ||
}; | ||
// link Payment Account | ||
core.linkPaymentAccount(parameter) | ||
.then((response)=>{ | ||
// do something to `response` object | ||
}); | ||
// Get payment account by account id | ||
core.getPaymentAccount(activeAccountId) | ||
.then((response)=>{ | ||
// do something to `response` object | ||
}); | ||
// unlink payment account by accountId | ||
core.unlinkPaymentAccount(activeAccountId) | ||
.then((response)=>{ | ||
// do something to `response` object | ||
}); | ||
``` | ||
### 2.3 Handle HTTP Notification | ||
@@ -653,2 +777,12 @@ | ||
## Notes | ||
#### Not Designed for Frontend Usage | ||
This library/package is mainly **NOT FOR FRONTEND** (Browser's javascript) usage, but for backend (Node JS server) usage: | ||
- This is mainly for backend usage, to do **secure server-to-server/backend-to-backend API call**. | ||
- You may/will encounter **CORS issue if you are using** this to do API request **from frontend**. | ||
- Your API **ServerKey may also be exposed to public** if you are using this **on frontend**. | ||
- If what you need is to display Snap payment page on your frontend, please [follow this official documentation](https://docs.midtrans.com/en/snap/integration-guide?id=_2-displaying-snap-payment-page-on-frontend) | ||
- If you are using [ReactJS follow this recommendation](https://docs.midtrans.com/en/other/faq/technical?id=my-developer-uses-react-js-frontend-framework-and-is-unable-to-use-midtransminjssnapjs-what-should-i-do). | ||
## Get help | ||
@@ -655,0 +789,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
61938
16
785
793
1