midtrans

package module
v1.3.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 28, 2024 License: MIT Imports: 11 Imported by: 50

README

Midtrans Go Library

Go Report Card

Midtrans ❤ Go !

Go is a very modern, terse, and combine aspect of dynamic and static typing that in a way very well suited for web development, among other things. Its small memory footprint is also an advantage of itself. This module will help you use Midtrans product's REST APIs in Go.

1. Installation

1.1 Using Go Module

Run this command on your project to initialize Go mod (if you haven't):

go mod init

then reference midtrans-go in your project file with import:

import (
    "github.com/midtrans/midtrans-go"
    "github.com/midtrans/midtrans-go/coreapi"
    "github.com/midtrans/midtrans-go/snap"
    "github.com/midtrans/midtrans-go/iris"
)
1.2 Using go get

Also, the alternative way you can use go get the package into your project

go get -u github.com/midtrans/midtrans-go

2. Usage

There is a type named Client (coreapi.Client, snap.Client, iris.Client) that should be instantiated through function New which holds any possible setting to the library. Any activity (charge, approve, etc) is done in the client level.

2.1 Choose Product/Method

We have 3 different products that you can use:

  • Snap - Customizable payment popup will appear on your web/app (no redirection). doc ref
  • Snap Redirect - Customer need to be redirected to payment url hosted by midtrans. doc ref
  • Core API (VT-Direct) - Basic backend implementation, you can customize the frontend embedded on your web/app as you like (no redirection). doc ref
  • Iris Disbursement - Iris is Midtrans’ cash management solution that allows you to disburse payments to any bank accounts in Indonesia securely and easily. doc ref

To learn more and understand each of the product's quick overview you can visit https://docs.midtrans.com.

2.2 Client Initialization and Configuration

Get your client key and server key from Midtrans Dashboard

Create API client object, You can also check the project's implementation for more examples. Please proceed there for more detail on how to run the example.

2.2.1 Using global config

Set a config with globally, (except for iris api)

midtrans.ServerKey = "YOUR-SERVER-KEY"
midtrans.Environment = midtrans.Sandbox
2.2.2 Using Client
//Initiate client for Midtrans CoreAPI
var c = coreapi.Client
c.New("YOUR-SERVER-KEY", midtrans.Sandbox)

//Initiate client for Midtrans Snap
var s = snap.Client
s.New("YOUR-SERVER-KEY", midtrans.Sandbox)

//Initiate client for Iris disbursement
var i = iris.Client
i.New("IRIS-API-KEY", midtrans.Sandbox)
2.3 Snap

Snap is Midtrans existing tool to help merchant charge customers using a mobile-friendly, in-page, no-redirect checkout facilities. Using snap is simple.

Available methods for Snap

// CreateTransaction : Do `/transactions` API request to SNAP API to get Snap token and redirect url with `snap.Request`
func CreateTransaction(req *snap.Request) (*Response, *midtrans.Error)

// CreateTransactionToken : Do `/transactions` API request to SNAP API to get Snap token with `snap.Request`
func CreateTransactionToken(req *snap.Request) (string, *midtrans.Error)

// CreateTransactionUrl : Do `/transactions` API request to SNAP API to get Snap redirect url with `snap.Request`
func CreateTransactionUrl(req *snap.Request) (string, *midtrans.Error)

// CreateTransactionWithMap : Do `/transactions` API request to SNAP API to get Snap token and redirect url with Map request
func CreateTransactionWithMap(req *snap.RequestParamWithMap) (ResponseWithMap, *midtrans.Error)

// CreateTransactionTokenWithMap : Do `/transactions` API request to SNAP API to get Snap token with Map request
func CreateTransactionTokenWithMap(req *snap.RequestParamWithMap) (string, *midtrans.Error)

// CreateTransactionUrlWithMap : Do `/transactions` API request to SNAP API to get Snap redirect url with Map request
func CreateTransactionUrlWithMap(req *snap.RequestParamWithMap) (string, *midtrans.Error) 

Snap usage example, create transaction with minimum Snap parameters (choose one of alternatives below):

2.3.1 Using global Config & static function

Sample usage if you prefer Midtrans global configuration & using static function. Useful if you only use 1 merchant account API key, and keep the code short.

// 1. Set you ServerKey with globally
midtrans.ServerKey = "YOUR-SERVER-KEY"
midtrans.Environment = midtrans.Sandbox

// 2. Initiate Snap request
req := & snap.RequestParam{
	TransactionDetails: midtrans.TransactionDetails{
		OrderID:  "YOUR-ORDER-ID-12345", 
		GrossAmt: 100000,
	}, 
	CreditCard: &snap.CreditCardDetails{
		Secure: true,
	},
}

// 3. Request create Snap transaction to Midtrans
snapResp, _ := CreateTransaction(req)
fmt.Println("Response :", snapResp)
2.3.2 Using Client

Sample usage if you prefer to use client instance & config. Useful if you plan to use multiple merchant account API keys, want to have multiple client instances, or prefer the code to be object-oriented.

// 1. Initiate Snap client
var s = snap.Client
s.New("YOUR-SERVER-KEY", midtrans.Sandbox)

// 2. Initiate Snap request
req := & snap.RequestParam{
		TransactionDetails: midtrans.TransactionDetails{
			OrderID:  "YOUR-ORDER-ID-12345",
			GrossAmt: 100000,
		}, 
		CreditCard: &snap.CreditCardDetails{
			Secure: true,
		},
	}

// 3. Request create Snap transaction to Midtrans
snapResp, _ := s.CreateTransaction(req)
fmt.Println("Response :", snapResp)

On the frontend side (on the HTML payment page), you will need to include snap.js library and implement the payment page.

Sample HTML payment page implementation:

<html>
  <body>
    <button id="pay-button">Pay!</button>
    <pre><div id="result-json">JSON result will appear here after payment:<br></div></pre> 

<!-- TODO: Remove ".sandbox" from script src URL for production environment. Also input your client key in "data-client-key" -->
    <script src="https://app.sandbox.midtrans.com/snap/snap.js" data-client-key="<Set your ClientKey here>"></script>
    <script type="text/javascript">
      document.getElementById('pay-button').onclick = function(){
        // SnapToken acquired from previous step
        snap.pay('PUT_TRANSACTION_TOKEN_HERE', {
          // Optional
          onSuccess: function(result){
            /* You may add your own js here, this is just example */ document.getElementById('result-json').innerHTML += JSON.stringify(result, null, 2);
          },
          // Optional
          onPending: function(result){
            /* You may add your own js here, this is just example */ document.getElementById('result-json').innerHTML += JSON.stringify(result, null, 2);
          },
          // Optional
          onError: function(result){
            /* You may add your own js here, this is just example */ document.getElementById('result-json').innerHTML += JSON.stringify(result, null, 2);
          }
        });
      };
    </script>
  </body>
</html>

You may want to override those onSuccess, onPending and onError functions to implement the behaviour that you want on each respective event.

Then implement Backend Notification Handler, Refer to this section

Alternativelly, more complete Snap parameter:

func GenerateSnapReq() *snap.Request {
	// Initiate Customer address
	custAddress := &midtrans.CustomerAddress{
		FName: "John",
		LName: "Doe",
		Phone: "081234567890",
		Address: "Baker Street 97th",
		City: "Jakarta",
		Postcode: "16000",
		CountryCode: "IDN",
	}
	
	// Initiate Snap Request
	snapReq := &snap.Request{
		TransactionDetails: midtrans.TransactionDetails{
			OrderID: "YOUR-UNIQUE-ORDER-ID-1234",
			GrossAmt: 200000,
		}, 
		CreditCard: &snap.CreditCardDetails{
			Secure: true,
		},
		CustomerDetail: &midtrans.CustomerDetails{
			FName: "John",
			LName: "Doe",
			Email: "john@doe.com",
			Phone: "081234567890",
			BillAddr: custAddress,
			ShipAddr: custAddress,
		},
		Items: &[]midtrans.ItemDetails{
			midtrans.ItemDetails{
				ID: "ITEM1",
				Price: 200000,
				Qty: 1,
				Name: "Someitem",
			},
		},
	}

 return snapReq
}

INFO: When using client, you can set config options like SetIdempotencyKey, SetContext, SetPaymentOverrideNotif, etc from Options object on the client, check the usage detail on how to configure options here

Alternative, perform Core API Charge with Map type

Snap client have ...WithMap function, which is useful if you want to send custom JSON payload that the type/struct is not defined in this module. Refer to file sample.go in folder Snap API simple sample.

2.4 CoreApi

Available methods for CoreApi

// ChargeTransaction : Do `/charge` API request to Midtrans Core API return `coreapi.Response` with `coreapi.ChargeReq`
func ChargeTransaction(req *ChargeReq) (*Response, *midtrans.Error)

// ChargeTransactionWithMap : Do `/charge` API request to Midtrans Core API return RAW MAP with Map as
func ChargeTransactionWithMap(req *ChargeReqWithMap) (ResponseWithMap, *midtrans.Error)

// CardToken : Do `/token` API request to Midtrans Core API return `coreapi.Response`,
func CardToken(cardNumber string, expMonth int, expYear int, cvv string) (*CardTokenResponse, *midtrans.Error)

// RegisterCard : Do `/card/register` API request to Midtrans Core API return `coreapi.Response`,
func RegisterCard(cardNumber string, expMonth int, expYear int, cvv string) (*CardRegisterResponse, *midtrans.Error) 

// CardPointInquiry : Do `/point_inquiry/{tokenId}` API request to Midtrans Core API return `coreapi.Response`,
func CardPointInquiry(cardToken string) (*CardTokenResponse, *midtrans.Error)

// GetBIN : Do `/v1/bins/{bin}` API request to Midtrans Core API return `coreapi.BinResponse`,
func GetBIN(binNumber string) (*BinResponse, *midtrans.Error)

// CheckTransaction : Do `/{orderId}/status` API request to Midtrans Core API return `coreapi.Response`,
func CheckTransaction(param string) (*Response, *midtrans.Error)

// ApproveTransaction : Do `/{orderId}/approve` API request to Midtrans Core API return `coreapi.Response`,
func ApproveTransaction(param string) (*Response, *midtrans.Error)

// DenyTransaction : Do `/{orderId}/deny` API request to Midtrans Core API return `coreapi.Response`,
func DenyTransaction(param string) (*Response, *midtrans.Error)

// CancelTransaction : Do `/{orderId}/cancel` API request to Midtrans Core API return `coreapi.Response`,
func CancelTransaction(param string) (*Response, *midtrans.Error)

// ExpireTransaction : Do `/{orderId}/expire` API request to Midtrans Core API return `coreapi.Response`,
func ExpireTransaction(param string) (*Response, *midtrans.Error)

// RefundTransaction : Do `/{orderId}/refund` API request to Midtrans Core API return `coreapi.Response`,
// with `coreapi.RefundReq` as body parameter, will be converted to JSON,
func RefundTransaction(param string, req *RefundReq) (*Response, *midtrans.Error)

// DirectRefundTransaction : Do `/{orderId}/refund/online/direct` API request to Midtrans Core API return `coreapi.Response`,
// with `coreapi.RefundReq` as body parameter, will be converted to JSON,
func DirectRefundTransaction(param string, req *RefundReq) (*Response, *midtrans.Error)

// CaptureTransaction : Do `/{orderId}/capture` API request to Midtrans Core API return `coreapi.Response`,
// with `coreapi.CaptureReq` as body parameter, will be converted to JSON,
func CaptureTransaction(req *CaptureReq) (*Response, *midtrans.Error)

// GetStatusB2B : Do `/{orderId}/status/b2b` API request to Midtrans Core API return `coreapi.Response`,
func GetStatusB2B(param string) (*Response, *midtrans.Error)
2.4.1 Using global Config & static function

Sample usage if you prefer Midtrans global configuration & using static function. Useful if you only use 1 merchant account API key, and keep the code short.

// 1. Set you ServerKey with globally
midtrans.ServerKey = "YOUR-SERVER-KEY"
midtrans.Environment = midtrans.Sandbox

// 2. Initiate charge request
chargeReq := &coreapi.ChargeReq{
	PaymentType: coreapi.PaymentTypeCreditCard,
	TransactionDetails: midtrans.TransactionDetails{
		OrderID:  "12345",
		GrossAmt: 200000,
	},
	CreditCard: &coreapi.CreditCardDetails{
		TokenID:        "YOUR-CC-TOKEN",
		Authentication: true,
	},
	Items: &[]midtrans.ItemDetails{
		{
			ID:    "ITEM1",
			Price: 200000,
			Qty:   1,
			Name:  "Someitem",
		},
	},
}
	
// 3. Request to Midtrans using global config
coreApiRes, _ := coreapi.ChargeTransaction(chargeReq)
fmt.Println("Response :", coreApiRes)
2.4.2 Using Client

Sample usage if you prefer to use client instance & config. Useful if you plan to use multiple merchant account API keys, want to have multiple client instances, or prefer the code to be object-oriented.

// 1. Initiate coreapi client  
c := coreapi.Client{}
c.New("YOUR-SERVER-KEY", midtrans.Sandbox)

// 2. Initiate charge request
chargeReq := &coreapi.ChargeReq{
	PaymentType: midtrans.SourceCreditCard,
	TransactionDetails: midtrans.TransactionDetails{
		OrderID:  "12345",
		GrossAmt: 200000,
	},
	CreditCard: &coreapi.CreditCardDetails{
		TokenID:        "YOUR-CC-TOKEN",
		Authentication: true,
	},
	Items: &[]midtrans.ItemDetail{
		coreapi.ItemDetail{
			ID:    "ITEM1",
			Price: 200000,
			Qty:   1,
			Name:  "Someitem",
		},
	},
}

// 3. Request to Midtrans
coreApiRes, _ := c.ChargeTransaction(chargeReq)
fmt.Println("Response :", coreApiRes)

INFO: When using client, you can set config options like SetIdempotencyKey, SetContext, SetPaymentOverrideNotif, etc from Options object on the client, check the usage detail on how to configure options here

Alternative, perform Core API Charge with Map type

CoreApi client have ChargeTransactionWithMap function, which is useful if you want to send custom JSON payload that the type/struct is not defined in this module. Refer to file sample.go in folder Core API simple sample.

2.5 Iris Client

Iris is Midtrans cash management solution that allows you to disburse payments to any supported bank accounts securely and easily. Iris connects to the banks’ hosts to enable seamless transfer using integrated APIs. Available methods for Iris

// CreateBeneficiaries : to perform create a new beneficiary information for quick access on the payout page in Iris Portal.
func (c Client) CreateBeneficiaries(req Beneficiaries) (*BeneficiariesResponse, *midtrans.Error)

// UpdateBeneficiaries : to update an existing beneficiary identified by its alias_name.
func (c Client) UpdateBeneficiaries(aliasName string, req Beneficiaries) (*BeneficiariesResponse, *midtrans.Error)

// GetBeneficiaries : This method to fetch list of all beneficiaries saved in Iris Portal.
func (c Client) GetBeneficiaries() ([]Beneficiaries, *midtrans.Error)

// CreatePayout : This method for Creator to create a payout. It can be used for single payout and also multiple payouts.
func (c Client) CreatePayout(req CreatePayoutReq) (*CreatePayoutResponse, *midtrans.Error)

// ApprovePayout : this method for Apporver to approve multiple payout request.
func (c Client) ApprovePayout(req ApprovePayoutReq) (*ApprovePayoutResponse, *midtrans.Error)

// RejectPayout : This method for Apporver to reject multiple payout request.
func (c Client) RejectPayout(req RejectPayoutReq) (*RejectPayoutResponse, *midtrans.Error)

// GetPayoutDetails : Get details of a single payout.
func (c Client) GetPayoutDetails(referenceNo string) (*PayoutDetailResponse, *midtrans.Error)

// GetTransactionHistory : Returns all the payout details for specific dates 
func (c Client) GetTransactionHistory(fromDate string, toDate string) ([]TransactionHistoryResponse, *midtrans.Error) 

// GetTopUpChannels : Provide top up information channel for Aggregator Partner
func (c Client) GetTopUpChannels() ([]TopUpAccountResponse, *midtrans.Error)

// GetBalance : For Aggregator Partner, you need to top up to Iris’ bank account. Every partner have their own balance in Iris’
// bank account. Use this API is to get current balance information.
func (c Client) GetBalance() (*BalanceResponse, *midtrans.Error) 

// GetListBankAccount : Show list of registered bank accounts for facilitator partner
func (c Client) GetListBankAccount() ([]BankAccountResponse, *midtrans.Error)

// GetFacilitatorBalance : For Facilitator Partner, use this API is to get current balance information of your registered bank account.
func (c Client) GetFacilitatorBalance(accountId string) (*BalanceResponse, *midtrans.Error) 

// GetBeneficiaryBanks : Show list of supported banks in IRIS.
func (c Client) GetBeneficiaryBanks() (*ListBeneficiaryBankResponse, *midtrans.Error)

// ValidateBankAccount : Check if an account is valid, if valid return account information.
func (c Client) ValidateBankAccount(bankName string, accountNo string) (*BankAccountDetailResponse, *midtrans.Error)

Note: IrisApiKey will be used in Iris.Client's the API Key can be found in Iris Dashboard. The API Key is different to Midtrans' payment gateway account's API key.

var i iris.Client
i.New("YOUR-IRIS-API-KEY", midtrans.Sandbox)

res, _ := i.GetBeneficiaryBanks()
fmt.Println("Response: ", res)
2.6 Handle HTTP Notification

Create separated web endpoint (notification url) to receive HTTP POST notification callback/webhook. HTTP notification will be sent whenever transaction status is changed. Example also available in sample.go in folder example/simple/coreapi

func notification(w http.ResponseWriter, r *http.Request) {
	// 1. Initialize empty map
	var notificationPayload map[string]interface{}

	// 2. Parse JSON request body and use it to set json to payload
	err := json.NewDecoder(r.Body).Decode(&notificationPayload)
	if err != nil {
		// do something on error when decode
		return
	}
	// 3. Get order-id from payload
	orderId, exists := notificationPayload["order_id"].(string)
	if !exists {
		// do something when key `order_id` not found
		return
	}

	// 4. Check transaction to Midtrans with param orderId
	transactionStatusResp, e := c.CheckTransaction(orderId)
	if e != nil {
		http.Error(w, e.GetMessage(), http.StatusInternalServerError)
		return
	} else {
		if transactionStatusResp != nil {
			// 5. Do set transaction status based on response from check transaction status
			if transactionStatusResp.TransactionStatus == "capture" {
				if transactionStatusResp.FraudStatus == "challenge" {
					// TODO set transaction status on your database to 'challenge'
					// e.g: 'Payment status challenged. Please take action on your Merchant Administration Portal
				} else if transactionStatusResp.FraudStatus == "accept" {
					// TODO set transaction status on your database to 'success'
				}
			} else if transactionStatusResp.TransactionStatus == "settlement" {
				// TODO set transaction status on your databaase to 'success'
			} else if transactionStatusResp.TransactionStatus == "deny" {
				// TODO you can ignore 'deny', because most of the time it allows payment retries
				// and later can become success
			} else if transactionStatusResp.TransactionStatus == "cancel" || transactionStatusResp.TransactionStatus == "expire" {
				// TODO set transaction status on your databaase to 'failure'
			} else if transactionStatusResp.TransactionStatus == "pending" {
				// TODO set transaction status on your databaase to 'pending' / waiting payment
			}
		}
	}
	w.Header().Set("Content-Type", "application/json")
	w.Write([]byte("ok"))
}
2.7 Transaction Action

Other functions related to actions that can be performed to transaction(s). Also available as examples here

Get Status
// get status of transaction that already recorded on midtrans (already `charge`-ed) 
res, _ := c.CheckTransaction("YOUR_ORDER_ID OR TRANSACTION_ID")
if res != nil {
// do something to `res` object
}
Get Status B2B
// get transaction status of VA b2b transaction
res, _ := c.GetStatusB2B("YOUR_ORDER_ID OR TRANSACTION_ID")
if res != nil {
// do something to `res` object
}
Approve Transaction
// approve a credit card transaction with `challenge` fraud status
res, _ := c.ApproveTransaction("YOUR_ORDER_ID OR TRANSACTION_ID")
if res != nil {
// do something to `res` object
}
Deny Transaction
// deny a credit card transaction with `challenge` fraud status
res, _ := c.DenyTransaction("YOUR_ORDER_ID OR TRANSACTION_ID")
if res != nil {
// do something to `res` object
}
Cancel Transaction
// cancel a credit card transaction or pending transaction
res, _ := c.CancelTransaction("YOUR_ORDER_ID OR TRANSACTION_ID")
if res != nil {
// do something to `res` object
}
Capture Transaction
// Capture an authorized transaction for card payment
refundRequest := &coreapi.CaptureReq{
	TransactionID: "TRANSACTION-ID", 
	GrossAmt:      10000,
}
res, _ := c.CaptureTransaction(refundRequest)
if res != nil {
// do something to `res` object
}
Expire Transaction
// expire a pending transaction
res, _ := c.ExpireTransaction("YOUR_ORDER_ID OR TRANSACTION_ID")
if res != nil {
// do something to `res` object
}
Refund Transaction
refundRequest := &coreapi.RefundReq{
	Amount:    5000, 
	Reason:    "Item out of stock",
}

res, _ := c.RefundTransaction("YOUR_ORDER_ID OR TRANSACTION_ID", refundRequest)
if res != nil {
// do something to `res` object
}
Refund Transaction with Direct Refund
refundRequest := &coreapi.RefundReq{
		RefundKey: "order1-ref1",
		Amount:    5000,
		Reason:    "Item out of stock",
	}
	
res, _ := c.DirectRefundTransaction("YOUR_ORDER_ID OR TRANSACTION_ID", refundRequest)
if res != nil {
// do something to `res` object
}

3. Advance Usage

3.1 Override Notification Url

Merchant can opt to change or add custom notification urls on every transaction. It can be achieved by adding additional HTTP headers into charge request. For Midtrans Payment, there are two headers we provide:

  1. X-Append-Notification: to add new notification url(s) alongside the settings on dashboard
  2. X-Override-Notification: to use new notification url(s) disregarding the settings on dashboard Both header can only receive up to maximum of 3 urls.

Note: When both SetPaymentAppendNotif and SetPaymentOverrideNotif are used together then only OverrideNotif will be used.

3.1.1 Set Override/Append notification globally
// Set override or append for globally
midtrans.SetPaymentAppendNotification("YOUR-APPEND-NOTIFICATION-ENDPOINT")
midtrans.SetPaymentOverrideNotification("YOUR-OVERRID-NOTIFICATION-ENDPOINT")
3.1.2 Set Override/Append notification via client options
// 1. Initiate Gateway
var c = coreapi.Client
c.New("YOUR-SERVER-KEY", midtrans.Sandbox)

// 2. Set Payment Override or Append via gateway options for specific request
c.Options.SetPaymentAppendNotification("YOUR-APPEND-NOTIFICATION-ENDPOINT")
c.Options.SetPaymentOverrideNotification("YOUR-APPEND-NOTIFICATION-ENDPOINT")

// 3. Then request to Midtrans API
res, _ := c.ChargeRequest("YOUR-REQUEST")

Please see our documentation for the details about the feature

3.2 Request using go Context

With Gateway options object you can set Go Context for each request by the net/http machinery, and is available with SetContext() method.

c.Options.SetContext(context.Background())
3.3 Log Configuration

By default in Sandbox the log level will use LogDebug level, that outputs informational messages for debugging. In Production this module will only logs the error messages (LogError level), that outputs error message to os.stderr. You have option to change the default log level configuration with global variable midtrans.DefaultLoggerLevel:

midtrans.DefaultLoggerLevel = &midtrans.LoggerImplementation{LogLevel: midtrans.LogDebug}

// Details Log Level
// NoLogging    : sets a logger to not show the messages
// LogError     : sets a logger to show error messages only.
// LogInfo      : sets a logger to show information messages
// LogDebug     : sets a logger to show informational messages for debugging
3.4 Override HTTP Client timeout

By default, timeout value for HTTP Client 80 seconds. But you can override the HTTP client default config from global variable midtrans.DefaultGoHttpClient:

t := 300 * time.Millisecond
midtrans.DefaultGoHttpClient = &http.Client{
	Timeout:       t,
}

4. Handling Error

When using function that result in Midtrans API call e.g: c.ChargeTransaction(...) or s.CreateTransaction(...) there's a chance it may throw error (Midtrans Error object), the error object will contains below properties that can be used as information to your error handling logic:

    _, err = c.chargeTransaction(param)
    if err != nil {
        msg := err.Error()                     // general message error
        stsCode := err.GetStatusCode()         // HTTP status code e.g: 400, 401, etc.
        rawApiRes := err.GetRawApiResponse()   // raw Go HTTP response object
        rawErr := err.Unwrap()                 // raw Go err object
    }

midtrans.error complies with Go standard error. which support Error, Unwrap, Is, As.

// sample using errors.As
_, err := c.chargeTransaction(param)
var Err *midtrans.Error
if errors.As(err, &Err) {
	fmt.Println(Err.Message)
	fmt.Println(Err.StatusCode)
}

// sample using unwrap
_, err := c.chargeTransaction(param)
if err != nil {
	log.Print(errors.Unwrap(err))
    fmt.Print(err)
}

5. Examples

Examples are available on /examples folder There are:

Integration test are available

Get help

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	//Environment default Environment for Midtrans API
	Environment = Sandbox

	//DefaultHttpTimeout default timeout for go HTTP HttpClient
	DefaultHttpTimeout = 80 * time.Second

	//DefaultGoHttpClient default Go HTTP Client for Midtrans HttpClient API
	DefaultGoHttpClient = &http.Client{Timeout: DefaultHttpTimeout}

	//DefaultLoggerLevel logging level that will be used for config globally by Midtrans logger
	DefaultLoggerLevel = &LoggerImplementation{LogLevel: LogError}
)
View Source
var ClientKey string

