SoFunction
Updated on 2025-03-02

How to implement Alipay sandbox payment in Golang

First of all, I use gin as my backend framework, and we set up several interfaces first.

func main() {
    r := ()
    ("/pay", )
    ("/return", )
    (":8100")
}

/smartwalle/alipay This third-party library defines many Alipay interfaces. We directly use this library to continue our development.

Initialize first

var (
    appID = "Your Alipay ID"
    privateKey = "Your Alipay Private Key"
    client, _  = (appID, privateKey, false)
)
 
func init() {
    ("Your Alipay Public Key")
}

Fill in the corresponding location of the key and public key applied in the Alipay open platform sandbox

Use the Web Scan code to pay the payment interface

// WebPageAlipay Scan the QR code to payfunc WebPageAlipay(c *) {
    ......
    var p = {}
     = "http://127.0.0.1:8080/return" //The URL page that jumps after order payment     = ("Order number: %s", orderSn) //Payment title     = orderSn                     // Merchant Order Number     = price                      //price     = "FAST_INSTANT_TRADE_PAY"
    
    var url, err = (p)
    if err != nil {
        (err)
    }
 
    var payURL = () //Scan the QR code to pay the web page link, and then open after returning to the front end}

After the payment is successful, the payment page will automatically jump to the address set in (the front-end address I set here. After jumping to the front-end address, the front-end requests /return from the back-end, completely obtains the page's query parameters and splices them all into the back-end /return), and the back-end obtains the query parameters for signature verification

// AliPay Notify Receive Notification Interfacefunc AliPayNotify(c *) {
    //Get the url and convert it to *URL    x, _ := (())
    //Verify whether the payment is successful    ok, err := (())
}

This is the article about the methods and steps of Golang to implement Alipay sandbox payment. For more related Golang, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!