From b264cd773d2507182651014ef87b024ffe56d46f Mon Sep 17 00:00:00 2001 From: Ancor Cruz Date: Fri, 16 Aug 2024 12:02:05 +0100 Subject: [PATCH] feat(dunning): Add payment requests endpoint (get list) --- payment_request.go | 72 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 payment_request.go diff --git a/payment_request.go b/payment_request.go new file mode 100644 index 0000000..ff54b15 --- /dev/null +++ b/payment_request.go @@ -0,0 +1,72 @@ +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"` + Currency Currency `json:"currency,omitempty"` + AmountCents int `json:"amount_cents,omitempty"` + CreatedAt time.Time `json:"created_at,omitempty"` + + Customer *Customer `json:"customer,omitempty"` + Invoices []Invoice `json:"fees,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 +}