ClientKey is config payment public API key for global use

View Source
var PaymentAppendNotification *string

PaymentAppendNotification opt to change or set custom notification urls globally on every transaction.

View Source
var PaymentOverrideNotification *string

PaymentOverrideNotification opt to change or add custom notification urls globally on every transaction.

View Source
var ServerKey string

ServerKey is config payment API key for global use

Functions

func HasOwnProperty

func HasOwnProperty(key string, body []byte) (bool, map[string]interface{})

HasOwnProperty : Convert HTTP raw response body to map and check if the body has own field

func SetPaymentAppendNotification

func SetPaymentAppendNotification(val string)

SetPaymentAppendNotification opt to change or add custom notification urls globally on every transaction. To use new notification url(s) disregarding the settings on Midtrans dashboard, only receive up to maximum of 3 urls.

func SetPaymentOverrideNotification

func SetPaymentOverrideNotification(val string)

SetPaymentOverrideNotification opt to change or set custom notification urls globally on every transaction. To use new notification url(s) disregarding the settings on Midtrans dashboard, only receive up to maximum of 3 urls.

Types

type ApiResponse

type ApiResponse struct {
	Status     string // e.g. "200 OK"
	StatusCode int    // e.g. 200
	Proto      string // e.g. "HTTP/1.0"

	// response Header contain a map of all HTTP header keys to values.
	Header http.Header
	// response body
	RawBody []byte
	// request that was sent to obtain the response
	Request *http.Request
}

