irankish

package module
v0.2.12 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2023 License: MIT Imports: 14 Imported by: 0

README

IranKish Payment Gateway API for Go

The package is used for handling payments through Iranian IranKish payment gateway.

Based on IPG_TechnicalGuide.V9.pdf

For older version with SOAP check soap branch.

Install

go get -u github.com/aliforever/go-irankish-api

Usage

Variables:

terminalID := "" // <- Place your terminal ID 
acceptorID := "" // <- Place your acceptor ID
passphrase := "" // <- Place your Passphrase
publicKey := "" // <- Place your Public Key

ik, err := irankish.New(terminalID, acceptorID, passphrase, publicKey)
if err != nil {
    panic(err)
}
  • MakeToken for normal Purchase:
paymentID := ""
requestID := ""
amount := 0
revertUri := ""

token, err := ik.MakePurchaseToken(paymentID, requestID, amount, revertUri)
if err != nil {
	panic(err)
}

fmt.Println(token)

After making token you can get a simple redirecting form as a html to redirect users to the gateway:

htmlForm := token.RedirectForm() // <- Token Received from Make Token Method

Also, you can either handle incoming POST requests from the gateway yourself or listen to package's incoming callbacks. Here's an example to listen and verify payments using package's callback

http.HandleFunc("/payment_callback", ik.CallbackHandler) // Registers a http handler for callbacks
go http.ListenAndServe(":8001", nil) // Run your http server

for request := range ik.IncomingCallbacks() {
    input, err := request.ParseUserInput()
    if err != nil {
        request.WriteResponse(http.StatusBadRequest, []byte(err.Error()))
        return
    }
    
    fmt.Println(fmt.Sprintf("%+v", input))
    
    if input.RetrievalReferenceNumber != "" && input.SystemTraceAuditNumber != "" {
        result, err := ik.VerifyPurchase(input.Token, input.RetrievalReferenceNumber, input.SystemTraceAuditNumber)
        if err != nil {
            request.WriteResponse(http.StatusBadRequest, []byte(err.Error()))
            fmt.Println(err)
            continue
        }
	
        fmt.Println(result)
    }
    
    request.WriteResponse(http.StatusOK, []byte("ok"))
}

-- Make sure to write the response or it will hang on the goroutine

  • VerifyPayment:
token := ""
referenceNumber := ""
auditNumber := ""

result, err := ik.VerifyPurchase(token, referenceNumber, auditNumber)
if err != nil {
    request.WriteResponse(http.StatusBadRequest, []byte(err.Error()))
    panic(err)
}

fmt.Println(result)

Documentation

Index

Constants

View Source
const (
	IranKishShaparakUrl = "https://ikc.shaparak.ir"
	TokenUrl            = "/api/v3/tokenization/make"
	RedirectUrl         = IranKishShaparakUrl + "/iuiv3/IPG/Index/"
	ConfirmationUrl     = "/api/v3/confirmation/purchase"
)
View Source
const (
	TransactionTypePurchase  transactionType = "Purchase"
	TransactionTypeBill      transactionType = "Bill"
	TransactionTypeAsnShpWPP transactionType = "AsanShpWPP"
)

Variables

This section is empty.

Functions

func AddCallbackUrl added in v0.1.8

func AddCallbackUrl(serverAddress, endpoint, callbackUrl string) error

func NewAsanShp added in v0.0.6

func NewAsanShp(prepaymentAmount, loanAmount, loanCount int64) asanShp

func NewBillInfo added in v0.0.6

func NewBillInfo(billID string, billPaymentID string) billInfo

func NewMultiplexParameters added in v0.0.6

func NewMultiplexParameters() multiplexParameters

Types

type AdditionalParameter added in v0.0.7

type AdditionalParameter struct {
	Key   string `json:"Key"`
	Value string `json:"Value"`
}

type IncomingRequest added in v0.0.6

type IncomingRequest struct {
	Request *http.Request

	Done chan bool
	// contains filtered or unexported fields
}

func (*IncomingRequest) ParseUserInput added in v0.0.6

func (i *IncomingRequest) ParseUserInput() (*UserInput, error)

func (*IncomingRequest) WriteResponse added in v0.0.6

func (i *IncomingRequest) WriteResponse(statusCode int, data []byte)

type IranKish

type IranKish struct {
	// contains filtered or unexported fields
}

func New

