account_payments.go raw

   1  package linodego
   2  
   3  import (
   4  	"context"
   5  	"encoding/json"
   6  	"time"
   7  
   8  	"github.com/linode/linodego/internal/parseabletime"
   9  )
  10  
  11  // Payment represents a Payment object
  12  type Payment struct {
  13  	// The unique ID of the Payment
  14  	ID int `json:"id"`
  15  
  16  	// The amount, in US dollars, of the Payment.
  17  	USD json.Number `json:"usd"`
  18  
  19  	// When the Payment was made.
  20  	Date *time.Time `json:"-"`
  21  }
  22  
  23  // PaymentCreateOptions fields are those accepted by CreatePayment
  24  type PaymentCreateOptions struct {
  25  	// CVV (Card Verification Value) of the credit card to be used for the Payment
  26  	CVV string `json:"cvv,omitempty"`
  27  
  28  	// The amount, in US dollars, of the Payment
  29  	USD json.Number `json:"usd"`
  30  }
  31  
  32  // UnmarshalJSON implements the json.Unmarshaler interface
  33  func (i *Payment) UnmarshalJSON(b []byte) error {
  34  	type Mask Payment
  35  
  36  	p := struct {
  37  		*Mask
  38  
  39  		Date *parseabletime.ParseableTime `json:"date"`
  40  	}{
  41  		Mask: (*Mask)(i),
  42  	}
  43  
  44  	if err := json.Unmarshal(b, &p); err != nil {
  45  		return err
  46  	}
  47  
  48  	i.Date = (*time.Time)(p.Date)
  49  
  50  	return nil
  51  }
  52  
  53  // GetCreateOptions converts a Payment to PaymentCreateOptions for use in CreatePayment
  54  func (i Payment) GetCreateOptions() (o PaymentCreateOptions) {
  55  	o.USD = i.USD
  56  	return o
  57  }
  58  
  59  // ListPayments lists Payments
  60  func (c *Client) ListPayments(ctx context.Context, opts *ListOptions) ([]Payment, error) {
  61  	return getPaginatedResults[Payment](ctx, c, "account/payments", opts)
  62  }
  63  
  64  // GetPayment gets the payment with the provided ID
  65  func (c *Client) GetPayment(ctx context.Context, paymentID int) (*Payment, error) {
  66  	e := formatAPIPath("account/payments/%d", paymentID)
  67  	return doGETRequest[Payment](ctx, c, e)
  68  }
  69  
  70  // CreatePayment creates a Payment
  71  func (c *Client) CreatePayment(ctx context.Context, opts PaymentCreateOptions) (*Payment, error) {
  72  	return doPOSTRequest[Payment](ctx, c, "account/payments", opts)
  73  }
  74