ApiResponse : is a structs that may come from Midtrans API endpoints

type Bank

type Bank string

Bank value

const (
	//BankBni : bni
	BankBni Bank = "bni"

	//BankMandiri : mandiri
	BankMandiri Bank = "mandiri"

	//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"

	//BankMega : mega
	BankMega Bank = "mega"
)

type ConfigOptions

type ConfigOptions struct {
	PaymentIdempotencyKey       *string
	PaymentOverrideNotification *string
	PaymentAppendNotification   *string
	IrisIdempotencyKey          *string
	Ctx                         context.Context
}

ConfigOptions : is used to configure some feature before request to Midtrans API via `coreapi.Gateway` `snap.Gateway` and `iris.Gateway`

func (*ConfigOptions) SetContext

func (o *ConfigOptions) SetContext(ctx context.Context)

SetContext : options to change or add Context for each API request

func (*ConfigOptions) SetIrisIdempotencyKey

func (o *ConfigOptions) SetIrisIdempotencyKey(val string)

SetIrisIdempotencyKey : options to change or add unique idempotency-key on header on Iris API request with key maximum length is 100. To safely handle retry request without performing the same operation twice. This is helpful for cases where merchant didn't receive the response because of network issue or other unexpected error.

func (*ConfigOptions) SetPaymentAppendNotification

