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 listing source transactions #488

Merged
merged 1 commit into from
Oct 27, 2017
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
55 changes: 55 additions & 0 deletions sourcetransaction.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package stripe

import "encoding/json"

// 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]interface{}
}

// 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 (t *SourceTransaction) UnmarshalJSON(data []byte) error {
type sourceTransaction SourceTransaction
var st sourceTransaction
err := json.Unmarshal(data, &st)
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
}
67 changes: 67 additions & 0 deletions sourcetransaction/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Package sourcetransaction provides the /source/transactions APIs.
package sourcetransaction

import (
"errors"
"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 {
body := &form.Values{}
var lp *stripe.ListParams
var p *stripe.Params

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{}
var err error

if params != nil && len(params.Source) > 0 {
err = c.B.Call("GET", fmt.Sprintf("/sources/%v/source_transactions", params.Source), c.Key, b, p, list)
} else {
err = errors.New("Invalid source transaction params: Source needs to be set")
}

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())
}