pasargad

package module
v0.0.0-...-3e000d1 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2021 License: Apache-2.0 Imports: 16 Imported by: 0

README

Golang SDK for Pasargad IPG

Golang SDK for Pasargad Internet Payment Gateway (RESTful API)

Installation

go get -u github.com/pepco-api/golang-rest-sdk

Usage

Redirect User to Payment Gateway

package main
import (
	"fmt"
	"github.com/pepco-api/golang-rest-sdk"
)

func main() {	
    // Create an object from PasargadAPI struct
    // e.q: pasargadApi := pasargad.PasargadAPI(4412312,123456,"https://pep.co.ir/ipgtest","cert.xml");
    pasargadApi := pasargad.PasargadAPI(YOUR_MERCHANT_CODE, YOUR_TERMINAL_ID, REDIRECT_URL, CERT_FILE_HERE)
    request := pasargad.CreatePaymentRequest{
        Amount:        15000,
        InvoiceNumber: "4029",
        InvoiceDate:   "2021/08/17 16:12:00",
        //Mobile:        "09121231234",   // Optional
        //Email:         "xxxx@xxxx.xxx", // Optional
    }
    response, err := pasargadApi.Redirect(request)
    if err != nil {
        fmt.Println(err)
    } else {
        fmt.Println(response)
    }
}

This method, will return a string like this:

// output:
 https://pep.shaparak.ir/payment.aspx?n=LySl+5tYkxL5qNMBRthW7DWzV8e3ALnTJUqiCS0V/io=
// Redirect User to the generated URL to make payment

Checking and Verifying Transaction

After Payment Process, User is going to be returned to your REDIRECT_URL.

payment gateway is going to answer the payment result with sending below parameters to your redirectURL (as QueryString parameters):

  • InvoiceNumber (iN field)
  • InvoiceDate (iD field)
  • TransactionReferenceID (tref field)

Store this information in a proper data storage and let's check transaction result by sending a check api request to the Bank:

package main
import (
	"fmt"
	"github.com/pepco-api/golang-rest-sdk"
)

func main() {	
    pasargadApi := pasargad.PasargadAPI(4412312,123456,"https://pep.co.ir/ipgtest","cert.xml");
    request := pasargad.CreateCheckTransactionRequest{
        InvoiceNumber:          "4029",
        InvoiceDate:            "2021/08/17 16:12:00",
        TransactionReferenceID: "637648135297288707", // optional
    }
    response, err := pasargadApi.CheckTransaction(request)
    if err != nil {
        fmt.Println(err)
    } else {
        fmt.Println(response)
    }
}

Successful result:

{
    "TraceNumber": 13,
    "ReferenceNumber": 100200300400500,
    "TransactionDate": "2021/08/08 11:58:23",
    "Action": "1003",
    "TransactionReferenceID": "636843820118990203",
    "InvoiceNumber": "4029",
    "InvoiceDate": "2021/08/08 11:54:03",
    "MerchantCode": 100123,
    "TerminalCode": 200123,
    "Amount": 15000,
    "IsSuccess": true,
    "Message": " "
}

Our code will return CheckTransactionResponse struct as response or err in case of any errors.

If you got IsSuccess with true value, so everything is O.K!

Now, for your successful transaction, you should call VerifyPayment() method to keep the money and Bank makes sure the checking process done properly:

package main
import (
	"fmt"
	"github.com/pepco-api/golang-rest-sdk"
)

func main() {	
	pasargadApi := pasargad.PasargadAPI(4412312,123456,"https://pep.co.ir/ipgtest","cert.xml");
	request := pasargad.CreateVerifyPaymentRequest{
		Amount:        15000,
		InvoiceDate:   "2021/08/17 16:12:00",,
		InvoiceNumber: "4029",
	}
	response, err := pasargadApi.VerifyPayment(request)
	if err != nil {
		fmt.Println(err)
	} else {
		fmt.Println(response)
	}
}

...and the successful response looks like this response:

{
 "IsSuccess": true,
 "Message": " ",
 "MaskedCardNumber": "5022-29**-****-2328",
 "HashedCardNumber": "2DDB1E270C598677AE328AA37C2970E3075E1DB....",
 "ShaparakRefNumber": "100200300400500"
}

Our code will return VerifyPaymentResponse struct as response or err in case of any errors.

Payment Refund

If for any reason, you decided to cancel an order in early hours after taking the order (maximum 2 hours later), you can refund the client payment to his/her bank card.

for this, use Refund() method:

package main
import (
	"fmt"
	"github.com/pepco-api/golang-rest-sdk"
)

func main() {
	pasargadApi := pasargad.PasargadAPI(4412312,123456,"https://pep.co.ir/ipgtest","cert.xml");
	request := pasargad.CreateRefundRequest{
		InvoiceDate:   "2021/08/17 16:12:00",
		InvoiceNumber: "4029",
	}
	response, err := pasargadApi.Refund(request)
	if err != nil {
		fmt.Println(err)
	} else {
		fmt.Println(response)
	}
}

Our code will return RefundResponse struct as response or err in case of any errors.

Support

Please use your credentials to login into Support Panel Contact Author/Maintainer: Reza Seyf

Documentation

Index

Constants

View Source
const (
	URL_GET_TOKEN = "https://pep.shaparak.ir/Api/v1/Payment/GetToken"

	// Redirect User with token to this URL.
	// e.q: https://pep.shaparak.ir/payment.aspx?n=Token
	URL_PAYMENT_GATEWAY   = "https://pep.shaparak.ir/payment.aspx"
	URL_CHECK_TRANSACTION = "https://pep.shaparak.ir/Api/v1/Payment/CheckTransactionResult"
	URL_VERIFY_PAYMENT    = "https://pep.shaparak.ir/Api/v1/Payment/VerifyPayment"
	URL_REFUND            = "https://pep.shaparak.ir/Api/v1/Payment/RefundPayment"
)

The API URLs

View Source
const ACTION_PAYMENT = "1003"

Variables

This section is empty.

Functions

This section is empty.

Types

type CheckTransactionResponse

type CheckTransactionResponse struct {
	IsSuccess              bool   `json:"IsSuccess"`              // status of request (True or False)
	Message                string `json:"Message"`                // Message - The response of server
	ReferenceNumber        int64  `json:"ReferenceNumber"`        // Reference number
	TraceNumber            int64  `json:"TraceNumber"`            // trace number
	TransactionDate        string `json:"TransactionDate"`        // transaction date
	Action                 string `json:"Action"`                 // action identifier
	TransactionReferenceID string `json:"TransactionReferenceID"` // transaaction internal reference id
	InvoiceNumber          string `json:"InvoiceNumber"`          // invoice number
	InvoiceDate            string `json:"InvoiceDate"`            // invoice date
	MerchantCode           int64  `json:"MerchantCode"`           // merchant code
	TerminalCode           int64  `json:"TerminalCode"`           // terminal code
	Amount                 int64  `json:"amount"`                 // transaction amount
}

RedirectResponse data type

type CreateCheckTransactionRequest

type CreateCheckTransactionRequest struct {
	TransactionReferenceID string `json:"transactionReferenceID"` // transaction reference id
	InvoiceNumber          string `json:"invoiceNumber"`          // invoice number
	InvoiceDate            string `json:"invoiceDate"`            // invoice date
	TerminalCode           int64  `json:"terminalCode"`           // terminal code
	MerchantCode           int64  `json:"merchantCode"`           // merchant code
}

Check Transaction Data Types =================================================== CreateCheckTransactionRequest is the struct to create a checkTransaction request

func (*CreateCheckTransactionRequest) GetCheckTransactionRequest

func (m *CreateCheckTransactionRequest) GetCheckTransactionRequest() CreateCheckTransactionRequest

GetCheckTransactionRequest and parameters

type CreatePaymentRequest

type CreatePaymentRequest struct {
	Amount          int64  `json:"amount"`          // invoice amount
	InvoiceNumber   string `json:"invoiceNumber"`   // invoice number
	InvoiceDate     string `json:"invoiceDate"`     // invoice date
	Action          string `json:"action"`          // request action identifier
	Mobile          string `json:"mobile"`          // mobile number of the user
	Email           string `json:"email"`           // email address of the user
	MerchantCode    int64  `json:"merchantCode"`    // merchant code
	TerminalCode    int64  `json:"terminalCode"`    // terminal code
	RedirectAddress string `json:"redirectAddress"` // redirect url
	TimeStamp       string `json:"timeStamp"`       // Current timestamp (Y/m/d H:i:s)
}

Redirect Data Types ========================================== CreatePaymentRequest is the format of our data to send to bank

func (*CreatePaymentRequest) GetRedirectRequest

func (m *CreatePaymentRequest) GetRedirectRequest() CreatePaymentRequest

Get Redirect Request and it's parameters

type CreateRefundRequest

type CreateRefundRequest struct {
	InvoiceNumber string `json:"invoiceNumber"` // invoice number
	InvoiceDate   string `json:"invoiceDate"`   // invoice date
	TerminalCode  int64  `json:"terminalCode"`  // terminal code
	MerchantCode  int64  `json:"merchantCode"`  // merchant code
	TimeStamp     string `json:"timeStamp"`     // Current timestamp (Y/m/d H:i:s)
}

Refund Data Types ================================================== CreateRefundRequest is the struct to create a Refund Payment to user

func (*CreateRefundRequest) GetRefundRequest

func (m *CreateRefundRequest) GetRefundRequest() CreateRefundRequest

GetRefundRequest and parameters

type CreateVerifyPaymentRequest

type CreateVerifyPaymentRequest struct {
	Amount        int64  `json:"amount"`        // invoice amount
	InvoiceNumber string `json:"invoiceNumber"` // invoice number
	InvoiceDate   string `json:"invoiceDate"`   // invoice date
	TerminalCode  int64  `json:"terminalCode"`  // terminal code
	MerchantCode  int64  `json:"merchantCode"`  // merchant code
	TimeStamp     string `json:"timeStamp"`     // Current timestamp (Y/m/d H:i:s)
}

Verify Payment Data Types ================================================ CreateVerifyPaymentRequest is the struct to create a VerifyPayment request

func (*CreateVerifyPaymentRequest) GetVerifyPaymentRequest

func (m *CreateVerifyPaymentRequest) GetVerifyPaymentRequest() CreateVerifyPaymentRequest

GetVerifyPaymentRequest and parameters

type ErrorResponse

type ErrorResponse struct {
	IsSuccess bool   `json:"IsSuccess"` // status of request (True or False)
	Message   string `json:"Message"`   // Message - The response of server.
}

General Types ================================================ ErrorResponse is the API error response.

func (ErrorResponse) Error

func (m ErrorResponse) Error() string

Error returns a formatted error string.

type HTTPClient

type HTTPClient interface {
	Do(req *http.Request) (*http.Response, error)
}

HTTPClient is HTTP client.

type PasargadPaymentAPI

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

PasargadPaymentAPI for rest

func PasargadAPI

func PasargadAPI(merchantCode int64, terminalId int64, redirectUrl string, certificationFile string) *PasargadPaymentAPI

PasargadAPI creates a new PasargadPaymentAPI instance.

func PasargadAPIClient

func PasargadAPIClient(merchantCode int64, terminalId int64, redirectUrl string, certificationFile string, httpClient HTTPClient) *PasargadPaymentAPI

PasargadAPIClient creates a new PasargadPaymentAPI instance and allows you to pass a http.Client.

func (*PasargadPaymentAPI) CheckTransaction

CheckTransaction method

func (*PasargadPaymentAPI) Redirect

func (m *PasargadPaymentAPI) Redirect(request CreatePaymentRequest) (string, error)

Generate Payment URL

func (*PasargadPaymentAPI) Refund

Refund method

func (*PasargadPaymentAPI) SetSign

func (m *PasargadPaymentAPI) SetSign(sign string)

SetSign sets new key.

func (*PasargadPaymentAPI) VerifyPayment

VerifyPayment method

type RedirectResponse

type RedirectResponse struct {
	IsSuccess bool   `json:"IsSuccess"` // status of request (True or False)
	Message   string `json:"Message"`   // Message - The response of server
	Token     string `json:"Token"`     // Token (for successful requests)
}

RedirectResponse data type

type RefundResponse

type RefundResponse struct {
	IsSuccess bool   `json:"IsSuccess"` // status of request (True or False)
	Message   string `json:"Message"`   // Message - The response of server
}

RefundResponse data type

type Signer

type Signer struct {
	Key *rsa.PrivateKey
}

func NewSigner

func NewSigner(pemKey []byte) (*Signer, error)

func (*Signer) Sign

func (s *Signer) Sign(data []byte) ([]byte, error)

func (*Signer) SignBase64

func (s *Signer) SignBase64(data []byte) (string, error)

func (*Signer) SignHex

func (s *Signer) SignHex(data []byte) (string, error)

type VerifyPaymentResponse

type VerifyPaymentResponse struct {
	IsSuccess         bool   `json:"IsSuccess"`         // status of request (True or False)
	Message           string `json:"Message"`           // Message - The response of server
	MaskedCardNumber  string `json:"MaskedCardNumber"`  // Masked Card Number (like: 5022-29**-****-2328)
	HashedCardNumber  string `json:"HashedCardNumber"`  // Hashed Card Number (like: 2DDB1E270C598.....)
	ShaparakRefNumber string `json:"ShaparakRefNumber"` // Shaparak Reference ID (like: 100200300400500)
}

VerifyPaymentResponse VerifyPaymentResponse data type

type XMLRSAKey

type XMLRSAKey struct {
	Modulus  string
	Exponent string
	P        string
	Q        string
	DP       string
	DQ       string
	InverseQ string
	D        string
}

Jump to

Keyboard shortcuts

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