diff --git a/README.md b/README.md index ff84d5b..f27f062 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ +[![Go Report Card](https://goreportcard.com/badge/github.com/veritrans/go-midtrans)](https://goreportcard.com/report/github.com/veritrans/go-midtrans) + # Midtrans Library for Go(lang) Midtrans :heart: Go ! diff --git a/bank.go b/bank.go index 4322523..fc478be 100644 --- a/bank.go +++ b/bank.go @@ -1,13 +1,27 @@ package midtrans +// Bank value type Bank string const ( - BankBni Bank = "bni" + //BankBni : bni + BankBni Bank = "bni" + + //BankMandiri : mandiri BankMandiri Bank = "mandiri" - BankCimb Bank = "cimb" - BankBca Bank = "bca" - BankBri Bank = "bri" + + //BankCimb : cimb + BankCimb Bank = "cimb" + + //BankBca : bca + BankBca Bank = "bca" + + //BankBri : bri + BankBri Bank = "bri" + + //BankMaybank : maybank BankMaybank Bank = "maybank" + + //BankPermata : permata BankPermata Bank = "permata" ) diff --git a/client.go b/client.go index e3c5905..959e1d0 100644 --- a/client.go +++ b/client.go @@ -1,119 +1,129 @@ package midtrans import ( - "log" - "os" - "net/http" - "time" - "io" - "io/ioutil" - "encoding/json" + "encoding/json" + "io" + "io/ioutil" + "log" + "net/http" + "os" + "time" ) +// Client struct type Client struct { - ApiEnvType EnvironmentType - ClientKey string - ServerKey string + APIEnvType EnvironmentType + ClientKey string + ServerKey string - LogLevel int - Logger *log.Logger + LogLevel int + Logger *log.Logger } -// this function will always be called when the library is in use +// NewClient : this function will always be called when the library is in use func NewClient() Client { - return Client{ - ApiEnvType: Sandbox, - - // LogLevel is the logging level used by the Midtrans library - // 0: No logging - // 1: Errors only - // 2: Errors + informational (default) - // 3: Errors + informational + debug - LogLevel: 2, - Logger: log.New(os.Stderr, "", log.LstdFlags), - } + return Client{ + APIEnvType: Sandbox, + + // LogLevel is the logging level used by the Midtrans library + // 0: No logging + // 1: Errors only + // 2: Errors + informational (default) + // 3: Errors + informational + debug + LogLevel: 2, + Logger: log.New(os.Stderr, "", log.LstdFlags), + } } // ===================== HTTP CLIENT ================================================ -var defHttpTimeout = 80 * time.Second -var httpClient = &http.Client{Timeout: defHttpTimeout} +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) { - logLevel := c.LogLevel - logger := c.Logger - - req, err := http.NewRequest(method, fullPath, body) - if err != nil { - if logLevel > 0 { - logger.Println("Request creation failed: ", err) - } - return nil, err - } - - req.Header.Add("Content-Type", "application/json") - req.Header.Add("Accept", "application/json") - req.SetBasicAuth(c.ServerKey, "") - - return req, nil + logLevel := c.LogLevel + logger := c.Logger + + req, err := http.NewRequest(method, fullPath, body) + if err != nil { + if logLevel > 0 { + logger.Println("Request creation failed: ", err) + } + return nil, err + } + + req.Header.Add("Content-Type", "application/json") + req.Header.Add("Accept", "application/json") + 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 - - if logLevel > 1 { - logger.Println("Request ", req.Method, ": ", req.URL.Host, req.URL.Path) - } - - start := time.Now() - - res, err := httpClient.Do(req) - defer res.Body.Close() - - if logLevel > 2 { - logger.Println("Completed in ", time.Since(start)) - } - - if err != nil { - if logLevel > 0 { - logger.Println("Request failed: ", err) - } - return err - } - - resBody, err := ioutil.ReadAll(res.Body) - if err != nil { - if logLevel > 0 { - logger.Println("Cannot read response body: ", err) - } - return err - } - - if logLevel > 2 { - logger.Println("Midtrans response: ", resBody) - } - - if v != nil { - return json.Unmarshal(resBody, v) - } - - return nil + logLevel := c.LogLevel + logger := c.Logger + + if logLevel > 1 { + logger.Println("Request ", req.Method, ": ", req.URL.Host, req.URL.Path) + } + + 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() + + if logLevel > 2 { + logger.Println("Completed in ", time.Since(start)) + } + + if err != nil { + if logLevel > 0 { + logger.Println("Request failed: ", err) + } + return err + } + + resBody, err := ioutil.ReadAll(res.Body) + if err != nil { + if logLevel > 0 { + logger.Println("Cannot read response body: ", err) + } + return err + } + + if logLevel > 2 { + logger.Println("Midtrans response: ", resBody) + } + + if v != nil { + return json.Unmarshal(resBody, v) + } + + return nil } // Call the Midtrans API at specific `path` using the specified HTTP `method`. The result will be -// given to `v` if there is no error. If any error occured, the return of this function is the error +// given to `v` if there is no error. If any error occurred, the return of this function is the error // itself, otherwise nil. func (c *Client) Call(method, path string, body io.Reader, v interface{}) error { - req, err := c.NewRequest(method, path, body) + req, err := c.NewRequest(method, path, body) - if err != nil { - return err - } + if err != nil { + return err + } - if err := c.ExecuteRequest(req, v); err != nil { - return err - } + if err := c.ExecuteRequest(req, v); err != nil { + return err + } - return nil + return nil } -// ===================== END HTTP CLIENT ================================================ \ No newline at end of file + +// ===================== END HTTP CLIENT ================================================ diff --git a/core.go b/core.go index 5a282cd..8839795 100644 --- a/core.go +++ b/core.go @@ -1,114 +1,133 @@ package midtrans import ( - "encoding/json" - "bytes" - "io" - "strings" + "bytes" + "encoding/json" + "io" + "strings" ) +// CoreGateway struct type CoreGateway struct { - Client Client + Client Client } -func (c *CoreGateway) Call(method, path string, body io.Reader, v interface{}) error { - if !strings.HasPrefix(path, "/") { - path = "/" + path - } +// Call : base method to call Core API +func (gateway *CoreGateway) Call(method, path string, body io.Reader, v interface{}) error { + if !strings.HasPrefix(path, "/") { + path = "/" + path + } - path = c.Client.ApiEnvType.String() + path - return c.Client.Call(method, path, body, v) + path = gateway.Client.APIEnvType.String() + path + return gateway.Client.Call(method, path, body, v) } -func (g *CoreGateway) Charge(req *ChargeReq) (Response, error) { - resp := Response{} - jsonReq, _ := json.Marshal(req) +// Charge : Perform transaction using ChargeReq +func (gateway *CoreGateway) Charge(req *ChargeReq) (Response, error) { + resp := Response{} + jsonReq, _ := json.Marshal(req) - err := g.Call("POST", "v2/charge", bytes.NewBuffer(jsonReq), &resp) - if err != nil { - g.Client.Logger.Println("Error charging: ", err) - return resp, err - } + err := gateway.Call("POST", "v2/charge", bytes.NewBuffer(jsonReq), &resp) + if err != nil { + gateway.Client.Logger.Println("Error charging: ", err) + return resp, err + } - if resp.StatusMessage != "" { - g.Client.Logger.Println(resp.StatusMessage) - } + if resp.StatusMessage != "" { + gateway.Client.Logger.Println(resp.StatusMessage) + } - return resp, nil + return resp, nil } -func (g *CoreGateway) PreauthCard(req *ChargeReq) (Response, error) { - req.CreditCard.Type = "authorize" - return g.Charge(req) +// PreauthCard : Perform authorized transactions using ChargeReq +func (gateway *CoreGateway) PreauthCard(req *ChargeReq) (Response, error) { + req.CreditCard.Type = "authorize" + return gateway.Charge(req) } -func (g *CoreGateway) CaptureCard(req *CaptureReq) (Response, error) { - resp := Response{} - jsonReq, _ := json.Marshal(req) +// CaptureCard : Capture an authorized transaction for card payment +func (gateway *CoreGateway) CaptureCard(req *CaptureReq) (Response, error) { + resp := Response{} + jsonReq, _ := json.Marshal(req) - err := g.Call("POST", "v2/capture", bytes.NewBuffer(jsonReq), &resp) - if err != nil { - g.Client.Logger.Println("Error capturing: ", err) - return resp, err - } + 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 != "" { g.Client.Logger.Println(resp.StatusMessage) } + if resp.StatusMessage != "" { + gateway.Client.Logger.Println(resp.StatusMessage) + } - return resp, nil + return resp, nil } -func (g *CoreGateway) Approve(orderId string) (Response, error) { - resp := Response{} +// Approve : Approve order using order ID +func (gateway *CoreGateway) Approve(orderID string) (Response, error) { + resp := Response{} - err := g.Call("POST", "v2/" + orderId + "/approve", nil, &resp) - if err != nil { - g.Client.Logger.Println("Error approving: ", err) - return resp, err - } + 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 != "" { g.Client.Logger.Println(resp.StatusMessage) } + if resp.StatusMessage != "" { + gateway.Client.Logger.Println(resp.StatusMessage) + } - return resp, nil + return resp, nil } -func (g *CoreGateway) Cancel(orderId string) (Response, error) { - resp := Response{} +// Cancel : Cancel order using order ID +func (gateway *CoreGateway) Cancel(orderID string) (Response, error) { + resp := Response{} - err := g.Call("POST", "v2/" + orderId + "/cancel", nil, &resp) - if err != nil { - g.Client.Logger.Println("Error approving: ", err) - return resp, err - } + 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 != "" { g.Client.Logger.Println(resp.StatusMessage) } + if resp.StatusMessage != "" { + gateway.Client.Logger.Println(resp.StatusMessage) + } - return resp, nil + return resp, nil } -func (g *CoreGateway) Expire(orderId string) (Response, error) { - resp := Response{} +// Expire : change order status to expired using order ID +func (gateway *CoreGateway) Expire(orderID string) (Response, error) { + resp := Response{} - err := g.Call("POST", "v2/" + orderId + "/expire", nil, &resp) - if err != nil { - g.Client.Logger.Println("Error approving: ", err) - return resp, err - } + 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 != "" { g.Client.Logger.Println(resp.StatusMessage) } + if resp.StatusMessage != "" { + gateway.Client.Logger.Println(resp.StatusMessage) + } - return resp, nil + return resp, nil } -func (g *CoreGateway) Status(orderId string) (Response, error) { - resp := Response{} +// Status : get order status using order ID +func (gateway *CoreGateway) Status(orderID string) (Response, error) { + resp := Response{} - err := g.Call("GET", "v2/" + orderId + "/status", nil, &resp) - if err != nil { - g.Client.Logger.Println("Error approving: ", err) - return resp, err - } + err := gateway.Call("GET", "v2/"+orderID+"/status", nil, &resp) + if err != nil { + gateway.Client.Logger.Println("Error approving: ", err) + return resp, err + } - if resp.StatusMessage != "" { g.Client.Logger.Println(resp.StatusMessage) } + if resp.StatusMessage != "" { + gateway.Client.Logger.Println(resp.StatusMessage) + } - return resp, nil -} \ No newline at end of file + return resp, nil +} diff --git a/envtype.go b/envtype.go index 739e036..e382ff0 100644 --- a/envtype.go +++ b/envtype.go @@ -2,11 +2,16 @@ package midtrans import "strings" +// EnvironmentType value type EnvironmentType int8 const ( _ EnvironmentType = iota + + // Sandbox : represent sandbox environment Sandbox + + // Production : represent production environment Production ) @@ -25,6 +30,7 @@ func (e EnvironmentType) String() string { return "undefined" } -func (e EnvironmentType) SnapUrl() string { +// SnapURL : Get environment API URL +func (e EnvironmentType) SnapURL() string { return strings.Replace(e.String(), "api.", "app.", 1) } diff --git a/paysource.go b/paysource.go index eaaff0d..9248e28 100644 --- a/paysource.go +++ b/paysource.go @@ -1,47 +1,84 @@ package midtrans +// PaymentType value type PaymentType string const ( - SourceBankTransfer PaymentType = "bank_transfer" - SourcePermataVA PaymentType = "permata_va" - SourceBCAVA PaymentType = "bca_va" - SourceBbmMoney PaymentType = "bbm_money" - SourceBcaKlikpay PaymentType = "bca_klikpay" - SourceBriEpay PaymentType = "bri_epay" - - SourceCreditCard PaymentType = "credit_card" - SourceCimbClicks PaymentType = "cimb_clicks" - SourceConvStore PaymentType = "cstore" - - SourceKlikBca PaymentType = "bca_klikbca" - SourceEchannel PaymentType = "echannel" - SourceMandiriClickpay PaymentType = "mandiri_clickpay" - SourceTelkomselCash PaymentType = "telkomsel_cash" - SourceXlTunai PaymentType = "xl_tunai" - SourceIndosatDompetku PaymentType = "indosat_dompetku" - SourceMandiriEcash PaymentType = "mandiri_ecash" - SourceKioson PaymentType = "kioson" - SourceIndomaret PaymentType = "indomaret" - SourceGiftCardIndo PaymentType = "gci" + // SourceBankTransfer : bank_transfer + SourceBankTransfer PaymentType = "bank_transfer" + + // SourcePermataVA : permata_va + SourcePermataVA PaymentType = "permata_va" + + // SourceBCAVA : bca_va + SourceBCAVA PaymentType = "bca_va" + + // SourceBbmMoney : bbm_money + SourceBbmMoney PaymentType = "bbm_money" + + // SourceBcaKlikpay : bca_klikpay + SourceBcaKlikpay PaymentType = "bca_klikpay" + + // SourceBriEpay : bri_epay + SourceBriEpay PaymentType = "bri_epay" + + // SourceCreditCard : credit_card + SourceCreditCard PaymentType = "credit_card" + + // SourceCimbClicks : cimb_clicks + SourceCimbClicks PaymentType = "cimb_clicks" + + // SourceConvStore : cstore + SourceConvStore PaymentType = "cstore" + + // SourceKlikBca : bca_klikbca + SourceKlikBca PaymentType = "bca_klikbca" + + // SourceEchannel : echannel + SourceEchannel PaymentType = "echannel" + + // SourceMandiriClickpay : mandiri_clickpay + SourceMandiriClickpay PaymentType = "mandiri_clickpay" + + // SourceTelkomselCash : telkomsel_cash + SourceTelkomselCash PaymentType = "telkomsel_cash" + + // SourceXlTunai : xl_tunai + SourceXlTunai PaymentType = "xl_tunai" + + // SourceIndosatDompetku : indosat_dompetku + SourceIndosatDompetku PaymentType = "indosat_dompetku" + + // SourceMandiriEcash : mandiri_ecash + SourceMandiriEcash PaymentType = "mandiri_ecash" + + // SourceKioson : kioson + SourceKioson PaymentType = "kioson" + + // SourceIndomaret : indomaret + SourceIndomaret PaymentType = "indomaret" + + // SourceGiftCardIndo : gci + SourceGiftCardIndo PaymentType = "gci" ) +// AllPaymentSource : Get All available PaymentType var AllPaymentSource = []PaymentType{ - SourceCreditCard, - SourceMandiriClickpay, - SourceCimbClicks, - SourceKlikBca, - SourceBcaKlikpay, - SourceBriEpay, - SourceTelkomselCash, - SourceEchannel, - SourceBbmMoney, - SourceXlTunai, - SourceIndosatDompetku, - SourceMandiriEcash, - SourcePermataVA, - SourceBCAVA, - SourceIndomaret, - SourceKioson, - SourceGiftCardIndo, -} \ No newline at end of file + SourceCreditCard, + SourceMandiriClickpay, + SourceCimbClicks, + SourceKlikBca, + SourceBcaKlikpay, + SourceBriEpay, + SourceTelkomselCash, + SourceEchannel, + SourceBbmMoney, + SourceXlTunai, + SourceIndosatDompetku, + SourceMandiriEcash, + SourcePermataVA, + SourceBCAVA, + SourceIndomaret, + SourceKioson, + SourceGiftCardIndo, +} diff --git a/request.go b/request.go index 7178201..d64f02d 100644 --- a/request.go +++ b/request.go @@ -1,13 +1,14 @@ package midtrans -// Represent the transaction details +// ItemDetail : Represent the transaction details type ItemDetail struct { - Id string `json:"id"` + ID string `json:"id"` Name string `json:"name"` Price int64 `json:"price"` Qty int32 `json:"quantity"` } +// CustAddress : Represent the customer address type CustAddress struct { FName string `json:"first_name"` LName string `json:"last_name"` @@ -18,7 +19,7 @@ type CustAddress struct { CountryCode string `json:"country_code"` } -// Represent the customer detail +// CustDetail : Represent the customer detail type CustDetail struct { // first name FName string `json:"first_name"` @@ -32,11 +33,13 @@ type CustDetail struct { ShipAddr *CustAddress `json:"customer_address,omitempty"` } +// TransactionDetails : Represent transaction details type TransactionDetails struct { OrderID string `json:"order_id"` GrossAmt int64 `json:"gross_amount"` } +// CreditCardDetail : Represent credit card detail type CreditCardDetail struct { Secure bool `json:"secure,omitempty"` TokenID string `json:"token_id"` @@ -46,13 +49,15 @@ type CreditCardDetail struct { Type string `json:"type,omitempty"` // indicate if generated token should be saved for next charge SaveTokenID bool `json:"save_token_id,omitempty"` - SavedTokenIdExpireAt string `json:"saved_token_id_expired_at,omitempty"` + SavedTokenIDExpireAt string `json:"saved_token_id_expired_at,omitempty"` } +// PermataBankTransferDetail : Represent Permata bank_transfer detail type PermataBankTransferDetail struct { Bank Bank `json:"bank"` } +// BCABankTransferLangDetail : Represent BCA bank_transfer lang detail type BCABankTransferLangDetail struct { LangID string `json:"id,omitempty"` LangEN string `json:"en,omitempty"` @@ -72,22 +77,26 @@ type BCABankTransferLangDetail struct { } */ +// BCABankTransferDetailFreeText : Represent BCA bank_transfer detail free_text type BCABankTransferDetailFreeText struct { Inquiry []BCABankTransferLangDetail `json:"inquiry,omitempty"` Payment []BCABankTransferLangDetail `json:"payment,omitempty"` } +// BCABankTransferDetail : Represent BCA bank_transfer detail type BCABankTransferDetail struct { Bank Bank `json:"bank"` VaNumber string `json:"va_number"` FreeText BCABankTransferDetailFreeText `json:"free_text"` } +// MandiriBillBankTransferDetail : Represent Mandiri Bill bank_transfer detail type MandiriBillBankTransferDetail struct { BillInfo1 string `json:"bill_info1,omitempty"` BillInfo2 string `json:"bill_info2,omitempty"` } +// BankTransferDetail : Represent bank_transfer detail type BankTransferDetail struct { Bank Bank `json:"bank,omitempty"` VaNumber string `json:"va_number,omitempty"` @@ -95,7 +104,7 @@ type BankTransferDetail struct { *MandiriBillBankTransferDetail } -// Internet Banking for BCA KlikPay +// BCAKlikPayDetail : Represent Internet Banking for BCA KlikPay type BCAKlikPayDetail struct { // 1 = normal, 2 = installment, 3 = normal + installment Type string `json:"type"` @@ -103,11 +112,13 @@ type BCAKlikPayDetail struct { MiscFee int64 `json:"misc_fee,omitempty"` } +// BCAKlikBCADetail : Represent BCA KlikBCA detail type BCAKlikBCADetail struct { Desc string `json:"description"` UserID string `json:"user_id"` } +// MandiriClickPayDetail : Represent Mandiri ClickPay detail type MandiriClickPayDetail struct { CardNumber string `json:"card_number"` Input1 string `json:"input1"` @@ -116,30 +127,35 @@ type MandiriClickPayDetail struct { Token string `json:"token"` } +// CIMBClicksDetail : Represent CIMB Clicks detail type CIMBClicksDetail struct { Desc string `json:"description"` } +// TelkomselCashDetail : Represent Telkomsel Cash detail type TelkomselCashDetail struct { Promo bool `json:"promo"` IsReversal int8 `json:"is_reversal"` Customer string `json:"customer"` } +// IndosatDompetkuDetail : Represent Indosat Dompetku detail type IndosatDompetkuDetail struct { MSISDN string `json:"msisdn"` } +// MandiriEcashDetail : Represent Mandiri e-Cash detail type MandiriEcashDetail struct { Desc string `json:"description"` } +// ConvStoreDetail : Represent cstore detail type ConvStoreDetail struct { Store string `json:"store"` Message string `json:"message"` } -// Represent the request payload +// ChargeReq : Represent Charge request payload type ChargeReq struct { PaymentType PaymentType `json:"payment_type"` TransactionDetails TransactionDetails `json:"transaction_details"` @@ -163,6 +179,7 @@ type ChargeReq struct { CustField3 string `json:"custom_field3,omitempty"` } +// SnapReq : Represent SNAP API request payload type SnapReq struct { TransactionDetails TransactionDetails `json:"transaction_details"` EnabledPayments []PaymentType `json:"enabled_payments"` @@ -174,6 +191,7 @@ type SnapReq struct { CustomField3 string `json:"custom_field3"` } +// CaptureReq : Represent Capture request payload type CaptureReq struct { TransactionID string `json:"transaction_id"` GrossAmt float64 `json:"gross_amount"` diff --git a/response.go b/response.go index eba7c36..106ed1b 100644 --- a/response.go +++ b/response.go @@ -1,5 +1,6 @@ package midtrans +// VANumber : bank virtual account number type VANumber struct { Bank string `json:"bank"` VANumber string `json:"va_number"` @@ -29,7 +30,7 @@ type Response struct { FraudStatus string `json:"fraud_status"` PaymentType string `json:"payment_type"` OrderID string `json:"order_id"` - TransactionId string `json:"transaction_id"` + TransactionID string `json:"transaction_id"` TransactionTime string `json:"transaction_time"` TransactionStatus string `json:"transaction_status"` GrossAmount string `json:"gross_amount"` @@ -37,7 +38,7 @@ type Response struct { PaymentCode string `json:"payment_code"` } -// Response after calling the Snap API +// SnapResponse : Response after calling the Snap API type SnapResponse struct { StatusCode string `json:"status_code"` Token string `json:"token"` diff --git a/snap.go b/snap.go index 2e9be2e..0b3576a 100644 --- a/snap.go +++ b/snap.go @@ -1,45 +1,48 @@ package midtrans import ( - "encoding/json" - "bytes" - "io" - "strings" + "bytes" + "encoding/json" + "io" + "strings" ) +// SnapGateway struct type SnapGateway struct { - Client Client + Client Client } -func (gway *SnapGateway) Call(method, path string, body io.Reader, v interface{}) error { - if !strings.HasPrefix(path, "/") { - path = "/" + path - } +// Call : base method to call Snap API +func (gateway *SnapGateway) Call(method, path string, body io.Reader, v interface{}) error { + if !strings.HasPrefix(path, "/") { + path = "/" + path + } - path = gway.Client.ApiEnvType.SnapUrl() + path - return gway.Client.Call(method, path, body, v) + path = gateway.Client.APIEnvType.SnapURL() + path + return gateway.Client.Call(method, path, body, v) } -// Quickly get token without constructing the body manually -func (g *SnapGateway) GetTokenQuick(orderId string, gross_amount int64) (SnapResponse, error) { - return g.GetToken(&SnapReq{ - TransactionDetails: TransactionDetails{ - OrderID: orderId, - GrossAmt: gross_amount, - }, - EnabledPayments: AllPaymentSource, - }) +// GetTokenQuick : Quickly get token without constructing the body manually +func (gateway *SnapGateway) GetTokenQuick(orderID string, grossAmount int64) (SnapResponse, error) { + return gateway.GetToken(&SnapReq{ + TransactionDetails: TransactionDetails{ + OrderID: orderID, + GrossAmt: grossAmount, + }, + EnabledPayments: AllPaymentSource, + }) } -func (gway *SnapGateway) GetToken(r *SnapReq) (SnapResponse, error) { - resp := SnapResponse{} - jsonReq, _ := json.Marshal(r) +// GetToken : Get token by consuming SnapReq +func (gateway *SnapGateway) GetToken(r *SnapReq) (SnapResponse, error) { + resp := SnapResponse{} + jsonReq, _ := json.Marshal(r) - err := gway.Call("POST", "snap/v1/transactions", bytes.NewBuffer(jsonReq), &resp) - if err != nil { - gway.Client.Logger.Println("Error getting snap token: ", err) - return resp, err - } + err := gateway.Call("POST", "snap/v1/transactions", bytes.NewBuffer(jsonReq), &resp) + if err != nil { + gateway.Client.Logger.Println("Error getting snap token: ", err) + return resp, err + } - return resp, nil + return resp, nil }