func (o *ConfigOptions) SetPaymentAppendNotification(val string)

SetPaymentAppendNotification : options to change or add custom notification urls on every transaction. To use new notification url(s) disregarding the settings on Midtrans dashboard, only receive up to maximum of 3 urls

func (*ConfigOptions) SetPaymentIdempotencyKey

func (o *ConfigOptions) SetPaymentIdempotencyKey(val string)

SetPaymentIdempotencyKey : options to change or add unique idempotency-key on header on Midtrans Payment API request with key maximum length is 36. To safely handle retry request without performing the same operation twice. This is helpful for cases where merchant didn't receive the response because of network issue or other unexpected error.

func (*ConfigOptions) SetPaymentOverrideNotification

func (o *ConfigOptions) SetPaymentOverrideNotification(val string)

SetPaymentOverrideNotification : options to change or add custom notification urls on every transaction. To use new notification url(s) disregarding the settings on Midtrans dashboard, only receive up to maximum of 3 urls

type CustomerAddress

type CustomerAddress struct {
	FName       string `json:"first_name,omitempty"`
	LName       string `json:"last_name,omitempty"`
	Phone       string `json:"phone,omitempty"`
	Address     string `json:"address,omitempty"`
	City        string `json:"city,omitempty"`
	Postcode    string `json:"postal_code,omitempty"`
	CountryCode string `json:"country_code,omitempty"`
}

