Skip to content

Commit

Permalink
added a clear method to accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
nilsolofsson committed Feb 7, 2023
1 parent a456a24 commit e52ccec
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
24 changes: 24 additions & 0 deletions accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ type AccountsService interface {
// https://dev.recurly.com/docs/update-account
Update(ctx context.Context, accountCode string, a Account) (*Account, error)

// Clear empties added info on a account.
Clear(ctx context.Context, accountCode string) error

// Close marks an account as closed and cancels any active subscriptions.
// Paused subscriptions will be expired.
//
Expand Down Expand Up @@ -97,6 +100,16 @@ type Account struct {
TransactionType string `xml:"transaction_type,omitempty"` // Create only
}

// clearAccount is a helper object for clearing info from a recurly account.
type clearAccount struct {
XMLName xml.Name `xml:"account"`
Code string `xml:"account_code,omitempty"`
FirstName string `xml:"first_name"`
LastName string `xml:"last_name"`
VATNumber string `xml:"vat_number"`
Address *Address `xml:"address"`
}

// AccountBalance is used for getting the account balance.
type AccountBalance struct {
XMLName xml.Name `xml:"account_balance"`
Expand Down Expand Up @@ -194,6 +207,17 @@ func (s *accountsImpl) Update(ctx context.Context, code string, a Account) (*Acc
return &dst, err
}

func (s *accountsImpl) Clear(ctx context.Context, code string) error {
path := fmt.Sprintf("/accounts/%s", code)
a := clearAccount{}
req, err := s.client.newRequest("PUT", path, a)
if err != nil {
return err
}
_, err = s.client.do(ctx, req, nil)
return err
}

func (s *accountsImpl) Close(ctx context.Context, code string) error {
path := fmt.Sprintf("/accounts/%s", code)
req, err := s.client.newRequest("DELETE", path, nil)
Expand Down
16 changes: 16 additions & 0 deletions accounts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,22 @@ func TestAccounts_Update(t *testing.T) {
}
}

func TestAccounts_Clear(t *testing.T) {
client, s := recurly.NewTestServer()
defer s.Close()

s.HandleFunc("PUT", "/v2/accounts/1", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write(MustOpenFile("account.xml"))
}, t)

if err := client.Accounts.Clear(context.Background(), "1"); !s.Invoked {
t.Fatal("expected fn invocation")
} else if err != nil {
t.Fatal(err)
}
}

func TestAccounts_Close(t *testing.T) {
client, s := recurly.NewTestServer()
defer s.Close()
Expand Down

0 comments on commit e52ccec

Please sign in to comment.