Skip to content

Commit 9b3634e

Browse files
author
Zaki Ibrahim
authored
Merge pull request #8 from Xaxxis/main
[fix bug] nil StatusCode handling on HTTP Client & Iris error type field struct
2 parents ad331a7 + 54f0f0e commit 9b3634e

File tree

7 files changed

+107
-51
lines changed

7 files changed

+107
-51
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,7 @@ There are:
631631
- [Core Api examples](example/simple/coreapi/sample.go)
632632
- [Snap examples](example/simple/snap/sample.go)
633633
- [Iris examples](example/simple/iris/sample.go)
634+
- [Readme Example](example/README.md)
634635

635636
Integration test are available
636637
- [CoreApi Sample Functional Test](coreapi/client_test.go)

example/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Sample App
2+
This is a very simple, very minimalist example to demonstrate integrating Midtrans with Go
3+
4+
## Run The app
5+
1. Clone the repository, open terminal in this `/example/simple/coreapi-card-3ds` folder.
6+
2. Run the web server using: `go run main.go`
7+
3. The smple app will run at port 3000. Open `localhost:3000` from browser.
8+
9+
## Run command line app
10+
1. Clone the repository, open terminal in this `/example/simple/coreapi` folder.
11+
2. Run the app using: `go run main.go`

example/simple/coreapi-card-3ds/main.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,16 @@
33
package main
44

55
import (
6+
"encoding/json"
7+
"fmt"
8+
"github.com/midtrans/midtrans-go"
9+
"github.com/midtrans/midtrans-go/coreapi"
610
"html/template"
7-
"net/http"
811
"log"
12+
"net/http"
913
"path"
10-
"time"
1114
"strconv"
12-
"github.com/midtrans/midtrans-go"
13-
"github.com/midtrans/midtrans-go/coreapi"
14-
"encoding/json"
15+
"time"
1516
)
1617

1718
// Set Your server key
@@ -48,7 +49,7 @@ func HomeHandler(w http.ResponseWriter, r *http.Request) {
4849
}
4950