CustomerAddress : Represent the customer address

type CustomerDetails

type CustomerDetails struct {
	// first name
	FName string `json:"first_name,omitempty"`

	// last name
	LName string `json:"last_name,omitempty"`

	Email    string           `json:"email,omitempty"`
	Phone    string           `json:"phone,omitempty"`
	BillAddr *CustomerAddress `json:"billing_address,omitempty"`
	ShipAddr *CustomerAddress `json:"customer_address,omitempty"`
}

CustomerDetails : Represent the customer detail

type EnvironmentType

type EnvironmentType int8

EnvironmentType is global config Environment for Midtrans api

const (

	//Sandbox : represent sandbox environment
	Sandbox EnvironmentType

	//Production : represent production environment
	Production
)

func (EnvironmentType) BaseUrl

func (e EnvironmentType) BaseUrl() string

BaseUrl To get Midtrans Base URL

func (EnvironmentType) IrisURL

func (e EnvironmentType) IrisURL() string

IrisURL : To get Iris environment API URL

func (EnvironmentType) SnapURL

func (e EnvironmentType) SnapURL() string

SnapURL : To get Snap environment API URL

type Error

type Error struct {
	Message        string
	StatusCode     int
	RawError       error
	RawApiResponse *ApiResponse
}

func (*Error) Error added in v1.3.0

