1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
| package main
import ( "context" "errors" "fmt" "log" "net/http" "os" "os/signal" "syscall" "time"
"github.com/gin-gonic/gin" "github.com/skip2/go-qrcode" "github.com/smartwalle/alipay/v3" "github.com/spf13/viper" )
var ( cfg Config client *alipay.Client )
type Config struct { AppId string `mapstructure:"AppId"` PrivateKey string `mapstructure:"PrivateKey"` AliPublicKey string `mapstructure:"AliPublicKey"` NotifyURL string `mapstructure:"NotifyURL"` ReturnURL string `mapstructure:"ReturnURL"` }
func init() { v := viper.New() v.SetConfigFile("config.yaml") if err := v.ReadInConfig(); err != nil { panic(err) } if err := v.Unmarshal(&cfg); err != nil { panic(err) } client, _ = alipay.New(cfg.AppId, cfg.PrivateKey, false) if err := client.LoadAliPayPublicKey(cfg.AliPublicKey); err != nil { panic(err) } }
func main() { r := gin.Default() r.GET("/sitePay", SitePay) r.GET("/facePay", FacePay) r.POST("/notify", Notify) r.GET("/callback", Callback) srv := &http.Server{ Addr: fmt.Sprintf(":%d", 8080), Handler: r, } go func() { log.Println("服务器启动成功!") if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) { panic(err) } }()
quit := make(chan os.Signal, 1) signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM) <-quit log.Println("服务器正在关闭...") ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() if err := srv.Shutdown(ctx); err != nil { panic(err) } log.Println("服务器已关闭.") }
func SitePay(c *gin.Context) { pay := alipay.TradePagePay{ Trade: alipay.Trade{ Subject: "测试订单balabala", OutTradeNo: fmt.Sprintf("%d", time.Now().Unix()), TotalAmount: "100.00", ProductCode: "FAST_INSTANT_TRADE_PAY", NotifyURL: cfg.NotifyURL, ReturnURL: cfg.ReturnURL, }, } url, err := client.TradePagePay(pay) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{ "code": 500, "msg": err.Error(), }) } c.Redirect(http.StatusTemporaryRedirect, url.String()) }
func FacePay(c *gin.Context) { pay := alipay.TradePreCreate{ Trade: alipay.Trade{ Subject: "测试订单balabala", OutTradeNo: fmt.Sprintf("%d", time.Now().Unix()), TotalAmount: "100.00", ProductCode: "FACE_TO_FACE_PAYMENT", NotifyURL: cfg.NotifyURL, }, } resp, err := client.TradePreCreate(c, pay) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{ "code": 500, "msg": err.Error(), }) } qrCode, err := qrcode.New(resp.QRCode, qrcode.Medium) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{ "code": 500, "msg": err.Error(), }) }
qrCode.DisableBorder = true png, err := qrCode.PNG(256) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to generate QR code image"}) return }
c.Data(http.StatusOK, "image/png", png) }
func Callback(c *gin.Context) { _ = c.Request.ParseForm() err := client.VerifySign(c.Request.Form) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{ "code": 500, "msg": err.Error(), }) return } _, err = c.Writer.Write([]byte("<h1>成功支付的界面,可由商家自由定制</h1>")) if err != nil { panic(err) } }
func Notify(c *gin.Context) { _ = c.Request.ParseForm() fmt.Println(c.Request.Form.Get("trade_no")) fmt.Println(c.Request.Form.Get("out_trade_no")) fmt.Println(c.Request.Form.Get("total_amount")) fmt.Println(c.Request.Form.Get("subject")) fmt.Println(c.Request.Form.Get("trade_status")) c.JSON(http.StatusOK, gin.H{ "code": 200, "msg": "ok", }) }
|