-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathcustomer_payment_method.go
More file actions
92 lines (74 loc) · 2.52 KB
/
Copy pathcustomer_payment_method.go
File metadata and controls
92 lines (74 loc) · 2.52 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
package lago
import (
"context"
"encoding/json"
"fmt"
)
type CustomerPaymentMethodResult struct {
PaymentMethods []PaymentMethod `json:"payment_methods,omitempty"`
Meta Metadata `json:"meta,omitempty"`
}
type CustomerPaymentMethodParams struct {
PaymentMethod *PaymentMethod `json:"payment_method,omitempty"`
}
type CustomerPaymentMethodListInput struct {
PerPage *int `json:"per_page,omitempty,string"`
Page *int `json:"page,omitempty,string"`
}
func (cr *CustomerRequest) GetPaymentMethodList(ctx context.Context, externalCustomerID string, listInput *CustomerPaymentMethodListInput) (*CustomerPaymentMethodResult, *Error) {
jsonQueryParams, err := json.Marshal(listInput)
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}
}
subPath := fmt.Sprintf("customers/%s/payment_methods", externalCustomerID)
clientRequest := &ClientRequest{
Path: subPath,
QueryParams: queryParams,
Result: &CustomerPaymentMethodResult{},
}
result, clientErr := cr.client.Get(ctx, clientRequest)
if clientErr != nil {
return nil, clientErr
}
paymentMethodResult, ok := result.(*CustomerPaymentMethodResult)
if !ok {
return nil, &ErrorTypeAssert
}
return paymentMethodResult, nil
}
func (cr *CustomerRequest) DestroyPaymentMethod(ctx context.Context, externalCustomerID string, paymentMethodID string) (*PaymentMethod, *Error) {
subPath := fmt.Sprintf("customers/%s/payment_methods/%s", externalCustomerID, paymentMethodID)
clientRequest := &ClientRequest{
Path: subPath,
Result: &CustomerPaymentMethodParams{},
}
result, err := cr.client.Delete(ctx, clientRequest)
if err != nil {
return nil, err
}
paymentMethodResult, ok := result.(*CustomerPaymentMethodParams)
if !ok {
return nil, &ErrorTypeAssert
}
return paymentMethodResult.PaymentMethod, nil
}
func (cr *CustomerRequest) SetPaymentMethodAsDefault(ctx context.Context, externalCustomerID string, paymentMethodID string) (*PaymentMethod, *Error) {
subPath := fmt.Sprintf("customers/%s/payment_methods/%s/set_as_default", externalCustomerID, paymentMethodID)
clientRequest := &ClientRequest{
Path: subPath,
Result: &CustomerPaymentMethodParams{},
}
result, err := cr.client.Put(ctx, clientRequest)
if err != nil {
return nil, err
}
paymentMethodResult, ok := result.(*CustomerPaymentMethodParams)
if !ok {
return nil, &ErrorTypeAssert
}
return paymentMethodResult.PaymentMethod, nil
}