func (e *Error) Error() string

Error returns error message. To comply midtrans.Error with Go error interface.

func (*Error) GetMessage

func (e *Error) GetMessage() string

GetMessage this get general message error when call api

func (*Error) GetRawApiResponse

func (e *Error) GetRawApiResponse() *ApiResponse

GetRawApiResponse this get api raw response from midtrans backend

func (*Error) GetRawError

func (e *Error) GetRawError() error

GetRawError GetRawApiResponse this get api raw response from midtrans backend

func (*Error) GetStatusCode

func (e *Error) GetStatusCode() int

GetStatusCode this get api response status code coming from midtrans backend

func (*Error) Unwrap added in v1.3.0

func (e *Error) Unwrap() error

Unwrap method that returns its contained error if there is RawError supplied during error creation, return RawError. Else, will return nil

type HttpClient

type HttpClient interface {
	Call(method string, url string, apiKey *string, options *ConfigOptions, body io.Reader, result interface{}) *Error
}

type HttpClientImplementation

type HttpClientImplementation struct {
	HttpClient *http.Client
	Logger     LoggerInterface
}

HttpClientImplementation : this is for midtrans HttpClient Implementation

func GetHttpClient

func GetHttpClient(Env EnvironmentType) *HttpClientImplementation

GetHttpClient : get HttpClient implementation

