From f6f23c2fab5d489a49be5eb1cdd34c385326ba9b Mon Sep 17 00:00:00 2001 From: Aashish Date: Wed, 20 Mar 2024 17:35:26 +0700 Subject: [PATCH 1/5] Added support for IP field on source creation. --- operations/source.go | 3 +-- operations/source_test.go | 17 +++++++++++++++++ source.go | 1 + 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/operations/source.go b/operations/source.go index f3015fa..b1f0e8b 100644 --- a/operations/source.go +++ b/operations/source.go @@ -16,7 +16,6 @@ import ( // } // // fmt.Println("created source:", source.ID) -// type CreateSource struct { Type string `json:"type"` Amount int64 `json:"amount"` @@ -32,6 +31,7 @@ type CreateSource struct { TerminalID string `json:"terminal_id,omitempty"` ZeroInterestInstallments bool `json:"zero_interest_installments,omitempty"` PlatformType string `json:"platform_type,omitempty"` + Ip string `json:"ip,omitempty"` } func (req *CreateSource) Describe() *internal.Description { @@ -52,7 +52,6 @@ func (req *CreateSource) Describe() *internal.Description { // } // // fmt.Printf("source #123: %#v\n", source) -// type RetrieveSource struct { SourceID string `json:"-"` } diff --git a/operations/source_test.go b/operations/source_test.go index fd3119b..e8c799c 100644 --- a/operations/source_test.go +++ b/operations/source_test.go @@ -56,3 +56,20 @@ func TestCreateSourceWithPlatformType(t *testing.T) { client.MustDo(exampleSource, createSource) r.Equal(t, SourceID, exampleSource.ID) } + +func TestCreateSourceWithIP(t *testing.T) { + const ( + SourceID = "src_test_5mygxph6d55vvy8nn9i" + ) + client := testutil.NewFixedClient(t) + + exampleSource, createSource := &omise.Source{}, &operations.CreateSource{ + Type: "wechat_pay", + Amount: 20000, + Currency: "thb", + Ip: "192.168.1.1", + } + + client.MustDo(exampleSource, createSource) + r.Equal(t, SourceID, exampleSource.ID) +} diff --git a/source.go b/source.go index f5abbad..235ae90 100644 --- a/source.go +++ b/source.go @@ -12,4 +12,5 @@ type Source struct { References *References `json:"references"` ZeroInterestInstallments bool `json:"zero_interest_installments"` PlatformType string `json:"platform_type"` + Ip string `json:"ip"` } From 09b83292015b1beab8c097f011a845248b7807dd Mon Sep 17 00:00:00 2001 From: Aashish Date: Thu, 21 Mar 2024 14:17:14 +0700 Subject: [PATCH 2/5] Throw an error if IP is not passed for source wechat_pay --- client.go | 28 ++++++++++++++++++++++++++++ client_test.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/client.go b/client.go index d09a188..0d55125 100644 --- a/client.go +++ b/client.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "encoding/json" + "errors" "fmt" "go/build" "io" @@ -162,6 +163,12 @@ func (c *Client) setRequestHeaders(req *http.Request, desc *internal.Description // non-nil error should be returned. Error maybe of the omise-go.Error struct type, in // which case you can further inspect the Code and Message field for more information. func (c *Client) Do(result interface{}, operation internal.Operation) error { + err := c.checkRequiredFields(operation) + + if err != nil { + return err + } + req, err := c.Request(operation) if err != nil { @@ -205,3 +212,24 @@ func (c *Client) Do(result interface{}, operation internal.Operation) error { return nil } + +func (c *Client) checkRequiredFields(operation internal.Operation) error { + b, err := json.Marshal(operation) + if err != nil { + return err + } + + // Unmarshal JSON data into a map + var newResult map[string]interface{} + json.Unmarshal(b, &newResult) + + if err != nil { + return err + } + + if newResult["type"] == "wechat_pay" && (newResult["ip"] == nil) { + return errors.New("ip is required in the source") + } + + return nil +} diff --git a/client_test.go b/client_test.go index 10daf5d..c8d89d4 100644 --- a/client_test.go +++ b/client_test.go @@ -7,6 +7,7 @@ import ( "log" "testing" + "github.com/omise/omise-go" . "github.com/omise/omise-go" "github.com/omise/omise-go/internal" "github.com/omise/omise-go/internal/testutil" @@ -141,6 +142,34 @@ func TestClient_WithContext(t *testing.T) { r.Equal(t, account.ID, "acct_4yq6tcsyoged5c0ocxd") } +func TestClient_WeChatPayError(t *testing.T) { + pkey, skey := testutil.Keys() + client, _ := NewClient(pkey, skey) + + wechatSource, createSource := &omise.Source{}, &operations.CreateSource{ + Type: "wechat_pay", + Amount: 20000, + Currency: "thb", + } + + err := client.Do(wechatSource, createSource) + r.NotNil(t, err) + r.Equal(t, err.Error(), "ip is required in the source") +} + +func TestClient_WeChatPay(t *testing.T) { + client := testutil.NewFixedClient(t) + + wechatSource, createSource := &omise.Source{}, &operations.CreateSource{ + Type: "wechat_pay", + Amount: 20000, + Currency: "thb", + Ip: "192.168.1.1", + } + + client.MustDo(wechatSource, createSource) +} + func ExampleClient_Do() { // gets your API keys pkey, skey := "pkey_test_4yq6tct0llin5nyyi5l", "skey_test_4yq6tct0lblmed2yp5t" From 88543fc1c8b84ee56ca00be7a33a99381645aece Mon Sep 17 00:00:00 2001 From: Aashish Date: Thu, 21 Mar 2024 14:20:36 +0700 Subject: [PATCH 3/5] Added missing error return of json.Unmarshall --- client.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client.go b/client.go index 0d55125..6d248c8 100644 --- a/client.go +++ b/client.go @@ -221,7 +221,7 @@ func (c *Client) checkRequiredFields(operation internal.Operation) error { // Unmarshal JSON data into a map var newResult map[string]interface{} - json.Unmarshal(b, &newResult) + err = json.Unmarshal(b, &newResult) if err != nil { return err From f642d57fa1a5e6ea7c114bc4e8d71a5bc135e0b9 Mon Sep 17 00:00:00 2001 From: Aashish Date: Thu, 21 Mar 2024 14:41:43 +0700 Subject: [PATCH 4/5] Remove added code from client.go --- client.go | 28 ---------------------------- client_test.go | 29 ----------------------------- operations/source_test.go | 4 +++- 3 files changed, 3 insertions(+), 58 deletions(-) diff --git a/client.go b/client.go index 6d248c8..d09a188 100644 --- a/client.go +++ b/client.go @@ -4,7 +4,6 @@ import ( "bytes" "context" "encoding/json" - "errors" "fmt" "go/build" "io" @@ -163,12 +162,6 @@ func (c *Client) setRequestHeaders(req *http.Request, desc *internal.Description // non-nil error should be returned. Error maybe of the omise-go.Error struct type, in // which case you can further inspect the Code and Message field for more information. func (c *Client) Do(result interface{}, operation internal.Operation) error { - err := c.checkRequiredFields(operation) - - if err != nil { - return err - } - req, err := c.Request(operation) if err != nil { @@ -212,24 +205,3 @@ func (c *Client) Do(result interface{}, operation internal.Operation) error { return nil } - -func (c *Client) checkRequiredFields(operation internal.Operation) error { - b, err := json.Marshal(operation) - if err != nil { - return err - } - - // Unmarshal JSON data into a map - var newResult map[string]interface{} - err = json.Unmarshal(b, &newResult) - - if err != nil { - return err - } - - if newResult["type"] == "wechat_pay" && (newResult["ip"] == nil) { - return errors.New("ip is required in the source") - } - - return nil -} diff --git a/client_test.go b/client_test.go index c8d89d4..10daf5d 100644 --- a/client_test.go +++ b/client_test.go @@ -7,7 +7,6 @@ import ( "log" "testing" - "github.com/omise/omise-go" . "github.com/omise/omise-go" "github.com/omise/omise-go/internal" "github.com/omise/omise-go/internal/testutil" @@ -142,34 +141,6 @@ func TestClient_WithContext(t *testing.T) { r.Equal(t, account.ID, "acct_4yq6tcsyoged5c0ocxd") } -func TestClient_WeChatPayError(t *testing.T) { - pkey, skey := testutil.Keys() - client, _ := NewClient(pkey, skey) - - wechatSource, createSource := &omise.Source{}, &operations.CreateSource{ - Type: "wechat_pay", - Amount: 20000, - Currency: "thb", - } - - err := client.Do(wechatSource, createSource) - r.NotNil(t, err) - r.Equal(t, err.Error(), "ip is required in the source") -} - -func TestClient_WeChatPay(t *testing.T) { - client := testutil.NewFixedClient(t) - - wechatSource, createSource := &omise.Source{}, &operations.CreateSource{ - Type: "wechat_pay", - Amount: 20000, - Currency: "thb", - Ip: "192.168.1.1", - } - - client.MustDo(wechatSource, createSource) -} - func ExampleClient_Do() { // gets your API keys pkey, skey := "pkey_test_4yq6tct0llin5nyyi5l", "skey_test_4yq6tct0lblmed2yp5t" diff --git a/operations/source_test.go b/operations/source_test.go index e8c799c..4b90fe7 100644 --- a/operations/source_test.go +++ b/operations/source_test.go @@ -60,6 +60,7 @@ func TestCreateSourceWithPlatformType(t *testing.T) { func TestCreateSourceWithIP(t *testing.T) { const ( SourceID = "src_test_5mygxph6d55vvy8nn9i" + Ip = "192.168.1.1" ) client := testutil.NewFixedClient(t) @@ -67,9 +68,10 @@ func TestCreateSourceWithIP(t *testing.T) { Type: "wechat_pay", Amount: 20000, Currency: "thb", - Ip: "192.168.1.1", + Ip: Ip, } client.MustDo(exampleSource, createSource) r.Equal(t, SourceID, exampleSource.ID) + r.Equal(t, Ip, exampleSource.Ip) } From 2a0078865b04a3bef08b9602d8e58f34533408a0 Mon Sep 17 00:00:00 2001 From: Aashish Date: Thu, 21 Mar 2024 14:45:28 +0700 Subject: [PATCH 5/5] Update source testdata --- testdata/fixtures/api.omise.co/sources-post.json | 1 + 1 file changed, 1 insertion(+) diff --git a/testdata/fixtures/api.omise.co/sources-post.json b/testdata/fixtures/api.omise.co/sources-post.json index 61c2632..41f3986 100644 --- a/testdata/fixtures/api.omise.co/sources-post.json +++ b/testdata/fixtures/api.omise.co/sources-post.json @@ -10,6 +10,7 @@ "currency": "MYR", "email": "example@omise.co", "flow": "redirect", + "ip": "192.168.1.1", "installment_term": null, "name": null, "mobile_number": null,