forked from gmurt/ksStripe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathksStripe.pas
1620 lines (1409 loc) · 44.3 KB
/
ksStripe.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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{ *******************************************************************************
* *
* ksStripe - Stripe Interface for Delphi *
* *
* https://github.com/gmurt/ksStripe *
* *
* Copyright 2020 Graham Murt *
* *
* email: graham@kernow-software.co.uk *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); *
* you may not use this file except in compliance with the License. *
* You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, software *
* distributed under the License is distributed on an "AS IS" BASIS, *
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
* See the License for the specific language governing permissions and *
* limitations under the License. *
* *
*******************************************************************************}
unit ksStripe;
interface
uses Classes, Generics.Collections
{.$DEFINE JSONDATAOBJECTS}
{$IFDEF JSONDATAOBJECTS}
, JsonDataObjects
{$ELSE}
, Json
{$ENDIF}
;
{$IF CompilerVersion >= 29}
{$DEFINE USE_NET_HTTP}
{$ENDIF}
type
TStripeCurrency = (scUnknown, scGbp, scUsd);
//------------------------------------------------------------------------------
IStripeBaseObject = interface
['{AC396FFE-A89C-4811-8DDD-5A3A69546155}']
function GetID: string;
function GetObject: string;
procedure SetID(const Value: string);
procedure Clear;
procedure LoadFromJson(AJson: TJsonObject);
property ID: string read GetID write SetID;
property Obj: string read GetObject;
end;
//------------------------------------------------------------------------------
IStripeBaseObjectList<T> = interface
['{3FD36F72-3FF3-4377-AE0E-120A19C63354}']
function GetCount: integer;
function GetItem(index: integer): T;
function GetListID: string;
procedure Clear;
procedure LoadFromJson(AJson: TJSONObject);
property Count: integer read GetCount;
property Item[index: integer]: T read GetItem; default;
end;
//------------------------------------------------------------------------------
IStripeCard = interface(IStripeBaseObject)
['{76652D56-42CE-4C2F-B0B2-1E6485D501AD}']
function GetBrand: string;
function GetLast4: string;
function GetExpMonth: integer;
function GetExpYear: integer;
property Last4: string read GetLast4;
property Brand: string read GetBrand;
property ExpMonth: integer read GetExpMonth;
property ExpYear: integer read GetExpYear;
end;
//------------------------------------------------------------------------------
IStripeCharge = interface(IStripeBaseObject)
['{197B9D1A-B4F1-4220-AFDC-22DE5031F1B4}']
function GetCreated: TDatetime;
function GetLiveMode: Boolean;
function GetPaid: Boolean;
function GetStatus: string;
function GetAmountPence: integer;
function GetCurrency: TStripeCurrency;
function GetRefunded: Boolean;
function GetCard: IStripeCard;
function GetCustomer: string;
function GetDesc: string;
property Created: TDateTime read GetCreated;
property LiveMode: Boolean read GetLiveMode;
property Paid: Boolean read GetPaid;
property Status: string read GetStatus;
property AmountPence: integer read GetAmountPence;
property Currency: TStripeCurrency read GetCurrency;
property Refunded: Boolean read GetRefunded;
property Customer: string read GetCustomer;
property Card: IStripeCard read GetCard;
property Desc: string read GetDesc;
end;
//------------------------------------------------------------------------------
IStripePlan = interface(IStripeBaseObject)
['{E37D8D42-0FDE-4108-BD58-56603955FDCC}']
function GetAmountPence: integer;
function GetCreated: TDateTime;
function GetCurrency: TStripeCurrency;
function GetInterval: string;
function GetIntervalCount: integer;
function GetName: string;
function GetStatementDescriptor: string;
function GetTrialPeriodDays: integer;
property Interval: string read GetInterval;
property Name: string read GetName;
property Created: TDateTime read GetCreated;
property AmountPence: integer read GetAmountPence;
property Currency: TStripeCurrency read GetCurrency;
property IntervalCount: integer read GetIntervalCount;
property TrialPeriodDays: integer read GetTrialPeriodDays;
property StatementDescriptor: string read GetStatementDescriptor;
end;
//------------------------------------------------------------------------------
IStripeSubscription = interface(IStripeBaseObject)
['{3F2BE016-7483-4020-BEB6-F0A3B55E9753}']
function GetCancelledAt: TDateTime;
function GetCurrentPeriodEnd: TDateTime;
function GetCurrentPeriodStart: TDateTime;
function GetCustomer: string;
function GetEndedAt: TDateTime;
function GetPlan: IStripePlan;
function GetQuantity: integer;
function GetStart: TDateTime;
function GetStatus: string;
function GetTaxPercent: single;
function GetTrialEnd: TDateTime;
function GetTrialStart: TDateTime;
property Plan: IStripePlan read GetPlan;
property Start: TDateTime read GetStart;
property Status: string read GetStatus;
property Customer: string read GetCustomer;
property CurrentPeriodStart: TDateTime read GetCurrentPeriodStart;
property CurrentPeriodEnd: TDateTime read GetCurrentPeriodEnd;
property EndedAt: TDateTime read GetEndedAt;
property TrialStart: TDateTime read GetTrialStart;
property TrialEnd: TDateTime read GetTrialEnd;
property CancelledAt: TDateTime read GetCancelledAt;
property Quantity: integer read GetQuantity;
property TaxPercent: single read GetTaxPercent;
end;
//------------------------------------------------------------------------------
IStripeCustomer = interface(IStripeBaseObject)
['{CFA07B51-F63C-4972-ACAB-FA51D6DF5779}']
function GetEmail: string;
function GetName: string;
function GetDescription: string;
procedure SetName(const Value: string);
procedure SetEmail(const Value: string);
procedure Assign(ACustomer: IStripeCustomer);
procedure SetDescription(const Value: string);
property Name: string read GetName write SetName;
property Email: string read GetEmail write SetEmail;
property Description: string read GetDescription write SetDescription;
end;
//------------------------------------------------------------------------------
IStripeCustomerList = interface(IStripeBaseObjectList<IStripeCustomer>)
['{A84D8E11-C142-4E4C-9698-A6DFBCE14742}']
end;
//------------------------------------------------------------------------------
IStripe = interface
['{A00E2188-0DDB-469F-9C4A-0900DEEFD27B}']
function GetLastError: string;
function GetLastJsonResult: string;
function CreateToken(ACardNum: string; AExpMonth, AExpYear: integer; ACvc: string): string;
function CreateCharge(AToken, ADescription: string;
AAmountPence: integer;
AMetaData: TStrings;
var AError: string;
const ACurrency: TStripeCurrency = scGbp): IStripeCharge;
function CreateCard(ACustID, ACardToken: string; var AError: string): IStripeCard; overload;
function CreateCard(ACustID, ACardNum: string; AExpMonth, AExpYear: integer; ACvc: string; var AError: string): IStripeCard; overload;
function DeleteCard(ACustID, ACardID: string): Boolean;
function UpdateDefaultSource(ACustID, ACardID: string): Boolean;
function CreateChargeForCustomer(ACustID, ADescription: string; AAmountPence: integer; const ACurrency: TStripeCurrency = scGbp): IStripeCharge;
function GetCustomer(ACustID: string): IStripeCustomer;
function CustomerExists(ACustID: string; ACustomer: IStripeCustomer): Boolean;
function GetAccount: string;
function GetCapabilities: string;
function GetPersons: string;
function GetCustomers(const ACreatedAfter: TDateTime = -1;
const ACreatedBefore: TDateTime = -1;
const ALimit: Integer = 10): IStripeCustomerList;
function CreateCustomer(AEmail, ADescription: string): IStripeCustomer;
function GetBalance: Extended;
procedure UpdateCustomerValue(ACustID, AField, AValue: string);
property LastError: string read GetLastError;
property LastJsonResult: string read GetLastJsonResult;
end;
//------------------------------------------------------------------------------
function CreateStripe(ASecretKey: string): IStripe;
function CreateStripeCustomer: IStripeCustomer;
implementation
uses
SysUtils, DateUtils,
{$IFDEF USE_NET_HTTP}
System.Net.URLClient, System.Net.HttpClient, System.Net.HttpClientComponent;
{$ELSE}
IdHttp, IdIOHandlerStack, IdSSL, IdSSLOpenSSL, IdAuthentication;
{$ENDIF}
const
C_ACCOUNT = 'account';
C_CAPABILITIES = 'capabilities';
C_BALANCE = 'balance';
C_CARD = 'card';
C_CARDS = 'cards';
C_CHARGE = 'charge';
C_CHARGES = 'charges';
C_CUSTOMER = 'customer';
C_CUSTOMERS = 'customers';
C_TOKEN = 'token';
C_TOKENS = 'tokens';
C_PERSONS = 'persons';
C_PLAN = 'plan';
C_PLANS = 'plans';
C_SUBSCRIPTION = 'subscription';
C_SUBSCRIPTIONS = 'subscriptions';
type
//------------------------------------------------------------------------------
TStripeBaseObject = class(TInterfacedObject, IStripeBaseObject)
strict private
FJson: TJSONObject;
FId: string;
private
procedure SetID(const Value: string);
protected
function GetID: string;
function GetObject: string; virtual; abstract;
procedure LoadFromJson(AJson: TJsonObject); virtual;
public
constructor Create; virtual;
procedure Clear; virtual;
property ID: string read GetID write SetID;
end;
//------------------------------------------------------------------------------
TStripeBaseObjectList<T> = class(TInterfacedObject, IStripeBaseObjectList<T>)
strict private
FItems: TList<T>;
private
function GetCount: integer;
protected
constructor Create; virtual;
destructor Destroy; override;
function CreateObject: T; virtual; abstract;
function AddObject: T; virtual;
function GetListID: string; virtual; abstract;
procedure Clear;
procedure LoadFromJson(AJson: TJSONObject); virtual; abstract;
function GetItem(index: integer): T;
property Count: integer read GetCount;
property Item[index: integer]: T read GetItem; default;
end;
//------------------------------------------------------------------------------
TStripeCharge = class(TStripeBaseObject, IStripeCharge)
strict private
FCreated: TDateTime;
FDesc: string;
FLiveMode: Boolean;
FPaid: Boolean;
FStatus: string;
FAmountPence: integer;
FCurrency: TStripeCurrency;
FRefunded: Boolean;
FCustomer: string;
FCard: IStripeCard;
private
function GetCreated: TDatetime;
function GetLiveMode: Boolean;
function GetPaid: Boolean;
function GetStatus: string;
function GetAmountPence: integer;
function GetCurrency: TStripeCurrency;
function GetRefunded: Boolean;
function GetCustomer: string;
function GetCard: IStripeCard;
function GetDesc: string;
protected
function GetObject: string; override;
procedure Clear; override;
procedure LoadFromJson(AJson: TJsonObject); override;
property Created: TDateTime read GetCreated;
property LiveMode: Boolean read GetLiveMode;
property Paid: Boolean read GetPaid;
property Status: string read GetStatus;
property AmountPence: integer read GetAmountPence;
property Currency: TStripeCurrency read GetCurrency;
property Refunded: Boolean read GetRefunded;
property Customer: string read GetCustomer;
property Card: IStripeCard read GetCard;
property Desc: string read GetDesc;
public
constructor Create; override;
end;
//------------------------------------------------------------------------------
TStripeCard = class(TStripeBaseObject, IStripeCard)
strict private
FBrand: string;
FLast4: string;
FExpMonth: integer;
FExpYear: integer;
private
function GetBrand: string;
function GetLast4: string;
function GetExpMonth: integer;
function GetExpYear: integer;
protected
function GetObject: string; override;
procedure Clear; override;
procedure LoadFromJson(AJson: TJsonObject); override;
public
property Last4: string read GetLast4;
property Brand: string read GetBrand;
property ExpMonth: integer read GetExpMonth;
property ExpYear: integer read GetExpYear;
end;
//------------------------------------------------------------------------------
TStripePlan = class(TStripeBaseObject, IStripePlan)
strict private
FInterval: string;
FName: string;
FCreated: TDateTime;
FAmountPence: integer;
FCurrency: TStripeCurrency;
FIntervalCount: integer;
FTrialPeriodDays: integer;
FStatementDescriptor: string;
private
function GetAmountPence: integer;
function GetCreated: TDateTime;
function GetCurrency: TStripeCurrency;
function GetInterval: string;
function GetIntervalCount: integer;
function GetName: string;
function GetStatementDescriptor: string;
function GetTrialPeriodDays: integer;
protected
function GetObject: string; override;
procedure LoadFromJson(AJson: TJsonObject); override;
property Interval: string read GetInterval;
property Name: string read GetName;
property Created: TDateTime read GetCreated;
property AmountPence: integer read GetAmountPence;
property Currency: TStripeCurrency read GetCurrency;
property IntervalCount: integer read GetIntervalCount;
property TrialPeriodDays: integer read GetTrialPeriodDays;
property StatementDescriptor: string read GetStatementDescriptor;
end;
//------------------------------------------------------------------------------
TStripeSubscription = class(TStripeBaseObject, IStripeSubscription)
strict private
FPlan: IStripePlan;
FStart: TDateTime;
FStatus: string;
FCustomer: string;
FCurrentPeriodStart: TDateTime;
FCurrentPeriodEnd: TDateTime;
FEndedAt: TDateTime;
FTrialStart: TDateTime;
FTrialEnd: TDateTime;
FCancelledAt: TDateTime;
FQuantity: integer;
FTaxPercent: Single;
private
function GetCancelledAt: TDateTime;
function GetCurrentPeriodEnd: TDateTime;
function GetCurrentPeriodStart: TDateTime;
function GetCustomer: string;
function GetEndedAt: TDateTime;
function GetPlan: IStripePlan;
function GetQuantity: integer;
function GetStart: TDateTime;
function GetStatus: string;
function GetTaxPercent: single;
function GetTrialEnd: TDateTime;
function GetTrialStart: TDateTime;
protected
function GetObject: string; override;
procedure LoadFromJson(AJson: TJsonObject); override;
public
constructor Create; override;
property Plan: IStripePlan read GetPlan;
property Start: TDateTime read GetStart;
property Status: string read GetStatus;
property Customer: string read GetCustomer;
property CurrentPeriodStart: TDateTime read GetCurrentPeriodStart;
property CurrentPeriodEnd: TDateTime read GetCurrentPeriodEnd;
property EndedAt: TDateTime read GetEndedAt;
property TrialStart: TDateTime read GetTrialStart;
property TrialEnd: TDateTime read GetTrialEnd;
property CancelledAt: TDateTime read GetCancelledAt;
property Quantity: integer read GetQuantity;
property TaxPercent: single read GetTaxPercent;
end;
//------------------------------------------------------------------------------
TStripeCustomer = class(TStripeBaseObject, IStripeCustomer)
strict private
FEmail: string;
FName: string;
FDescription: string;
private
function GetEmail: string;
procedure SetEmail(const Value: string);
function GetDescription: string;
procedure SetDescription(const Value: string);
function GetName: string;
procedure SetName(const Value: string);
protected
function GetObject: string; override;
procedure LoadFromJson(AJson: TJsonObject); override;
property Name: string read GetName write SetName;
property Email: string read GetEmail write SetEmail;
property Description: string read GetDescription write SetDescription;
public
procedure Assign(ACustomer: IStripeCustomer);
procedure Clear; override;
end;
//------------------------------------------------------------------------------
TStripeCustomerList = class(TStripeBaseObjectList<IStripeCustomer>, IStripeCustomerList)
protected
function CreateObject: IStripeCustomer; override;
function GetListID: string; override;
procedure LoadFromJson(AJson: TJsonObject); override;
end;
//------------------------------------------------------------------------------
TStripe = class(TInterfacedObject, IStripe)
strict private
FSecretKey: string;
FLastError: string;
FLastJsonResult: string;
private
procedure CheckForError(AJson: TJsonObject);
{$IFDEF USE_NET_HTTP}
procedure NetHTTPClient1AuthEvent(const Sender: TObject;
AnAuthTarget: TAuthTargetType;
const ARealm, AURL: string; var AUserName,
APassword: string; var AbortAuth: Boolean;
var Persistence: TAuthPersistenceType);
function CreateHttp: TNetHTTPClient;
{$ELSE}
function CreateHttp: TIdHTTP;
procedure DoAuthorization(Sender: TObject; Authentication: TIdAuthentication; var Handled: Boolean);
{$ENDIF}
function GetHttp(AMethod: string; AParams: TStrings): string;
function PostHttp(AToken, AMethod: string; AParams: TStrings): string;
function DeleteHttp(AMethod: string): string;
function GetLastError: string;
function GetLastJsonResult: string;
protected
function CreateToken(ACardNum: string; AExpMonth, AExpYear: integer; ACvc: string): string;
function CreateCharge(AToken, ADescription: string;
AAmountPence: integer;
AMetaData: TStrings;
var AError: string;
const ACurrency: TStripeCurrency = scGbp): IStripeCharge;
function CreateCard(ACustID, ACardToken: string; var AError: string): IStripeCard; overload;
function CreateCard(ACustID, ACardNum: string; AExpMonth, AExpYear: integer; ACvc: string; var AError: string): IStripeCard; overload;
function DeleteCard(ACustID, ACardID: string): Boolean;
function UpdateDefaultSource(ACustID, ACardID: string): Boolean;
procedure UpdateCustomerValue(ACustID, AField, AValue: string);
function CreateChargeForCustomer(ACustID, ADescription: string; AAmountPence: integer; const ACurrency: TStripeCurrency = scGbp): IStripeCharge;
function GetAccount: string;
function GetCapabilities: string;
function GetPersons: string;
function CustomerExists(ACustID: string; ACustomer: IStripeCustomer): Boolean;
function GetCustomer(ACustID: string): IStripeCustomer;
function GetCustomers(const ACreatedAfter: TDateTime = -1;
const ACreatedBefore: TDateTime = -1;
const ALimit: Integer = 10): IStripeCustomerList;
function GetBalance: Extended;
function CreateCustomer(AEmail, ADescription: string): IStripeCustomer;
property LastError: string read GetLastError;
property LastJsonResult: string read GetLastJsonResult;
public
constructor Create(ASecretKey: string);
end;
//------------------------------------------------------------------------------
{$IFNDEF USE_NET_HTTP}
var
ASsl: TIdSSLIOHandlerSocketOpenSSL;
{$ENDIF}
function CreateStripe(ASecretKey: string): IStripe;
begin
Result := TStripe.Create(ASecretKey);
end;
function CreateStripeCustomer: IStripeCustomer;
begin
Result := TStripeCustomer.Create;
end;
function CurrencyToString(ACurrency: TStripeCurrency): string;
begin
Result := '';
case ACurrency of
scGbp: Result := 'gbp';
scUsd: Result := 'usd';
end;
end;
function StringToCurrency(AValue: string): TStripeCurrency;
begin
AValue := LowerCase(AValue);
Result := scUnknown;
if AValue = 'gbp' then Result := scGbp;
if AValue = 'usd' then Result := scUsd;
end;
//------------------------------------------------------------------------------
{ TStripe }
constructor TStripe.Create(ASecretKey: string);
begin
inherited Create;
FSecretKey := ASecretKey;
end;
{$IFNDEF USE_NET_HTTP}
procedure TStripe.DoAuthorization(Sender: TObject; Authentication: TIdAuthentication; var Handled: Boolean);
begin
Authentication.Username := FSecretKey;
Authentication.Password := '';
end;
{$ENDIF}
procedure TStripe.CheckForError(AJson: TJsonObject);
var
AError: TJSONObject;
begin
FLastError := '';
{$IFDEF JSONDATAOBJECTS}
if AJson.Contains('error') then
begin
AError := AJson.O['error'] as TJSONObject;
FLastError := AError.s['message'];
end;
{$ELSE}
if AJson.Values['error'] <> nil then
begin
AError := AJson.Values['error'] as TJSONObject;
FLastError := AError.Values['message'].Value;
end;
{$ENDIF}
end;
function TStripe.CreateCard(ACustID, ACardToken: string; var AError: string): IStripeCard;
var
AParams: TStrings;
AResult: string;
AJson: TJSONObject;
begin
Result := TStripeCard.Create;
AParams := TStringList.Create;
try
AParams.Values['source'] := ACardToken;
AResult := PostHttp('', C_CUSTOMERS+'/'+ACustID+'/sources',AParams);
{$IFDEF JSONDATAOBJECTS}
AJson := TJSONObject.Parse(AResult) as TJSONObject;
{$ELSE}
AJson := TJSONObject.ParseJSONValue(AResult) as TJSONObject;
{$ENDIF}
try
CheckForError(AJson);
AError := FLastError;
if FLastError <> '' then
Exit;
Result.LoadFromJson(AJson);// := // AJson.Values['id'].Value;
finally
AJson.Free;
end;
finally
AParams.Free;
end;
end;
function TStripe.CreateCharge(AToken, ADescription: string; AAmountPence: integer; AMetaData: TStrings; var AError: string; const ACurrency: TStripeCurrency = scGbp): IStripeCharge;
var
AParams: TStrings;
AResult: string;
AJson: TJSONObject;
ICount: integer;
begin
Result := TStripeCharge.Create;
AParams := TStringList.Create;
try
AParams.Values['amount'] := IntToStr(AAmountPence);
AParams.Values['currency'] := CurrencyToString(ACurrency);
AParams.Values['description'] := ADescription;
if AMetaData <> nil then
begin
for ICount := 0 to AMetaData.Count-1 do
AParams.Values['metadata['+AMetaData.Names[ICount]+']'] := AMetaData.ValueFromIndex[ICount];
end;
AResult := PostHttp(AToken, C_CHARGES, AParams);
{$IFDEF JSONDATAOBJECTS}
AJson := TJSONObject.Parse(AResult) as TJSONObject;
{$ELSE}
AJson := TJSONObject.ParseJSONValue(AResult) as TJSONObject;
{$ENDIF}
try
CheckForError(AJson);
AError := FLastError;
Result.LoadFromJson(AJson);
finally
AJson.Free;
end;
finally
AParams.Free;
end;
end;
function TStripe.CreateChargeForCustomer(ACustID, ADescription: string;
AAmountPence: integer; const ACurrency: TStripeCurrency): IStripeCharge;
var
AError: string;
begin
Result := CreateCharge(ACustID, ADescription, AAmountPence, nil, AError, ACurrency);
end;
{$IFDEF USE_NET_HTTP}
function TStripe.CreateHttp: TNetHTTPClient;
begin
Result := TNetHTTPClient.Create(nil);
Result.OnAuthEvent := NetHTTPClient1AuthEvent;
end;
{$ELSE}
function TStripe.CreateHttp: TIdHTTP;
begin
Result := TIdHTTP.Create(nil);
Result.IOHandler := ASsl;
Result.OnAuthorization := DoAuthorization;
end;
{$ENDIF}
function TStripe.CreateToken(ACardNum: string; AExpMonth, AExpYear: integer;
ACvc: string): string;
var
AParams: TStrings;
AResult: string;
AJson: TJSONObject;
begin
AParams := TStringList.Create;
try
AParams.Values['card[number]'] := ACardNum;
AParams.Values['card[exp_month]'] := IntToStr(AExpMonth);
AParams.Values['card[exp_year]'] := IntToStr(AExpYear);
AParams.Values['card[cvc]'] := ACvc;
AResult := PostHttp('', C_TOKENS,AParams);
{$IFDEF JSONDATAOBJECTS}
AJson := TJSONObject.Parse(AResult) as TJSONObject;
{$ELSE}
AJson := TJSONObject.ParseJSONValue(AResult) as TJSONObject;
{$ENDIF}
CheckForError(AJson);
try
Result := AJson.Values['id'].Value;
finally
AJson.Free;
end;
finally
AParams.Free;
end;
end;
function TStripe.CreateCard(ACustID, ACardNum: string; AExpMonth, AExpYear: integer;
ACvc: string; var AError: string): IStripeCard;
var
AParams: TStrings;
AResult: string;
AJson: TJSONObject;
begin
Result := TStripeCard.Create;
AParams := TStringList.Create;
try
AParams.Values['source[object]'] := 'card';
AParams.Values['source[number]'] := ACardNum;
AParams.Values['source[exp_month]'] := IntToStr(AExpMonth);
AParams.Values['source[exp_year]'] := IntToStr(AExpYear);
AParams.Values['source[cvc]'] := ACvc;
AResult := PostHttp('', C_CUSTOMERS+'/'+ACustID+'/sources',AParams);
{$IFDEF JSONDATAOBJECTS}
AJson := TJSONObject.Parse(AResult) as TJSONObject;
{$ELSE}
AJson := TJSONObject.ParseJSONValue(AResult) as TJSONObject;
{$ENDIF}
try
CheckForError(AJson);
AError := FLastError;
if FLastError <> '' then
Exit;
Result.LoadFromJson(AJson);
finally
AJson.Free;
end;
finally
AParams.Free;
end;
end;
function TStripe.CustomerExists(ACustID: string; ACustomer: IStripeCustomer): Boolean;
var
ACust: IStripeCustomer;
begin
ACust := GetCustomer(ACustID);
Result := ACust.ID <> '';
if (Result) and (ACustomer <> nil) then
ACustomer.Assign(ACust);
end;
function TStripe.DeleteCard(ACustID, ACardID: string): Boolean;
var
AResult: string;
begin
AResult := DeleteHttp('customers/'+ACustID+'/sources/'+ACardID);
Result := True;
end;
function TStripe.GetAccount: string;
begin
Result := GetHttp(C_ACCOUNT, nil);
end;
function TStripe.GetBalance: Extended;
var
AResult: string;
begin
Result := 0;
AResult := GetHttp(C_BALANCE, nil);
end;
function TStripe.GetCapabilities: string;
begin
Result := GetHttp('accounts/acct_1Ei2lxJ9jnzBMBPN/capabilities', nil);
end;
function TStripe.GetCustomer(ACustID: string): IStripeCustomer;
var
AResult: string;
AJson: TJSONObject;
begin
Result := TStripeCustomer.Create;
AResult := GetHttp(C_CUSTOMERS+'/'+ACustID, nil);
{$IFDEF JSONDATAOBJECTS}
AJson := TJSONObject.Parse(AResult) as TJSONObject;
{$ELSE}
AJson := TJSONObject.ParseJSONValue(AResult) as TJSONObject;
{$ENDIF}
try
CheckForError(AJson);
if FLastError <> '' then
begin
Result.Clear;
Exit;
end;
{$IFDEF JSONDATAOBJECTS}
if AJson.S['deleted'] = 'true' then
{$ELSE}
if AJson.Values['deleted'].Value = 'true' then
{$ENDIF}
begin
Result.Clear;
Exit;
end;
Result.LoadFromJson(AJson);
finally
AJson.Free;
end;
end;
function TStripe.GetCustomers(const ACreatedAfter: TDateTime = -1;
const ACreatedBefore: TDateTime = -1;
const ALimit: Integer = 10): IStripeCustomerList;
var
AResult: string;
AJson: TJSONObject;
AParams: TStrings;
begin
AParams := TStringList.Create;
try
if ACreatedAfter > -1 then AParams.Values['created[gt]'] := IntToStr(DateTimeToUnix(ACreatedAfter));
if ACreatedBefore > -1 then AParams.Values['created[lt]'] := IntToStr(DateTimeToUnix(ACreatedBefore));
AParams.Values['limit'] := IntToStr(ALimit);
Result := TStripeCustomerList.Create;
AResult := GetHttp(C_CUSTOMERS, AParams);
{$IFDEF JSONDATAOBJECTS}
AJson := TJSONObject.Parse(AResult) as TJSONObject;
{$ELSE}
AJson := TJSONObject.ParseJSONValue(AResult) as TJSONObject;
{$ENDIF}
try
Result.LoadFromJson(AJson);
finally
AJson.Free;
end;
finally
AParams.Free;
end;
end;
function TStripe.GetHttp(AMethod: string; AParams: TStrings): string;
function ParamsToUrl(AStrings: TStrings): string;
var
ICount: integer;
begin
Result := '';
for ICount := 0 to AStrings.Count-1 do
begin
Result := Result + AStrings[ICount];
if ICount < AStrings.Count-1 then
Result := Result + '&';
end;
end;
var
{$IFDEF USE_NET_HTTP}
AHttp: TNetHTTPClient;
AResponse: IHTTPResponse;
{$ELSE}
AHttp: TIdHTTP;
{$ENDIF}
AUrl: string;
begin
AHttp := CreateHttp;
try
AUrl := 'https://api.stripe.com/v1/'+AMethod;
if AParams <> nil then
begin
if AParams.Count > 0 then
AUrl := AUrl + '?'+ParamsToUrl(AParams);
end;
{$IFDEF USE_NET_HTTP}
AHttp.CustomHeaders['Authorization'] := 'Bearer '+FSecretKey;
AResponse := AHttp.Get(AUrl);
Result := AResponse.ContentAsString;
FLastJsonResult := Result;
{$ELSE}
AHttp.Request.CustomHeaders.AddValue('Authorization', 'Bearer '+FSecretKey);
Result := AHttp.Get(AUrl);
{$ENDIF}
finally
AHttp.Free;
end;
end;
function TStripe.GetLastError: string;
begin
Result := FLastError;
end;
function TStripe.GetLastJsonResult: string;
begin
Result := FLastJsonResult;
end;
function TStripe.GetPersons: string;
begin
Result := GetHttp('accounts/acct_1Ehfk3C0L5u7blDo/persons', nil);
end;
function TStripe.PostHttp(AToken, AMethod: string; AParams: TStrings): string;
var
{$IFDEF USE_NET_HTTP}
AHttp: TNetHTTPClient;
AResponse: IHTTPResponse;
{$ELSE}
AHttp: TIdHTTP;
AResponse: TStringStream;
{$ENDIF}
begin
AHttp := CreateHttp;
try
if AToken <> '' then
begin
if Pos('tok_', AToken) = 1 then AParams.Values['source'] := AToken;
if Pos('cus_', AToken) = 1 then AParams.Values['customer'] := AToken;
end;
{$IFDEF USE_NET_HTTP}
AHttp.CustomHeaders['Authorization'] := 'Bearer '+FSecretKey;
AResponse := AHttp.Post('https://api.stripe.com/v1/'+AMethod, AParams);
Result := AResponse.ContentAsString;
FLastJsonResult := Result;
{$ELSE}
AHttp.Request.CustomHeaders.AddValue('Authorization', 'Bearer '+FSecretKey);
AResponse := TStringStream.Create;
try
AHttp.Post('https://api.stripe.com/v1/'+AMethod, AParams, AResponse);
Result := AResponse.DataString;
finally
AResponse.Free;
end;
{$ENDIF}
finally
AHttp.Free;
end;
end;
procedure TStripe.UpdateCustomerValue(ACustID, AField, AValue: string);
var
AParams: TStrings;
AResult: string;
AJson: TJSONObject;
begin
AParams := TStringList.Create;
try
AParams.Values[AField] := AValue;
AResult := PostHttp('', C_CUSTOMERS+'/'+ACustID, AParams);
{$IFDEF JSONDATAOBJECTS}
AJson := TJSONObject.Parse(AResult) as TJSONObject;
{$ELSE}
AJson := TJSONObject.ParseJSONValue(AResult) as TJSONObject;
{$ENDIF}
try
CheckForError(AJson);
finally
AJson.Free;
end;
finally
AParams.Free;
end;
end;
function TStripe.UpdateDefaultSource(ACustID, ACardID: string): Boolean;