支付宝沙箱支付使用背景:
支付宝沙箱支付是支付宝提供的一个测试环境,用于开发者在不影响真实交易的情况下进行支付接口的开发和调试。在沙箱环境中,开发者可以模拟真实的支付流程,包括支付、退款、查询等操作,以便更好地调试和验证自己的支付接口是否正确。
使用支付宝沙箱支付可以帮助开发者提高支付接口的稳定性和安全性,避免在开发过程中对真实交易造成影响。同时,沙箱环境还提供了丰富的测试工具和文档,方便开发者快速上手和进行测试。
实现的页面效果
第一步:建立项目,复制下方main.go代码
代码架构展示
main里的代码
Go
package main
import (
"fmt"
"log"
"net/http"
"github.com/gin-gonic/gin"
"github.com/smartwalle/alipay/v3"
"github.com/smartwalle/xid"
)
var client *alipay.Client
const (
kAppId = "9021000134674475"
//私钥
kPrivateKey = "MIIEpAIBAAKCAQEAzSHBDVo52MqoAMvn1KFY7iuzFEUHDp/Iya3vK5s6Qp1N9hxvxwbN5Y3CjtyapDyPwBNGFYpxsoDFUOoH55vc5Z0mTxWAI3a9R1WSrL3cAAi7/2DIf0CHk9RCnzOJWGOY8luNY3iEHl2hdn0qRqBSryTKwD5znedpowphTdIDeXHBzEhkSGTAQ5+VDT9H7j3Fka6BMgmMaNvrmMh/23y6ZEYKvFncTJOBWeHcWX5wdONle1RDEC2YL9hkxZhjC3wIpMwDgDLA3J+Bx8nNhuVJW+eTV6iVaXy4HVojgoIYYyniT64VwFRIYsp9ArCKbWWJz+IIueaZe0eSiOfsH/hIuQIDAQABAoIBAG0/N8h7r+kkbPsXnV0hnPIjNdlqtcp1EINQbdsEba/ChR24oVMW7Zr+t4pMNNOZKVm7Kg0Y0Vqmt+9S5GjWGnvzCM7/fWHlwAJ4sYfsCXm/LRhiHAWhgVMarWyKDvCTAoa4eYFrUrm1VeN734MDqWV7q7tcx0hmUV0yjwXZ/l2zYkiyN5z6ElqNDI7gfKNEBmlCm42qwOEs6Xc3So2+LTSI+fpkLoI0FBqsj0AJ6nKc2gzQzTkfZ0MNY9hd//tCpz8FINxiO3z/ZcKDU90Ug3QIVxXEg/XLEYySPoetaCzaG+Qnrkz4JHBXObppObGmbff+7eConipHMPubCb81xzECgYEA9OyYmy7K+ibsdARgwEweV8KzVYCWND3O0KC/73eNOJi4thqDMV9RVJWp3CgFIVR1c1z3Gf4ZctO9jC/GvmPI72DWc/K9NyrHTGTnZhIVEQy85c9/sdm2SByTOoBWF9OwPlhlP+meOGmMxECvgL3dlFiGUoIYMu7JXNkF/eiS2F0CgYEA1mh+2qSZ6wtuW+F7Z/T4PhKgzWH4by1oOdMc4BDnA4+rw/Yp1emBlnIFr5TbTaQmS+HNl6mXsHyZiZ1/2FNVDPIaKU3nbkEVQ33LYMV/qPUixxH5ArO+btUHDPac7U4hBoxt/xdVTRqjV57x59+/eFHokQ542dekDe12u4orvA0CgYEA06uyS9NJ6p0/Fxk9gJOIWbJUCJ4DsgyEs3tpxYxdyT77y63oGgBVqrNZxIH1xRNYGiaLC7S1y65BD2mu6pYUbwUS8cwkSQw4h8Zg0a7o8/49NqN4qX0Ncw73EsUdIqR5mbgNcQn3Cqv10xpQUwRD/lPvalkyjrod3pdU/VZFqqUCgYEAlYenAMFO/xenzbiBoky8XD4Nm286Hf9i8wEC4FmpY1htiGtDXgZN9ChtQrjkAU/7zWwbQCxsuA8N3WXUZYAojrDCAhBs2P3W4Iz4q/LRR3o+2Or16SaGJzG2ixnq861LZw/FHuSc/vsW8+LL/tSS6kzlj1Syf+reZUA5UC3atnkCgYBfQjNAhDpUJQKpt+9KgdqjBa6YUtVxeFWQF9Ves9Rdk6KSE+mWounV4u3Tq/FUjDaHUOdyfiV+Q5Vrs9yQdwrWRTnMGFCTM6zfJEoU19ahtXaodoeaDpefOZhQU7Nfnv1r/xjlAP/LZsPACPIXPayKK+UWN1/ay2Jq7jp4yWna7Q=="
// TODO 设置回调地址域名
kServerDomain = ""
)
func main() {
var err error
//支付宝提供了用于开发时测试的 sandbox 环境,对接的时候需要注意相关的 app id 和密钥是 sandbox 环境还是 production 环境的。如果是 sandbox 环境,本参数应该传 false,否则为 true。
if client, err = alipay.New(kAppId, kPrivateKey, false); err != nil {
log.Println("初始化支付宝失败", err)
return
}
// 加载证书
// 加载应用公钥证书
if err = client.LoadAppCertPublicKeyFromFile("appPublicCert.crt"); err != nil {
log.Println("加载证书发生错误", err)
return
}
// 加载支付宝根证书
if err = client.LoadAliPayRootCertFromFile("alipayRootCert.crt"); err != nil {
log.Println("加载证书发生错误", err)
return
}
// 加载支付宝公钥证书
if err = client.LoadAlipayCertPublicKeyFromFile("alipayPublicCert.crt"); err != nil {
log.Println("加载证书发生错误", err)
return
}
//接口内容加密
// if err = client.SetEncryptKey("iotxR/d99T9Awom/UaSqiQ=="); err != nil {
log.Println("加载内容加密密钥发生错误", err)
return
}
//路由函数
r := gin.Default()
r.GET("/alipay/pay", pay)
r.GET("/alipay/callback", callback)
r.GET("/alipay/notify", notify)
//http.HandleFunc("/alipay/pay", pay)
//http.HandleFunc("/alipay/callback", callback)
//http.HandleFunc("/alipay/notify", notify)
//http.ListenAndServe(":"+kServerPort, nil)
r.Run()
}
func pay(c *gin.Context) {
var tradeNo = fmt.Sprintf("%d", xid.Next())
var p = alipay.TradePagePay{}
p.NotifyURL = kServerDomain + "/alipay/notify"
p.ReturnURL = kServerDomain + "/alipay/callback"
p.Subject = "支付测试:" + tradeNo
p.OutTradeNo = tradeNo
p.TotalAmount = "20.00"
p.ProductCode = "FAST_INSTANT_TRADE_PAY"
url, _ := client.TradePagePay(p)
c.Redirect(http.StatusTemporaryRedirect, url.String())
//http.Redirect(writer, request, url.String(), http.StatusTemporaryRedirect)
}
func callback(c *gin.Context) {
// 解析请求参数
if err := c.Request.ParseForm(); err != nil {
log.Println("解析请求参数发生错误", err)
c.JSON(http.StatusBadRequest, gin.H{"error": "解析请求参数发生错误"})
return
}
// 获取通知参数
outTradeNo := c.Request.Form.Get("out_trade_no")
// 验证签名
if err := client.VerifySign(c.Request.Form); err != nil {
log.Println("回调验证签名发生错误", err)
c.JSON(http.StatusBadRequest, gin.H{"error": "回调验证签名发生错误"})
return
}
log.Println("回调验证签名通过")
// 查询订单状态
var p = alipay.TradeQuery{}
p.OutTradeNo = outTradeNo
rsp, err := client.TradeQuery(p)
if err != nil {
log.Printf("验证订单 %s 信息发生错误: %s", outTradeNo, err.Error())
c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("验证订单 %s 信息发生错误: %s", outTradeNo, err.Error())})
return
}
if rsp.IsFailure() {
log.Printf("验证订单 %s 信息发生错误: %s-%s", outTradeNo, rsp.Msg, rsp.SubMsg)
c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("验证订单 %s 信息发生错误: %s-%s", outTradeNo, rsp.Msg, rsp.SubMsg)})
return
}
c.JSON(http.StatusOK, gin.H{"message": fmt.Sprintf("订单 %s 支付成功", outTradeNo)})
}
func notify(c *gin.Context) {
// 解析请求参数
if err := c.Request.ParseForm(); err != nil {
log.Println("解析请求参数发生错误", err)
c.JSON(http.StatusBadRequest, gin.H{"error": "解析请求参数发生错误"})
return
}
// 解析异步通知
notification, err := client.DecodeNotification(c.Request.Form)
if err != nil {
log.Println("解析异步通知发生错误", err)
c.JSON(http.StatusBadRequest, gin.H{"error": "解析异步通知发生错误"})
return
}
log.Println("解析异步通知成功:", notification.NotifyId)
// 查询订单状态
var p = alipay.NewPayload("alipay.trade.query")
p.AddBizField("out_trade_no", notification.OutTradeNo)
var rsp *alipay.TradeQueryRsp
if err := client.Request(p, &rsp); err != nil {
log.Printf("异步通知验证订单 %s 信息发生错误: %s", notification.OutTradeNo, err.Error())
c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("异步通知验证订单 %s 信息发生错误: %s", notification.OutTradeNo, err.Error())})
return
}
if rsp.IsFailure() {
log.Printf("异步通知验证订单 %s 信息发生错误: %s-%s", notification.OutTradeNo, rsp.Msg, rsp.SubMsg)
c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("异步通知验证订单 %s 信息发生错误: %s-%s", notification.OutTradeNo, rsp.Msg, rsp.SubMsg)})
return
}
log.Printf("订单 %s 支付成功", notification.OutTradeNo)
client.ACKNotification(c.Writer)
}
第二步去支付宝开放平台下载自己的三个密钥证书以及APPID
支付宝开放平台链接
扫码登录
进入控制台
下滑到最底部点击沙箱
第三步在main.go中修改配置信息
(1)修改私钥和KAppId
Go
const (
kAppId = "9021000134674475"
//私钥
kPrivateKey = "MIIEpAIBAAKCAQEAzSHBDVo52MqoAMvn1KFY7iuzFEUHDp/Iya3vK5s6Qp1N9hxvxwbN5Y3CjtyapDyPwBNGFYpxsoDFUOoH55vc5Z0mTxWAI3a9R1WSrL3cAAi7/2DIf0CHk9RCnzOJWGOY8luNY3iEHl2hdn0qRqBSryTKwD5znedpowphTdIDeXHBzEhkSGTAQ5+VDT9H7j3Fka6BMgmMaNvrmMh/23y6ZEYKvFncTJOBWeHcWX5wdONle1RDEC2YL9hkxZhjC3wIpMwDgDLA3J+Bx8nNhuVJW+eTV6iVaXy4HVojgoIYYyniT64VwFRIYsp9ArCKbWWJz+IIueaZe0eSiOfsH/hIuQIDAQABAoIBAG0/N8h7r+kkbPsXnV0hnPIjNdlqtcp1EINQbdsEba/ChR24oVMW7Zr+t4pMNNOZKVm7Kg0Y0Vqmt+9S5GjWGnvzCM7/fWHlwAJ4sYfsCXm/LRhiHAWhgVMarWyKDvCTAoa4eYFrUrm1VeN734MDqWV7q7tcx0hmUV0yjwXZ/l2zYkiyN5z6ElqNDI7gfKNEBmlCm42qwOEs6Xc3So2+LTSI+fpkLoI0FBqsj0AJ6nKc2gzQzTkfZ0MNY9hd//tCpz8FINxiO3z/ZcKDU90Ug3QIVxXEg/XLEYySPoetaCzaG+Qnrkz4JHBXObppObGmbff+7eConipHMPubCb81xzECgYEA9OyYmy7K+ibsdARgwEweV8KzVYCWND3O0KC/73eNOJi4thqDMV9RVJWp3CgFIVR1c1z3Gf4ZctO9jC/GvmPI72DWc/K9NyrHTGTnZhIVEQy85c9/sdm2SByTOoBWF9OwPlhlP+meOGmMxECvgL3dlFiGUoIYMu7JXNkF/eiS2F0CgYEA1mh+2qSZ6wtuW+F7Z/T4PhKgzWH4by1oOdMc4BDnA4+rw/Yp1emBlnIFr5TbTaQmS+HNl6mXsHyZiZ1/2FNVDPIaKU3nbkEVQ33LYMV/qPUixxH5ArO+btUHDPac7U4hBoxt/xdVTRqjV57x59+/eFHokQ542dekDe12u4orvA0CgYEA06uyS9NJ6p0/Fxk9gJOIWbJUCJ4DsgyEs3tpxYxdyT77y63oGgBVqrNZxIH1xRNYGiaLC7S1y65BD2mu6pYUbwUS8cwkSQw4h8Zg0a7o8/49NqN4qX0Ncw73EsUdIqR5mbgNcQn3Cqv10xpQUwRD/lPvalkyjrod3pdU/VZFqqUCgYEAlYenAMFO/xenzbiBoky8XD4Nm286Hf9i8wEC4FmpY1htiGtDXgZN9ChtQrjkAU/7zWwbQCxsuA8N3WXUZYAojrDCAhBs2P3W4Iz4q/LRR3o+2Or16SaGJzG2ixnq861LZw/FHuSc/vsW8+LL/tSS6kzlj1Syf+reZUA5UC3atnkCgYBfQjNAhDpUJQKpt+9KgdqjBa6YUtVxeFWQF9Ves9Rdk6KSE+mWounV4u3Tq/FUjDaHUOdyfiV+Q5Vrs9yQdwrWRTnMGFCTM6zfJEoU19ahtXaodoeaDpefOZhQU7Nfnv1r/xjlAP/LZsPACPIXPayKK+UWN1/ay2Jq7jp4yWna7Q=="
// TODO 设置回调地址域名
kServerDomain = ""
)
将私钥 和 KAppId改成自己的
(2)将下载好的应用公钥、支付宝公钥证书、支付宝根证书复制到文件夹下
(3)修改main.go里支付宝里证书的名称
Go
// 加载应用公钥证书
if err = client.LoadAppCertPublicKeyFromFile("自己公钥证书的文件名"); err != nil {
log.Println("加载证书发生错误", err)
return
}
// 加载支付宝根证书
if err = client.LoadAliPayRootCertFromFile("自己支付宝根证书的文件名"); err != nil {
log.Println("加载证书发生错误", err)
return
}
// 加载支付宝公钥证书
if err = client.LoadAlipayCertPublicKeyFromFile("自己支付宝公钥证书的文件名"); err != nil {
log.Println("加载证书发生错误", err)
return
}
结束
在控制台的沙箱操作时可以查看自己的沙箱账号用于模拟收付款