Skip to content

Commit

Permalink
Add support for radar.early_fraud_warning resource
Browse files Browse the repository at this point in the history
  • Loading branch information
ob-stripe committed May 23, 2019
1 parent d31ea0f commit 3a2a7ef
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ cache:
env:
global:
# If changing this number, please also change it in `testing/testing.go`.
- STRIPE_MOCK_VERSION=0.56.0
- STRIPE_MOCK_VERSION=0.57.0

go:
- "1.9.x"
Expand Down
65 changes: 65 additions & 0 deletions radar/earlyfraudwarning/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Package earlyfraudwarning provides API functions related to early fraud
// warnings.
//
// For more details, see: https://stripe.com/docs/api/early_fraud_warnings?lang=go
package earlyfraudwarning

import (
"net/http"

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

// Client is used to interact with the /radar/early_fraud_warnings API.
type Client struct {
B stripe.Backend
Key string
}

// Get returns the details of an early fraud warning.
func Get(id string, params *stripe.RadarEarlyFraudWarningParams) (*stripe.RadarEarlyFraudWarning, error) {
return getC().Get(id, params)
}

// Get returns the details of an early fraud warning.
func (c Client) Get(id string, params *stripe.RadarEarlyFraudWarningParams) (*stripe.RadarEarlyFraudWarning, error) {
path := stripe.FormatURLPath("/v1/issuer_fraud_records/%s", id)
ifr := &stripe.RadarEarlyFraudWarning{}
err := c.B.Call(http.MethodGet, path, c.Key, params, ifr)
return ifr, err
}

// List returns a list of early fraud warnings.
func List(params *stripe.RadarEarlyFraudWarningListParams) *Iter {
return getC().List(params)
}

// List returns a list of early fraud warnings.
func (c Client) List(listParams *stripe.RadarEarlyFraudWarningListParams) *Iter {
return &Iter{stripe.GetIter(listParams, func(p *stripe.Params, b *form.Values) ([]interface{}, stripe.ListMeta, error) {
list := &stripe.RadarEarlyFraudWarningList{}
err := c.B.CallRaw(http.MethodGet, "/v1/issuer_fraud_records", c.Key, b, p, list)

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

return ret, list.ListMeta, err
})}
}

// Iter is an iterator for early fraud warnings.
type Iter struct {
*stripe.Iter
}

// RadarEarlyFraudWarning returns the early fraud warning which the iterator is currently pointing to.
func (i *Iter) RadarEarlyFraudWarning() *stripe.RadarEarlyFraudWarning {
return i.Current().(*stripe.RadarEarlyFraudWarning)
}

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

import (
"testing"

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

func TestRadarEarlyFraudWarningGet(t *testing.T) {
ifr, err := Get("ifr_123", nil)
assert.Nil(t, err)
assert.NotNil(t, ifr)
}

func TestRadarEarlyFraudWarningList(t *testing.T) {
i := List(&stripe.RadarEarlyFraudWarningListParams{})

// Verify that we can get at least one issuer fraud record
assert.True(t, i.Next())
assert.Nil(t, i.Err())
assert.NotNil(t, i.RadarEarlyFraudWarning())
}

func TestRadarEarlyFraudWarningListByChargeID(t *testing.T) {
i := List(&stripe.RadarEarlyFraudWarningListParams{
Charge: stripe.String("ch_123"),
})

// Verify that we can get at least one issuer fraud record
assert.True(t, i.Next())
assert.Nil(t, i.Err())
assert.NotNil(t, i.RadarEarlyFraudWarning())
}
48 changes: 48 additions & 0 deletions radar_earlyfraudwarning.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package stripe

// RadarEarlyFraudWarningFraudType are strings that map to the type of fraud labelled by the issuer.
type RadarEarlyFraudWarningFraudType string

// List of values that RadarEarlyFraudWarningFraudType can take.
const (
RadarEarlyFraudWarningFraudTypeCardNeverReceived RadarEarlyFraudWarningFraudType = "card_never_received"
RadarEarlyFraudWarningFraudTypeFraudulentCardApplication RadarEarlyFraudWarningFraudType = "fraudulent_card_application"
RadarEarlyFraudWarningFraudTypeMadeWithCounterfeitCard RadarEarlyFraudWarningFraudType = "made_with_counterfeit_card"
RadarEarlyFraudWarningFraudTypeMadeWithLostCard RadarEarlyFraudWarningFraudType = "made_with_lost_card"
RadarEarlyFraudWarningFraudTypeMadeWithStolenCard RadarEarlyFraudWarningFraudType = "made_with_stolen_card"
RadarEarlyFraudWarningFraudTypeMisc RadarEarlyFraudWarningFraudType = "misc"
RadarEarlyFraudWarningFraudTypeUnauthorizedUseOfCard RadarEarlyFraudWarningFraudType = "unauthorized_use_of_card"
)

// RadarEarlyFraudWarningParams is the set of parameters that can be used when
// retrieving early fraud warnings. For more details see
// https://stripe.com/docs/api/early_fraud_warnings/retrieve.
type RadarEarlyFraudWarningParams struct {
Params `form:"*"`
}

// RadarEarlyFraudWarningListParams is the set of parameters that can be used when
// listing early fraud warnings. For more details see
// https://stripe.com/docs/api/early_fraud_warnings/list.
type RadarEarlyFraudWarningListParams struct {
ListParams `form:"*"`
Charge *string `form:"charge"`
}

// RadarEarlyFraudWarningList is a list of early fraud warnings as retrieved from a
// list endpoint.
type RadarEarlyFraudWarningList struct {
ListMeta
Values []*RadarEarlyFraudWarning `json:"data"`
}

// RadarEarlyFraudWarning is the resource representing an early fraud warning. For
// more details see https://stripe.com/docs/api/early_fraud_warnings/object.
type RadarEarlyFraudWarning struct {
Actionable bool `json:"actionable"`
Charge *Charge `json:"charge"`
Created int64 `json:"created"`
FraudType RadarEarlyFraudWarningFraudType `json:"fraud_type"`
ID string `json:"id"`
Livemode bool `json:"livemode"`
}
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.56.0"
MockMinimumVersion = "0.57.0"

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

0 comments on commit 3a2a7ef

Please sign in to comment.