-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinvoice.go
executable file
·1947 lines (1713 loc) · 60.4 KB
/
invoice.go
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
package processout
import (
"bytes"
"context"
"encoding/json"
"io"
"net/http"
"net/url"
"strings"
"time"
"gopkg.in/processout.v5/errors"
)
// Invoice represents the Invoice API object
type Invoice struct {
// ID is the iD of the invoice
ID *string `json:"id,omitempty"`
// Project is the project to which the invoice belongs
Project *Project `json:"project,omitempty"`
// ProjectID is the iD of the project to which the invoice belongs
ProjectID *string `json:"project_id,omitempty"`
// Transaction is the transaction generated by the invoice
Transaction *Transaction `json:"transaction,omitempty"`
// TransactionID is the iD of the transaction generated by the invoice
TransactionID *string `json:"transaction_id,omitempty"`
// Customer is the customer linked to the invoice, if any
Customer *Customer `json:"customer,omitempty"`
// CustomerID is the iD of the customer linked to the invoice, if any
CustomerID *string `json:"customer_id,omitempty"`
// Subscription is the subscription to which the invoice is linked to, if any
Subscription *Subscription `json:"subscription,omitempty"`
// SubscriptionID is the iD of the subscription to which the invoice is linked to, if any
SubscriptionID *string `json:"subscription_id,omitempty"`
// Token is the token used to pay the invoice, if any
Token *Token `json:"token,omitempty"`
// TokenID is the iD of the token used to pay the invoice, if any
TokenID *string `json:"token_id,omitempty"`
// Details is the details of the invoice
Details *[]*InvoiceDetail `json:"details,omitempty"`
// URL is the uRL to which you may redirect your customer to proceed with the payment
URL *string `json:"url,omitempty"`
// URLQrcode is the base64-encoded QR code for the invoice URL
URLQrcode *string `json:"url_qrcode,omitempty"`
// Name is the name of the invoice
Name *string `json:"name,omitempty"`
// OrderID is the iD of the order for this transaction in merchant's system
OrderID *string `json:"order_id,omitempty"`
// Amount is the amount to be paid
Amount *string `json:"amount,omitempty"`
// Currency is the currency of the invoice
Currency *string `json:"currency,omitempty"`
// MerchantInitiatorType is the type of the transaction initiated by the merchant (off-session). Can be either one-off or recurring, depending on the nature of the merchant initiated transaction.
MerchantInitiatorType *string `json:"merchant_initiator_type,omitempty"`
// StatementDescriptor is the statement to be shown on the bank statement of your customer
StatementDescriptor *string `json:"statement_descriptor,omitempty"`
// StatementDescriptorPhone is the support phone number shown on the customer's bank statement
StatementDescriptorPhone *string `json:"statement_descriptor_phone,omitempty"`
// StatementDescriptorCity is the city shown on the customer's bank statement
StatementDescriptorCity *string `json:"statement_descriptor_city,omitempty"`
// StatementDescriptorCompany is the your company name shown on the customer's bank statement
StatementDescriptorCompany *string `json:"statement_descriptor_company,omitempty"`
// StatementDescriptorURL is the uRL shown on the customer's bank statement
StatementDescriptorURL *string `json:"statement_descriptor_url,omitempty"`
// Metadata is the metadata related to the invoice, in the form of a dictionary (key-value pair)
Metadata *map[string]string `json:"metadata,omitempty"`
// GatewayData is the dictionary that transmit specific informations to gateways (key-value pair)
GatewayData *map[string]string `json:"gateway_data,omitempty"`
// ReturnURL is the uRL where the customer will be redirected upon payment
ReturnURL *string `json:"return_url,omitempty"`
// CancelURL is the uRL where the customer will be redirected if the payment was canceled
CancelURL *string `json:"cancel_url,omitempty"`
// WebhookURL is the custom webhook URL where updates about this specific payment will be sent, on top of your project-wide URLs
WebhookURL *string `json:"webhook_url,omitempty"`
// RequireBackendCapture is the define whether the invoice can be captured from the front-end or not
RequireBackendCapture *bool `json:"require_backend_capture,omitempty"`
// Sandbox is the define whether or not the invoice is in sandbox environment
Sandbox *bool `json:"sandbox,omitempty"`
// CreatedAt is the date at which the invoice was created
CreatedAt *time.Time `json:"created_at,omitempty"`
// ExpiresAt is the date at which the invoice will expire
ExpiresAt *time.Time `json:"expires_at,omitempty"`
// Risk is the risk information
Risk *InvoiceRisk `json:"risk,omitempty"`
// Shipping is the shipping information
Shipping *InvoiceShipping `json:"shipping,omitempty"`
// Device is the device information
Device *InvoiceDevice `json:"device,omitempty"`
// ExternalFraudTools is the contain objects that'll be forwarded to external fraud tools
ExternalFraudTools *InvoiceExternalFraudTools `json:"external_fraud_tools,omitempty"`
// ExemptionReason3ds2 is the (Deprecated - use sca_exemption_reason) Reason provided to request 3DS2 exemption
ExemptionReason3ds2 *string `json:"exemption_reason_3ds2,omitempty"`
// ScaExemptionReason is the reason provided to request SCA exemption
ScaExemptionReason *string `json:"sca_exemption_reason,omitempty"`
// ChallengeIndicator is the challenge indicator when requesting 3DS2
ChallengeIndicator *string `json:"challenge_indicator,omitempty"`
// Incremental is the a boolean to indicate if an invoice can have incremental authorizations created for it.
Incremental *bool `json:"incremental,omitempty"`
// Tax is the tax for an invoice
Tax *InvoiceTax `json:"tax,omitempty"`
// PaymentType is the payment type
PaymentType *string `json:"payment_type,omitempty"`
// NativeApm is the native APM data
NativeApm *NativeAPMRequest `json:"native_apm,omitempty"`
// InitiationType is the initiation type of invoice
InitiationType *string `json:"initiation_type,omitempty"`
// PaymentIntent is the payment intent of invoice
PaymentIntent *string `json:"payment_intent,omitempty"`
// Billing is the billing information
Billing *InvoiceBilling `json:"billing,omitempty"`
// UnsupportedFeatureBypass is the flags to bypass unsupported features
UnsupportedFeatureBypass *UnsupportedFeatureBypass `json:"unsupported_feature_bypass,omitempty"`
// Verification is the a boolean to indicate if an invoice is a verification invoice. This is used to manually create a verification invoice.
Verification *bool `json:"verification,omitempty"`
// AutoCaptureAt is the a timestamp to indicate when an auto capture should take place following an authorization. This takes priority over the value sent in the authorization request.
AutoCaptureAt *time.Time `json:"auto_capture_at,omitempty"`
client *ProcessOut
}
// GetID implements the Identiable interface
func (s *Invoice) GetID() string {
if s.ID == nil {
return ""
}
return *s.ID
}
// SetClient sets the client for the Invoice object and its
// children
func (s *Invoice) SetClient(c *ProcessOut) *Invoice {
if s == nil {
return s
}
s.client = c
if s.Project != nil {
s.Project.SetClient(c)
}
if s.Transaction != nil {
s.Transaction.SetClient(c)
}
if s.Customer != nil {
s.Customer.SetClient(c)
}
if s.Subscription != nil {
s.Subscription.SetClient(c)
}
if s.Token != nil {
s.Token.SetClient(c)
}
if s.Risk != nil {
s.Risk.SetClient(c)
}
if s.Shipping != nil {
s.Shipping.SetClient(c)
}
if s.Device != nil {
s.Device.SetClient(c)
}
if s.ExternalFraudTools != nil {
s.ExternalFraudTools.SetClient(c)
}
if s.Tax != nil {
s.Tax.SetClient(c)
}
if s.NativeApm != nil {
s.NativeApm.SetClient(c)
}
if s.Billing != nil {
s.Billing.SetClient(c)
}
if s.UnsupportedFeatureBypass != nil {
s.UnsupportedFeatureBypass.SetClient(c)
}
return s
}
// Prefil prefills the object with data provided in the parameter
func (s *Invoice) Prefill(c *Invoice) *Invoice {
if c == nil {
return s
}
s.ID = c.ID
s.Project = c.Project
s.ProjectID = c.ProjectID
s.Transaction = c.Transaction
s.TransactionID = c.TransactionID
s.Customer = c.Customer
s.CustomerID = c.CustomerID
s.Subscription = c.Subscription
s.SubscriptionID = c.SubscriptionID
s.Token = c.Token
s.TokenID = c.TokenID
s.Details = c.Details
s.URL = c.URL
s.URLQrcode = c.URLQrcode
s.Name = c.Name
s.OrderID = c.OrderID
s.Amount = c.Amount
s.Currency = c.Currency
s.MerchantInitiatorType = c.MerchantInitiatorType
s.StatementDescriptor = c.StatementDescriptor
s.StatementDescriptorPhone = c.StatementDescriptorPhone
s.StatementDescriptorCity = c.StatementDescriptorCity
s.StatementDescriptorCompany = c.StatementDescriptorCompany
s.StatementDescriptorURL = c.StatementDescriptorURL
s.Metadata = c.Metadata
s.GatewayData = c.GatewayData
s.ReturnURL = c.ReturnURL
s.CancelURL = c.CancelURL
s.WebhookURL = c.WebhookURL
s.RequireBackendCapture = c.RequireBackendCapture
s.Sandbox = c.Sandbox
s.CreatedAt = c.CreatedAt
s.ExpiresAt = c.ExpiresAt
s.Risk = c.Risk
s.Shipping = c.Shipping
s.Device = c.Device
s.ExternalFraudTools = c.ExternalFraudTools
s.ExemptionReason3ds2 = c.ExemptionReason3ds2
s.ScaExemptionReason = c.ScaExemptionReason
s.ChallengeIndicator = c.ChallengeIndicator
s.Incremental = c.Incremental
s.Tax = c.Tax
s.PaymentType = c.PaymentType
s.NativeApm = c.NativeApm
s.InitiationType = c.InitiationType
s.PaymentIntent = c.PaymentIntent
s.Billing = c.Billing
s.UnsupportedFeatureBypass = c.UnsupportedFeatureBypass
s.Verification = c.Verification
s.AutoCaptureAt = c.AutoCaptureAt
return s
}
// InvoiceIncrementAuthorizationParameters is the structure representing the
// additional parameters used to call Invoice.IncrementAuthorization
type InvoiceIncrementAuthorizationParameters struct {
*Options
*Invoice
Metadata interface{} `json:"metadata"`
}
// IncrementAuthorization allows you to create an incremental authorization
func (s Invoice) IncrementAuthorization(amount float64, options ...InvoiceIncrementAuthorizationParameters) (*Transaction, error) {
return s.IncrementAuthorizationWithContext(context.Background(), amount, options...)
}
// IncrementAuthorization allows you to create an incremental authorization, passes the provided context to the request
func (s Invoice) IncrementAuthorizationWithContext(ctx context.Context, amount float64, options ...InvoiceIncrementAuthorizationParameters) (*Transaction, error) {
if s.client == nil {
panic("Please use the client.NewInvoice() method to create a new Invoice object")
}
if len(options) > 1 {
panic("The options parameter should only be provided once.")
}
opt := InvoiceIncrementAuthorizationParameters{}
if len(options) == 1 {
opt = options[0]
}
if opt.Options == nil {
opt.Options = &Options{}
}
s.Prefill(opt.Invoice)
type Response struct {
Transaction *Transaction `json:"transaction"`
HasMore bool `json:"has_more"`
Success bool `json:"success"`
Message string `json:"message"`
Code string `json:"error_type"`
}
data := struct {
*Options
Metadata interface{} `json:"metadata"`
Amount interface{} `json:"amount"`
}{
Options: opt.Options,
Metadata: opt.Metadata,
Amount: amount,
}
body, err := json.Marshal(data)
if err != nil {
return nil, errors.New(err, "", "")
}
path := "/invoices/" + url.QueryEscape(*s.ID) + "/increment_authorization"
req, err := http.NewRequestWithContext(
ctx,
"POST",
Host+path,
bytes.NewReader(body),
)
if err != nil {
return nil, errors.NewNetworkError(err)
}
setupRequest(s.client, opt.Options, req)
res, err := s.client.HTTPClient.Do(req)
if err != nil {
return nil, errors.NewNetworkError(err)
}
payload := &Response{}
defer res.Body.Close()
if res.StatusCode >= 500 {
return nil, errors.New(nil, "", "An unexpected error occurred while processing your request.. A lot of sweat is already flowing from our developers head!")
}
err = json.NewDecoder(res.Body).Decode(payload)
if err != nil {
return nil, errors.New(err, "", "")
}
if !payload.Success {
erri := errors.NewFromResponse(res.StatusCode, payload.Code,
payload.Message)
return nil, erri
}
payload.Transaction.SetClient(s.client)
return payload.Transaction, nil
}
// InvoiceAuthorizeParameters is the structure representing the
// additional parameters used to call Invoice.Authorize
type InvoiceAuthorizeParameters struct {
*Options
*Invoice
Synchronous interface{} `json:"synchronous"`
RetryDropLiabilityShift interface{} `json:"retry_drop_liability_shift"`
CaptureAmount interface{} `json:"capture_amount"`
EnableThreeDS2 interface{} `json:"enable_three_d_s_2"`
AllowFallbackToSale interface{} `json:"allow_fallback_to_sale"`
AutoCaptureAt interface{} `json:"auto_capture_at"`
Metadata interface{} `json:"metadata"`
OverrideMacBlocking interface{} `json:"override_mac_blocking"`
ExternalThreeDS interface{} `json:"external_three_d_s"`
SaveSource interface{} `json:"save_source"`
}
// Authorize allows you to authorize the invoice using the given source (customer or token)
func (s Invoice) Authorize(source string, options ...InvoiceAuthorizeParameters) (*Transaction, *CustomerAction, error) {
return s.AuthorizeWithContext(context.Background(), source, options...)
}
// Authorize allows you to authorize the invoice using the given source (customer or token), passes the provided context to the request
func (s Invoice) AuthorizeWithContext(ctx context.Context, source string, options ...InvoiceAuthorizeParameters) (*Transaction, *CustomerAction, error) {
if s.client == nil {
panic("Please use the client.NewInvoice() method to create a new Invoice object")
}
if len(options) > 1 {
panic("The options parameter should only be provided once.")
}
opt := InvoiceAuthorizeParameters{}
if len(options) == 1 {
opt = options[0]
}
if opt.Options == nil {
opt.Options = &Options{}
}
s.Prefill(opt.Invoice)
type Response struct {
Transaction *Transaction `json:"transaction"`
CustomerAction *CustomerAction `json:"customer_action"`
HasMore bool `json:"has_more"`
Success bool `json:"success"`
Message string `json:"message"`
Code string `json:"error_type"`
}
data := struct {
*Options
Device interface{} `json:"device"`
Incremental interface{} `json:"incremental"`
Synchronous interface{} `json:"synchronous"`
RetryDropLiabilityShift interface{} `json:"retry_drop_liability_shift"`
CaptureAmount interface{} `json:"capture_amount"`
EnableThreeDS2 interface{} `json:"enable_three_d_s_2"`
AllowFallbackToSale interface{} `json:"allow_fallback_to_sale"`
AutoCaptureAt interface{} `json:"auto_capture_at"`
Metadata interface{} `json:"metadata"`
OverrideMacBlocking interface{} `json:"override_mac_blocking"`
ExternalThreeDS interface{} `json:"external_three_d_s"`
SaveSource interface{} `json:"save_source"`
Source interface{} `json:"source"`
}{
Options: opt.Options,
Device: s.Device,
Incremental: s.Incremental,
Synchronous: opt.Synchronous,
RetryDropLiabilityShift: opt.RetryDropLiabilityShift,
CaptureAmount: opt.CaptureAmount,
EnableThreeDS2: opt.EnableThreeDS2,
AllowFallbackToSale: opt.AllowFallbackToSale,
AutoCaptureAt: opt.AutoCaptureAt,
Metadata: opt.Metadata,
OverrideMacBlocking: opt.OverrideMacBlocking,
ExternalThreeDS: opt.ExternalThreeDS,
SaveSource: opt.SaveSource,
Source: source,
}
body, err := json.Marshal(data)
if err != nil {
return nil, nil, errors.New(err, "", "")
}
path := "/invoices/" + url.QueryEscape(*s.ID) + "/authorize"
req, err := http.NewRequestWithContext(
ctx,
"POST",
Host+path,
bytes.NewReader(body),
)
if err != nil {
return nil, nil, errors.NewNetworkError(err)
}
setupRequest(s.client, opt.Options, req)
res, err := s.client.HTTPClient.Do(req)
if err != nil {
return nil, nil, errors.NewNetworkError(err)
}
payload := &Response{}
defer res.Body.Close()
if res.StatusCode >= 500 {
return nil, nil, errors.New(nil, "", "An unexpected error occurred while processing your request.. A lot of sweat is already flowing from our developers head!")
}
err = json.NewDecoder(res.Body).Decode(payload)
if err != nil {
return nil, nil, errors.New(err, "", "")
}
if !payload.Success {
erri := errors.NewFromResponse(res.StatusCode, payload.Code,
payload.Message)
return nil, nil, erri
}
payload.Transaction.SetClient(s.client)
payload.CustomerAction.SetClient(s.client)
return payload.Transaction, payload.CustomerAction, nil
}
// InvoiceCaptureParameters is the structure representing the
// additional parameters used to call Invoice.Capture
type InvoiceCaptureParameters struct {
*Options
*Invoice
AuthorizeOnly interface{} `json:"authorize_only"`
Synchronous interface{} `json:"synchronous"`
RetryDropLiabilityShift interface{} `json:"retry_drop_liability_shift"`
CaptureAmount interface{} `json:"capture_amount"`
AutoCaptureAt interface{} `json:"auto_capture_at"`
EnableThreeDS2 interface{} `json:"enable_three_d_s_2"`
Metadata interface{} `json:"metadata"`
CaptureStatementDescriptor interface{} `json:"capture_statement_descriptor"`
OverrideMacBlocking interface{} `json:"override_mac_blocking"`
ExternalThreeDS interface{} `json:"external_three_d_s"`
SaveSource interface{} `json:"save_source"`
}
// Capture allows you to capture the invoice using the given source (customer or token)
func (s Invoice) Capture(source string, options ...InvoiceCaptureParameters) (*Transaction, *CustomerAction, error) {
return s.CaptureWithContext(context.Background(), source, options...)
}
// Capture allows you to capture the invoice using the given source (customer or token), passes the provided context to the request
func (s Invoice) CaptureWithContext(ctx context.Context, source string, options ...InvoiceCaptureParameters) (*Transaction, *CustomerAction, error) {
if s.client == nil {
panic("Please use the client.NewInvoice() method to create a new Invoice object")
}
if len(options) > 1 {
panic("The options parameter should only be provided once.")
}
opt := InvoiceCaptureParameters{}
if len(options) == 1 {
opt = options[0]
}
if opt.Options == nil {
opt.Options = &Options{}
}
s.Prefill(opt.Invoice)
type Response struct {
Transaction *Transaction `json:"transaction"`
CustomerAction *CustomerAction `json:"customer_action"`
HasMore bool `json:"has_more"`
Success bool `json:"success"`
Message string `json:"message"`
Code string `json:"error_type"`
}
data := struct {
*Options
Device interface{} `json:"device"`
Incremental interface{} `json:"incremental"`
AuthorizeOnly interface{} `json:"authorize_only"`
Synchronous interface{} `json:"synchronous"`
RetryDropLiabilityShift interface{} `json:"retry_drop_liability_shift"`
CaptureAmount interface{} `json:"capture_amount"`
AutoCaptureAt interface{} `json:"auto_capture_at"`
EnableThreeDS2 interface{} `json:"enable_three_d_s_2"`
Metadata interface{} `json:"metadata"`
CaptureStatementDescriptor interface{} `json:"capture_statement_descriptor"`
OverrideMacBlocking interface{} `json:"override_mac_blocking"`
ExternalThreeDS interface{} `json:"external_three_d_s"`
SaveSource interface{} `json:"save_source"`
Source interface{} `json:"source"`
}{
Options: opt.Options,
Device: s.Device,
Incremental: s.Incremental,
AuthorizeOnly: opt.AuthorizeOnly,
Synchronous: opt.Synchronous,
RetryDropLiabilityShift: opt.RetryDropLiabilityShift,
CaptureAmount: opt.CaptureAmount,
AutoCaptureAt: opt.AutoCaptureAt,
EnableThreeDS2: opt.EnableThreeDS2,
Metadata: opt.Metadata,
CaptureStatementDescriptor: opt.CaptureStatementDescriptor,
OverrideMacBlocking: opt.OverrideMacBlocking,
ExternalThreeDS: opt.ExternalThreeDS,
SaveSource: opt.SaveSource,
Source: source,
}
body, err := json.Marshal(data)
if err != nil {
return nil, nil, errors.New(err, "", "")
}
path := "/invoices/" + url.QueryEscape(*s.ID) + "/capture"
req, err := http.NewRequestWithContext(
ctx,
"POST",
Host+path,
bytes.NewReader(body),
)
if err != nil {
return nil, nil, errors.NewNetworkError(err)
}
setupRequest(s.client, opt.Options, req)
res, err := s.client.HTTPClient.Do(req)
if err != nil {
return nil, nil, errors.NewNetworkError(err)
}
payload := &Response{}
defer res.Body.Close()
if res.StatusCode >= 500 {
return nil, nil, errors.New(nil, "", "An unexpected error occurred while processing your request.. A lot of sweat is already flowing from our developers head!")
}
err = json.NewDecoder(res.Body).Decode(payload)
if err != nil {
return nil, nil, errors.New(err, "", "")
}
if !payload.Success {
erri := errors.NewFromResponse(res.StatusCode, payload.Code,
payload.Message)
return nil, nil, erri
}
payload.Transaction.SetClient(s.client)
payload.CustomerAction.SetClient(s.client)
return payload.Transaction, payload.CustomerAction, nil
}
// InvoiceFetchCustomerParameters is the structure representing the
// additional parameters used to call Invoice.FetchCustomer
type InvoiceFetchCustomerParameters struct {
*Options
*Invoice
}
// FetchCustomer allows you to get the customer linked to the invoice.
func (s Invoice) FetchCustomer(options ...InvoiceFetchCustomerParameters) (*Customer, error) {
return s.FetchCustomerWithContext(context.Background(), options...)
}
// FetchCustomer allows you to get the customer linked to the invoice., passes the provided context to the request
func (s Invoice) FetchCustomerWithContext(ctx context.Context, options ...InvoiceFetchCustomerParameters) (*Customer, error) {
if s.client == nil {
panic("Please use the client.NewInvoice() method to create a new Invoice object")
}
if len(options) > 1 {
panic("The options parameter should only be provided once.")
}
opt := InvoiceFetchCustomerParameters{}
if len(options) == 1 {
opt = options[0]
}
if opt.Options == nil {
opt.Options = &Options{}
}
s.Prefill(opt.Invoice)
type Response struct {
Customer *Customer `json:"customer"`
HasMore bool `json:"has_more"`
Success bool `json:"success"`
Message string `json:"message"`
Code string `json:"error_type"`
}
data := struct {
*Options
}{
Options: opt.Options,
}
body, err := json.Marshal(data)
if err != nil {
return nil, errors.New(err, "", "")
}
path := "/invoices/" + url.QueryEscape(*s.ID) + "/customers"
req, err := http.NewRequestWithContext(
ctx,
"GET",
Host+path,
bytes.NewReader(body),
)
if err != nil {
return nil, errors.NewNetworkError(err)
}
setupRequest(s.client, opt.Options, req)
res, err := s.client.HTTPClient.Do(req)
if err != nil {
return nil, errors.NewNetworkError(err)
}
payload := &Response{}
defer res.Body.Close()
if res.StatusCode >= 500 {
return nil, errors.New(nil, "", "An unexpected error occurred while processing your request.. A lot of sweat is already flowing from our developers head!")
}
err = json.NewDecoder(res.Body).Decode(payload)
if err != nil {
return nil, errors.New(err, "", "")
}
if !payload.Success {
erri := errors.NewFromResponse(res.StatusCode, payload.Code,
payload.Message)
return nil, erri
}
payload.Customer.SetClient(s.client)
return payload.Customer, nil
}
// InvoiceAssignCustomerParameters is the structure representing the
// additional parameters used to call Invoice.AssignCustomer
type InvoiceAssignCustomerParameters struct {
*Options
*Invoice
}
// AssignCustomer allows you to assign a customer to the invoice.
func (s Invoice) AssignCustomer(customerID string, options ...InvoiceAssignCustomerParameters) (*Customer, error) {
return s.AssignCustomerWithContext(context.Background(), customerID, options...)
}
// AssignCustomer allows you to assign a customer to the invoice., passes the provided context to the request
func (s Invoice) AssignCustomerWithContext(ctx context.Context, customerID string, options ...InvoiceAssignCustomerParameters) (*Customer, error) {
if s.client == nil {
panic("Please use the client.NewInvoice() method to create a new Invoice object")
}
if len(options) > 1 {
panic("The options parameter should only be provided once.")
}
opt := InvoiceAssignCustomerParameters{}
if len(options) == 1 {
opt = options[0]
}
if opt.Options == nil {
opt.Options = &Options{}
}
s.Prefill(opt.Invoice)
type Response struct {
Customer *Customer `json:"customer"`
HasMore bool `json:"has_more"`
Success bool `json:"success"`
Message string `json:"message"`
Code string `json:"error_type"`
}
data := struct {
*Options
CustomerID interface{} `json:"customer_id"`
}{
Options: opt.Options,
CustomerID: customerID,
}
body, err := json.Marshal(data)
if err != nil {
return nil, errors.New(err, "", "")
}
path := "/invoices/" + url.QueryEscape(*s.ID) + "/customers"
req, err := http.NewRequestWithContext(
ctx,
"POST",
Host+path,
bytes.NewReader(body),
)
if err != nil {
return nil, errors.NewNetworkError(err)
}
setupRequest(s.client, opt.Options, req)
res, err := s.client.HTTPClient.Do(req)
if err != nil {
return nil, errors.NewNetworkError(err)
}
payload := &Response{}
defer res.Body.Close()
if res.StatusCode >= 500 {
return nil, errors.New(nil, "", "An unexpected error occurred while processing your request.. A lot of sweat is already flowing from our developers head!")
}
err = json.NewDecoder(res.Body).Decode(payload)
if err != nil {
return nil, errors.New(err, "", "")
}
if !payload.Success {
erri := errors.NewFromResponse(res.StatusCode, payload.Code,
payload.Message)
return nil, erri
}
payload.Customer.SetClient(s.client)
return payload.Customer, nil
}
// InvoicePayoutParameters is the structure representing the
// additional parameters used to call Invoice.Payout
type InvoicePayoutParameters struct {
*Options
*Invoice
ForceGatewayConfigurationID interface{} `json:"force_gateway_configuration_id"`
}
// Payout allows you to process the payout invoice using the given source (customer or token)
func (s Invoice) Payout(gatewayConfigurationID, source string, options ...InvoicePayoutParameters) (*Transaction, error) {
return s.PayoutWithContext(context.Background(), gatewayConfigurationID, source, options...)
}
// Payout allows you to process the payout invoice using the given source (customer or token), passes the provided context to the request
func (s Invoice) PayoutWithContext(ctx context.Context, gatewayConfigurationID, source string, options ...InvoicePayoutParameters) (*Transaction, error) {
if s.client == nil {
panic("Please use the client.NewInvoice() method to create a new Invoice object")
}
if len(options) > 1 {
panic("The options parameter should only be provided once.")
}
opt := InvoicePayoutParameters{}
if len(options) == 1 {
opt = options[0]
}
if opt.Options == nil {
opt.Options = &Options{}
}
s.Prefill(opt.Invoice)
type Response struct {
Transaction *Transaction `json:"transaction"`
HasMore bool `json:"has_more"`
Success bool `json:"success"`
Message string `json:"message"`
Code string `json:"error_type"`
}
data := struct {
*Options
ForceGatewayConfigurationID interface{} `json:"force_gateway_configuration_id"`
GatewayConfigurationID interface{} `json:"gateway_configuration_id"`
Source interface{} `json:"source"`
}{
Options: opt.Options,
ForceGatewayConfigurationID: opt.ForceGatewayConfigurationID,
GatewayConfigurationID: gatewayConfigurationID,
Source: source,
}
body, err := json.Marshal(data)
if err != nil {
return nil, errors.New(err, "", "")
}
path := "/invoices/" + url.QueryEscape(*s.ID) + "/payout"
req, err := http.NewRequestWithContext(
ctx,
"POST",
Host+path,
bytes.NewReader(body),
)
if err != nil {
return nil, errors.NewNetworkError(err)
}
setupRequest(s.client, opt.Options, req)
res, err := s.client.HTTPClient.Do(req)
if err != nil {
return nil, errors.NewNetworkError(err)
}
payload := &Response{}
defer res.Body.Close()
if res.StatusCode >= 500 {
return nil, errors.New(nil, "", "An unexpected error occurred while processing your request.. A lot of sweat is already flowing from our developers head!")
}
err = json.NewDecoder(res.Body).Decode(payload)
if err != nil {
return nil, errors.New(err, "", "")
}
if !payload.Success {
erri := errors.NewFromResponse(res.StatusCode, payload.Code,
payload.Message)
return nil, erri
}
payload.Transaction.SetClient(s.client)
return payload.Transaction, nil
}
// InvoiceShowNativePaymentTransactionParameters is the structure representing the
// additional parameters used to call Invoice.ShowNativePaymentTransaction
type InvoiceShowNativePaymentTransactionParameters struct {
*Options
*Invoice
}
// ShowNativePaymentTransaction allows you to fetches the Native APM payment
func (s Invoice) ShowNativePaymentTransaction(invoiceID, gatewayConfigurationID string, options ...InvoiceShowNativePaymentTransactionParameters) (*NativeAPMTransactionDetails, error) {
return s.ShowNativePaymentTransactionWithContext(context.Background(), invoiceID, gatewayConfigurationID, options...)
}
// ShowNativePaymentTransaction allows you to fetches the Native APM payment, passes the provided context to the request
func (s Invoice) ShowNativePaymentTransactionWithContext(ctx context.Context, invoiceID, gatewayConfigurationID string, options ...InvoiceShowNativePaymentTransactionParameters) (*NativeAPMTransactionDetails, error) {
if s.client == nil {
panic("Please use the client.NewInvoice() method to create a new Invoice object")
}
if len(options) > 1 {
panic("The options parameter should only be provided once.")
}
opt := InvoiceShowNativePaymentTransactionParameters{}
if len(options) == 1 {
opt = options[0]
}
if opt.Options == nil {
opt.Options = &Options{}
}
s.Prefill(opt.Invoice)
type Response struct {
NativeAPMTransactionDetails *NativeAPMTransactionDetails `json:"native_apm"`
HasMore bool `json:"has_more"`
Success bool `json:"success"`
Message string `json:"message"`
Code string `json:"error_type"`
}
data := struct {
*Options
}{
Options: opt.Options,
}
body, err := json.Marshal(data)
if err != nil {
return nil, errors.New(err, "", "")
}
path := "/invoices/" + url.QueryEscape(invoiceID) + "/native-payment/" + url.QueryEscape(gatewayConfigurationID) + ""
req, err := http.NewRequestWithContext(
ctx,
"GET",
Host+path,
bytes.NewReader(body),
)
if err != nil {
return nil, errors.NewNetworkError(err)
}
setupRequest(s.client, opt.Options, req)
res, err := s.client.HTTPClient.Do(req)
if err != nil {
return nil, errors.NewNetworkError(err)
}
payload := &Response{}
defer res.Body.Close()
if res.StatusCode >= 500 {
return nil, errors.New(nil, "", "An unexpected error occurred while processing your request.. A lot of sweat is already flowing from our developers head!")
}
err = json.NewDecoder(res.Body).Decode(payload)
if err != nil {
return nil, errors.New(err, "", "")
}
if !payload.Success {
erri := errors.NewFromResponse(res.StatusCode, payload.Code,
payload.Message)
return nil, erri
}
payload.NativeAPMTransactionDetails.SetClient(s.client)
return payload.NativeAPMTransactionDetails, nil
}
// InvoiceProcessNativePaymentParameters is the structure representing the
// additional parameters used to call Invoice.ProcessNativePayment
type InvoiceProcessNativePaymentParameters struct {
*Options
*Invoice
GatewayConfigurationID interface{} `json:"gateway_configuration_id"`
NativeApm interface{} `json:"native_apm"`
}
// ProcessNativePayment allows you to process the Native APM payment flow
func (s Invoice) ProcessNativePayment(invoiceID string, options ...InvoiceProcessNativePaymentParameters) (*Transaction, *NativeAPMResponse, error) {
return s.ProcessNativePaymentWithContext(context.Background(), invoiceID, options...)
}
// ProcessNativePayment allows you to process the Native APM payment flow, passes the provided context to the request
func (s Invoice) ProcessNativePaymentWithContext(ctx context.Context, invoiceID string, options ...InvoiceProcessNativePaymentParameters) (*Transaction, *NativeAPMResponse, error) {
if s.client == nil {
panic("Please use the client.NewInvoice() method to create a new Invoice object")
}
if len(options) > 1 {
panic("The options parameter should only be provided once.")
}
opt := InvoiceProcessNativePaymentParameters{}
if len(options) == 1 {
opt = options[0]
}
if opt.Options == nil {
opt.Options = &Options{}
}
s.Prefill(opt.Invoice)
type Response struct {
Transaction *Transaction `json:"transaction"`
NativeAPMResponse *NativeAPMResponse `json:"native_apm"`
HasMore bool `json:"has_more"`
Success bool `json:"success"`
Message string `json:"message"`
Code string `json:"error_type"`
}
data := struct {
*Options
GatewayConfigurationID interface{} `json:"gateway_configuration_id"`
NativeApm interface{} `json:"native_apm"`
}{
Options: opt.Options,
GatewayConfigurationID: opt.GatewayConfigurationID,
NativeApm: opt.NativeApm,
}
body, err := json.Marshal(data)
if err != nil {
return nil, nil, errors.New(err, "", "")
}
path := "/invoices/" + url.QueryEscape(invoiceID) + "/native-payment"