Skip to content

Commit 7eecb81

Browse files
authored
Merge pull request #8 from PaymentRails/offline-payments-support
added offline payments support
2 parents ea4454e + 8dfecee commit 7eecb81

File tree

8 files changed

+293
-12
lines changed

8 files changed

+293
-12
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ node_modules
22
Gruntfile
33
gulpfile
44
main.ts
5+
main.js
56
bower
67
.DS_Store
78
__MACOSX

lib/Gateway.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { BatchGateway } from "./BatchGateway";
55
import { PaymentGateway } from "./PaymentGateway";
66
import { BalancesGateway } from "./BalancesGateway";
77
import { Client } from "./Client";
8+
import { OfflinePaymentGateway } from "./OfflinepaymentGateway";
89

910
export class Gateway {
1011
config: Configuration;
@@ -14,6 +15,7 @@ export class Gateway {
1415
recipientAccount: RecipientAccountGateway;
1516
balances: BalancesGateway;
1617
payment: PaymentGateway;
18+
offlinePayment: OfflinePaymentGateway;
1719

1820
/**
1921
* This should be called by the connect() method to setup a client gateway
@@ -28,5 +30,6 @@ export class Gateway {
2830
this.recipientAccount = new RecipientAccountGateway(this);
2931
this.balances = new BalancesGateway(this);
3032
this.payment = new PaymentGateway(this);
33+
this.offlinePayment = new OfflinePaymentGateway(this);
3134
}
3235
}

lib/OfflinePayment.ts

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import { Configuration } from "./Configuration";
2+
import { Recipient } from "./Recipient";
3+
import * as types from "./types";
4+
5+
// tslint:disable:function-name
6+
/**
7+
* @name OfflinePayment
8+
*/
9+
export class OfflinePayment {
10+
id: string = "";
11+
recipientId: Recipient["id"] = "";
12+
13+
externalId: string | null = "";
14+
memo: string = "";
15+
tags: string[] = [];
16+
taxReportable: boolean = true;
17+
category: string = "";
18+
amount: string = "";
19+
currency: string = "";
20+
withholdingAmount: string = "";
21+
withholdingCurrency: string = "";
22+
processedAt: string = "";
23+
24+
/**
25+
* Creates an offline payment
26+
* @param {string} recipientId
27+
* @param {array} body
28+
* @hidden
29+
*/
30+
static async create(recipientId: string, body: any) {
31+
return Configuration.gateway().offlinePayment.create(recipientId, body);
32+
}
33+
34+
/**
35+
* Updates an offline payment based on the offline payment id
36+
* @param {string} offlinePaymentId
37+
* @param {string} recipientId
38+
* @param {array} body
39+
* @hidden
40+
*/
41+
static async update(
42+
offlinePaymentId: string,
43+
recipientId: string,
44+
body: any,
45+
) {
46+
return Configuration.gateway().offlinePayment.update(
47+
recipientId,
48+
offlinePaymentId,
49+
body,
50+
);
51+
}
52+
53+
/**
54+
* Delete an offline payment based on the offline payment id and the recipient's ID that it's associated with
55+
* @param {string} offlinePaymentId
56+
* @param {string} recipientId
57+
* @hidden
58+
*/
59+
static async remove(offlinePaymentId: string, recipientId: string) {
60+
return Configuration.gateway().offlinePayment.remove(
61+
offlinePaymentId,
62+
recipientId,
63+
);
64+
}
65+
66+
/**
67+
* List all offline payments
68+
* (optional) a given wildcard, page amount and page size
69+
* @param {int} page
70+
* @param {int} pageSize
71+
* @param {string} search
72+
* @hidden
73+
*/
74+
static async search(
75+
query: { [key: string]: string },
76+
page: number = 1,
77+
pageSize: number = 10,
78+
term: string = "",
79+
) {
80+
const data = await Configuration.gateway().offlinePayment.search(
81+
query,
82+
page,
83+
pageSize,
84+
term,
85+
);
86+
const jsonObj: any = JSON.parse(data.toString());
87+
88+
return <types.Payment.ListResult>jsonObj;
89+
}
90+
91+
/**
92+
* Should only be called by the Gateway
93+
* @hidden
94+
*/
95+
static factory(offlinePayment: types.OfflinePayment.OfflinePayment) {
96+
const instance = new OfflinePayment();
97+
instance._initialize(offlinePayment);
98+
99+
return instance;
100+
}
101+
102+
/**
103+
* @hidden
104+
*/
105+
private _initialize(offlinePayment: types.OfflinePayment.OfflinePayment) {
106+
Object.keys(offlinePayment).forEach(k => {
107+
(this as any)[k] = (offlinePayment as any)[k];
108+
});
109+
}
110+
}

lib/OfflinePaymentGateway.ts

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
import { Configuration } from "./Configuration";
2+
import { Gateway } from "./Gateway";
3+
import { OfflinePayment } from "./OfflinePayment";
4+
import { buildURL } from "./util";
5+
import * as querystring from "querystring";
6+
import * as types from "./types";
7+
8+
export class OfflinePaymentGateway {
9+
/**
10+
* @hidden
11+
*/
12+
gateway: Gateway;
13+
/**
14+
* @hidden
15+
*/
16+
config: Configuration;
17+
18+
/**
19+
* @param gateway
20+
* @hidden
21+
*/
22+
constructor(gateway: Gateway) {
23+
this.gateway = gateway;
24+
this.config = this.gateway.config;
25+
}
26+
27+
/**
28+
* Create a new offline payment
29+
* ```
30+
* const payment = await client.offlinePayment.create('OP-xx99bb', {
31+
* taxReportable: true,
32+
* amount: "10.00",
33+
* currency: "USD",
34+
* withholdingAmount: "2.40",
35+
* withholdingCurrency: "USD",
36+
* });
37+
* ```
38+
* @param recipientId Payment Rails recipient id (e.g. "R-xx999bb")
39+
* @param body Offline Payment information
40+
*/
41+
async create(recipientId: string, body: any) {
42+
const endPoint = buildURL("recipients", recipientId, "offlinePayments");
43+
44+
const result = await this.gateway.client.post<
45+
types.OfflinePayment.Response
46+
>(endPoint, body);
47+
48+
return OfflinePayment.factory(result.offlinePayment);
49+
}
50+
51+
/**
52+
* Update a given offline payment
53+
* ```
54+
* const success = await client.offlinePayment.update('R-aabbccc', 'OP-xx99bb', {
55+
* taxReportable: true,
56+
* amount: "10.00",
57+
* currency: "USD",
58+
* withholdingAmount: "2.40",
59+
* withholdingCurrency: "USD",
60+
* });
61+
* ```
62+
* @param recipientId Payment Rails recipient id (e.g. "R-aabccc")
63+
* @param offlinePaymentId Payment Rails offline payment id (e.g. "OP-xx999bb")
64+
* @param body Payment update information
65+
*/
66+
async update(recipientId: string, offlinePaymentId: string, body: any) {
67+
const endPoint = buildURL(
68+
"recipients",
69+
recipientId,
70+
"offlinePayments",
71+
offlinePaymentId,
72+
);
73+
74+
const result = await this.gateway.client.patch<{ ok: boolean }>(
75+
endPoint,
76+
body,
77+
);
78+
79+
return true;
80+
}
81+
82+
/**
83+
* Delete a given offline payment
84+
* ```
85+
* const success = await client.offlinePayment.remove('R-aabbccc', 'OP-xx99bb');
86+
* ```
87+
* @param recipientId Payment Rails recipient id (e.g. "R-aabccc")
88+
* @param offlinePaymentId Payment Rails offline payment id (e.g. "OP-xx999bb")
89+
*/
90+
async remove(recipientId: string, offlinePaymentId: string) {
91+
const endPoint = buildURL(
92+
"recipients",
93+
recipientId,
94+
"offlinePayments",
95+
offlinePaymentId,
96+
);
97+
98+
const result = await this.gateway.client.remove<{ ok: boolean }>(endPoint);
99+
100+
return true;
101+
}
102+
103+
/**
104+
* Search for offline payments
105+
* @param query Object containing either search key, usually either "recipientId"
106+
* @param page Page number (1 based)
107+
* @param pageSize Page size (0...1000)
108+
* @param term Any search terms to look for
109+
*/
110+
async search(
111+
query: { [key: string]: string },
112+
page: number = 1,
113+
pageSize: number = 10,
114+
term: string = "",
115+
) {
116+
// tslint:disable-next-line:max-line-length
117+
118+
const endPoint = !!(query && query.recipientId)
119+
? buildURL("recipients", query.recipientId, "offline-payments")
120+
: buildURL("offline-payments");
121+
122+
const urlQuery = querystring.stringify({
123+
...query,
124+
page,
125+
pageSize,
126+
search: term,
127+
});
128+
129+
const result = await this.gateway.client.get<
130+
types.OfflinePayment.ListResponse
131+
>(`${endPoint}?${urlQuery}`);
132+
133+
return result.offlinePayments.map(p => OfflinePayment.factory(p));
134+
}
135+
}

lib/index.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
import { Gateway } from './Gateway';
2-
import { Configuration, ConfigurationParams } from './Configuration';
1+
import { Gateway } from "./Gateway";
2+
import { Configuration, ConfigurationParams } from "./Configuration";
33
export { Configuration };
4-
export { Gateway } from './Gateway';
5-
export { Recipient } from './Recipient';
6-
export { RecipientAccount } from './RecipientAccount';
7-
export { Balance } from './Balance';
8-
export { Batch } from './Batch';
9-
export { Payment } from './Payment';
4+
export { Gateway } from "./Gateway";
5+
export { Recipient } from "./Recipient";
6+
export { RecipientAccount } from "./RecipientAccount";
7+
export { Balance } from "./Balance";
8+
export { Batch } from "./Batch";
9+
export { Payment } from "./Payment";
10+
export { OfflinePayment } from "./OfflinePayment";
1011

1112
/**
1213
* Create a client for the Payment Rails JavasScript API

lib/types.ts

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,6 @@ export namespace ComplianceCheck {
188188
}
189189
}
190190

191-
192191
/**
193192
* @hidden
194193
*/
@@ -266,6 +265,39 @@ export namespace Payment {
266265
}
267266
}
268267

268+
export namespace OfflinePayment {
269+
export interface OfflinePayment {
270+
id: string;
271+
272+
recipientId: string;
273+
externalId: string | null;
274+
memo: string;
275+
tags: string[];
276+
taxReportable: boolean;
277+
category: string;
278+
amount: string;
279+
currency: string;
280+
withholdingAmount: string;
281+
withholdingCurrency: string;
282+
equivalentWithholdingAmount: string;
283+
equivalentWithholdingCurrency: string;
284+
285+
processedAt: string;
286+
287+
updatedAt: string;
288+
createdAt: string;
289+
deletedAt: string | null;
290+
}
291+
292+
export interface Response {
293+
offlinePayment: OfflinePayment;
294+
}
295+
296+
export interface ListResponse extends Serializer.WithMeta {
297+
offlinePayments: OfflinePayment[];
298+
}
299+
}
300+
269301
/**
270302
* @hidden
271303
*/
@@ -366,7 +398,6 @@ export namespace TaxForm {
366398
}
367399
}
368400

369-
370401
/**
371402
* @hidden
372403
*/

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "paymentrails",
3-
"version": "0.9.7",
3+
"version": "0.10.0",
44
"description": "",
55
"main": "./dist/index",
66
"types": "./dist/index",

0 commit comments

Comments
 (0)