Skip to content

Commit

Permalink
Add support for listing source transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
ob-stripe committed Oct 26, 2017
1 parent 3cb5b5a commit d469f2b
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 0 deletions.
53 changes: 53 additions & 0 deletions sourcetransaction.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package stripe

// SourceTransactionListParams is the set of parameters that can be used when listing SourceTransactions.
type SourceTransactionListParams struct {
ListParams `form:"*"`
Source string `form:"-"` // Sent in with the URL
}

// SourceTransactionList is a list object for SourceTransactions.
type SourceTransactionList struct {
ListMeta
Values []*SourceTransaction `json:"data"`
}

// SourceTransaction is the resource representing a Stripe source transaction.
type SourceTransaction struct {
Amount int64 `json:"amount"`
Created int64 `json:"created"`
Currency Currency `json:"currency"`
CustomerData string `json:"customer_data"`
ID string `json:"id"`
Live bool `json:"livemode"`
Source string `json:"source"`
Type string `json:"type"`
TypeData map[string]string `form:"-"`
}

// UnmarshalJSON handles deserialization of a SourceTransaction. This custom
// unmarshaling is needed to extract the type specific data (accessible under
// `TypeData`) but stored in JSON under a hash named after the `type` of the
// source transaction.
func (s *SourceTransaction) UnmarshalJSON(data []byte) error {
type sourceTransaction SourceTransaction
var st sourceTransaction
err := json.Unmarshal(data, &ss)
if err != nil {
return err
}
*t = SourceTransaction(st)

var raw map[string]interface{}
err = json.Unmarshal(data, &raw)
if err != nil {
return err
}
if d, ok := raw[t.Type]; ok {
if m, ok := d.(map[string]interface{}); ok {
t.TypeData = m
}
}

return nil
}
63 changes: 63 additions & 0 deletions sourcetransaction/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Package sourcetransaction provides the /source/transactions APIs.
package sourcetransaction

import (
"fmt"

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

// Client is used to invoke /sources/:source_id/transactions APIs.
type Client struct {
B stripe.Backend
Key string
}

// List returns a list of source transactions.
// For more details see https://stripe.com/docs/api#retrieve_source.
func List(params *stripe.SourceTransactionListParams) *Iter {
return getC().List(params)
}

func (c Client) List(params *stripe.SourceTransactionListParams) *Iter {
var body *form.Values
var lp *stripe.ListParams
var p *stripe.Params

if params != nil {
body = &form.Values{}
form.AppendTo(body, params)
lp = &params.ListParams
p = params.ToParams()
}

return &Iter{stripe.GetIter(lp, body, func(b *form.Values) ([]interface{}, stripe.ListMeta, error) {
list := &stripe.SourceTransactionList{}
err := c.B.Call("GET", fmt.Sprintf("/sources/%v/source_transactions", params.Source), 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 lists of SourceTransactions.
// The embedded Iter carries methods with it;
// see its documentation for details.
type Iter struct {
*stripe.Iter
}

// SourceTransaction returns the most recent SourceTransaction
// visited by a call to Next.
func (i *Iter) SourceTransaction() *stripe.SourceTransaction {
return i.Current().(*stripe.SourceTransaction)
}

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

import (
"testing"

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

func TestSourceTransactionList(t *testing.T) {
// TODO: unskip the test once stripe-mock supports the /v1/sources/src_.../source_transactions endpoint
t.Skip("not yet supported by stripe-mock")

i := List(&stripe.SourceTransactionListParams{
Source: "src_123",
})

// Verify that we can get at least one transaction
assert.True(t, i.Next())
assert.Nil(t, i.Err())
assert.NotNil(t, i.SourceTransaction())
}

0 comments on commit d469f2b

Please sign in to comment.