Skip to content

Commit

Permalink
Merge pull request #14 from Midtrans/add-direct-refund-api-function
Browse files Browse the repository at this point in the history
- Add direct refund api function
- Node v7 support dropped, due to Mocha dependency update not compatible
- Fix test and travis to support proper node version
  • Loading branch information
rizdaprasetya authored Jan 30, 2020
2 parents faf8a73 + c018073 commit 5057fa5
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 16 deletions.
6 changes: 4 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ language: node_js
node_js:
- "node"
- "lts/*"
- "9"
# - "14" # Node v14 not yet available on travis apparently
- "12"
- "10"
- "8"
- "7"
# - "7" # Node v7 is no longer officially supported by NodeJS
install:
- npm install --dev
script:
Expand Down
2 changes: 1 addition & 1 deletion Maintaining.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
## Updating Package

- Make your changes
- Update version on `package.json` file (and `user-agent` header version on `httpClient.js` file)
- Update `version` value on `package.json` file (and `user-agent` header version on `httpClient.js` file)
- To install dev dependencies `npm install --dev` on repo folder
- To run test, run `npm test` or `mocha`
- To run specific test
Expand Down
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,7 @@ apiClient.transaction.expire('YOUR_ORDER_ID OR TRANSACTION_ID')
#### Refund Transaction
```javascript
let parameter = {
"refund_key": "order1-ref1",
"amount": 5000,
"reason": "Item out of stock"
}
Expand All @@ -416,6 +417,18 @@ apiClient.transaction.refund('YOUR_ORDER_ID OR TRANSACTION_ID',parameter)
// do something to `response` object
});
```
#### Refund Transaction with Direct Refund
```javascript
let parameter = {
"refund_key": "order1-ref1",
"amount": 5000,
"reason": "Item out of stock"
}
apiClient.transaction.refundDirect('YOUR_ORDER_ID OR TRANSACTION_ID',parameter)
.then((response)=>{
// do something to `response` object
});
```