func (*HttpClientImplementation) Call

func (c *HttpClientImplementation) Call(method string, url string, apiKey *string, options *ConfigOptions, body io.Reader, result interface{}) *Error

Call the Midtrans API at specific `path` using the specified HTTP `method`. The result will be given to `result` if there is no error. If any error occurred, the return of this function is the `midtrans.Error` itself, otherwise nil.

func (*HttpClientImplementation) DoRequest

func (c *HttpClientImplementation) DoRequest(req *http.Request, result interface{}) *Error

DoRequest : is used by Call to execute an API request using HTTP client and parse the response into `result`.

type ItemDetails

type ItemDetails struct {
	ID           string `json:"id,omitempty"`
	Name         string `json:"name"`
	Price        int64  `json:"price"`
	Qty          int32  `json:"quantity"`
	Brand        string `json:"brand,omitempty"`
	Category     string `json:"category,omitempty"`
	MerchantName string `json:"merchant_name,omitempty"`
}

ItemDetails : Represent the transaction details

type LogLevel

type LogLevel uint32

LogLevel is the logging level used by the Midtrans go library

const (
	// NoLogging sets a logger to not show the messages
	NoLogging LogLevel = 0

	// LogError sets a logger to show error messages only.
	LogError LogLevel = 1

	// LogInfo sets a logger to show information messages
	LogInfo LogLevel = 2

	// LogDebug sets a logger to show informational messages for debugging
	LogDebug LogLevel = 3
)

