Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for the SetupAttempt resource and List API #1194

Merged
merged 1 commit into from
Sep 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ cache:
env:
global:
# If changing this number, please also change it in `testing/testing.go`.
- STRIPE_MOCK_VERSION=0.98.0
- STRIPE_MOCK_VERSION=0.99.0

go:
- "1.10.x"
Expand Down
4 changes: 4 additions & 0 deletions client/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import (
"github.com/stripe/stripe-go/v72/reporting/reporttype"
"github.com/stripe/stripe-go/v72/reversal"
"github.com/stripe/stripe-go/v72/review"
"github.com/stripe/stripe-go/v72/setupattempt"
"github.com/stripe/stripe-go/v72/setupintent"
"github.com/stripe/stripe-go/v72/sigma/scheduledqueryrun"
"github.com/stripe/stripe-go/v72/sku"
Expand Down Expand Up @@ -185,6 +186,8 @@ type API struct {
Reversals *reversal.Client
// Reviews is the client used to invoke /reviews APIs.
Reviews *review.Client
// SetupAttempts is the client used to invoke /setup_attempts APIs.
SetupAttempts *setupattempt.Client
// SetupIntents is the client used to invoke /setup_intents APIs.
SetupIntents *setupintent.Client
// SigmaScheduledQueryRuns is the client used to invoke /sigma/scheduled_query_runs APIs.
Expand Down Expand Up @@ -289,6 +292,7 @@ func (a *API) Init(key string, backends *stripe.Backends) {
a.ReportTypes = &reporttype.Client{B: backends.API, Key: key}
a.Reversals = &reversal.Client{B: backends.API, Key: key}
a.Reviews = &review.Client{B: backends.API, Key: key}
a.SetupAttempts = &setupattempt.Client{B: backends.API, Key: key}
a.SetupIntents = &setupintent.Client{B: backends.API, Key: key}
a.SigmaScheduledQueryRuns = &scheduledqueryrun.Client{B: backends.API, Key: key}
a.Skus = &sku.Client{B: backends.API, Key: key}
Expand Down
1 change: 0 additions & 1 deletion product/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ func TestProductNew(t *testing.T) {
Width: stripe.Float64(6.50),
Weight: stripe.Float64(10),
},
Type: stripe.String(string(stripe.ProductTypeGood)),
})
assert.Nil(t, err)
assert.NotNil(t, product)
Expand Down
140 changes: 140 additions & 0 deletions setupattempt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package stripe

import (
"encoding/json"
)

// SetupAttemptPaymentMethodDetailsCardThreeDSecureAuthenticationFlow indicates the type of 3D Secure authentication performed.
type SetupAttemptPaymentMethodDetailsCardThreeDSecureAuthenticationFlow string

// List of values that SetupAttemptPaymentMethodDetailsCardThreeDSecureAuthenticationFlow can take.
const (
SetupAttemptPaymentMethodDetailsCardThreeDSecureAuthenticationFlowChallenge SetupAttemptPaymentMethodDetailsCardThreeDSecureAuthenticationFlow = "challenge"
SetupAttemptPaymentMethodDetailsCardThreeDSecureAuthenticationFlowFrictionless SetupAttemptPaymentMethodDetailsCardThreeDSecureAuthenticationFlow = "frictionless"
)

// SetupAttemptPaymentMethodDetailsCardThreeDSecureResult indicates the outcome of 3D Secure authentication.
type SetupAttemptPaymentMethodDetailsCardThreeDSecureResult string

// List of values that SetupAttemptPaymentMethodDetailsCardThreeDSecureResult can take.
const (
SetupAttemptPaymentMethodDetailsCardThreeDSecureResultAttemptAcknowledged SetupAttemptPaymentMethodDetailsCardThreeDSecureResult = "attempt_acknowledged"
SetupAttemptPaymentMethodDetailsCardThreeDSecureResultAuthenticated SetupAttemptPaymentMethodDetailsCardThreeDSecureResult = "authenticated"
SetupAttemptPaymentMethodDetailsCardThreeDSecureResultFailed SetupAttemptPaymentMethodDetailsCardThreeDSecureResult = "failed"
SetupAttemptPaymentMethodDetailsCardThreeDSecureResultNotSupported SetupAttemptPaymentMethodDetailsCardThreeDSecureResult = "not_supported"
SetupAttemptPaymentMethodDetailsCardThreeDSecureResultProcessingError SetupAttemptPaymentMethodDetailsCardThreeDSecureResult = "processing_error"
)

// SetupAttemptPaymentMethodDetailsCardThreeDSecureResultReason represents dditional information about why 3D Secure succeeded or failed
type SetupAttemptPaymentMethodDetailsCardThreeDSecureResultReason string

// List of values that SetupAttemptPaymentMethodDetailsCardThreeDSecureResultReason can take.
const (
SetupAttemptPaymentMethodDetailsCardThreeDSecureResultReasonAbandoned SetupAttemptPaymentMethodDetailsCardThreeDSecureResultReason = "abandoned"
SetupAttemptPaymentMethodDetailsCardThreeDSecureResultReasonBypassed SetupAttemptPaymentMethodDetailsCardThreeDSecureResultReason = "bypassed"
SetupAttemptPaymentMethodDetailsCardThreeDSecureResultReasonCanceled SetupAttemptPaymentMethodDetailsCardThreeDSecureResultReason = "canceled"
SetupAttemptPaymentMethodDetailsCardThreeDSecureResultReasonCardNotEnrolled SetupAttemptPaymentMethodDetailsCardThreeDSecureResultReason = "card_not_enrolled"
SetupAttemptPaymentMethodDetailsCardThreeDSecureResultReasonNetworkNotSupported SetupAttemptPaymentMethodDetailsCardThreeDSecureResultReason = "network_not_supported"
SetupAttemptPaymentMethodDetailsCardThreeDSecureResultReasonProtocolError SetupAttemptPaymentMethodDetailsCardThreeDSecureResultReason = "protocol_error"
SetupAttemptPaymentMethodDetailsCardThreeDSecureResultReasonRejected SetupAttemptPaymentMethodDetailsCardThreeDSecureResultReason = "rejected"
)

// SetupAttemptPaymentMethodDetailsType is the type of the payment method associated with the setup attempt's payment method details.
type SetupAttemptPaymentMethodDetailsType string

// List of values that SetupAttemptPaymentMethodDetailsType can take.
const (
SetupAttemptPaymentMethodDetailsTypeCard SetupAttemptPaymentMethodDetailsType = "card"
)

// SetupAttemptUsage is the list of allowed values for usage.
type SetupAttemptUsage string

// List of values that SetupAttemptUsage can take.
const (
SetupAttemptUsageOffSession SetupAttemptUsage = "off_session"
SetupAttemptUsageOnSession SetupAttemptUsage = "on_session"
)

// SetupAttemptStatus is the list of allowed values for the setup attempt's status.
type SetupAttemptStatus string

// List of values that SetupAttemptStatus can take.
const (
SetupAttemptStatusAbandoned SetupAttemptStatus = "abandoned"
SetupAttemptStatusFailed SetupAttemptStatus = "failed"
SetupAttemptStatusProcessing SetupAttemptStatus = "processing"
SetupAttemptStatusRequiresAction SetupAttemptStatus = "requires_action"
SetupAttemptStatusRequiresConfirmation SetupAttemptStatus = "requires_confirmation"
SetupAttemptStatusSucceeded SetupAttemptStatus = "succeeded"
)

// SetupAttemptListParams is the set of parameters that can be used when listing setup attempts.
type SetupAttemptListParams struct {
ListParams `form:"*"`
Created *int64 `form:"created"`
CreatedRange *RangeQueryParams `form:"created"`
SetupIntent *string `form:"setup_intent"`
}

// SetupAttemptPaymentMethodDetailsCardThreeDSecure represents details about 3DS associated with the setup attempt's payment method.
type SetupAttemptPaymentMethodDetailsCardThreeDSecure struct {
AuthenticationFlow SetupAttemptPaymentMethodDetailsCardThreeDSecureAuthenticationFlow `json:"authentication_flow"`
Result SetupAttemptPaymentMethodDetailsCardThreeDSecureResult `json:"result"`
ResultReason SetupAttemptPaymentMethodDetailsCardThreeDSecureResultReason `json:"result_reason"`
Version string `json:"version"`
}

// SetupAttemptPaymentMethodDetailsCard represents details about the Card PaymentMethod.
type SetupAttemptPaymentMethodDetailsCard struct {
ThreeDSecure *SetupAttemptPaymentMethodDetailsCardThreeDSecure `json:"three_d_secure"`
}

// SetupAttemptPaymentMethodDetails represents the details about the PaymentMethod associated with the setup attempt.
type SetupAttemptPaymentMethodDetails struct {
Card *SetupAttemptPaymentMethodDetailsCard `json:"card"`
Type SetupAttemptPaymentMethodDetailsType `json:"type"`
}

// SetupAttempt is the resource representing a Stripe setup attempt.
type SetupAttempt struct {
APIResource
Application *Application `json:"application"`
Created int64 `json:"created"`
Customer *Customer `json:"customer"`
ID string `json:"id"`
Livemode bool `json:"livemode"`
Object string `json:"object"`
OnBehalfOf *Account `json:"on_behalf_of"`
PaymentMethod *PaymentMethod `json:"payment_method"`
PaymentMethodDetails *SetupAttemptPaymentMethodDetails `json:"payment_method_details"`
SetupError *Error `json:"setup_error"`
Status SetupAttemptStatus `json:"status"`
Usage SetupAttemptUsage `json:"usage"`
}

// SetupAttemptList is a list of setup attempts as retrieved from a list endpoint.
type SetupAttemptList struct {
APIResource
ListMeta
Data []*SetupAttempt `json:"data"`
}

// UnmarshalJSON handles deserialization of a SetupAttempt.
// This custom unmarshaling is needed because the resulting
// property may be an id or the full struct if it was expanded.
func (p *SetupAttempt) UnmarshalJSON(data []byte) error {
if id, ok := ParseID(data); ok {
p.ID = id
return nil
}

type setupAttempt SetupAttempt
var v setupAttempt
if err := json.Unmarshal(data, &v); err != nil {
return err
}

*p = SetupAttempt(v)
return nil
}
58 changes: 58 additions & 0 deletions setupattempt/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Package setupattempt provides API functions related to setup attempts.
//
// For more details, see: https://stripe.com/docs/api/go#setup_attempts.
package setupattempt

import (
"net/http"

stripe "github.com/stripe/stripe-go/v72"
"github.com/stripe/stripe-go/v72/form"
)

// Client is used to invoke APIs related to setup attempts.
type Client struct {
B stripe.Backend
Key string
}

// List returns a list of setup attempts.
func List(params *stripe.SetupAttemptListParams) *Iter {
return getC().List(params)
}

// List returns a list of setup attempts.
func (c Client) List(listParams *stripe.SetupAttemptListParams) *Iter {
return &Iter{stripe.GetIter(listParams, func(p *stripe.Params, b *form.Values) ([]interface{}, stripe.ListContainer, error) {
list := &stripe.SetupAttemptList{}
err := c.B.CallRaw(http.MethodGet, "/v1/setup_attempts", c.Key, b, p, list)

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

return ret, list, err
})}
}

// Iter is an iterator for setup attempts.
type Iter struct {
*stripe.Iter
}

// SetupAttempt returns the setup attempt which the iterator is currently pointing to.
func (i *Iter) SetupAttempt() *stripe.SetupAttempt {
return i.Current().(*stripe.SetupAttempt)
}

// SetupAttemptList 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 *Iter) SetupAttemptList() *stripe.SetupAttemptList {
return i.List().(*stripe.SetupAttemptList)
}