## 3. Handling Error / Exception
When using function that result in Midtrans API call e.g: `core.charge(...)` or `snap.createTransaction(...)`
Expand Down
2 changes: 1 addition & 1 deletion lib/httpClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class HttpClient{
let headers = {
'content-type': 'application/json',
'accept': 'application/json',
'user-agent': 'midtransclient-nodejs/1.0.3'
'user-agent': 'midtransclient-nodejs/1.1.0'
};
return new Promise(function(resolve,reject){
// Reject if param is not JSON
Expand Down
9 changes: 9 additions & 0 deletions lib/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,15 @@ class Transaction{
parameter);
return responsePromise;
}
refundDirect(transactionId='',parameter={}){
let apiUrl = this.parent.apiConfig.getCoreApiBaseUrl()+'/'+transactionId+'/refund/online/direct';
let responsePromise = this.parent.httpClient.request(
'post',
this.parent.apiConfig.get().serverKey,
apiUrl,
parameter);
return responsePromise;
}
notification(notificationObj={}){
let self = this;
return new Promise(function(resolve,reject){
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "midtrans-client",
"version": "1.0.3",
"version": "1.1.0",
"description": "Official Midtrans Payment API Client for Node JS",
"main": "index.js",
"scripts": {
Expand All @@ -24,11 +24,11 @@
"author": "rizda.prasetya@midtrans.com",
"license": "MIT",
"dependencies": {
"axios": "^0.18.0",
"axios": "^0.19.2",
"lodash": "^4.17.11"
},
"devDependencies": {
"chai": "^4.2.0",
"mocha": "^5.2.0"
"mocha": "^7.0.1"
}
}
28 changes: 22 additions & 6 deletions test/coreApi.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ const cons = require('./sharedConstants');
let tokenId = '';
let savedTokenId = '';
let reuseOrderId = [
"node-midtransclient-test1-"+Math.round((new Date()).getTime() / 1000),
"node-midtransclient-test2-"+Math.round((new Date()).getTime() / 1000),
"node-midtransclient-test3-"+Math.round((new Date()).getTime() / 1000),
"node-midtransclient-test1-"+generateTimestamp(),
"node-midtransclient-test2-"+generateTimestamp(),
"node-midtransclient-test3-"+generateTimestamp(),
];
let apiResponse = {};

Expand Down Expand Up @@ -196,6 +196,18 @@ describe('CoreApi.js',()=> {
})
})

it('fail to direct refund non settlement transaction',()=>{
let core = new CoreApi(generateConfig());
let parameter = {
"amount": 5000,
"reason": "for some reason"
}
return core.transaction.refundDirect(reuseOrderId[2],parameter)
.catch((e)=>{
expect(e.message).to.includes('412');
})
})

it('fail to status 404 non exists transaction',()=>{
let core = new CoreApi(generateConfig());
return core.transaction.status('non-exists-transaction')
Expand Down Expand Up @@ -275,6 +287,10 @@ describe('CoreApi.js',()=> {
* Helper functions
*/

function generateTimestamp(devider=1){
return Math.round((new Date()).getTime() / devider);
}

function generateConfig(){
return {
isProduction: false,
Expand All @@ -288,7 +304,7 @@ function generateParamMin(orderId=null){
"payment_type": "bank_transfer",
"transaction_details": {
"gross_amount": 44145,
"order_id": orderId == null ? "node-midtransclient-test-"+Math.round((new Date()).getTime() / 1000) : orderId,
"order_id": orderId == null ? "node-midtransclient-test-"+generateTimestamp() : orderId,
},
"bank_transfer":{
"bank": "bca"
Expand All @@ -301,7 +317,7 @@ function generateCCParamMin(orderId=null,tokenId=null){
"payment_type": "credit_card",
"transaction_details": {
"gross_amount": 12145,
"order_id": orderId == null ? "node-midtransclient-test-"+Math.round((new Date()).getTime() / 1000) : orderId,
"order_id": orderId == null ? "node-midtransclient-test-"+generateTimestamp() : orderId,
},
"credit_card":{
"token_id": tokenId
Expand All @@ -312,7 +328,7 @@ function generateCCParamMin(orderId=null,tokenId=null){
function generateParamMax(){
return {
"transaction_details": {
"order_id": "node-midtransclient-test-"+Math.round((new Date()).getTime() / 1000),
"order_id": "node-midtransclient-test-"+generateTimestamp(),
"gross_amount": 10000
},
"item_details": [{
Expand Down
2 changes: 1 addition & 1 deletion test/httpClient.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const cons = require('./sharedConstants');
function generateParamMin(){
return {
"transaction_details": {
"order_id": "node-midtransclient-test-"+Math.round((new Date()).getTime() / 1000),
"order_id": "node-midtransclient-test-"+Math.round((new Date()).getTime() / 1),
"gross_amount": 200000
}, "credit_card":{
"secure" : true
Expand Down
8 changes: 6 additions & 2 deletions test/snap.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,10 @@ describe('Snap.js',()=> {

})

function generateTimestamp(devider=1){
return Math.round((new Date()).getTime() / devider);
}

function generateConfig(){
return {
isProduction: false,
Expand All @@ -181,7 +185,7 @@ function generateConfig(){
function generateParamMin(){
return {
"transaction_details": {
"order_id": "node-midtransclient-test-"+Math.round((new Date()).getTime() / 1000),
"order_id": "node-midtransclient-test-"+generateTimestamp(),
"gross_amount": 200000
}, "credit_card":{
"secure" : true
Expand All @@ -192,7 +196,7 @@ function generateParamMin(){
function generateParamMax(){
return {
"transaction_details": {
"order_id": "node-midtransclient-test-"+Math.round((new Date()).getTime() / 1000),
"order_id": "node-midtransclient-test-"+generateTimestamp(),
"gross_amount": 10000
},
"item_details": [{
Expand Down

0 comments on commit 5057fa5

Please sign in to comment.