From 0a9659fa5ef8c4eacaa22312e3f7210de73f301a Mon Sep 17 00:00:00 2001 From: Alexandru Giurgiu Date: Thu, 19 Jan 2017 17:00:35 +0200 Subject: [PATCH 1/4] extended the result with an extra message field. Used to communicate to the status of the payment --- service.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/service.go b/service.go index d585875..cf98647 100644 --- a/service.go +++ b/service.go @@ -11,7 +11,8 @@ type Service interface { } type Authorisation struct { - Authorised bool `json:"authorised"` + Authorised bool `json:"authorised"` + Message string `json:"message"` } type Health struct { @@ -41,11 +42,16 @@ func (s *service) Authorise(amount float32) (Authorisation, error) { return Authorisation{}, ErrInvalidPaymentAmount } authorised := false + message := "Payment declined" if amount <= s.declineOverAmount { authorised = true + message = "Payment authorised" + } else { + message = "Payment declined: amount exceeds $100" } return Authorisation{ Authorised: authorised, + Message: message }, nil } From f9ed86ac4c70e20164cf70e115d648e653c757b4 Mon Sep 17 00:00:00 2001 From: Alexandru Giurgiu Date: Fri, 20 Jan 2017 11:50:35 +0200 Subject: [PATCH 2/4] message is now based on the amount stored in the variable --- service.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/service.go b/service.go index cf98647..7a71e03 100644 --- a/service.go +++ b/service.go @@ -47,7 +47,7 @@ func (s *service) Authorise(amount float32) (Authorisation, error) { authorised = true message = "Payment authorised" } else { - message = "Payment declined: amount exceeds $100" + message = fmt.Sprintf("Payment declined: amount exceeds $%d", s.declineOverAmount) } return Authorisation{ Authorised: authorised, From 8ed9b16a008d4ec754334167e8099fb6504fb80b Mon Sep 17 00:00:00 2001 From: Alexandru Giurgiu Date: Fri, 20 Jan 2017 13:33:27 +0200 Subject: [PATCH 3/4] addapted tests to check for the new message field --- service.go | 3 ++- service_test.go | 20 ++++++++++---------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/service.go b/service.go index 7a71e03..3819e6d 100644 --- a/service.go +++ b/service.go @@ -2,6 +2,7 @@ package payment import ( "errors" + "fmt" "time" ) @@ -51,7 +52,7 @@ func (s *service) Authorise(amount float32) (Authorisation, error) { } return Authorisation{ Authorised: authorised, - Message: message + Message: message, }, nil } diff --git a/service_test.go b/service_test.go index f87c0b2..5b9cbd3 100644 --- a/service_test.go +++ b/service_test.go @@ -1,24 +1,24 @@ package payment -import ( - "testing" -) +import "testing" +import "fmt" func TestAuthorise(t *testing.T) { result, _ := NewAuthorisationService(100).Authorise(10) - expected := true - if result.Authorised != expected { + expected := Authorisation{true, "Payment authorised"} + if result != expected { t.Errorf("Authorise returned unexpected result: got %v want %v", - result.Authorised, expected) + result, expected) } } func TestFailOverCertainAmount(t *testing.T) { - result, _ := NewAuthorisationService(10).Authorise(100) - expected := false - if result.Authorised != expected { + declineAmount := float32(10) + result, _ := NewAuthorisationService(declineAmount).Authorise(100) + expected := Authorisation{false, fmt.Sprintf("Payment declined: amount exceeds $%d", declineAmount)} + if result != expected { t.Errorf("Authorise returned unexpected result: got %v want %v", - result.Authorised, expected) + result, expected) } } From 240ba8939b392a875ae9b8982978d5fc1d28ef57 Mon Sep 17 00:00:00 2001 From: Alexandru Giurgiu Date: Fri, 20 Jan 2017 14:51:05 +0200 Subject: [PATCH 4/4] proper float formating --- service.go | 2 +- service_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/service.go b/service.go index 3819e6d..a22211a 100644 --- a/service.go +++ b/service.go @@ -48,7 +48,7 @@ func (s *service) Authorise(amount float32) (Authorisation, error) { authorised = true message = "Payment authorised" } else { - message = fmt.Sprintf("Payment declined: amount exceeds $%d", s.declineOverAmount) + message = fmt.Sprintf("Payment declined: amount exceeds %.2f", s.declineOverAmount) } return Authorisation{ Authorised: authorised, diff --git a/service_test.go b/service_test.go index 5b9cbd3..3a24b46 100644 --- a/service_test.go +++ b/service_test.go @@ -15,7 +15,7 @@ func TestAuthorise(t *testing.T) { func TestFailOverCertainAmount(t *testing.T) { declineAmount := float32(10) result, _ := NewAuthorisationService(declineAmount).Authorise(100) - expected := Authorisation{false, fmt.Sprintf("Payment declined: amount exceeds $%d", declineAmount)} + expected := Authorisation{false, fmt.Sprintf("Payment declined: amount exceeds %.2f", declineAmount)} if result != expected { t.Errorf("Authorise returned unexpected result: got %v want %v", result, expected)