mirror of
https://github.com/plutov/paypal.git
synced 2025-01-23 02:11:02 +01:00
added GetPaymentsWithFilter and Filter to list filtered payments
This commit is contained in:
parent
f7201fdd87
commit
36f64e9a3b
14
README.md
14
README.md
|
@ -122,6 +122,20 @@ payment, err := c.GetPayment("PAY-17S8410768582940NKEE66EQ")
|
|||
|
||||
```go
|
||||
payments, err := c.GetPayments()
|
||||
|
||||
//Or list payments with filters
|
||||
filter := &paypalsdk.Filter{}
|
||||
|
||||
//With text fields
|
||||
filter.AddTextField("sort_by").Is = "create_time"
|
||||
filter.AddTextField("count").Is = "30"
|
||||
filter.AddTextField("sort_order").Is = "desc"
|
||||
|
||||
//And time fields
|
||||
filter.AddTimeField("start_time").Is = time.Now().Add(-time.Hour * 24 * 30)
|
||||
filter.AddTimeField("stop_time").Is = time.Now()
|
||||
|
||||
payments, err := c.GetPaymentsWithFilter(filter)
|
||||
```
|
||||
|
||||
### Get authorization by ID
|
||||
|
|
59
filter.go
Normal file
59
filter.go
Normal file
|
@ -0,0 +1,59 @@
|
|||
package paypalsdk
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
const format = "2006-01-02T15:04:05Z"
|
||||
|
||||
type stringable interface {
|
||||
String() string
|
||||
}
|
||||
|
||||
type Filter struct {
|
||||
fields []stringable
|
||||
}
|
||||
|
||||
func (s *Filter) String() string {
|
||||
filter := ""
|
||||
for i, f := range s.fields {
|
||||
if i == 0 {
|
||||
filter = "?" + f.String()
|
||||
} else {
|
||||
filter = filter + "&" + f.String()
|
||||
}
|
||||
}
|
||||
|
||||
return filter
|
||||
}
|
||||
|
||||
type TextField struct {
|
||||
name string
|
||||
Is string
|
||||
}
|
||||
|
||||
func (d TextField) String() string {
|
||||
return fmt.Sprintf("%s=%s", d.name, d.Is)
|
||||
}
|
||||
|
||||
type TimeField struct {
|
||||
name string
|
||||
Is time.Time
|
||||
}
|
||||
|
||||
func (d TimeField) String() string {
|
||||
return fmt.Sprintf("%s=%s", d.name, d.Is.UTC().Format(format))
|
||||
}
|
||||
|
||||
func (s *Filter) AddTextField(field string) *TextField {
|
||||
f := &TextField{name: field}
|
||||
s.fields = append(s.fields, f)
|
||||
return f
|
||||
}
|
||||
|
||||
func (s *Filter) AddTimeField(field string) *TimeField {
|
||||
f := &TimeField{name: field}
|
||||
s.fields = append(s.fields, f)
|
||||
return f
|
||||
}
|
44
filter_test.go
Normal file
44
filter_test.go
Normal file
|
@ -0,0 +1,44 @@
|
|||
package paypalsdk
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestFilter_AddTextField(t *testing.T) {
|
||||
filter := &Filter{}
|
||||
filter.AddTextField("sort_by").Is = "create_time"
|
||||
filter.AddTextField("count").Is = "30"
|
||||
filter.AddTextField("sort_order").Is = "desc"
|
||||
|
||||
expected := "?sort_by=create_time&count=30&sort_order=desc"
|
||||
if filter.String() != expected {
|
||||
t.Errorf("filter string was %s, wanted %s", filter.String(), expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilter_AddTimeField(t *testing.T) {
|
||||
filter := &Filter{}
|
||||
startTime := time.Time{}
|
||||
endTime := startTime.Add(time.Hour * 24 * 30)
|
||||
filter.AddTimeField("start_time").Is = startTime
|
||||
filter.AddTimeField("stop_time").Is = endTime
|
||||
|
||||
expected := "?start_time=0001-01-01T00:00:00Z&stop_time=0001-01-31T00:00:00Z"
|
||||
if filter.String() != expected {
|
||||
t.Errorf("filter string was %s, wanted %s", filter.String(), expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilter_AddMixedFields(t *testing.T) {
|
||||
filter := &Filter{}
|
||||
startTime := time.Time{}
|
||||
endTime := startTime.Add(time.Hour * 24 * 30)
|
||||
filter.AddTimeField("stop_time").Is = endTime
|
||||
filter.AddTextField("count").Is = "30"
|
||||
|
||||
expected := "?stop_time=0001-01-31T00:00:00Z&count=30"
|
||||
if filter.String() != expected {
|
||||
t.Errorf("filter string was %s, wanted %s", filter.String(), expected)
|
||||
}
|
||||
}
|
12
payment.go
12
payment.go
|
@ -131,9 +131,19 @@ func (c *Client) PatchPayment(paymentID string, p []PaymentPatch) (*Payment, err
|
|||
// GetPayments retrieve payments resources from Paypal
|
||||
// Endpoint: GET /v1/payments/payment/
|
||||
func (c *Client) GetPayments() ([]Payment, error) {
|
||||
return c.getPayments(nil)
|
||||
}
|
||||
|
||||
// GetPayments retrieve payments resources from Paypal by the provided filter
|
||||
// Endpoint: GET /v1/payments/payment/
|
||||
func (c *Client) GetPaymentsWithFilter(filter *Filter) ([]Payment, error) {
|
||||
return c.getPayments(filter)
|
||||
}
|
||||
|
||||
func (c *Client) getPayments(filter *Filter) ([]Payment, error) {
|
||||
var p ListPaymentsResp
|
||||
|
||||
req, err := http.NewRequest("GET", fmt.Sprintf("%s%s", c.APIBase, "/v1/payments/payment/"), nil)
|
||||
req, err := http.NewRequest("GET", fmt.Sprintf("%s%s%s", c.APIBase, "/v1/payments/payment/", filter), nil)
|
||||
if err != nil {
|
||||
return p.Payments, err
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user