func getC() Client {
return Client{stripe.GetBackend(stripe.APIBackend), stripe.Key}
}
22 changes: 22 additions & 0 deletions setupattempt/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package setupattempt

import (
"testing"

assert "github.com/stretchr/testify/require"
stripe "github.com/stripe/stripe-go/v72"
_ "github.com/stripe/stripe-go/v72/testing"
)

func TestSetupAttemptList(t *testing.T) {
params := &stripe.SetupAttemptListParams{
SetupIntent: stripe.String("seti_123"),
}
i := List(params)

// Verify that we can get at least one setup attempt
assert.True(t, i.Next())
assert.Nil(t, i.Err())
assert.NotNil(t, i.SetupAttempt())
assert.NotNil(t, i.SetupAttemptList())
}
1 change: 1 addition & 0 deletions setupintent.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ type SetupIntent struct {
Description string `json:"description"`
ID string `json:"id"`
LastSetupError *Error `json:"last_setup_error"`
LatestAttempt *SetupAttempt `json:"latest_attempt"`
Livemode bool `json:"livemode"`
Mandate *Mandate `json:"mandate"`
Metadata map[string]string `json:"metadata"`
Expand Down
2 changes: 1 addition & 1 deletion testing/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const (
// added in a more recent version of stripe-mock, we can show people a
// better error message instead of the test suite crashing with a bunch of
// confusing 404 errors or the like.
MockMinimumVersion = "0.98.0"
MockMinimumVersion = "0.99.0"

// TestMerchantID is a token that can be used to represent a merchant ID in
// simple tests.
Expand Down