Skip to content

Commit

Permalink
Add Create Purchase endpoint (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
nelz9999 authored and cristiangraz committed Jun 21, 2018
1 parent 6d2a55e commit 3fe41ed
Show file tree
Hide file tree
Showing 10 changed files with 362 additions and 24 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ The services are (each link to the GoDoc documentation):
* [Redemptions](https://godoc.org/github.com/blacklightcms/recurly#RedemptionsService)
* [Subscriptions](https://godoc.org/github.com/blacklightcms/recurly#SubscriptionsService)
* [Transactions](https://godoc.org/github.com/blacklightcms/recurly#TransactionsService)
* [Purchases](https://godoc.org/github.com/blacklightcms/recurly#PurchasesService)

Each of the services correspond to their respective sections in the
[Recurly API Documentation](https://dev.recurly.com/docs/).
Expand Down
33 changes: 17 additions & 16 deletions accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,23 @@ import (

// Account represents an individual account on your site
type Account struct {
XMLName xml.Name `xml:"account"`
Code string `xml:"account_code,omitempty"`
State string `xml:"state,omitempty"`
Username string `xml:"username,omitempty"`
Email string `xml:"email,omitempty"`
FirstName string `xml:"first_name,omitempty"`
LastName string `xml:"last_name,omitempty"`
CompanyName string `xml:"company_name,omitempty"`
VATNumber string `xml:"vat_number,omitempty"`
TaxExempt NullBool `xml:"tax_exempt,omitempty"`
BillingInfo *Billing `xml:"billing_info,omitempty"`
Address Address `xml:"address,omitempty"`
AcceptLanguage string `xml:"accept_language,omitempty"`
HostedLoginToken string `xml:"hosted_login_token,omitempty"`
CreatedAt NullTime `xml:"created_at,omitempty"`
HasPausedSubscription bool `xml:"has_paused_subscription,omitempty"`
XMLName xml.Name `xml:"account"`
Code string `xml:"account_code,omitempty"`
State string `xml:"state,omitempty"`
Username string `xml:"username,omitempty"`
Email string `xml:"email,omitempty"`
FirstName string `xml:"first_name,omitempty"`
LastName string `xml:"last_name,omitempty"`
CompanyName string `xml:"company_name,omitempty"`
VATNumber string `xml:"vat_number,omitempty"`
TaxExempt NullBool `xml:"tax_exempt,omitempty"`
BillingInfo *Billing `xml:"billing_info,omitempty"`
Address Address `xml:"address,omitempty"`
AcceptLanguage string `xml:"accept_language,omitempty"`
HostedLoginToken string `xml:"hosted_login_token,omitempty"`
CreatedAt NullTime `xml:"created_at,omitempty"`
HasPausedSubscription bool `xml:"has_paused_subscription,omitempty"`
ShippingAddresses *[]ShippingAddress `xml:"shipping_addresses>shipping_address,omitempty"`
}

// AccountBalance is used for getting the account balance.
Expand Down
17 changes: 10 additions & 7 deletions adjustments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io/ioutil"
"net/http"
"strconv"
"testing"
"time"

Expand Down Expand Up @@ -35,13 +36,15 @@ func TestAdjustments_Encoding(t *testing.T) {
{v: recurly.Adjustment{StartDate: recurly.NullTime{Time: &now}, EndDate: recurly.NullTime{Time: &now}, UnitAmountInCents: 2000, Currency: "USD"}, expected: "<adjustment><unit_amount_in_cents>2000</unit_amount_in_cents><currency>USD</currency><start_date>2000-01-01T00:00:00Z</start_date><end_date>2000-01-01T00:00:00Z</end_date></adjustment>"},
}

for _, tt := range tests {
var buf bytes.Buffer
if err := xml.NewEncoder(&buf).Encode(tt.v); err != nil {
t.Fatalf("unexpected error: %v", err)
} else if buf.String() != tt.expected {
t.Fatalf("unexpected value: %s", buf.String())
}
for i, tt := range tests {
t.Run(strconv.Itoa(i), func(t *testing.T) {
var buf bytes.Buffer
if err := xml.NewEncoder(&buf).Encode(tt.v); err != nil {
t.Fatalf("unexpected error: %v", err)
} else if buf.String() != tt.expected {
t.Fatalf("unexpected value: %s\nexpected: %s", buf.String(), tt.expected)
}
})
}
}

Expand Down
2 changes: 2 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type Client struct {
Subscriptions SubscriptionsService
Transactions TransactionsService
CreditPayments CreditPaymentsService
Purchases PurchasesService
}

// NewClient returns a new instance of *Client.
Expand Down Expand Up @@ -69,6 +70,7 @@ func NewClient(subDomain, apiKey string, httpClient *http.Client) *Client {
client.ShippingAddresses = &shippingAddressesImpl{client: client}
client.Transactions = &transactionsImpl{client: client}
client.CreditPayments = &creditInvoicesImpl{client: client}
client.Purchases = &purchasesImpl{client: client}

return client
}
Expand Down
1 change: 1 addition & 0 deletions mock/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func NewClient(httpClient *http.Client) *recurly.Client {
client.Subscriptions = &SubscriptionsService{}
client.Transactions = &TransactionsService{}
client.CreditPayments = &CreditPaymentsService{}
client.Purchases = &PurchasesService{}

return client
}
12 changes: 12 additions & 0 deletions mock/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -671,3 +671,15 @@ func (m *CreditPaymentsService) Get(uuid string) (*recurly.Response, *recurly.Cr
m.GetInvoked = true
return m.OnGet(uuid)
}

var _ recurly.PurchasesService = &PurchasesService{}

type PurchasesService struct {
OnCreate func(p recurly.Purchase) (*recurly.Response, *recurly.InvoiceCollection, error)
CreateInvoked bool
}

func (m *PurchasesService) Create(p recurly.Purchase) (*recurly.Response, *recurly.InvoiceCollection, error) {
m.CreateInvoked = true
return m.OnCreate(p)
}
22 changes: 22 additions & 0 deletions purchases.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package recurly

import "encoding/xml"

// Purchase represents an individual checkout holding at least one
// subscription OR one adjustment
type Purchase struct {
XMLName xml.Name `xml:"purchase"`
Account Account `xml:"account,omitempty"`
Adjustments []Adjustment `xml:"adjustments>adjustment,omitempty"`
CollectionMethod string `xml:"collection_method,omitempty"`
Currency string `xml:"currency"`
PONumber string `xml:"po_number,omitempty"`
NetTerms NullInt `xml:"net_terms,omitempty"`
GiftCard string `xml:"gift_card>redemption_code,omitempty"`
CouponCodes []string `xml:"coupon_codes>coupon_code,omitempty"`
Subscriptions []NewSubscription `xml:"subscriptions>subscription,omitempty"`
CustomerNotes string `xml:"customer_notes,omitempty"`
TermsAndConditions string `xml:"terms_and_conditions,omitempty"`
VATReverseChargeNotes string `xml:"vat_reverse_charge_notes,omitempty"`
ShippingAddressID int `xml:"shipping_address_id,omitempty"`
}
26 changes: 26 additions & 0 deletions purchases_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package recurly

import (
"net/http"
)

var _ PurchasesService = &purchasesImpl{}

type purchasesImpl struct {
client *Client
}

func (s *purchasesImpl) Create(p Purchase) (*Response, *InvoiceCollection, error) {
req, err := s.client.newRequest("POST", "purchases", nil, p)
if err != nil {
return nil, nil, err
}

var dst InvoiceCollection
resp, err := s.client.do(req, &dst)
if err != nil || resp.StatusCode >= http.StatusBadRequest {
return resp, nil, err
}

return resp, &dst, err
}
Loading

0 comments on commit 3fe41ed

Please sign in to comment.