Skip to content

Commit

Permalink
add refund partially (#12)
Browse files Browse the repository at this point in the history
* add refund partially

* update version

* Update .travis.yml
  • Loading branch information
AbdullahDahmash authored and ecleel committed Aug 27, 2018
1 parent b34ff88 commit 8482dc3
Show file tree
Hide file tree
Showing 7 changed files with 1,937 additions and 16 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ lib
*.swp
npm-debug.log
test.config.json
.DS_Store
5 changes: 1 addition & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
language: node_js
node_js:
- '6'
- '6.1'
- '5.11'
- iojs
- 'node'
deploy:
provider: npm
email: [email protected]
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "moyasar",
"version": "0.4.4",
"version": "0.4.5",
"description": "Moyasar node SDK",
"main": "lib/moyasar.js",
"scripts": {
Expand Down Expand Up @@ -28,7 +28,7 @@
"babel-preset-stage-2": "^6.5.0",
"babel-register": "^6.8.0",
"mocha": "^2.4.5",
"nock": "^8.1.0"
"nock": "^9.0.14"
},
"dependencies": {
"request-promise": "^3.0.0"
Expand Down
15 changes: 12 additions & 3 deletions src/payment.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,25 @@ export default class extends Service {
return this.sendRequest('payments/'+id);
}

refund(receipt){
refund(receipt, option){
let id;
let params = {};
if (typeof receipt == "object" && receipt.id)
id = receipt.id;
else if (typeof receipt == "string")
id = receipt;
else
throw new Error("Please provide a valid payment object or payment id");

return this.sendRequest('payments/'+id+'/refund','POST');
if (option && option.amount) {
if (!isNaN(parseFloat(option.amount)) && isFinite(option.amount)) {
params = {
amount: option.amount,
};
} else {
throw new Error("Please provide a valid amount");
}
}
return this.sendRequest('payments/'+id+'/refund','POST', params);

}

Expand Down
7 changes: 0 additions & 7 deletions test.config.json

This file was deleted.

58 changes: 58 additions & 0 deletions test/payment.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,64 @@ describe('Payment API', () => {
})
})

it('Retreive a payment and refund it partially', done => {
fakeServer.get('/payments/d256ac99-ada1-5ef3-ab00-8e837b54ad5f').basicAuth(basicAuth).reply(200, {
"id": "d256ac99-ada1-5ef3-ab00-8e837b54ad5f",
"status": "paid",
"amount": 23097,
"fee": 0,
"currency": "SAR",
"refunded": 0,
"refunded_at": null,
"description": null,
"amount_format": "230.97 SAR",
"fee_format": "0.00 SAR",
"invoice_id": "495e3cfd-abe1-5c48-bb05-aeb009548830",
"ip": null,
"created_at": "2016-03-21T19:25:35.093Z",
"updated_at": "2016-03-21T19:25:35.093Z",
"source": {
"type": "creditcard",
"company": "visa",
"name": "Abdulaziz AlShetwi",
"number": "XXXX-XXXX-XXXX-1111",
"message": null
}
});
fakeServer.post('/payments/d256ac99-ada1-5ef3-ab00-8e837b54ad5f/refund', { amount: 300 }).basicAuth(basicAuth).reply(200, {
id: 'd256ac99-ada1-5ef3-ab00-8e837b54ad5f',
status: 'refunded',
amount: 23097,
fee: 0,
currency: "SAR",
refunded: 300,
refunded_at: '2016-06-25T22:09:52.467Z',
description: null,
amount_format: "230.97 SAR",
fee_format: "0.00 SAR",
invoice_id: "495e3cfd-abe1-5c48-bb05-aeb009548830",
ip: null,
created_at: "2016-03-21T19:25:35.093Z",
updated_at: "2016-03-21T19:25:35.093Z",
source: {
type: "creditcard",
company: "visa",
name: "Abdulaziz AlShetwi",
number: "XXXX-XXXX-XXXX-1111",
message: null
}
});
moyasar.payment.fetch("d256ac99-ada1-5ef3-ab00-8e837b54ad5f").then(p => {
return moyasar.payment.refund(p, { amount: 300 }).then(r => {
assert(r.id);
assert(r.refunded); // not zero
done();
fakeServer.isDone();
return r;
});
})
})

it('Fetch and update payment', done => {
fakeServer.get('/payments/d256ac99-ada1-5ef3-ab00-8e837b54ad5f').basicAuth(basicAuth).reply(200, {
"id": "d256ac99-ada1-5ef3-ab00-8e837b54ad5f",
Expand Down
Loading

0 comments on commit 8482dc3

Please sign in to comment.