Skip to content

Commit

Permalink
Merge 535ad22 into a63faa1
Browse files Browse the repository at this point in the history
  • Loading branch information
KostiantynKobziev committed Nov 19, 2019
2 parents a63faa1 + 535ad22 commit 4e6e2cc
Show file tree
Hide file tree
Showing 2 changed files with 207 additions and 0 deletions.
54 changes: 54 additions & 0 deletions src/Hyperwallet.js
Expand Up @@ -798,6 +798,60 @@ export default class Hyperwallet {
this.client.doPost(`transfers/${encodeURIComponent(transferToken)}/status-transitions`, data, {}, callback);
}

//--------------------------------------
// Transfer Refunds
//--------------------------------------

/**
* Create a transfer refund
*
* @param {string} transferToken - The transfer token
* @param {Object} data - The transfer refund data
* @param {api-callback} callback - The callback for this call
*/
createTransferRefund(transferToken, data, callback) {
if (!transferToken) {
throw new Error("transferToken is required");
}
if (!data.clientTransferId) {
throw new Error("clientTransferId is required");
}
this.client.doPost(`transfers/${encodeURIComponent(transferToken)}/refunds`, data, {}, callback);
}

/**
* Get a transfer
*
* @param {string} transferToken - The transfer token
* @param {string} transferRefundToken - The transfer refund token
* @param {api-callback} callback - The callback for this call
*
* @throws Will throw an error if transferToken is not provided
*/
getTransferRefund(transferToken, transferRefundToken, callback) {
if (!transferToken) {
throw new Error("transferToken is required");
}
if (!transferRefundToken) {
throw new Error("transferRefundToken is required");
}
this.client.doGet(`transfers/${encodeURIComponent(transferToken)}/refunds/${encodeURIComponent(transferRefundToken)}`, {}, callback);
}

/**
* List all transfers
*
* @param {string} transferToken - The transfer token
* @param {Object} options - The query parameters to send
* @param {api-callback} callback - The callback for this call
*/
listTransferRefunds(transferToken, options, callback) {
if (!transferToken) {
throw new Error("transferToken is required");
}
this.client.doGet(`transfers/${encodeURIComponent(transferToken)}/refunds`, options, Hyperwallet.handle204Response(callback));
}

//--------------------------------------
// PayPal Accounts
//--------------------------------------
Expand Down
153 changes: 153 additions & 0 deletions test/Hyperwallet.spec.js
Expand Up @@ -1359,6 +1359,159 @@ describe("Hyperwallet", () => {
});
});

//--------------------------------------
// Transfer Refunds
//--------------------------------------

/** @test {Hyperwallet#createTransferRefund} */
describe("createTransferRefund()", () => {
let client;
let apiClientSpy;

beforeEach(() => {
apiClientSpy = sinon.spy();
client = new Hyperwallet({
username: "test-username",
password: "test-password",
});
client.client = {
doPost: apiClientSpy,
};
});

/** @test {Hyperwallet#createTransferRefund} */
it("should throw error if transferToken is missing", () => {
const callback = () => null;
expect(() => client.createTransferRefund(undefined, {
clientTransferId: "value",
}, callback)).to.throw("transferToken is required");
});

/** @test {Hyperwallet#createTransferRefund} */
it("should throw error if clientTransferId is missing", () => {
const callback = () => null;
expect(() => client.createTransferRefund("test-transfer-token", {
test: "value",
}, callback)).to.throw("clientTransferId is required");
});

/** @test {Hyperwallet#createTransferRefund} */
it("should do post call to transfer refunds endpoint", () => {
const callback = () => null;
client.createTransferRefund("test-transfer-token", {
clientTransferId: "clientTransferId",
}, callback);

apiClientSpy.should.have.been.calledOnce();
apiClientSpy.should.have.been.calledWith("transfers/test-transfer-token/refunds", {
clientTransferId: "clientTransferId",
}, {}, callback);
});
});

/** @test {Hyperwallet#getTransferRefund} */
describe("getTransferRefund()", () => {
let client;
let apiClientSpy;

beforeEach(() => {
apiClientSpy = sinon.spy();
client = new Hyperwallet({
username: "test-username",
password: "test-password",
});
client.client = {
doGet: apiClientSpy,
};
});

/** @test {Hyperwallet#getTransferRefund} */
it("should throw error if transferToken is missing", () => {
const callback = () => null;
expect(() => client.getTransferRefund(undefined, "test-transfer-refund-token",
callback)).to.throw("transferToken is required");
});

/** @test {Hyperwallet#getTransferRefund} */
it("should throw error if transferRefundToken is missing", () => {
const callback = () => null;
expect(() => client.getTransferRefund("test-transfer-token", undefined,
callback)).to.throw("transferRefundToken is required");
});

/** @test {Hyperwallet#getTransferRefund} */
it("should do get call if transferToken is provided", () => {
const callback = () => null;
client.getTransferRefund("test-transfer-token", "test-transfer-refund-token", callback);

apiClientSpy.should.have.been.calledOnce();
apiClientSpy.should.have.been.calledWith("transfers/test-transfer-token/refunds/test-transfer-refund-token", {}, callback);
});
});

/** @test {Hyperwallet#listTransferRefunds} */
describe("listTransferRefunds()", () => {
let client;
let apiClientSpy;

beforeEach(() => {
apiClientSpy = sinon.spy();
client = new Hyperwallet({
username: "test-username",
password: "test-password",
});
client.client = {
doGet: apiClientSpy,
};
});

/** @test {Hyperwallet#listTransferRefunds} */
it("should throw error if transferToken is missing", () => {
const callback = () => null;
expect(() => client.listTransferRefunds(undefined, {
test: "value",
}, callback)).to.throw("transferToken is required");
});

/** @test {Hyperwallet#listTransferRefunds} */
it("should do get call with options", () => {
const callback = () => null;
client.listTransferRefunds("test-transfer-token", { test: "value" }, callback);

apiClientSpy.should.have.been.calledOnce();
apiClientSpy.should.have.been.calledWith("transfers/test-transfer-token/refunds", { test: "value" });
});

/** @test {Hyperwallet#listTransferRefunds} */
it("should do get call without options", () => {
const callback = () => null;
client.listTransferRefunds("test-transfer-token", {}, callback);

apiClientSpy.should.have.been.calledOnce();
apiClientSpy.should.have.been.calledWith("transfers/test-transfer-token/refunds", {});
});

/** @test {Hyperwallet#listTransferRefunds} */
it("should handle 204 return", (cb) => {
const callback = (err, data) => {
data.should.be.deep.equal({
count: 0,
data: [],
});

cb();
};
client.listTransferRefunds("test-transfer-token", {}, callback);

apiClientSpy.should.have.been.calledOnce();
apiClientSpy.should.have.been.calledWith("transfers/test-transfer-token/refunds", {});

apiClientSpy.getCall(0).args[2](undefined, {}, {
status: 204,
});
});
});

//--------------------------------------
// PayPal accounts
//--------------------------------------
Expand Down

0 comments on commit 4e6e2cc

Please sign in to comment.