func New(terminalID, acceptorID, passphrase, publicKey string, logger Logger) (*IranKish, error)

func NewWithProxyHost added in v0.1.0

func NewWithProxyHost(
	terminalID,
	acceptorID,
	passphrase,
	publicKey string,
	proxyAddress string,
	logger Logger,
) (*IranKish, error)

func (*IranKish) CallbackHandler

func (i *IranKish) CallbackHandler(wr http.ResponseWriter, r *http.Request)

func (*IranKish) IncomingCallbacks added in v0.0.6

func (i *IranKish) IncomingCallbacks() <-chan IncomingRequest

func (*IranKish) MakePurchaseToken added in v0.0.6

func (i *IranKish) MakePurchaseToken(
	paymentID,
	requestID string,
	amount int64,
	revertUri string,
	params ...AdditionalParameter,
) (*MakeTokenResult, error)

func (*IranKish) VerifyPurchase added in v0.0.6

func (i *IranKish) VerifyPurchase(token, referenceNumber, auditNumber string) (*TransactionResult, error)

type Logger added in v0.0.8

type Logger interface {
	Println(v ...any)
}

type MakeTokenResult

type MakeTokenResult struct {
	Token             string          `json:"token"`
	InitiateTimestamp int64           `json:"initiateTimeStamp"`
	ExpiryTimestamp   int64           `json:"expiryTimeStamp"`
	TransactionType   transactionType `json:"transactionType"`
	BillInfo          interface{}     `json:"billInfo"`
	// contains filtered or unexported fields
}

func (*MakeTokenResult) RedirectForm added in v0.0.6

func (m *MakeTokenResult) RedirectForm() string

type Proxy added in v0.1.0

type Proxy struct {
	// contains filtered or unexported fields
}

Proxy is to run a http proxy server to redirect requests

This is because there's IP limit on payment gateways
Each payment gateway is only allowed to be accessed by specific ip addresses
You can run this proxy server in an allowed IP address and initiate your gateway using WithProxy

func NewProxy added in v0.1.0

func NewProxy(httpUri string, logger Logger) *Proxy

func NewProxyWithMux added in v0.1.0

func NewProxyWithMux(httpUri string, mux *http.ServeMux, logger Logger) *Proxy

func (*Proxy) EnableCallbackUrls added in v0.1.2

func (p *Proxy) EnableCallbackUrls() *Proxy

EnableCallbackUrls by calling this method /add_callback_url endpoint will be activated

This is to proxy back callback (revert url) requests to specified endpoints

func (*Proxy) SetHomePageHandler added in v0.1.4

func (p *Proxy) SetHomePageHandler(handler func(writer http.ResponseWriter, r *http.Request)) *Proxy

func (*Proxy) SetTargetUrl added in v0.1.6

func (p *Proxy) SetTargetUrl(target *url.URL) *Proxy

SetTargetUrl is used to replace payment gateway's base url

func (*Proxy) Start added in v0.1.0

func (p *Proxy) Start() error

type Response added in v0.0.6

type Response struct {
	ResponseCode string          `json:"responseCode"`
	Description  string          `json:"description"`
	Status       bool            `json:"status"`
	Result       json.RawMessage `json:"result"`
}

type TransactionResult added in v0.0.6

type TransactionResult struct {
	ResponseCode             string `json:"responseCode"`
	SystemTraceAuditNumber   string `json:"systemTraceAuditNumber"`
	RetrievalReferenceNumber string `json:"retrievalReferenceNumber"`
	TransactionDate          int64  `json:"transactionDate"`
	TransactionTime          int64  `json:"transactionTime"`
	ProcessCode              string `json:"processCode"`
	BillType                 string `json:"billType"`
	BillId                   string `json:"billId"`
	PaymentId                string `json:"paymentId"`
	Amount                   string `json:"amount"`
	DuplicateVerify          bool   `json:"duplicateVerify"` // indicates if payment is already verified
}

type UserInput added in v0.0.6

type UserInput struct {
	Token                    string
	AcceptorID               string
	MerchantID               string
	ResponseCode             string
	PaymentID                string
	RequestID                string
	Sha256OfPan              string
	RetrievalReferenceNumber string
	SystemTraceAuditNumber   string
	Amount                   string
	MaskedPan                interface{}
	Ttl                      string
	Sha1OfPan                interface{}
}

Jump to

Keyboard shortcuts

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