-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathpayment.go
130 lines (105 loc) · 3.08 KB
/
payment.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
package lago
import (
"context"
"encoding/json"
"fmt"
"time"
"github.com/google/uuid"
)
type ManualPaymentRequest struct {
client *Client
}
type PaymentResult struct {
Payment *Payment `json:"payment,omitempty"`
Payments []Payment `json:"payments,omitempty"`
}
type PaymentListInput struct {
PerPage int `json:"per_page,omitempty,string"`
Page int `json:"page,omitempty,string"`
ExternalCustomerID string `json:"external_customer_id,omitempty"`
InvoiceID string `json:"invoice_id,omitempty"`
}
type Payment struct {
LagoID uuid.UUID `json:"lago_id,omitempty"`
AmountCurrency Currency `json:"amount_currency,omitempty"`
AmountCents int `json:"amount_cents,omitempty"`
PaymentStatus string `json:"payment_status,omitempty"`
Type string `json:"type,omitempty"`
Reference string `json:"reference,omitempty"`
ExternalPaymentID string `json:"external_payment_id,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
InvoiceIds []string `json:"invoice_ids,omitempty"`
}
type PaymentParams struct {
Payment *PaymentInput `json:"payment"`
}
type PaymentInput struct {
InvoiceId string `json:"invoice_id,omitempty"`
AmountCents int `json:"amount_cents,omitempty"`
Reference string `json:"reference,omitempty"`
PaidAt string `json:"paid_at,omitempty"`
}
func (c *Client) Payment() *ManualPaymentRequest {
return &ManualPaymentRequest{
client: c,
}
}
func (adr *ManualPaymentRequest) Get(ctx context.Context, paymentID string) (*Payment, *Error) {
subPath := fmt.Sprintf("%s/%s", "payments", paymentID)
clientRequest := &ClientRequest{
Path: subPath,
Result: &PaymentResult{},
}
result, err := adr.client.Get(ctx, clientRequest)
if err != nil {
return nil, err
}
paymentResult, ok := result.(*PaymentResult)
if !ok {
return nil, &ErrorTypeAssert
}
return paymentResult.Payment, nil
}
func (ir *ManualPaymentRequest) GetList(ctx context.Context, paymentListInput *PaymentListInput) (*PaymentResult, *Error) {
jsonQueryParams, err := json.Marshal(paymentListInput)
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: "payments",
QueryParams: queryParams,
Result: &PaymentResult{},
}
result, clientErr := ir.client.Get(ctx, clientRequest)
if clientErr != nil {
return nil, clientErr
}
paymentResult, ok := result.(*PaymentResult)
if !ok {
return nil, &ErrorTypeAssert
}
return paymentResult, nil
}
func (cr *ManualPaymentRequest) Create(ctx context.Context, paymentInput *PaymentInput) (*Payment, *Error) {
paymentParams := &PaymentParams{
Payment: paymentInput,
}
clientRequest := &ClientRequest{
Path: "payments",
Result: &PaymentResult{},
Body: paymentParams,
}
result, err := cr.client.Post(ctx, clientRequest)
if err != nil {
return nil, err
}
paymentResult, ok := result.(*PaymentResult)
if !ok {
return nil, &ErrorTypeAssert
}
return paymentResult.Payment, nil
}