service/library/payclients/alipaycli/client.go

123 lines
3.7 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package alipaycli
import (
"context"
"fmt"
"github.com/go-pay/gopay"
"github.com/go-pay/gopay/alipay"
"net/http"
"service/bizcommon/util"
"service/library/configcenter"
"service/library/logger"
"time"
)
const (
DefaultOrderTimeoutSeconds = 900 // 默认订单超时时间,单位: s
)
var defaultAlipayClient *AlipayClient
func GetDefaultAlipayClient() *AlipayClient {
return defaultAlipayClient
}
type AlipayClient struct {
*alipay.Client
}
func Init(cfg *configcenter.AlipayClientConfig) (err error) {
alipayCli, err := alipay.NewClient(cfg.Appid, cfg.PrivateKey, true)
if err != nil {
logger.Error("NewClient fail, cfg: %v, err: %v", util.ToJson(cfg), err)
return
}
alipayCli.SetNotifyUrl(cfg.NotifyUrl)
defaultAlipayClient = &AlipayClient{
Client: alipayCli,
}
return
}
// 解析回调参数
func (c *AlipayClient) ParseNotify(req *http.Request) (notify gopay.BodyMap, err error) {
// 解析参数
notifyTmp, err := alipay.ParseNotifyToBodyMap(req)
if err != nil {
logger.Error("ParseNotifyToBodyMap fail, req: %v, err: %v", util.ToJson(req), err)
return
}
logger.Info("Alipay ParseNotify, %v", util.ToJson(notifyTmp))
// 验签
ok, err := alipay.VerifySign("MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAl9Pj5j6cNRdaDyWTWWFkcBViDqw/py1bDrqe7VykND1JywgLsFogeKGG0YhJfTfEeExAlGlP2DUV7z0uc8QhCMF7N+qdfXNb/RemFIpSs71i9kkaFIJEl20JnWipps96LwZLDpNRck3Nho/Xnbs4wuft3sQtd7T0sB2zEYyl7OyuTqehcoz3uAQp0JOYBosVPpgofaD6BN2RSZQ33QvqS37w3xHLif8P7hfYXdeIe+JxeHmchzQK22dRzJinYfU+pKE5J2Wm+dq1l3/ls4w8Xc3RyqvvW2UMx2lOMtSg2WWXDDi1skmHn/l1BbBP/uogNq4Avf50eoZ1UE6JxPrZpQIDAQAB", notifyTmp)
if err != nil {
logger.Error("VerifySign fail, bm: %v, err: %v", util.ToJson(notifyTmp), err)
return
}
if !ok {
logger.Error("VerifySign fail, not ok, bm: %v", util.ToJson(notifyTmp))
return
}
notify = notifyTmp
return
}
// 支付宝 app支付
type AppPayParam struct {
OutTradeNo string // 商家订单id我们自己的订单id
Subject string // 主题
TotalAmount int64 // 金额,单位:分
TimeOutSeconds int // 订单有效时间,单位:秒
}
func (c *AlipayClient) AppPay(ctx context.Context, param *AppPayParam) (alipayAppParamStr string, err error) {
if param.TimeOutSeconds <= 0 {
param.TimeOutSeconds = DefaultOrderTimeoutSeconds
}
bm := gopay.BodyMap{
"out_trade_no": param.OutTradeNo,
"subject": param.Subject,
"total_amount": fmt.Sprintf("%.2f", float64(param.TotalAmount)/100.0),
"time_expire": time.Now().Add(time.Second * time.Duration(param.TimeOutSeconds)).Format("2006-01-02 15:04:05"),
"notify_url": c.NotifyUrl,
}
alipayAppParamStr, err = c.TradeAppPay(ctx, bm)
if err != nil {
return
}
logger.Info("alipay TradeAppPay param: %v", alipayAppParamStr)
return
}
// 支付宝 手机web支付
type WapPayParam struct {
OutTradeNo string // 商家订单id我们自己的订单id
Subject string // 主题
TotalAmount int64 // 金额,单位:分
TimeOutSeconds int // 订单有效时间,单位:秒
ReturnUrl string // 支付宝return_url
}
func (c *AlipayClient) WapPay(ctx context.Context, param *WapPayParam) (alipayWapParamStr string, err error) {
if param.TimeOutSeconds <= 0 {
param.TimeOutSeconds = DefaultOrderTimeoutSeconds
}
bm := gopay.BodyMap{
"out_trade_no": param.OutTradeNo,
"subject": param.Subject,
"total_amount": fmt.Sprintf("%.2f", float64(param.TotalAmount)/100.0),
"time_expire": time.Now().Add(time.Second * time.Duration(param.TimeOutSeconds)).Format("2006-01-02 15:04:05"),
"notify_url": c.NotifyUrl,
}
if len(param.ReturnUrl) > 0 {
bm["return_url"] = param.ReturnUrl
}
alipayWapParamStr, err = c.TradeWapPay(ctx, bm)
if err != nil {
return
}
logger.Info("alipay TradeWapPay param: %v", alipayWapParamStr)
return
}