5051
needCredential := false
51-
if(len(SERVER_KEY) == 0 || len(CLIENT_KEY) == 0 ){
52+
if len(SERVER_KEY) == 0 || len(CLIENT_KEY) == 0 {
5253
needCredential = true
5354
}
5455

httpclient.go

Lines changed: 53 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -97,66 +97,78 @@ func (c *HttpClientImplementation) DoRequest(req *http.Request, result interface
9797
res, err := c.HttpClient.Do(req)
9898
if err != nil {
9999
c.Logger.Error("Cannot send request: %v", err.Error())
100+
var statusCode int
101+
102+
if res != nil {
103+
statusCode = res.StatusCode
104+
} else if strings.Contains(err.Error(), "timeout") {
105+
statusCode = 408
106+
} else {
107+
statusCode = 0
108+
}
109+
100110
return &Error{
101111
Message: fmt.Sprintf("Error when request via HttpClient, Cannot send request with error: %s", err.Error()),
102-
StatusCode: res.StatusCode,
112+
StatusCode: statusCode,
103113
RawError: err,
104114
}
105115
}
106116

107-
defer res.Body.Close()
117+
if res != nil {
118+
defer res.Body.Close()
108119

109-
c.Logger.Info("================== END ==================")
110-
c.Logger.Info("Request completed in %v ", time.Since(start))
120+
c.Logger.Info("================== END ==================")
121+
c.Logger.Info("Request completed in %v ", time.Since(start))
111122

112-
resBody, err := ioutil.ReadAll(res.Body)
113-
if err != nil {
114-
c.Logger.Error("Request failed: %v", err)
115-
return &Error{
116-
Message: "Cannot read response body: " + err.Error(),
117-
StatusCode: res.StatusCode,
123+
resBody, err := ioutil.ReadAll(res.Body)
124+
if err != nil {
125+
c.Logger.Error("Request failed: %v", err)
126+
return &Error{
127+
Message: "Cannot read response body: " + err.Error(),
128+
StatusCode: res.StatusCode,
129+
RawError: err,
130+
}
118131
}
119-
}
120132

121-
rawResponse := newHTTPResponse(res, resBody)
122-
c.Logger.Debug("=============== Response ===============")
123-
// Loop through headers to perform log
124-
logHttpHeaders(c.Logger, rawResponse.Header, false)
125-
c.Logger.Debug("Response Body: %v", string(rawResponse.RawBody))
133+
rawResponse := newHTTPResponse(res, resBody)
134+
c.Logger.Debug("=============== Response ===============")
135+
// Loop through headers to perform log
136+
logHttpHeaders(c.Logger, rawResponse.Header, false)
137+
c.Logger.Debug("Response Body: %v", string(rawResponse.RawBody))
138+
139+
if result != nil {
140+
if err = json.Unmarshal(resBody, &result); err != nil {
141+
return &Error{
142+
Message: fmt.Sprintf("Invalid body response, parse error during API request to Midtrans with message: %s", err.Error()),
143+
StatusCode: res.StatusCode,
144+
RawError: err,
145+
RawApiResponse: rawResponse,
146+
}
147+
}
148+
}
126149

127-
if result != nil {
128-
if err = json.Unmarshal(resBody, &result); err != nil {
129-
return &Error{
130-
Message: fmt.Sprintf("Invalid body response, parse error during API request to Midtrans with message: %s", err.Error()),
131-
StatusCode: res.StatusCode,
132-
RawError: err,
133-
RawApiResponse: rawResponse,
150+
// Check status_code from Midtrans response body
151+
if found, data := HasOwnProperty("status_code", resBody); found {
152+
statusCode, _ := strconv.Atoi(data["status_code"].(string))
153+
if statusCode >= 401 && statusCode != 407 {
154+
return &Error{
155+
Message: fmt.Sprintf("Midtrans API is returning API error. HTTP status code: %s API response: %s", strconv.Itoa(statusCode), string(resBody)),
156+
StatusCode: statusCode,
157+
RawApiResponse: rawResponse,
158+
}
134159
}
135160
}
136-
}
137161

138-
// Check status_code from Midtrans response body
139-
if found, data := HasOwnProperty("status_code", resBody); found {
140-
statusCode, _ := strconv.Atoi(data["status_code"].(string))
141-
if statusCode >= 401 && statusCode != 407 {
162+
// Check StatusCode from Midtrans HTTP response api StatusCode
163+
if res.StatusCode >= 400 {
142164
return &Error{
143-
Message: fmt.Sprintf("Midtrans API is returning API error. HTTP status code: %s API response: %s", strconv.Itoa(statusCode), string(resBody)),
144-
StatusCode: statusCode,
165+
Message: fmt.Sprintf("Midtrans API is returning API error. HTTP status code: %s API response: %s", strconv.Itoa(res.StatusCode), string(resBody)),
166+
StatusCode: res.StatusCode,
145167
RawApiResponse: rawResponse,
168+
RawError: err,
146169
}
147170
}
148171
}
149-
150-
// Check StatusCode from Midtrans HTTP response api StatusCode
151-
if res.StatusCode >= 400 {
152-
return &Error{
153-
Message: fmt.Sprintf("Midtrans API is returning API error. HTTP status code: %s API response: %s", strconv.Itoa(res.StatusCode), string(resBody)),
154-
StatusCode: res.StatusCode,
155-
RawApiResponse: rawResponse,
156-
RawError: err,
157-
}
158-
}
159-
160172
return nil
161173
}
162174

iris/client_test.go

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,38 @@ func TestGetListBeneficiaryBank(t *testing.T) {
178178
func TestValidateBankAccount(t *testing.T) {
179179
iris := Client{}
180180
iris.New(irisApproverKeySandbox, midtrans.Sandbox)
181-
resp, err := iris.ValidateBankAccount("danamon", "000001137298")
181+
resp, err := iris.ValidateBankAccount("mandiri", "1111222233333")
182182
assert.Nil(t, err)
183-
assert.Equal(t, resp.AccountNo, "000001137298")
183+
assert.Equal(t, resp.AccountNo, "1111222233333")
184+
}
185+
186+
func TestCreatePayoutFail(t *testing.T) {
187+
iris := Client{}
188+
iris.New(irisCreatorKeySandbox, midtrans.Sandbox)
189+
190+
p1 := CreatePayoutDetailReq{
191+
BeneficiaryAccount: "1380011819286",
192+
BeneficiaryBank: "mandiri",
193+
BeneficiaryEmail: "tony.stark@mail.com",
194+
Amount: random(),
195+
Notes: "MidGoUnitTest",
196+
}
197+
198+
p2 := CreatePayoutDetailReq{
199+
BeneficiaryAccount: "1380011819286",
200+
BeneficiaryBank: "mandiri",
201+
BeneficiaryEmail: "jon.snow@mail.com",
202+
Amount: random(),
203+
Notes: "MidGoUnitTest",
204+
}
205+
var payouts []CreatePayoutDetailReq
206+
payouts = append(payouts, p1)
207+
payouts = append(payouts, p2)
208+
209+
210+
cp := CreatePayoutReq{Payouts: payouts}
211+
payoutReps, err := iris.CreatePayout(cp)
212+
assert.NotNil(t, payoutReps)
213+
assert.NotNil(t, err)
214+
assert.Equal(t, "An error occurred when creating payouts", payoutReps.ErrorMessage)
184215
}

iris/response.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ type BeneficiaryBankResponse struct {
2929
type CreatePayoutResponse struct {
3030
Payouts []CreatePayoutDetailResponse `json:"payouts"`
3131
ErrorMessage string `json:"error_message"`
32-
Errors []string `json:"errors"`
32+
Errors interface{} `json:"errors"`
3333
}
3434

3535
// CreatePayoutDetailResponse : Represent Create payout detail response payload

midtrans.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const (
2020
Production
2121

2222
//libraryVersion : midtrans go library version
23-
libraryVersion = "v1.2.1"
23+
libraryVersion = "v1.2.2"
2424
)
2525

2626
//ServerKey is config payment API key for global use

0 commit comments

Comments
 (0)