Skip to content

Commit

Permalink
Merge pull request #3 from togglhire/feat/clear_account_info
Browse files Browse the repository at this point in the history
Feat/clear account info
  • Loading branch information
nilsolofsson authored Feb 7, 2023
2 parents 00f6375 + e52ccec commit 52dcc94
Show file tree
Hide file tree
Showing 54 changed files with 173 additions and 132 deletions.
22 changes: 0 additions & 22 deletions Gopkg.lock

This file was deleted.

7 changes: 0 additions & 7 deletions Gopkg.toml

This file was deleted.

94 changes: 54 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,44 +1,49 @@
# Recurly Client for Go

[![Build Status](https://travis-ci.org/blacklightcms/recurly.svg?branch=master)](https://travis-ci.org/blacklightcms/recurly) [![GoDoc](https://godoc.org/github.com/blacklightcms/recurly?status.svg)](https://godoc.org/github.com/blacklightcms/recurly/)

Recurly is a Go (golang) API Client for the [Recurly](https://recurly.com/) API. It is actively maintained, unit tested, and uses no external dependencies. The vast majority of the API is implemented.

Supports:
- Recurly API `v2.27`
- Accounts
- Add Ons
- Adjustments
- Billing
- Coupons
- Credit Payments
- Invoices
- Plans
- Purchases
- Redemptions
- Shipping Addresses
- Shipping Methods
- Subscriptions
- Transactions
[![Build Status](https://travis-ci.org/togglhire/recurly.svg?branch=master)](https://travis-ci.org/togglhire/recurly) [![GoDoc](https://godoc.org/github.com/togglhire/recurly?status.svg)](https://godoc.org/github.com/togglhire/recurly/)

Recurly is a Go (golang) API Client for the [Recurly](https://recurly.com/) API. It is actively maintained, unit tested, and uses no external dependencies. The vast majority of the API is implemented.

Supports:

- Recurly API `v2.27`
- Accounts
- Add Ons
- Adjustments
- Billing
- Coupons
- Credit Payments
- Invoices
- Plans
- Purchases
- Redemptions
- Shipping Addresses
- Shipping Methods
- Subscriptions
- Transactions

## Installation

Install:

```shell
go get github.com/blacklightcms/recurly
go get github.com/togglhire/recurly
```

Import:

```go
import "github.com/blacklightcms/recurly"
import "github.com/togglhire/recurly"
```

Resources:
- [API Docs](https://godoc.org/github.com/blacklightcms/recurly/)
- [Examples](https://godoc.org/github.com/blacklightcms/recurly/#pkg-examples)

- [API Docs](https://godoc.org/github.com/togglhire/recurly/)
- [Examples](https://godoc.org/github.com/togglhire/recurly/#pkg-examples)

## Note on v1 and breaking changes
If migrating from a previous version of the library, there was a large refactor with breaking changes released to address some design issues with the library. See the [migration guide](https://github.com/blacklightcms/recurly/wiki/v1-Migration-Guide) for steps on how to migrate to the latest version.

If migrating from a previous version of the library, there was a large refactor with breaking changes released to address some design issues with the library. See the [migration guide](https://github.com/togglhire/recurly/wiki/v1-Migration-Guide) for steps on how to migrate to the latest version.

This is recommended for all users.

Expand All @@ -54,7 +59,8 @@ a, err := client.Accounts.Get(context.Background(), "1")
```

## Examples and How To
Please go through [examples](https://godoc.org/github.com/blacklightcms/recurly/#pkg-examples) for detailed examples of using this package.

Please go through [examples](https://godoc.org/github.com/togglhire/recurly/#pkg-examples) for detailed examples of using this package.

The examples explain important cases like:

Expand All @@ -66,6 +72,7 @@ The examples explain important cases like:
Here are a few snippets to demonstrate library usage.

### Create Account

```go
account, err := client.Accounts.Create(ctx, recurly.Account{
Code: "1",
Expand All @@ -75,11 +82,12 @@ account, err := client.Accounts.Create(ctx, recurly.Account{
})
```

> **NOTE**: An account can also be created along a subscription by embedding the
> account in the subscription during creation. The purchases API also supports
> **NOTE**: An account can also be created along a subscription by embedding the
> account in the subscription during creation. The purchases API also supports
> this, and likely other endpoints. See Recurly's documentation for details.
### Get Account

```go
account, err := client.Accounts.Get(ctx, "1")
if err != nil {
Expand All @@ -92,6 +100,7 @@ if err != nil {
```

### Create Billing Info

```go
// Using token obtained with recurly.js
// If you want to set billing info directly, omit the token and set the
Expand All @@ -100,6 +109,7 @@ billing, err := client.Billing.Create("1", recurly.Billing{
Token: token,
})
```

> **NOTE**: See the error handling section in GoDoc for how to handle transaction errors
### Creating Purchases
Expand Down Expand Up @@ -127,6 +137,7 @@ if err != nil {
> structs and [Recurly's documentation](https://dev.recurly.com/docs/create-purchase) for more info.
### Creating Subscriptions

```go
subscription, err := client.Subscriptions.Create(ctx, recurly.NewSubscription{
PlanCode: "gold",
Expand All @@ -141,18 +152,20 @@ if err != nil {
return err
}
```
> **NOTE**: Recurly offers several other ways to create subscriptions, often embedded
> within other requests (such as the `Purchases.Create()` call). See Recurly's

> **NOTE**: Recurly offers several other ways to create subscriptions, often embedded
> within other requests (such as the `Purchases.Create()` call). See Recurly's
> documentation for more details.
## Webhooks
This library supports webhooks via the `webhooks` sub package.

The usage is to parse the webhook from a reader, then use a switch statement
This library supports webhooks via the `webhooks` sub package.

The usage is to parse the webhook from a reader, then use a switch statement
to determine the type of webhook received.

```go
// import "github.com/blacklightcms/recurly/webhooks"
// import "github.com/togglhire/recurly/webhooks"

hook, err := webhooks.Parse(r)
if e, ok := err.(*webhooks.ErrUnknownNotification); ok {
Expand All @@ -177,30 +190,31 @@ default:
```

## Testing

Once you've imported this library into your application, you will want to add tests.

Internally this library sets up a test HTTPs server and validates methods, paths,
Internally this library sets up a test HTTPs server and validates methods, paths,
query strings, request body, and returns XML. You will not need to worry about those internals
when testing your own code that uses this library.

Instead we recommend using the `mock` package. The `mock` package provides mocks
Instead we recommend using the `mock` package. The `mock` package provides mocks
for all of the different services in this library.

For examples of how to test your code using mocks, visit the [GoDoc examples](https://godoc.org/github.com/blacklightcms/recurly/mock/).
For examples of how to test your code using mocks, visit the [GoDoc examples](https://godoc.org/github.com/togglhire/recurly/mock/).

> **NOTE**: If you need to go beyond mocks and test requests/responses, `testing.go` exports `TestServer`. This is how the library tests itself. See the GoDoc or the `*_test.go` files for usage examples.
## Contributing

We use [`dep`](https://github.com/golang/dep) for dependency management. If you
do not have it installed, see the [installation instructions](https://github.com/golang/dep#installation).
We use `go mod` for dependency management, it's included with the standard go tooling.

To contribute: fork and clone the repository, `cd` into the directory, and run:

```shell
dep ensure
go mod tidy
go mod vendor
```

That will ensure you have [`google/go-cmp`](https://github.com/google/go-cmp) which is used to run tests.

If you plan on submitting a patch, please write tests for it.
If you plan on submitting a patch, please write tests for it.
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
18 changes: 17 additions & 1 deletion accounts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"strconv"
"testing"

"github.com/blacklightcms/recurly"
"github.com/google/go-cmp/cmp"
"github.com/togglhire/recurly"
)

// Ensure structs are encoded to XML properly.
Expand Down 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
2 changes: 1 addition & 1 deletion add_ons_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"strconv"
"testing"

"github.com/blacklightcms/recurly"
"github.com/google/go-cmp/cmp"
"github.com/togglhire/recurly"
)

// Ensure structs are encoded to XML properly.
Expand Down
2 changes: 1 addition & 1 deletion adjustments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"testing"
"time"

"github.com/blacklightcms/recurly"
"github.com/google/go-cmp/cmp"
"github.com/togglhire/recurly"
)

// Ensure structs are encoded to XML properly.
Expand Down
2 changes: 1 addition & 1 deletion automated_exports_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"testing"
"time"

"github.com/blacklightcms/recurly"
"github.com/google/go-cmp/cmp"
"github.com/togglhire/recurly"
)

// Ensure structs are encoded to XML properly.
Expand Down
2 changes: 1 addition & 1 deletion billing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"strconv"
"testing"

"github.com/blacklightcms/recurly"
"github.com/google/go-cmp/cmp"
"github.com/togglhire/recurly"
)

// Ensure structs are encoded to XML properly.
Expand Down
2 changes: 1 addition & 1 deletion coupons_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"testing"
"time"

"github.com/blacklightcms/recurly"
"github.com/google/go-cmp/cmp"
"github.com/togglhire/recurly"
)

// Ensure structs are encoded to XML properly.
Expand Down
2 changes: 1 addition & 1 deletion credit_payments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"net/http"
"testing"

"github.com/blacklightcms/recurly"
"github.com/google/go-cmp/cmp"
"github.com/togglhire/recurly"
)

func TestCreditPayments_List(t *testing.T) {
Expand Down
Loading

0 comments on commit 52dcc94

Please sign in to comment.