@tomei/finance 0.1.10 → 0.1.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,236 +0,0 @@
1
- import * as OAuthClient from 'intuit-oauth';
2
- import { Customer } from '../customer';
3
- import { Payment } from '../payment';
4
- import { ClientScopes } from '../enum/quick-book-client-scopes.enum';
5
- import { IAccountSystem } from '../interfaces';
6
- import { IQuickBookAPICallOptions } from './interfaces/quickbook-client-call-options.interface';
7
- import { IQuickBookCreateAccountOptions } from './interfaces/quickbook-client-create-account-options.interface';
8
- import { IQuickBookCreateCustomerOptions } from './interfaces/quickbook-client-create-customer-options.interface';
9
- import { getConfig } from '../config';
10
- import { IQuickBookClientOptions } from './interfaces/quickbook-client-options.interface';
11
- export class QuickBookClient implements IAccountSystem {
12
- client: OAuthClient;
13
- apiUrl: string;
14
- authUrl: string;
15
-
16
- constructor(SysCode: string, scopes: ClientScopes[]) {
17
- const config = getConfig();
18
- const clientConfig: IQuickBookClientOptions = {
19
- apiUrl: config.accountingSystem.quickbooks.API_Url,
20
- clientId: config.systemConfig[SysCode].API_Key,
21
- clientSecret: config.systemConfig[SysCode].API_Secret,
22
- environment:
23
- config.environment === 'production' ? 'production' : 'sandbox',
24
- redirectUri: config.systemConfig[SysCode].redirectUrl,
25
- logging: config.environment === 'production' ? false : true,
26
- };
27
- this.client = new OAuthClient(clientConfig);
28
- this.apiUrl = config.accountingSystem.quickbooks.API_Url;
29
-
30
- this.authUrl = this.connect(scopes);
31
- }
32
-
33
- async createAccount(payload: IQuickBookCreateAccountOptions) {
34
- try {
35
- const companyId = this.getCompanyId();
36
- const response = await this.callApi({
37
- endpoint: `/v3/company/${companyId}/account`,
38
- method: 'POST',
39
- headers: {
40
- 'Content-Type': 'application/json',
41
- },
42
- body: JSON.stringify(payload),
43
- });
44
- return response.json.Account.id;
45
- } catch (error) {
46
- throw error;
47
- }
48
- }
49
-
50
- async createCustomer(payload: IQuickBookCreateCustomerOptions) {
51
- try {
52
- const requestBody = {
53
- PrimaryEmailAddr: {
54
- Address: payload.Email,
55
- },
56
- DisplayName: payload.FullName,
57
- GivenName: payload.FullName,
58
- FullyQualifiedName: payload.FullName,
59
- PrimaryPhone: {
60
- FreeFormNumber: payload.ContractNo,
61
- },
62
- BillAddr: {
63
- CountrySubDivisionCode: '',
64
- City: payload.Address.City,
65
- PostalCode: payload.Address.PostalCode,
66
- Line1: payload.Address.Line1,
67
- Country: payload.Address.Country,
68
- },
69
- };
70
-
71
- const companyId = this.getCompanyId();
72
- const response = await this.callApi({
73
- endpoint: `/v3/company/${companyId}/customer?minorversion=65`,
74
- method: 'POST',
75
- headers: {
76
- 'Content-Type': 'application/json',
77
- },
78
- body: JSON.stringify(requestBody),
79
- });
80
- return response.json.Customer.id;
81
- } catch (error) {
82
- throw error;
83
- }
84
- }
85
-
86
- async makePayment(customer: Customer, paymentInfo: Payment): Promise<any> {
87
- try {
88
- const requestBody = {
89
- TotalAmt: paymentInfo.getAmount(),
90
- sparse: false,
91
- DepositToAccountRef: {
92
- value: await paymentInfo.getDepositAccountNo(),
93
- },
94
- ARAccountRef: {
95
- value: await paymentInfo.getReceivableAccountNo(),
96
- },
97
- CustomerRef: {
98
- value: customer.getCustomerId(),
99
- name: customer.FullName,
100
- },
101
- Line: [],
102
- };
103
-
104
- paymentInfo.getPaymentItems().forEach((item) => {
105
- requestBody.Line.push({
106
- Amount: item.Amount,
107
- LinkedTxn: [
108
- {
109
- TxnId: item.Id,
110
- TxnType: item.Type,
111
- },
112
- ],
113
- });
114
- });
115
-
116
- const companyId = this.getCompanyId();
117
- const response = await this.callApi({
118
- endpoint: `/v3/company/${companyId}/payment?minorversion=65`,
119
- method: 'POST',
120
- headers: {
121
- 'Content-Type': 'application/json',
122
- },
123
- body: JSON.stringify(requestBody),
124
- });
125
- return response.json.Payment.Id;
126
- } catch (error) {}
127
- }
128
-
129
- collectPayments(customer: Customer, paymentInfo: Payment): Promise<any> {
130
- console.log(customer, paymentInfo);
131
- throw new Error('Method not implemented.');
132
- }
133
-
134
- async createInvoice() {
135
- throw new Error('Method not implemented.');
136
- }
137
- async createDebitNote() {
138
- throw new Error('Method not implemented.');
139
- }
140
- async createCreditNote() {
141
- throw new Error('Method not implemented.');
142
- }
143
- async postTransaction() {
144
- throw new Error('Method not implemented.');
145
- }
146
-
147
- connect(scopes: ClientScopes[]): string {
148
- try {
149
- const authUrl = this.client.authorizeUri({
150
- scope: scopes,
151
- state: 'testState',
152
- });
153
-
154
- return authUrl;
155
- } catch (error) {
156
- throw error;
157
- }
158
- }
159
-
160
- async authCallback(url: string): Promise<void> {
161
- try {
162
- await this.client.createToken(url);
163
- } catch (error) {
164
- throw error;
165
- }
166
- }
167
-
168
- isTokenValid(): boolean {
169
- try {
170
- return this.client.isAccessTokenValid();
171
- } catch (error) {
172
- throw error;
173
- }
174
- }
175
-
176
- async refreshAccessToken(): Promise<void> {
177
- try {
178
- await this.client.refresh();
179
- } catch (error) {
180
- throw error;
181
- }
182
- }
183
-
184
- async logout(): Promise<void> {
185
- try {
186
- await this.client.revokeToken();
187
- } catch (error) {
188
- throw error;
189
- }
190
- }
191
-
192
- public get token(): {
193
- token_type: string;
194
- access_token: string;
195
- refresh_token: string;
196
- expires_in: number;
197
- x_refresh_token_expires_in: number;
198
- id_token: string;
199
- createdAt: number;
200
- } {
201
- return this.client.token.getToken();
202
- }
203
-
204
- getCompanyId(): string {
205
- return this.client.getToken().realmId;
206
- }
207
-
208
- async validateAndRefreshToken(): Promise<void> {
209
- if (!this.client.isAccessTokenValid()) {
210
- await this.client.refresh();
211
- }
212
- }
213
-
214
- async callApi(options: IQuickBookAPICallOptions): Promise<any> {
215
- try {
216
- const headers = {
217
- 'Content-Type': 'application/json',
218
- };
219
-
220
- const response = this.client.makeApiCall({
221
- url: this.apiUrl + options.endpoint,
222
- method: options.method,
223
- headers: options.headers
224
- ? {
225
- ...headers,
226
- ...options.headers,
227
- }
228
- : headers,
229
- body: options.body ? options.body : '',
230
- });
231
- return response;
232
- } catch (error) {
233
- throw error;
234
- }
235
- }
236
- }
@@ -1,10 +0,0 @@
1
- import { QuickBookClient } from './client';
2
- import { IQuickBookCreateAccountOptions } from './interfaces/quickbook-client-create-account-options.interface';
3
- import { IQuickBookClientOptions } from './interfaces/quickbook-client-options.interface';
4
- import { IQuickBookCreateCustomerOptions } from './interfaces/quickbook-client-create-customer-options.interface';
5
- export {
6
- QuickBookClient,
7
- IQuickBookCreateAccountOptions,
8
- IQuickBookClientOptions,
9
- IQuickBookCreateCustomerOptions,
10
- };
@@ -1,6 +0,0 @@
1
- export interface IQuickBookAPICallOptions {
2
- endpoint: string;
3
- method: 'GET' | 'POST' | 'PUT' | 'DELETE';
4
- headers?: object;
5
- body?: string;
6
- }
@@ -1,9 +0,0 @@
1
- export interface IQuickBookCreateAccountOptions {
2
- Name: string;
3
- AcctNum: string;
4
- AccountType: string;
5
- AccountSubType: string;
6
- CurrencyRef?: string;
7
- ParentRef?: string;
8
- SubAccount?: boolean;
9
- }
@@ -1,8 +0,0 @@
1
- import { AddressBase } from '@tomei/general';
2
-
3
- export interface IQuickBookCreateCustomerOptions {
4
- FullName: string;
5
- Email: string;
6
- ContractNo: string;
7
- Address: AddressBase;
8
- }
@@ -1,8 +0,0 @@
1
- export interface IQuickBookClientOptions {
2
- clientId: string;
3
- clientSecret: string;
4
- redirectUri: string;
5
- apiUrl: string;
6
- environment: 'production' | 'sandbox';
7
- logging: boolean;
8
- }