diff --git a/client.go b/client.go index e9255e0..651090e 100644 --- a/client.go +++ b/client.go @@ -2,6 +2,7 @@ package midtrans import ( "encoding/json" + "fmt" "io" "io/ioutil" "log" @@ -15,8 +16,12 @@ import ( // Client struct type Client struct { APIEnvType EnvironmentType - ClientKey string - ServerKey string + + ClientKey string + ServerKey string + + ApproverKey string + CreatorKey string LogLevel int Logger *log.Logger @@ -42,7 +47,7 @@ var defHTTPTimeout = 80 * time.Second var httpClient = &http.Client{Timeout: defHTTPTimeout} // NewRequest : send new request -func (c *Client) NewRequest(method string, fullPath string, body io.Reader) (*http.Request, error) { +func (c *Client) NewRequest(method string, fullPath string, body io.Reader, key ...string) (*http.Request, error) { logLevel := c.LogLevel logger := c.Logger @@ -56,13 +61,18 @@ func (c *Client) NewRequest(method string, fullPath string, body io.Reader) (*ht req.Header.Add("Content-Type", "application/json") req.Header.Add("Accept", "application/json") - req.SetBasicAuth(c.ServerKey, "") + if len(key) > 0 { + req.SetBasicAuth(key[0], "") + } else { + req.SetBasicAuth(c.ServerKey, "") + } return req, nil } // ExecuteRequest : execute request func (c *Client) ExecuteRequest(req *http.Request, v interface{}) error { + logLevel := c.LogLevel logger := c.Logger @@ -71,12 +81,12 @@ func (c *Client) ExecuteRequest(req *http.Request, v interface{}) error { } start := time.Now() - res, err := httpClient.Do(req) if err != nil { if logLevel > 0 { logger.Println("Cannot send request: ", err) } + return err } defer res.Body.Close() @@ -105,10 +115,17 @@ func (c *Client) ExecuteRequest(req *http.Request, v interface{}) error { } if v != nil { + if err = json.Unmarshal(resBody, v); err != nil { return err } - reflect.ValueOf(v).Elem().FieldByName("StatusCode").SetString(strconv.Itoa(res.StatusCode)) + + fmt.Println(v) + + if reflect.ValueOf(v).Elem().FieldByName("StatuCode").IsValid() == true { + reflect.ValueOf(v).Elem().FieldByName("StatusCode").SetString(strconv.Itoa(res.StatusCode)) + } + } return nil diff --git a/envtype.go b/envtype.go index e382ff0..9e8c307 100644 --- a/envtype.go +++ b/envtype.go @@ -1,6 +1,8 @@ package midtrans -import "strings" +import ( + "strings" +) // EnvironmentType value type EnvironmentType int8 @@ -34,3 +36,13 @@ func (e EnvironmentType) String() string { func (e EnvironmentType) SnapURL() string { return strings.Replace(e.String(), "api.", "app.", 1) } + +// IrisURL : Get environment API URL +func (e EnvironmentType) IrisURL() string { + if Production == e { + return "https://app.midtrans.com/iris" + } else if Sandbox == e { + return "https://iris.sandbox.midtrans.com" + } + return "undefined" +} diff --git a/iris.go b/iris.go new file mode 100644 index 0000000..92fefac --- /dev/null +++ b/iris.go @@ -0,0 +1,124 @@ +package midtrans + +import ( + "bytes" + "encoding/json" + "io" + "strings" +) + +// IrisGateway struct +type IrisGateway struct { + Client Client +} + +// Call : base method to call Core API +func (gateway *IrisGateway) Call(method, path string, body io.Reader, v interface{}) error { + if !strings.HasPrefix(path, "/") { + path = "/" + path + } + + path = gateway.Client.APIEnvType.IrisURL() + path + req, err := gateway.Client.NewRequest(method, path, body, gateway.Client.ApproverKey) + if err != nil { + return err + } + + return gateway.Client.ExecuteRequest(req, v) +} + +// CreateBeneficiaries : Perform transaction using ChargeReq +func (gateway *IrisGateway) CreateBeneficiaries(req *BeneficiariesReq) (interface{}, error) { + resp := make(map[string]interface{}) + jsonReq, _ := json.Marshal(req) + + err := gateway.Call("POST", "api/v1/beneficiaries", bytes.NewBuffer(jsonReq), &resp) + if err != nil { + gateway.Client.Logger.Println("Error create beneficiaries: ", err) + return resp, err + } + + return resp, nil +} + +// ValidateBankAccount : get order status using order ID +func (gateway *IrisGateway) ValidateBankAccount(bankName string, account string) (ValidateBankAcount, error) { + resp := ValidateBankAcount{} + + err := gateway.Call("GET", "api/v1/account_validation?bank="+bankName+"&account="+account, nil, &resp) + if err != nil { + gateway.Client.Logger.Println("Error approving: ", err) + return resp, err + } + + return resp, nil +} + +// CaptureCard : Capture an authorized transaction for card payment +func (gateway *IrisGateway) CaptureCard(req *CaptureReq) (Response, error) { + resp := Response{} + jsonReq, _ := json.Marshal(req) + + err := gateway.Call("POST", "v2/capture", bytes.NewBuffer(jsonReq), &resp) + if err != nil { + gateway.Client.Logger.Println("Error capturing: ", err) + return resp, err + } + + if resp.StatusMessage != "" { + gateway.Client.Logger.Println(resp.StatusMessage) + } + + return resp, nil +} + +// Approve : Approve order using order ID +func (gateway *IrisGateway) Approve(orderID string) (Response, error) { + resp := Response{} + + err := gateway.Call("POST", "v2/"+orderID+"/approve", nil, &resp) + if err != nil { + gateway.Client.Logger.Println("Error approving: ", err) + return resp, err + } + + if resp.StatusMessage != "" { + gateway.Client.Logger.Println(resp.StatusMessage) + } + + return resp, nil +} + +// Cancel : Cancel order using order ID +func (gateway *IrisGateway) Cancel(orderID string) (Response, error) { + resp := Response{} + + err := gateway.Call("POST", "v2/"+orderID+"/cancel", nil, &resp) + if err != nil { + gateway.Client.Logger.Println("Error approving: ", err) + return resp, err + } + + if resp.StatusMessage != "" { + gateway.Client.Logger.Println(resp.StatusMessage) + } + + return resp, nil +} + +// Expire : change order status to expired using order ID +func (gateway *IrisGateway) Expire(orderID string) (Response, error) { + resp := Response{} + + err := gateway.Call("POST", "v2/"+orderID+"/expire", nil, &resp) + if err != nil { + gateway.Client.Logger.Println("Error approving: ", err) + return resp, err + } + + if resp.StatusMessage != "" { + gateway.Client.Logger.Println(resp.StatusMessage) + } + + return resp, nil +} diff --git a/request.go b/request.go index aaadd82..2260389 100644 --- a/request.go +++ b/request.go @@ -225,3 +225,12 @@ type CaptureReq struct { TransactionID string `json:"transaction_id"` GrossAmt float64 `json:"gross_amount"` } + +// BeneficiariesReq : Represent create or Beneficiaries request payload +type BeneficiariesReq struct { + Name string `json:"name"` + Account string `json:"account"` + Bank string `json:"bank"` + AliasName string `json:"alias_name"` + Email string `json:"email,omitempty"` +} diff --git a/response.go b/response.go index e8614b9..44ce5cd 100644 --- a/response.go +++ b/response.go @@ -53,3 +53,20 @@ type SnapResponse struct { RedirectURL string `json:"redirect_url"` ErrorMessages []string `json:"error_messages"` } + +// Beneficiaries : Response after calling the Beneficiaries API +type Beneficiaries struct { + Name string `json:"name"` + Bank string `json:"bank"` + Account string `json:"account"` + AliasName string `json:"alias_name"` + Email string `json:"email"` +} + +// ValidateBankAcount : Response for BankAcount +type ValidateBankAcount struct { + AccountName string `json:"account_name"` + AccountNo string `json:"account_no"` + BankName string `json:"bank_name"` + ReffNumber string `json:"retrieval_reff_num"` +}