diff --git a/README.md b/README.md index 632d156..74b1f4a 100644 --- a/README.md +++ b/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("end_time").Is = time.Now() + +payments, err := c.GetPaymentsWithFilter(filter) ``` ### Get authorization by ID diff --git a/filter.go b/filter.go new file mode 100644 index 0000000..81d6f1f --- /dev/null +++ b/filter.go @@ -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 +} diff --git a/filter_test.go b/filter_test.go new file mode 100644 index 0000000..1791269 --- /dev/null +++ b/filter_test.go @@ -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) + } +} diff --git a/payment.go b/payment.go index fe66071..9ef76c8 100644 --- a/payment.go +++ b/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 }