Skip to content

Commit

Permalink
Init Iris service
Browse files Browse the repository at this point in the history
  • Loading branch information
andhikamaheva committed Sep 27, 2018
1 parent 2fada1c commit e74c4b5
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 7 deletions.
29 changes: 23 additions & 6 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package midtrans

import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
Expand All @@ -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
Expand All @@ -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

Expand All @@ -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

Expand All @@ -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()
Expand Down Expand Up @@ -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
Expand Down
14 changes: 13 additions & 1 deletion envtype.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package midtrans

import "strings"
import (
"strings"
)

// EnvironmentType value
type EnvironmentType int8
Expand Down Expand Up @@ -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"
}
124 changes: 124 additions & 0 deletions iris.go
Original file line number Diff line number Diff line change
@@ -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
}
9 changes: 9 additions & 0 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
17 changes: 17 additions & 0 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

0 comments on commit e74c4b5

Please sign in to comment.