Golang Gin接入支付宝H5网页支付

Posted supramolecular

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Golang Gin接入支付宝H5网页支付相关的知识,希望对你有一定的参考价值。

安装依赖

go get github.com/smartwalle/alipay/v3

发起账单接口

func FinishAliPayment(c *gin.Context) 
	appG := app.GinC: c
	client, _ := alipay.New(setting.AppSetting.AliAppId, setting.AppSetting.AliPrivateKey, false)
	client.LoadAppPublicCertFromFile("conf/appCertPublicKey_2016091400512715.crt") // 加载应用公钥证书
	client.LoadAliPayRootCertFromFile("conf/alipayRootCert.crt")                   // 加载支付宝根证书
	client.LoadAliPayPublicCertFromFile("conf/alipayCertPublicKey_RSA2.crt")       // 加载支付宝公钥证书
	var p = alipay.TradeWapPay
	p.NotifyURL = setting.AppSetting.Notify
	p.ReturnURL = "http://test.yptech.tv"
	p.Subject = "*****"
	p.OutTradeNo = "46464613131314464"
	p.TotalAmount = "0.01"
	p.ProductCode = "QUICK_WAP_PAY"
	url, err := client.TradeWapPay(p)
	if err != nil 
		fmt.Println("pay client.TradeAppPay error:", err)
		return
	
	//logging.Info(url)
	binary, _ := url.MarshalBinary()
	fmt.Println(string(binary))
	data := make(map[string]interface)
	data["url"] = string(binary)
	appG.Response(http.StatusOK, 200, data)

回调接口(接收通知接口)

func AliPayNotify(c *gin.Context) 
	client, _ := alipay.New(setting.AppSetting.AliAppId, setting.AppSetting.AliPrivateKey, false)
	client.LoadAppPublicCertFromFile("conf/appCertPublicKey_2016091400512715.crt") // 加载应用公钥证书
	client.LoadAliPayRootCertFromFile("conf/alipayRootCert.crt")                   // 加载支付宝根证书
	client.LoadAliPayPublicCertFromFile("conf/alipayCertPublicKey_RSA2.crt")       // 加载支付宝公钥证书
	fmt.Println(c.Request.Body)
	var noti, _ = client.GetTradeNotification(c.Request)
	if noti != nil 
		fmt.Println("交易状态为:", noti.TradeStatus)
	
	bs, _ := json.Marshal(noti)
	var out bytes.Buffer
	json.Indent(&out, bs, "", "\\t")
	fmt.Printf("data=%v\\n", out.String())
	c.String(http.StatusOK, "%s", "success")

参考文献:

https://opendocs.alipay.com/open/203/105286https://opendocs.alipay.com/open/203/105286

https://github.com/smartwalle/alipayhttps://github.com/smartwalle/alipay

https://zhuanlan.zhihu.com/p/315591452https://zhuanlan.zhihu.com/p/315591452

以上是关于Golang Gin接入支付宝H5网页支付的主要内容,如果未能解决你的问题,请参考以下文章

Golang Gin接入支付宝H5网页支付

微信H5网页公众号外支付接入攻略demo

用h5开发的app想接入支付宝,应该去支付宝开放平台申请哪种支付方式?app支付还是手机网站支付?

H5网站接入支付宝的支付接口

支付宝怎么导出详细账单 支付宝详细账单导出的方法

Android WebView中的H5支付实践