Skip to content

Commit

Permalink
Replace the legacy Order API with the new Order API.
Browse files Browse the repository at this point in the history
  • Loading branch information
dcr-stripe committed Jul 18, 2022
1 parent a7b19af commit a8b9524
Show file tree
Hide file tree
Showing 9 changed files with 1,429 additions and 482 deletions.
2 changes: 0 additions & 2 deletions charge.go
Original file line number Diff line number Diff line change
Expand Up @@ -972,8 +972,6 @@ type Charge struct {
Object string `json:"object"`
// The account (if any) the charge was made on behalf of without triggering an automatic transfer. See the [Connect documentation](https://stripe.com/docs/connect/charges-transfers) for details.
OnBehalfOf *Account `json:"on_behalf_of"`
// Deprecated
Order *Order `json:"order"`
// Details about whether the payment was accepted, and why. See [understanding declines](https://stripe.com/docs/declines) for details.
Outcome *ChargeOutcome `json:"outcome"`
// `true` if the charge succeeded, or was successfully authorized for later capture.
Expand Down
4 changes: 0 additions & 4 deletions client/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ import (
"github.com/stripe/stripe-go/v72/mandate"
"github.com/stripe/stripe-go/v72/oauth"
"github.com/stripe/stripe-go/v72/order"
"github.com/stripe/stripe-go/v72/orderreturn"
"github.com/stripe/stripe-go/v72/paymentintent"
"github.com/stripe/stripe-go/v72/paymentlink"
"github.com/stripe/stripe-go/v72/paymentmethod"
Expand Down Expand Up @@ -200,8 +199,6 @@ type API struct {
Mandates *mandate.Client
// OAuth is the client used to invoke /oauth APIs
OAuth *oauth.Client
// OrderReturns is the client used to invoke /order_returns APIs.
OrderReturns *orderreturn.Client
// Orders is the client used to invoke /orders APIs.
Orders *order.Client
// PaymentIntents is the client used to invoke /payment_intents APIs.
Expand Down Expand Up @@ -381,7 +378,6 @@ func (a *API) Init(key string, backends *stripe.Backends) {
a.LoginLinks = &loginlink.Client{B: backends.API, Key: key}
a.Mandates = &mandate.Client{B: backends.API, Key: key}
a.OAuth = &oauth.Client{B: backends.Connect, Key: key}
a.OrderReturns = &orderreturn.Client{B: backends.API, Key: key}
a.Orders = &order.Client{B: backends.API, Key: key}
a.PaymentIntents = &paymentintent.Client{B: backends.API, Key: key}
a.PaymentLinks = &paymentlink.Client{B: backends.API, Key: key}
Expand Down
59 changes: 59 additions & 0 deletions example/generated_examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
issuing_dispute "github.com/stripe/stripe-go/v72/issuing/dispute"
issuing_transaction "github.com/stripe/stripe-go/v72/issuing/transaction"
mandate "github.com/stripe/stripe-go/v72/mandate"
order "github.com/stripe/stripe-go/v72/order"
paymentintent "github.com/stripe/stripe-go/v72/paymentintent"
paymentlink "github.com/stripe/stripe-go/v72/paymentlink"
paymentmethod "github.com/stripe/stripe-go/v72/paymentmethod"
Expand Down Expand Up @@ -236,6 +237,56 @@ func TestFinancialConnectionsSessionRetrieve(t *testing.T) {
assert.NotNil(t, result)
}

func TestOrderCreate(t *testing.T) {
params := &stripe.OrderParams{
Description: stripe.String("description"),
Currency: stripe.String(string(stripe.CurrencyUSD)),
LineItems: []*stripe.OrderLineItemParams{
&stripe.OrderLineItemParams{Description: stripe.String("my line item")},
},
}
result, _ := order.New(params)
assert.NotNil(t, result)
}

func TestOrderRetrieve(t *testing.T) {
params := &stripe.OrderParams{}
result, _ := order.Get("order_xyz", params)
assert.NotNil(t, result)
}

func TestOrderUpdate(t *testing.T) {
params := &stripe.OrderParams{IPAddress: stripe.String("0.0.0.0")}
params.AddMetadata("reference_number", "123")
result, _ := order.Update("order_xyz", params)
assert.NotNil(t, result)
}

func TestOrderCancel(t *testing.T) {
params := &stripe.OrderCancelParams{}
result, _ := order.Cancel("order_xyz", params)
assert.NotNil(t, result)
}

func TestOrderListLineItems(t *testing.T) {
params := &stripe.OrderListLineItemsParams{ID: stripe.String("order_xyz")}
result := order.ListLineItems(params)
assert.NotNil(t, result)
assert.Nil(t, result.Err())
}

func TestOrderReopen(t *testing.T) {
params := &stripe.OrderReopenParams{}
result, _ := order.Reopen("order_xyz", params)
assert.NotNil(t, result)
}

func TestOrderSubmit(t *testing.T) {
params := &stripe.OrderSubmitParams{ExpectedTotal: stripe.Int64(100)}
result, _ := order.Submit("order_xyz", params)
assert.NotNil(t, result)
}

func TestPaymentIntentCreate(t *testing.T) {
params := &stripe.PaymentIntentParams{
Amount: stripe.Int64(1099),
Expand Down Expand Up @@ -1408,6 +1459,14 @@ func TestMandateRetrieve(t *testing.T) {
assert.NotNil(t, result)
}

func TestOrderList(t *testing.T) {
params := &stripe.OrderListParams{}
params.Limit = stripe.Int64(3)
result := order.List(params)
assert.NotNil(t, result)
assert.Nil(t, result.Err())
}

func TestPaymentIntentList(t *testing.T) {
params := &stripe.PaymentIntentListParams{}
params.Limit = stripe.Int64(3)
Expand Down
1,509 changes: 1,299 additions & 210 deletions order.go

Large diffs are not rendered by default.

86 changes: 71 additions & 15 deletions order/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,40 +46,53 @@ func (c Client) Get(id string, params *stripe.OrderParams) (*stripe.Order, error
}

// Update updates an order's properties.
func Update(id string, params *stripe.OrderUpdateParams) (*stripe.Order, error) {
func Update(id string, params *stripe.OrderParams) (*stripe.Order, error) {
return getC().Update(id, params)
}

// Update updates an order's properties.
func (c Client) Update(id string, params *stripe.OrderUpdateParams) (*stripe.Order, error) {
func (c Client) Update(id string, params *stripe.OrderParams) (*stripe.Order, error) {
path := stripe.FormatURLPath("/v1/orders/%s", id)
order := &stripe.Order{}
err := c.B.Call(http.MethodPost, path, c.Key, params, order)
return order, err
}

// Pay is the method for the `POST /v1/orders/{id}/pay` API.
func Pay(id string, params *stripe.OrderPayParams) (*stripe.Order, error) {
return getC().Pay(id, params)
// Cancel is the method for the `POST /v1/orders/{id}/cancel` API.
func Cancel(id string, params *stripe.OrderCancelParams) (*stripe.Order, error) {
return getC().Cancel(id, params)
}

// Pay is the method for the `POST /v1/orders/{id}/pay` API.
func (c Client) Pay(id string, params *stripe.OrderPayParams) (*stripe.Order, error) {
path := stripe.FormatURLPath("/v1/orders/%s/pay", id)
// Cancel is the method for the `POST /v1/orders/{id}/cancel` API.
func (c Client) Cancel(id string, params *stripe.OrderCancelParams) (*stripe.Order, error) {
path := stripe.FormatURLPath("/v1/orders/%s/cancel", id)
order := &stripe.Order{}
err := c.B.Call(http.MethodPost, path, c.Key, params, order)
return order, err
}

// Return is the method for the `POST /v1/orders/{id}/returns` API.
func Return(id string, params *stripe.OrderReturnParams) (*stripe.OrderReturn, error) {
return getC().Return(id, params)
// Reopen is the method for the `POST /v1/orders/{id}/reopen` API.
func Reopen(id string, params *stripe.OrderReopenParams) (*stripe.Order, error) {
return getC().Reopen(id, params)
}

// Return is the method for the `POST /v1/orders/{id}/returns` API.
func (c Client) Return(id string, params *stripe.OrderReturnParams) (*stripe.OrderReturn, error) {
path := stripe.FormatURLPath("/v1/orders/%s/returns", id)
order := &stripe.OrderReturn{}
// Reopen is the method for the `POST /v1/orders/{id}/reopen` API.
func (c Client) Reopen(id string, params *stripe.OrderReopenParams) (*stripe.Order, error) {
path := stripe.FormatURLPath("/v1/orders/%s/reopen", id)
order := &stripe.Order{}
err := c.B.Call(http.MethodPost, path, c.Key, params, order)
return order, err
}

// Submit is the method for the `POST /v1/orders/{id}/submit` API.
func Submit(id string, params *stripe.OrderSubmitParams) (*stripe.Order, error) {
return getC().Submit(id, params)
}

// Submit is the method for the `POST /v1/orders/{id}/submit` API.
func (c Client) Submit(id string, params *stripe.OrderSubmitParams) (*stripe.Order, error) {
path := stripe.FormatURLPath("/v1/orders/%s/submit", id)
order := &stripe.Order{}
err := c.B.Call(http.MethodPost, path, c.Key, params, order)
return order, err
}
Expand Down Expand Up @@ -123,6 +136,49 @@ func (i *Iter) OrderList() *stripe.OrderList {
return i.List().(*stripe.OrderList)
}

// ListLineItems is the method for the `GET /v1/orders/{id}/line_items` API.
func ListLineItems(params *stripe.OrderListLineItemsParams) *LineItemIter {
return getC().ListLineItems(params)
}

// ListLineItems is the method for the `GET /v1/orders/{id}/line_items` API.
func (c Client) ListLineItems(listParams *stripe.OrderListLineItemsParams) *LineItemIter {
path := stripe.FormatURLPath(
"/v1/orders/%s/line_items",
stripe.StringValue(listParams.ID),
)
return &LineItemIter{
Iter: stripe.GetIter(listParams, func(p *stripe.Params, b *form.Values) ([]interface{}, stripe.ListContainer, error) {
list := &stripe.LineItemList{}
err := c.B.CallRaw(http.MethodGet, path, c.Key, b, p, list)

ret := make([]interface{}, len(list.Data))
for i, v := range list.Data {
ret[i] = v
}

return ret, list, err
}),
}
}

// LineItemIter is an iterator for line items.
type LineItemIter struct {
*stripe.Iter
}

// LineItem returns the line item which the iterator is currently pointing to.
func (i *LineItemIter) LineItem() *stripe.LineItem {
return i.Current().(*stripe.LineItem)
}

// LineItemList returns the current list object which the iterator is
// currently using. List objects will change as new API calls are made to
// continue pagination.
func (i *LineItemIter) LineItemList() *stripe.LineItemList {
return i.List().(*stripe.LineItemList)
}

func getC() Client {
return Client{stripe.GetBackend(stripe.APIBackend), stripe.Key}
}
92 changes: 0 additions & 92 deletions orderitem.go

This file was deleted.

Loading

0 comments on commit a8b9524

Please sign in to comment.