-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathpayment_request.go
107 lines (85 loc) · 2.81 KB
/
payment_request.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
package lago
import (
"context"
"encoding/json"
"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"`
}
type PaymentRequestListInput struct {
PerPage int `json:"per_page,omitempty,string"`
Page int `json:"page,omitempty,string"`
ExternalCustomerID string `json:"external_customer_id,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:"fees,omitempty"`
}
type PaymentRequestParams struct {
PaymentRequest *PaymentRequestInput `json:"payment_request"`
}
type PaymentRequestInput struct {
Email string `json:"email,omitempty"`
CustomerExternalId string `json:"customer_external_id,omitempty"`
LagoInvoiceIds []string `json:"lago_invoice_ids,omitempty"`
}
func (c *Client) PaymentRequest() *PaymentRequestRequest {
return &PaymentRequestRequest{
client: c,
}
}
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
}