-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/xendit/xendit-node
- Loading branch information
Showing
13 changed files
with
472 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
const x = require('../xendit'); | ||
|
||
const { Refund } = x; | ||
const r = new Refund({}); | ||
|
||
(async function() { | ||
try { | ||
let refund = await r.createRefund({ | ||
invoice_id: '63676ed0eb10cf38ce0550b7', | ||
reason: 'OTHERS', | ||
amount: 1, | ||
}); | ||
console.log('created refund', refund); // eslint-disable-line no-console | ||
|
||
const refundDetails = await r.getRefundById({ id: refund.id }); | ||
// eslint-disable-next-line no-console | ||
console.log('retrieved refund', refundDetails); | ||
|
||
const refundList = await r.listRefunds({}); | ||
// eslint-disable-next-line no-console | ||
console.log('list of refunds', refundList); | ||
|
||
process.exit(0); | ||
} catch (e) { | ||
console.error(e); // eslint-disable-line no-console | ||
process.exit(1); | ||
} | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
const x = require('../xendit'); | ||
|
||
const Refund = x.Refund; | ||
const ref = new Refund(); | ||
|
||
ref | ||
.createRefund({ | ||
invoice_id: '63676ed0eb10cf38ce0550b7', | ||
reason: 'OTHERS', | ||
amount: 1, | ||
}) | ||
.then(r => { | ||
// eslint-disable-next-line no-console | ||
console.log('refund created:', r); | ||
return r; | ||
}) | ||
.then(({ id }) => ref.getRefundById({ id })) | ||
.then(r => { | ||
// eslint-disable-next-line no-console | ||
console.log('refund details:', r); | ||
return r; | ||
}) | ||
.then(() => { | ||
return ref.listRefunds({}); | ||
}) | ||
.then(r => { | ||
// eslint-disable-next-line no-console | ||
console.log(':', r); | ||
return r; | ||
}) | ||
.catch(e => { | ||
console.error(e); // eslint-disable-line no-console | ||
process.exit(1); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
const x = require('./xendit.test'); | ||
|
||
const { Refund } = x; | ||
const r = new Refund({}); | ||
|
||
module.exports = function() { | ||
return r | ||
.createRefund({ | ||
invoice_id: '63676ed0eb10cf38ce0550b7', | ||
reason: 'FRAUDULENT', | ||
amount: 1, | ||
}) | ||
.then(id => { | ||
r.getRefundById({ id }); | ||
}) | ||
.then(() => { | ||
r.listRefunds({}); | ||
}) | ||
.then(() => { | ||
// eslint-disable-next-line no-console | ||
console.log('QR Code integration test done...'); | ||
}) | ||
.catch(e => { | ||
throw new Error( | ||
`Recurring integration tests failed with error: ${e.message}`, | ||
); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import RefundService from './refund'; | ||
|
||
export { RefundService }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
const RefundService = require('./refund'); | ||
|
||
module.exports = { RefundService }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
enum RefundReasons { | ||
Fraudulent = 'FRAUDULENT', | ||
Duplicate = 'DUPLICATE', | ||
RequestedByCustomer = 'REQUESTED_BY_CUSTOMER', | ||
Cancellation = 'CANCELLATION', | ||
Others = 'OTHERS', | ||
} | ||
|
||
export = class Refund { | ||
constructor({}); | ||
static _constructorWithInjectedXenditOpts: ( | ||
opts: XenditOptions, | ||
) => typeof Refund; | ||
|
||
createRefund(data: { | ||
payment_request_id?: string; | ||
reference_id?: string; | ||
invoice_id?: string; | ||
currency?: string; | ||
amount?: number; | ||
reason: RefundReasons; | ||
metadata?: object; | ||
idempotencty_key?: string; | ||
for_user_id?: string; | ||
}): Promise<object>; | ||
|
||
listRefunds(data: { | ||
payment_request_id?: string; | ||
invoice_id?: string; | ||
payment_method_type?: string; | ||
channel_code?: string; | ||
limit?: number; | ||
after_id?: string; | ||
before_id?: string; | ||
for_user_id?: string; | ||
}): Promise<object>; | ||
|
||
getRefundById(data: { id: string }): Promise<object>; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
const { | ||
promWithJsErr, | ||
Validate, | ||
fetchWithHTTPErr, | ||
Auth, | ||
queryStringWithoutUndefined, | ||
} = require('../utils'); | ||
|
||
const REFUND_PATH = '/refunds'; | ||
|
||
function Refund(options) { | ||
let aggOpts = options; | ||
if (Refund._injectedOpts && Object.keys(Refund._injectedOpts).length > 0) { | ||
aggOpts = Object.assign({}, options, Refund._injectedOpts); | ||
} | ||
|
||
this.opts = aggOpts; | ||
this.API_ENDPOINT = this.opts.xenditURL + REFUND_PATH; | ||
} | ||
|
||
Refund._injectedOpts = {}; | ||
Refund._constructorWithInjectedXenditOpts = function(options) { | ||
Refund._injectedOpts = options; | ||
return Refund; | ||
}; | ||
|
||
Refund.prototype.createRefund = function(data) { | ||
return promWithJsErr((resolve, reject) => { | ||
Validate.rejectOnMissingFields(['reason', 'amount'], data, reject); | ||
|
||
let headers = { | ||
Authorization: Auth.basicAuthHeader(this.opts.secretKey), | ||
'Content-Type': 'application/json', | ||
}; | ||
|
||
if (data && data.for_user_id) { | ||
headers['for-user-id'] = data.for_user_id; | ||
} | ||
|
||
if (data && data.idempotency_key) { | ||
headers['idempotency-key'] = data.idempotency_key; | ||
} | ||
|
||
fetchWithHTTPErr(`${this.API_ENDPOINT}`, { | ||
method: 'POST', | ||
headers, | ||
body: JSON.stringify({ | ||
payment_request_id: data.payment_request_id, | ||
reference_id: data.reference_id, | ||
invoice_id: data.invoice_id, | ||
currency: data.currency, | ||
amount: data.amount, | ||
reason: data.reason, | ||
metadata: data.metadata, | ||
}), | ||
}) | ||
.then(resolve) | ||
.catch(reject); | ||
}); | ||
}; | ||
|
||
Refund.prototype.getRefundById = function(data) { | ||
return promWithJsErr((resolve, reject) => { | ||
Validate.rejectOnMissingFields(['id'], data, reject); | ||
|
||
let headers = { | ||
Authorization: Auth.basicAuthHeader(this.opts.secretKey), | ||
'Content-Type': 'application/json', | ||
}; | ||
|
||
if (data && data.for_user_id) { | ||
headers['for-user-id'] = data.forUserID; | ||
} | ||
|
||
if (data && data.idempotency_key) { | ||
headers['idempotency-key'] = data.idempotency_key; | ||
} | ||
|
||
fetchWithHTTPErr(`${this.API_ENDPOINT}/${data.id}`, { | ||
method: 'GET', | ||
headers, | ||
}) | ||
.then(resolve) | ||
.catch(reject); | ||
}); | ||
}; | ||
|
||
Refund.prototype.listRefunds = function(data) { | ||
return promWithJsErr((resolve, reject) => { | ||
Validate.rejectOnMissingFields([], data, reject); | ||
|
||
let headers = { | ||
Authorization: Auth.basicAuthHeader(this.opts.secretKey), | ||
'Content-Type': 'application/json', | ||
}; | ||
|
||
if (data && data.for_user_id) { | ||
headers['for-user-id'] = data.for_user_id; | ||
} | ||
|
||
const queryStr = data | ||
? queryStringWithoutUndefined({ | ||
payment_request_id: data.payment_request_id | ||
? data.payment_request_id | ||
: undefined, | ||
invoice_id: data.invoice_id ? data.invoice_id : undefined, | ||
payment_method_id: data.payment_method_id | ||
? data.payment_method_id | ||
: undefined, | ||
channel_code: data.channel_code ? data.channel_code : undefined, | ||
limit: data.limit ? data.limit : undefined, | ||
after_id: data.after_id ? data.after_id : undefined, | ||
before_id: data.before_id ? data.before_id : undefined, | ||
}) | ||
: ''; | ||
|
||
const queryStrWithQuestionMark = queryStr ? `?${queryStr}` : ''; | ||
|
||
fetchWithHTTPErr(`${this.API_ENDPOINT}${queryStrWithQuestionMark}`, { | ||
method: 'GET', | ||
headers, | ||
}) | ||
.then(resolve) | ||
.catch(reject); | ||
}); | ||
}; | ||
|
||
module.exports = Refund; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.