-
Notifications
You must be signed in to change notification settings - Fork 4
/
dmIntuit.pas
492 lines (434 loc) · 15.7 KB
/
dmIntuit.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
unit dmIntuit;
interface
uses
System.SysUtils
, System.Classes
, System.JSON
, System.IOUtils
, Winapi.Windows
, REST.Types
, REST.Client
, REST.Authenticator.OAuth
, System.Net.URLClient
, Data.Bind.Components
, Data.Bind.ObjectScope
, FireDAC.Stan.Intf
, FireDAC.Stan.Option
, FireDAC.Stan.Param
, FireDAC.Stan.Error
, FireDAC.DatS
, FireDAC.Phys.Intf
, FireDAC.DApt.Intf
, Data.DB
, FireDAC.Comp.DataSet
, FireDAC.Comp.Client
, JSON.CustomerList
, JSON.InvoiceList
, JSON.AttachableList
, JSON.VendorList
, OAuth2.Intuit
, TokenManager
, fmLogin
;
type
TOnLogStatus = procedure(inText: string) of object;
EIntuitException = class(Exception)
private
FTid : string;
public
constructor Create(tid:string; msg: string);
property Tid: string read FTid;
end;
TdmIntuitAPI = class(TDataModule)
RESTResponse1: TRESTResponse;
RESTRequest1: TRESTRequest;
RESTClient1: TRESTClient;
tblCustomers: TFDMemTable;
tblCustomersActive: TBooleanField;
tblInvoices: TFDMemTable;
tblInvoicesTotalAmount: TFloatField;
tblCustomersId: TWideStringField;
tblCustomersDisplayName: TWideStringField;
tblCustomersSyncToken: TWideStringField;
tblCustomersPrimaryEmailAddr: TWideStringField;
tblInvoicesId: TWideStringField;
tblInvoicesSyncToken: TWideStringField;
tblInvoicesTxnDate: TWideStringField;
tblInvoicesCustomerRefName: TWideStringField;
tblInvoicesCustomerRefValue: TWideStringField;
tblVendors: TFDMemTable;
tblVendorsId: TWideStringField;
tblVendorsDisplayName: TWideStringField;
tblVendorsActive: TBooleanField;
tblVendorsSyncToken: TWideStringField;
tblVendorsBalance: TFloatField;
procedure DataModuleDestroy(Sender: TObject);
procedure DataModuleCreate(Sender: TObject);
strict private
FrealmId : String;
FOnLog : TOnLogStatus;
FfrmLogin: TfrmLogin;
FTokenManager: TTokenManager;
OAuth2Authenticator1: TIntuitOAuth2;
procedure DoLog(inText: string);
private
{ Private declarations }
FClientID : string;
FClientSecret : string;
function GetEnvironment: string;
public
{ Public declarations }
function CreateInvoice(invoice: TInvoiceClass): TInvoiceClass;
function GetCustomers: TCustomerListClass;
procedure ListInvoices;
procedure ListVendors;
procedure SetupInvoice(invoice: TInvoiceClass; TxnDate: TDate; invoiceID: String);
procedure UploadAttachment(inFilename, inInvoiceID: string);
procedure ChangeRefreshTokenToAccessToken;
procedure ShowLoginForm;
procedure HandleOAuthCodeRedirect(uri: TURI);
procedure SetEnvironment(inEnvironment: string);
function GetBaseURL: string;
function GetEnvironmentData(inName: string): string;
published
property RealmId : string read FrealmId write FrealmId;
property TokenManager: TTokenManager read FTokenManager;
property Environment: string read GetEnvironment;
end;
var
dmIntuitAPI: TdmIntuitAPI;
implementation
{%CLASSGROUP 'System.Classes.TPersistent'}
{$R *.dfm}
uses
secrets
;
procedure TdmIntuitAPI.DataModuleDestroy(Sender: TObject);
begin
FreeAndNil(OAuth2Authenticator1);
FreeAndNil(FfrmLogin);
FreeAndNil(FTokenManager);
end;
procedure TdmIntuitAPI.DataModuleCreate(Sender: TObject);
begin
FfrmLogin := TfrmLogin.Create(nil);
FTokenManager := TTokenManager.Create(ChangeFileExt(ParamStr(0), '.ini'));
OAuth2Authenticator1 := TIntuitOAuth2.Create(nil);
OAuth2Authenticator1.AuthorizationEndpoint := 'https://appcenter.intuit.com/connect/oauth2';
OAuth2Authenticator1.AccessTokenEndpoint := 'https://oauth.platform.intuit.com/oauth2/v1/tokens/bearer';
OAuth2Authenticator1.RedirectionEndpoint := SECRET_REDIRECT_URL; //'https://developer.intuit.com/v2/OAuth2Playground/RedirectUrl';
OAuth2Authenticator1.Scope := 'com.intuit.quickbooks.accounting openid';
SetEnvironment(environment);
OAuth2Authenticator1.ResponseType := TOAuth2ResponseType.rtCODE;
OAuth2Authenticator1.RefreshToken := FTokenManager.RetrieveDecryptedToken(Environment, 'refresh_token');
RealmId := GetEnvironmentData('RealmId');
RESTClient1.Authenticator := OAuth2Authenticator1;
RESTClient1.BaseURL := GetBaseURL;
end;
procedure TdmIntuitAPI.ChangeRefreshTokenToAccessToken;
begin
OAuth2Authenticator1.RefreshToken := TokenManager.UnprotectSecret(GetEnvironmentData('refresh_token'));
RealmId := GetEnvironmentData('RealmId');
OAuth2Authenticator1.ChangeRefreshTokenToAccesToken;
end;
function TdmIntuitAPI.CreateInvoice(invoice: TInvoiceClass): TInvoiceClass;
var
invoiceJSON: TJSONObject;
invoiceText: string;
retJSON : TJSONObject;
tid : string;
begin
RESTRequest1.ResetToDefaults;
RESTResponse1.ResetToDefaults;
try
invoiceJSON := TJSONObject.ParseJSONValue(invoice.ToJsonString) as TJSONObject;
invoiceJSON.RemovePair('allowIPNPayment');
invoiceJSON.RemovePair('allowOnlineACHPayment');
invoiceJSON.RemovePair('allowOnlinePayment');
invoiceJSON.RemovePair('allowOnlineCreditCardPayment');
invoiceJSON.RemovePair('CreditCardPayment');
invoiceJSON.RemovePair('EmailStatus');
invoiceJSON.RemovePair('Balance');
invoiceJSON.RemovePair('Deposit');
invoiceJSON.RemovePair('DocNumber');
invoiceJSON.RemovePair('Id');
invoiceJSON.RemovePair('DueDate');
invoiceJSON.RemovePair('TxnSource');
invoiceJSON.RemovePair('GlobalTaxCalculation');
invoiceJSON.RemovePair('MetaData');
invoiceJSON.RemovePair('TotalAmt');
invoiceJSON.RemovePair('domain');
invoiceJSON.RemovePair('sparse');
invoiceText := invoiceJSON.ToJSON;
doLog(invoiceText);
RESTRequest1.Method := rmPost;
FrealmID := GetEnvironmentData('RealmId');
RESTRequest1.Resource := '/v3/company/' + FrealmId + '/invoice';
RESTRequest1.AddBody(invoiceText, ctAPPLICATION_JSON);
RESTRequest1.Execute;
doLog(RESTResponse1.Content);
retJSON := TJSONObject.ParseJSONValue(RESTResponse1.Content) as TJSONObject;
try
Result := TInvoiceClass.FromJsonString(retJSON.Values['Invoice'].ToJSON);
finally
FreeAndNil(retJSON);
end;
except
on e : Exception do
begin
tid := RESTResponse1.Headers.Values['intuit_tid'];
raise EIntuitException.Create(tid, e.Message);
end;
end;
end;
procedure TdmIntuitAPI.SetEnvironment(inEnvironment: string);
begin
TokenManager.StoreExtraSectionData('Global', 'Environment', inEnvironment);
if inEnvironment.ToLower = 'sandbox' then
begin
FClientID := SECRET_SANDBOX_INTUIT_CLIENTID;
FClientSecret := SECRET_SANDBOX_INTUIT_CLIENTSECRET;
end
else if inEnvironment.ToLower = 'production' then
begin
FClientID := SECRET_PRODUCTION_INTUIT_CLIENTID;
FClientSecret := SECRET_PRODUCTION_INTUIT_CLIENTSECRET;
end
else
begin
raise Exception.Create('Unknown Environment Selected');
end;
OAuth2Authenticator1.ClientID := FClientID;
OAuth2Authenticator1.ClientSecret := FClientSecret;
OAuth2Authenticator1.RefreshToken := TokenManager.RetrieveDecryptedToken(Environment, 'refresh_token');
FrealmId := self.GetEnvironmentData('RealmId');
RESTClient1.BaseURL := GetBaseURL;
end;
procedure TdmIntuitAPI.SetupInvoice(invoice: TInvoiceClass; TxnDate:TDate; invoiceID:String);
begin
invoice.CurrencyRef.name := 'Australian Dollar';
invoice.CurrencyRef.value := 'AUD';
invoice.CustomerRef.name := SECRET_CUSTOMER_NAME;
invoice.CustomerRef.value := SECRET_CUSTOMER_ID;
invoice.DocNumber := invoiceID;
invoice.EmailStatus := 'NotSet';
invoice.GlobalTaxCalculation := 'NotApplicable';
invoice.PrintStatus := 'NotSet';
invoice.SyncToken := '0';
invoice.domain := 'QBO';
invoice.TxnDate := FormatDatetime('yyyy-mm-dd', TxnDate); //'2019-05-30';
invoice.CustomerMemo.value := invoiceID;
invoice.TrackingNum := invoiceID;
end;
procedure TdmIntuitAPI.DoLog(inText: string);
begin
if Assigned(FOnLog) then
FOnLog(inText);
end;
procedure TdmIntuitAPI.ListInvoices;
var
invoiceList : TJSONInvoiceListClass;
invoice : TInvoiceClass;
begin
RESTRequest1.Method := rmGET;
RESTRequest1.Resource := '/v3/company/{RealmId}/query?query=select * from Invoice';// Where Id > ' + QuotedStr('166');
RESTRequest1.AddParameter('RealmId', RealmId, pkURLSEGMENT);
RESTRequest1.ExecuteAsync(procedure ()
var
i: Integer;
begin
try
invoiceList := TJSONInvoiceListClass.FromJsonString(dmIntuitAPI.RESTRequest1.Response.Content);
tblInvoices.Active := True;
for i := 0 to Length(invoiceList.QueryResponse.Invoice) - 1 do
begin
invoice := invoiceList.QueryResponse.Invoice[i];
tblInvoices.Append;
tblInvoicesId.Value := invoice.Id;
tblInvoicesSyncToken.Value := invoice.SyncToken;
tblInvoicesTxnDate.Value := invoice.TxnDate;
tblInvoicesCustomerRefName.Value := invoice.CustomerRef.name;
tblInvoicesCustomerRefValue.Value := invoice.CustomerRef.value;
tblInvoicesTotalAmount.Value := invoice.TotalAmt;
tblInvoices.Post;
end;
finally
FreeAndNil(invoiceList);
end;
end);
end;
function TdmIntuitAPI.GetBaseURL: string;
begin
if Environment = 'production' then
Result := 'https://quickbooks.api.intuit.com'
else
Result := 'https://sandbox-quickbooks.api.intuit.com';
end;
function TdmIntuitAPI.GetCustomers: TCustomerListClass;
var
customers : TCustomerListClass;
begin
RESTRequest1.ResetToDefaults;
RESTRequest1.Method := rmGET;
RESTRequest1.Resource := '/v3/company/{RealmId}/query?query=select * from Customer Where Metadata.LastUpdatedTime > ' + QuotedStr('2015-03-01');
RESTRequest1.AddParameter('RealmId', RealmId, pkURLSEGMENT);
RESTRequest1.AddParameter('minorversion', '73', TRESTRequestParameterKind.pkQUERY);
RESTRequest1.ExecuteAsync(procedure ()
var
i : Integer;
begin
customers := TCustomerListClass.FromJsonString(RESTRequest1.Response.Content);
tblCustomers.Active := True;
for i := 0 to Length(customers.QueryResponse.Customer) - 1 do
begin
tblCustomers.Append;
tblCustomersId.Value := customers.QueryResponse.Customer[i].Id;
tblCustomersSyncToken.Value := customers.QueryResponse.Customer[i].SyncToken;
tblCustomersDisplayName.Value := customers.QueryResponse.Customer[i].DisplayName;
tblCustomersPrimaryEmailAddr.Value := customers.QueryResponse.Customer[i].PrimaryEmailAddr.Address;
tblCustomers.Post;
// Memo1.Lines.Add('Id: ' + customers.QueryResponse.Customer[i].Id);
// Memo1.Lines.Add('DisplayName: ' + customers.QueryResponse.Customer[i].DisplayName);
// Memo1.Lines.Add('Balance: ' + customers.QueryResponse.Customer[i].Balance.ToString)
end;
end);
end;
procedure TdmIntuitAPI.ListVendors;
var
vendors : TJSONVendorListClass;
begin
RESTRequest1.ResetToDefaults;
RESTRequest1.Method := rmGET;
RESTRequest1.Resource := '/v3/company/{RealmId}/query?query=select * from Vendor Where Metadata.LastUpdatedTime > ' + QuotedStr('2015-03-01');
RESTRequest1.AddParameter('RealmId', RealmId, pkURLSEGMENT);
RESTRequest1.AddParameter('minorversion', '73', TRESTRequestParameterKind.pkQUERY);
RESTRequest1.ExecuteAsync(procedure ()
var
i : Integer;
begin
vendors := TJSONVendorListClass.FromJsonString(RESTRequest1.Response.Content);
tblVendors.Active := True;
for i := 0 to Length(vendors.QueryResponse.Vendor) - 1 do
begin
tblVendors.Append;
tblVendorsId.Value := vendors.QueryResponse.Vendor[i].Id;
tblVendorsSyncToken.Value := vendors.QueryResponse.Vendor[i].SyncToken;
tblVendorsDisplayName.Value := vendors.QueryResponse.Vendor[i].DisplayName;
tblVendorsBalance.Value := vendors.QueryResponse.Vendor[i].Balance;
tblVendors.Post;
end;
end);
end;
function TdmIntuitAPI.GetEnvironment: string;
begin
Result := TokenManager.RetrieveExtraSectionData('Global', 'Environment');
if Result.IsEmpty then
Result := 'sandbox';
end;
function TdmIntuitAPI.GetEnvironmentData(inName: string): string;
begin
Result := TokenManager.RetrieveExtraSectionData(Environment, inName);
end;
procedure TdmIntuitAPI.HandleOAuthCodeRedirect(uri: TURI);
var
code : string;
state : string;
begin
code := uri.ParameterByName['code'];
state := uri.ParameterByName['state'];
RealmId := uri.ParameterByName['realmId'];
OAuth2Authenticator1.AuthCode := code;
// Form1.Memo1.Lines.Add('url:'+URL);
OAuth2Authenticator1.ChangeAuthCodeToAccesToken;
// Form1.Memo1.Lines.Add('Access Granted');
// Form1.Memo1.Lines.Add('RefreshToken=' + OAuth2Authenticator1.RefreshToken);
// Form1.Memo1.Lines.Add('Access Token');
// Form1.Memo1.Lines.Add(OAuth2Authenticator1.AccessToken);
TokenManager.StoreEncryptedToken(environment, 'refresh_token', OAuth2Authenticator1.RefreshToken);
TokenManager.StoreExtraSectionData(environment, 'RealmId', RealmId);
end;
procedure TdmIntuitAPI.ShowLoginForm;
var
uri : TURI;
begin
uri := TURI.Create('https://appcenter.intuit.com/connect/oauth2');
uri.AddParameter('client_id', FClientID);
uri.AddParameter('client_secret', FClientSecret);
uri.AddParameter('scope', 'com.intuit.quickbooks.accounting');
uri.AddParameter('redirect_uri', SECRET_REDIRECT_URL);
uri.AddParameter('state', '2342342323');
uri.AddParameter('response_type', 'code');
FfrmLogin.Login(uri.ToString, SECRET_REDIRECT_URL);
end;
procedure TdmIntuitAPI.UploadAttachment(inFilename:string; inInvoiceID:string);
var
attachable : TAttachableClass;
attachableRefArr : TArray<TAttachableRefClass>;
attachableRef : TAttachableRefClass;
attachJSON : TJSONObject;
param : TRESTRequestParameter;
tid : string;
begin
try
if not TFile.Exists(inFilename) then
begin
raise Exception.Create('File Does not exist - ' + inFilename);
end;
attachable := TAttachableClass.Create;
attachable.ContentType := 'application/pdf';
attachable.FileName := ExtractFileName(inFilename);
attachableRefArr := TArray<TAttachableRefClass>.Create();
SetLength(attachableRefArr, 1);
attachableRef := TAttachableRefClass.Create;
attachableRefArr[0] := attachableRef;
attachableRef.EntityRef.&type := 'Invoice';
attachableRef.EntityRef.value := inInvoiceID;
attachableRef.IncludeOnSend := False;
attachable.AttachableRef := attachableRefArr;
RESTRequest1.Method := rmPOST;
RESTRequest1.Resource := '/v3/company/{RealmId}/upload';
RESTRequest1.AddParameter('RealmId', RealmId, pkURLSEGMENT);
RESTRequest1.AddParameter('minorversion', '73', TRESTRequestParameterKind.pkQUERY);
DoLog(attachable.ToJsonString);
DoLog('--------');
attachJSON := TJSONObject.ParseJSONValue(attachable.ToJsonString) as TJSONObject;
attachJSON.RemovePair('MetaData');
attachJSON.RemovePair('Id');
attachJSON.RemovePair('Size');
attachJSON.RemovePair('TempDownloadUri');
attachJSON.RemovePair('domain');
attachJSON.RemovePair('sparse');
attachJSON.RemovePair('SyncToken');
RESTRequest1.ClearBody;
// This workaround is required to get around a bug in the Delphi REST Components
param := RESTRequest1.Params.AddItem('file_metadata_01', attachjson.ToJSON, TRESTRequestParameterKind.pkREQUESTBODY,
[], TRESTContentType.ctAPPLICATION_JSON);
{ RequestStream := TStringStream.Create;
try
RequestStream.WriteString(attachjson.ToJSON);
param.SetStream(RequestStream);
finally
FreeAndNil(RequestStream);
end;}
// End workaround
RESTRequest1.AddFile('file_content_01', inFilename, TRESTContentType.ctAPPLICATION_PDF);
RESTRequest1.Execute;
DoLog(attachJSON.ToJSON);
DoLog('--------');
DoLog(RESTResponse1.Content);
except
on e : Exception do
begin
tid := RESTResponse1.Headers.Values['intuit_tid'];
raise EIntuitException.Create(tid, e.Message);
end;
end;
end;
{ EIntuitException }
constructor EIntuitException.Create(tid, msg: string);
begin
inherited Create(msg);
FTid := tid;
OutputDebugString(PChar('Intuit_Tid:' + tid + ' EXCEPTION: ' + msg));
end;
end.