Skip to content

Commit 90ed4b4

Browse files
author
Aman Alam
authored
Merge pull request #30 from zero-bites/master
[Feature] Add support for Invoice
2 parents ebcc37b + 82933d9 commit 90ed4b4

File tree

11 files changed

+256
-4
lines changed

11 files changed

+256
-4
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ bower
88
__MACOSX
99
.eslintrc.js
1010
.vscode
11+
.idea
1112
dist/

lib/Client.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,14 @@ export class Client {
8080
async get<T>(endPoint: string): Promise<T> {
8181
const date: any = new Date();
8282
const timestamp = Math.round(date / 1000);
83-
const authoriation = this.generateAuthorization(timestamp, endPoint, "GET");
83+
const authorization = this.generateAuthorization(timestamp, endPoint, "GET");
8484
const options = {
8585
uri: endPoint,
8686
baseUrl: this.config.apiBase,
8787
method: "GET",
8888
headers: {
8989
"Content-Type": "application/json",
90-
Authorization: authoriation,
90+
Authorization: authorization,
9191
"X-PR-Timestamp": timestamp,
9292
},
9393
};

lib/Configuration.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ export class Configuration {
103103
// tslint:disable-next-line:no-http-string
104104
return "http://api.local.dev:3000";
105105
case "sandbox":
106-
return "https://api.sandbox.paymentrails.com";
106+
return "https://api.railz.io";
107107
case "production":
108108
return "https://api.paymentrails.com";
109109
default:

lib/Gateway.ts

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

1012
export class Gateway {
1113
config: Configuration;
@@ -16,6 +18,8 @@ export class Gateway {
1618
balances: BalancesGateway;
1719
payment: PaymentGateway;
1820
offlinePayment: OfflinePaymentGateway;
21+
invoice: InvoiceGateway;
22+
invoiceLine: InvoiceLineGateway;
1923

2024
/**
2125
* This should be called by the connect() method to setup a client gateway
@@ -31,5 +35,7 @@ export class Gateway {
3135
this.balances = new BalancesGateway(this);
3236
this.payment = new PaymentGateway(this);
3337
this.offlinePayment = new OfflinePaymentGateway(this);
38+
this.invoice = new InvoiceGateway(this);
39+
this.invoiceLine = new InvoiceLineGateway(this);
3440
}
3541
}

lib/Invoice.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { InvoiceLine, InvoiceLineInput } from "./InvoiceLine";
2+
import { Amount } from "./Types";
3+
4+
export interface InvoiceInput {
5+
recipientId: string;
6+
description: string;
7+
externalId: string;
8+
invoiceDate: string;
9+
dueDate: string;
10+
lines: InvoiceLineInput[];
11+
}
12+
13+
export class Invoice {
14+
id: string = "";
15+
recipientId: string = "";
16+
description: string = "";
17+
externalId: string = "";
18+
invoiceDate: string = "";
19+
invoiceNumber: number = 0;
20+
dueDate: string = "";
21+
status: string = "";
22+
releaseAfter: string = "";
23+
createdAt: string = "";
24+
updatedAt: string = "";
25+
lines: InvoiceLine[] = [];
26+
tags: string[] = [];
27+
totalAmount: Amount = {
28+
currency: "",
29+
value: 0,
30+
};
31+
paidAmount: Amount = {
32+
currency: "",
33+
value: 0,
34+
};
35+
dueAmount: Amount = {
36+
currency: "",
37+
value: 0,
38+
};
39+
}

lib/InvoiceGateway.ts

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import { Configuration } from './Configuration';
2+
import { Gateway } from './Gateway';
3+
import { Invoice, InvoiceInput } from './Invoice';
4+
import { ApiResponse } from './types';
5+
import { buildURL } from './util';
6+
7+
export class InvoiceGateway {
8+
gateway: Gateway;
9+
config: Configuration;
10+
11+
constructor(gateway: Gateway) {
12+
this.gateway = gateway;
13+
this.config = this.gateway.config;
14+
}
15+
16+
async find(invoiceId: string) {
17+
const endPoint = buildURL('invoices/get');
18+
19+
const result = await this.gateway.client.post<ApiResponse<Invoice>>(
20+
endPoint,
21+
{
22+
invoiceId: invoiceId,
23+
},
24+
);
25+
26+
return Object.assign(new Invoice(), result.invoice);
27+
}
28+
29+
async create(body: InvoiceInput) {
30+
const endPoint = buildURL('invoices/create');
31+
32+
const result = await this.gateway.client.post<ApiResponse<Invoice>>(
33+
endPoint,
34+
body,
35+
);
36+
37+
return Object.assign(new Invoice(), result.invoice);
38+
}
39+
40+
async update(invoiceId: string, body: InvoiceInput) {
41+
const endPoint = buildURL('invoices/update');
42+
43+
const result = await this.gateway.client.post<ApiResponse<Invoice>>(
44+
endPoint,
45+
{
46+
invoiceId: invoiceId,
47+
...body,
48+
},
49+
);
50+
51+
return Object.assign(new Invoice(), result.invoice);
52+
}
53+
54+
async remove(invoiceIds: string[]) {
55+
const endPoint = buildURL('invoices/delete');
56+
57+
const result = await this.gateway.client.post<ApiResponse<Invoice>>(
58+
endPoint,
59+
{
60+
invoiceIds: invoiceIds,
61+
},
62+
);
63+
64+
return true;
65+
}
66+
67+
async search(body: any) {
68+
const endPoint = buildURL('invoices/search');
69+
70+
const result = await this.gateway.client.post<ApiResponse<Invoice[]>>(
71+
endPoint,
72+
body,
73+
);
74+
75+
return result.invoices.map((line: Invoice) => Object.assign(new Invoice(), line));
76+
}
77+
}

lib/InvoiceLine.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { Amount } from "./Types";
2+
3+
export interface InvoiceLineInput {
4+
unitAmount: Amount;
5+
category: InvoiceLineCategory;
6+
description: string;
7+
externalId: string;
8+
taxReportable: boolean;
9+
forceUsTaxActivity: boolean;
10+
tags: string[];
11+
}
12+
13+
enum InvoiceLineCategory {
14+
'services',
15+
'rent',
16+
'royalties',
17+
'royalties_film',
18+
'prizes',
19+
'education',
20+
'refunds',
21+
}
22+
23+
export class InvoiceLine {
24+
id: string = '';
25+
status: string = '';
26+
description: string = '';
27+
quantity: number = 0;
28+
externalId: string = '';
29+
taxReportable: boolean = false;
30+
tags: string[] = [];
31+
category: string = '';
32+
forceUsTaxActivity: boolean = false;
33+
unitAmount: Amount = {
34+
currency: '',
35+
value: 0,
36+
};
37+
discountAmount: Amount = {
38+
currency: '',
39+
value: 0,
40+
};
41+
taxAmount: Amount = {
42+
currency: '',
43+
value: 0,
44+
};
45+
totalAmount: Amount = {
46+
currency: '',
47+
value: 0,
48+
};
49+
dueAmount: Amount = {
50+
currency: '',
51+
value: 0,
52+
};
53+
paidAmount: Amount = {
54+
currency: '',
55+
value: 0,
56+
};
57+
}

lib/InvoiceLineGateway.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { Gateway } from "./Gateway";
2+
import { Configuration } from "./Configuration";
3+
import { buildURL } from "./util";
4+
import { ApiResponse } from "./types";
5+
import { InvoiceLine, InvoiceLineInput } from "./InvoiceLine";
6+
import { Invoice } from "./Invoice";
7+
8+
export class InvoiceLineGateway {
9+
gateway: Gateway;
10+
config: Configuration;
11+
12+
constructor(gateway: Gateway) {
13+
this.gateway = gateway;
14+
this.config = gateway.config;
15+
}
16+
17+
async create(invoiceId: string, invoiceLines: InvoiceLineInput[]) {
18+
const endPoint = buildURL('invoices/create-lines');
19+
20+
const result = await this.gateway.client.post<ApiResponse<Invoice>>(
21+
endPoint,
22+
{
23+
invoiceId: invoiceId,
24+
...{ lines: invoiceLines },
25+
},
26+
);
27+
28+
return result.invoice.lines.map((line: InvoiceLine) => Object.assign(new InvoiceLine(), line));
29+
}
30+
31+
async update(invoiceId: string, body: any) {
32+
const endPoint = buildURL('invoices/update-lines');
33+
34+
const result = await this.gateway.client.post<ApiResponse<Invoice>>(
35+
endPoint,
36+
{
37+
invoiceId: invoiceId,
38+
...body,
39+
},
40+
);
41+
42+
return Object.assign(new Invoice(), result.invoice);
43+
}
44+
45+
async delete(invoiceId: string, lineIds: string[]) {
46+
const endPoint = buildURL('invoices/delete-lines');
47+
48+
const result = await this.gateway.client.post<ApiResponse<Invoice>>(
49+
endPoint,
50+
{
51+
invoiceId: invoiceId,
52+
invoiceLineIds: lineIds,
53+
},
54+
);
55+
56+
return true;
57+
}
58+
}

lib/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export { Balance } from "./Balance";
88
export { Batch } from "./Batch";
99
export { Payment } from "./Payment";
1010
export { OfflinePayment } from "./OfflinePayment";
11+
export { Invoice } from "./Invoice";
1112

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

lib/types.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
// tslint:disable:no-shadowed-variable
66
// tslint:disable:prefer-method-signature
77

8+
import { Invoice } from "./Invoice";
9+
810
/**
911
* @hidden
1012
*/
@@ -598,3 +600,13 @@ export namespace BankInfo {
598600
lookupData: BankInfo;
599601
};
600602
}
603+
604+
export interface ApiResponse<T> {
605+
[key: string]: any;
606+
ok: boolean;
607+
}
608+
609+
export type Amount = {
610+
value: number;
611+
currency: string;
612+
};

0 commit comments

Comments
 (0)