forked from getlago/lago-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpayment_request.go
More file actions
130 lines (104 loc) · 3.4 KB
/
payment_request.go
File metadata and controls
130 lines (104 loc) · 3.4 KB
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
package lago
import (
"context"
"encoding/json"
"fmt"
"time"
"github.com/google/uuid"
)
type PaymentRequestRequest struct {
client *Client
}
type PaymentRequestResult struct {
PaymentRequest *PaymentRequest `json:"payment_request,omitempty"`
PaymentRequests []PaymentRequest `json:"payment_requests,omitempty"`
Meta Metadata `json:"meta,omitempty"`
}
type PaymentRequestListInput struct {
PerPage *int `json:"per_page,omitempty,string"`
Page *int `json:"page,omitempty,string"`
ExternalCustomerID string `json:"external_customer_id,omitempty"`
PaymentStatus string `json:"payment_status,omitempty"`
}
type PaymentRequest struct {
LagoID uuid.UUID `json:"lago_id,omitempty"`
Email string `json:"email,omitempty"`
AmountCurrency Currency `json:"amount_currency,omitempty"`
AmountCents int `json:"amount_cents,omitempty"`
PaymentStatus string `json:"payment_status,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
Customer *Customer `json:"customer,omitempty"`
Invoices []Invoice `json:"invoices,omitempty"`
}
type PaymentRequestParams struct {
PaymentRequest *PaymentRequestInput `json:"payment_request"`
}
type PaymentRequestInput struct {
Email string `json:"email,omitempty"`
ExternalCustomerId string `json:"external_customer_id,omitempty"`
LagoInvoiceIds []string `json:"lago_invoice_ids,omitempty"`
}
func (c *Client) PaymentRequest() *PaymentRequestRequest {
return &PaymentRequestRequest{
client: c,
}
}
func (adr *PaymentRequestRequest) Get(ctx context.Context, paymentRequestID uuid.UUID) (*PaymentRequest, *Error) {
subPath := fmt.Sprintf("%s/%s", "payment_requests", paymentRequestID)
clientRequest := &ClientRequest{
Path: subPath,
Result: &PaymentRequestResult{},
}
result, err := adr.client.Get(ctx, clientRequest)
if err != nil {
return nil, err
}
paymentRequestResult, ok := result.(*PaymentRequestResult)
if !ok {
return nil, &ErrorTypeAssert
}
return paymentRequestResult.PaymentRequest, nil
}
func (ir *PaymentRequestRequest) GetList(ctx context.Context, paymentRequestListInput *PaymentRequestListInput) (*PaymentRequestResult, *Error) {
jsonQueryParams, err := json.Marshal(paymentRequestListInput)
if err != nil {
return nil, &Error{Err: err}
}
queryParams := make(map[string]string)
if err = json.Unmarshal(jsonQueryParams, &queryParams); err != nil {
return nil, &Error{Err: err}
}
clientRequest := &ClientRequest{
Path: "payment_requests",
QueryParams: queryParams,
Result: &PaymentRequestResult{},
}
result, clientErr := ir.client.Get(ctx, clientRequest)
if clientErr != nil {
return nil, clientErr
}
paymentRequestResult, ok := result.(*PaymentRequestResult)
if !ok {
return nil, &ErrorTypeAssert
}
return paymentRequestResult, nil
}
func (cr *PaymentRequestRequest) Create(ctx context.Context, paymentRequestInput *PaymentRequestInput) (*PaymentRequest, *Error) {
paymentRequestParams := &PaymentRequestParams{
PaymentRequest: paymentRequestInput,
}
clientRequest := &ClientRequest{
Path: "payment_requests",
Result: &PaymentRequestResult{},
Body: paymentRequestParams,
}
result, err := cr.client.Post(ctx, clientRequest)
if err != nil {
return nil, err
}
paymentRequestResult, ok := result.(*PaymentRequestResult)
if !ok {
return nil, &ErrorTypeAssert
}
return paymentRequestResult.PaymentRequest, nil
}