From 36f64e9a3b81b8d136b003aab1b8f770bb925db9 Mon Sep 17 00:00:00 2001 From: Rickard Lindroth Date: Mon, 10 Dec 2018 12:54:15 +0100 Subject: [PATCH 1/2] added GetPaymentsWithFilter and Filter to list filtered payments --- README.md | 14 ++++++++++++ filter.go | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++ filter_test.go | 44 +++++++++++++++++++++++++++++++++++++ payment.go | 12 +++++++++- 4 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 filter.go create mode 100644 filter_test.go diff --git a/README.md b/README.md index 632d156..618e28e 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("stop_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 8bd95ec..af27065 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 } From d5a45e7e9af8e031f5930878ef11f3555bffd8a1 Mon Sep 17 00:00:00 2001 From: Rickard Lindroth <344477+lindroth@users.noreply.github.com> Date: Mon, 10 Dec 2018 14:18:00 +0100 Subject: [PATCH 2/2] Update README.md wrong parameter name for listing payments --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 618e28e..74b1f4a 100644 --- a/README.md +++ b/README.md @@ -133,7 +133,7 @@ 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() +filter.AddTimeField("end_time").Is = time.Now() payments, err := c.GetPaymentsWithFilter(filter) ```