type LoggerImplementation

type LoggerImplementation struct {
	LogLevel LogLevel
}

LoggerImplementation is a logger interface implementation. It prints some info, errors message and debug message for debugging to `os.Stderr` and `os.Stdout`

func (*LoggerImplementation) Debug

func (l *LoggerImplementation) Debug(format string, val ...interface{})

Debug : Log debug message using Printf conventions.

func (*LoggerImplementation) Error

func (l *LoggerImplementation) Error(format string, val ...interface{})

Error : Logs a warning message using Printf conventions.

func (*LoggerImplementation) Info

func (l *LoggerImplementation) Info(format string, val ...interface{})

Info : Logs information message using Printf conventions.

type LoggerInterface

type LoggerInterface interface {
	// Error logs a warning message using Printf conventions.
	Error(format string, val ...interface{})

	// Info logs an informational message using Printf conventions.
	Info(format string, val ...interface{})

	// Debug logs debug message using Printf conventions.
	Debug(format string, val ...interface{})
}

func GetDefaultLogger

func GetDefaultLogger(env EnvironmentType) LoggerInterface

GetDefaultLogger the default logger that the library will use to log errors, debug, and informational messages.

type TransactionDetails

type TransactionDetails struct {
	OrderID  string `json:"order_id"`
	GrossAmt int64  `json:"gross_amount"`
}

TransactionDetails : Represent transaction details

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL