Skip to main content

Golang SDK access documentation

info

The Golang SDK encapsulates the signature verification, transfer encoding and other tasks in the server-side API, and provides a faster server-side integration solution in the Golang development environment. If your server-side development environment is not Golang, please refer to API access Documentation Implement related logic by yourself

Import dependencies

Add a dependency on paynicorn-sdk-golang to your project's go.mod file

require (
github.com/Transsion-mios/paynicorn-sdk-golang v1.0.2
)

Reference Code

package main

import (
"fmt"
"github.com/Transsion-mios/paynicorn-sdk-golang"
"github.com/gin-gonic/gin"
"net/http"
)

func main() {
var appkey = "PUT_YOUR_APPKEY_HERE"
var merchantkey = "PUT_YOUR_MERCHANT_SECRET_HERE"


//raise a payment request to PAYNICORN
request := paynicorn.InitPaymentRequest{}
request.OrderId="PUT_YOUR_ORDER_ID_HERE"
request.CountryCode="NG"
request.Currency="NGN"
request.Amount="10"
request.CpFrontPage="PUT_YOUR_WEB_REDIRECT_URL_HERE"
request.OrderDescription="TEST GOODS NAME"
response := paynicorn.InitPayment(appkey,merchantkey,request)
if response != nil{
fmt.Println(response)
}



//query a payment status from PAYNICORN
request1 := paynicorn.QueryPaymentRequest{}
request1.OrderId=request.OrderId
request1.TxnType=paynicorn.PAYMENT
response1 := paynicorn.QueryPayment(appkey,merchantkey,request1)
if response1 != nil{
fmt.Println(response1)
}



//receive a payment status postback from PAYNICORN
r := gin.Default()
r.POST("/postback", func(context *gin.Context) {

var req paynicorn.PostbackRequest
if err := context.BindJSON(&req); err != nil{
context.String(http.StatusInternalServerError,"")
}else{
postbackInfo := paynicorn.ReceivePostback(merchantkey,req)
if postbackInfo != nil && postbackInfo.Verified{
fmt.Println(postbackInfo)
context.String(http.StatusOK,"success_"+postbackInfo.TxnId)
}else{
context.String(http.StatusInternalServerError,"")
}
}

})
r.Run(":8080")


}

Setting parameters

Replace PUT_YOUR_APPKEY_HERE with the appKey of the corresponding payment application. The address for obtaining the appKey is: https://console.paynicorn.com/#/app/list

Replace PUT_YOUR_MERCHANT_SECRET_HERE with the merchant's Merchant secret. The address for obtaining the Merchant secret is: https://console.paynicorn.com/#/developer

var appkey = "PUT_YOUR_APPKEY_HERE"
var merchantkey = "PUT_YOUR_MERCHANT_SECRET_HERE"

initiate payment

Use the paynicorn.InitPaymentRequest object and the paynicorn.InitPayment method to initiate a new payment request to Paynicorn,

Replace PUT_YOUR_ORDER_ID_HERE with your merchant order number,

Replace PUT_YOUR_WEB_REDIRECT_URL_HERE with the jump address of your merchant website or the deeplink of the Android application. After the user pays, Paynicorn will call this address to jump back to the specified page of your merchant website or Android application.

request := paynicorn.InitPaymentRequest{}
request.OrderId="PUT_YOUR_ORDER_ID_HERE"
request.CountryCode="NG"
request.Currency="NGN"
request.Amount="10"
request.CpFrontPage="PUT_YOUR_WEB_REDIRECT_URL_HERE"
request.OrderDescription="TEST GOODS NAME"
response := paynicorn.InitPayment(appkey,merchantkey,request)

Judging the transaction query status according to the return value of the paynicorn.InitPaymentResponse object

type InitPaymentResponse struct {

Code string `json:"code"` // MANDATORY response code represent current request is success response or not refer to https://www.paynicorn.com/#/docs 6.5

Message string `json:"message"` // OPTIONAL response code refer to https://www.paynicorn.com/#/docs 6.5

TxnId string `json:"txnId"`// OPTIONAL unique transaction id generate by paynicorn.you can use it to query your transaction status or wait for a postback

Status string `json:"status"`// OPTIONAL transaction status (1:for success;-1:processing;0:failure)

WebUrl string `json:"webUrl"`// OPTIONAL paynicorn cashier uri
}

If InitPaymentResponse is empty, it can be directly judged that the transaction initiation failed.

If InitPaymentResponse is not empty, get the Status member status in InitPaymentResponse to judge the transaction status, Status=0 payment failed, Status=-1 payment processing, Status=1 payment successful,

When Status is -1, it indicates that the transaction is being processed and the user needs to take further action. At this time, it is judged whether WebUrl is empty, and no processing is required. If WebUrl has a value, you need to pass the WebUrl to The client loads the page for the user to complete subsequent payment actions

Query payment status

Use the paynicorn.QueryPaymentRequest object and the paynicorn.QueryPayment method to initiate a query order status request to Paynicorn,

Replace PUT_YOUR_ORDER_ID_HERE with your merchant order number

request := paynicorn.QueryPaymentRequest{}
request.OrderId="PUT_YOUR_ORDER_ID_HERE"
request.TxnType=paynicorn.PAYMENT
response := paynicorn.QueryPayment(appkey,merchantkey,request)

Judging the transaction query status according to the return value of the paynicorn.QueryPaymentResponse object

type QueryPaymentResponse struct{

Code string `json:"code"` // MANDATORY response code represent current request is success response or not refer to https://www.paynicorn.com/#/docs 6.5

Message string `json:"message"` // MANDATORY response code refer to https://www.paynicorn.com/#/docs 6.5

TxnId string `json:"txnId"`// MANDATORY unique transaction id generate by paynicorn.you can use it to query your transaction status or wait for a postback

Status string `json:"status"`// OPTIONAL transaction status (1:for success;-1:processing;0:failure)

CompleteTime string `json:"completeTime"` //OPTIONAL transaction complete time
}

If paynicorn.QueryPaymentResponse is empty, it can be judged that the order query failed,

If paynicorn.QueryPaymentResponse is not empty, get the Status member status in QueryPaymentResponse to judge the transaction status, Status=0 payment failed, Status=-1 payment processing, Status=1 payment successful

Receive callback notification

It is used to receive Paynicorn's asynchronous callback notification. When the payment order status changes, Paynicorn will call this address for notification. The callback notification address is set in the Callback address of each payment application:

Postback Address Setting

Confirm whether the signature verification is passed through the Verified member of the paynicorn.PostbackInfo object. After passing, you can update the transaction status according to the Status in the paynicorn.PostbackInfo. Status=0 payment failed, Status=-1 payment processing, Status=1 Payment is successful,

After receiving the notification, you need to return a "success_"+postbackInfo.TxnId response to Paynicorn to notify that the notification has been successfully received, otherwise Paynicorn will continue to notify the order status change until the maximum number of notifications is reached

r := gin.Default()
r.POST("/postback", func(context *gin.Context) {

var req paynicorn.PostbackRequest
if err := context.BindJSON(&req); err != nil{
context.String(http.StatusInternalServerError,"")
}else{
postbackInfo := paynicorn.ReceivePostback(merchantkey,req)
if postbackInfo != nil && postbackInfo.Verified{
fmt.Println(postbackInfo)
context.String(http.StatusOK,"success_"+postbackInfo.TxnId)
}else{
context.String(http.StatusInternalServerError,"")
}
}

})
r.Run(":8080")