Browse Source

Merge remote-tracking branch 'origin/master'

# Conflicts:
#	go.mod
master
huangjiajun 1 month ago
parent
commit
72c0444b0a
5 changed files with 124 additions and 266 deletions
  1. +0
    -261
      app/lib/alipay/api.go
  2. +74
    -0
      app/lib/alipay/init.go
  3. +41
    -0
      app/lib/alipay/utils_test.go
  4. +0
    -1
      app/pkg/pb/business.ext.pb.go
  5. +9
    -4
      go.mod

+ 0
- 261
app/lib/alipay/api.go View File

@@ -1,261 +0,0 @@
package alipay

import (
"applet/app/cfg"
"applet/app/utils/logx"
"fmt"

"github.com/iGoogle-ink/gopay"
"github.com/iGoogle-ink/gopay/alipay"
)

// TradeAppPay is 支付宝APP支付
// 抖音头条小程序使用APP调起
func TradeAppPay(appID, priKey, subject, orderID, amount, notiURL, RSA, PKCS string, paySet *md.PayData) (string, error) {
//初始化支付宝客户端
// appID 是在支付宝申请的APPID
// priKey 是支付宝私钥
// subject 是支付订单的主题
// orderID 是智莺这边生成的订单id
// amount 是付费金额
// notiURL 通知地址url
// passback_params 回调通知参数

client := alipay.NewClient(appID, priKey, true)
client.DebugSwitch = gopay.DebugOn
//判断密钥的类型
rsa_type := alipay.RSA2
pkcs_type := alipay.PKCS1
if RSA == "1" {
rsa_type = alipay.RSA
}
if PKCS == "1" {
pkcs_type = alipay.PKCS8
}
if paySet.PayAliUseType == "1" {
rsa_type = alipay.RSA2
pkcs_type = alipay.PKCS8
}
//配置公共参数
client.SetCharset("utf-8").
SetSignType(rsa_type).
SetPrivateKeyType(pkcs_type)
if notiURL != "" {
client.SetNotifyUrl(notiURL)
}
//新支付宝支付
if paySet.PayAliUseType == "1" {
appCertSN, err := alipay.GetCertSN(cfg.WxappletFilepath.URL + "/" + paySet.PayAppCertSn)
fmt.Println("-应用-")
fmt.Println(appCertSN)
if err != nil {
fmt.Println(err)
return "", err
}
if appCertSN == "" {
fmt.Println(err)
return "", err
}
client.SetAppCertSN(appCertSN)
//aliPayRootCertSN, err := alipay.GetCertSN(cfg.WxappletFilepath.URL + "/" + paySet.PayAlipayRootCertSn)
aliPayRootCertSN := "687b59193f3f462dd5336e5abf83c5d8_02941eef3187dddf3d3b83462e1dfcf6"
client.SetAliPayRootCertSN(aliPayRootCertSN)
aliPayPublicCertSN, err := alipay.GetCertSN(cfg.WxappletFilepath.URL + "/" + paySet.PayAlipayrsaPublicKey)
fmt.Println("-公钥-")
fmt.Println(aliPayPublicCertSN)

if err != nil {
fmt.Println(err)
return "", err
}
if aliPayPublicCertSN == "" {
fmt.Println(err)
return "", err
}
client.SetAliPayPublicCertSN(aliPayPublicCertSN)
}
fmt.Println(client)
//请求参数
body := make(gopay.BodyMap)
body.Set("subject", subject)
body.Set("body", subject)
body.Set("out_trade_no", orderID)
body.Set("total_amount", amount)
body.Set("timeout_express", "30m")

// body.Set("passback_params", orderID)
//手机APP支付参数请求
payParam, err := client.TradeAppPay(body)
if err != nil {
return "", logx.Warn(err)
}
return payParam, nil
}

// TradeAppPay is 支付宝H5支付
func TradeWapPay(appID, priKey, subject, orderID, amount, notiURL, RSA, PKCS, page_url string, paySet *md.PayData) (string, error) {
fmt.Println("notifyURL is:>>>>>>>>>>", notiURL)
//aliPayPublicKey := "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA1wn1sU/8Q0rYLlZ6sq3enrPZw2ptp6FecHR2bBFLjJ+sKzepROd0bKddgj+Mr1ffr3Ej78mLdWV8IzLfpXUi945DkrQcOUWLY0MHhYVG2jSs/qzFfpzmtut2Cl2TozYpE84zom9ei06u2AXLMBkU6VpznZl+R4qIgnUfByt3Ix5b3h4Cl6gzXMAB1hJrrrCkq+WvWb3Fy0vmk/DUbJEz8i8mQPff2gsHBE1nMPvHVAMw1GMk9ImB4PxucVek4ZbUzVqxZXphaAgUXFK2FSFU+Q+q1SPvHbUsjtIyL+cLA6H/6ybFF9Ffp27Y14AHPw29+243/SpMisbGcj2KD+evBwIDAQAB"
privateKey := priKey
//判断密钥的类型
rsa_type := alipay.RSA2
pkcs_type := alipay.PKCS1
if RSA == "1" {
rsa_type = alipay.RSA
}
if PKCS == "1" {
pkcs_type = alipay.PKCS8
}
if paySet.PayAliUseType == "1" {
rsa_type = alipay.RSA2
pkcs_type = alipay.PKCS8
}
//初始化支付宝客户端
// appId:应用ID
// privateKey:应用秘钥
// isProd:是否是正式环境
client := alipay.NewClient(appID, privateKey, true)
//配置公共参数
client.SetCharset("utf-8").
SetSignType(rsa_type).
SetPrivateKeyType(pkcs_type).
SetReturnUrl(page_url).
SetNotifyUrl(notiURL)
//新支付宝支付
if paySet.PayAliUseType == "1" {
appCertSN, err := alipay.GetCertSN(cfg.WxappletFilepath.URL + "/" + paySet.PayAppCertSn)
if err != nil {
fmt.Println("appCertSn_err:>>>>>>>>", err)
return "", err
}
if appCertSN == "" {
fmt.Println(err)
return "", err
}
client.SetAppCertSN(appCertSN)
//aliPayRootCertSN, err := alipay.GetCertSN(cfg.WxappletFilepath.URL + "/" + paySet.PayAlipayRootCertSn)
//if err != nil {
//
// fmt.Println("rootcertsn_err:>>>>>>>>>>", err)
// fmt.Println("rootcertsn_err:>>>>>>>>>>", cfg.WxappletFilepath.URL)
// fmt.Println("rootcertsn_err:>>>>>>>>>>", paySet.PayAlipayRootCertSn)
// return "", err
//}
//if aliPayRootCertSN == "" {
// fmt.Println(err)
// return "", err
//}
aliPayRootCertSN := "687b59193f3f462dd5336e5abf83c5d8_02941eef3187dddf3d3b83462e1dfcf6"
client.SetAliPayRootCertSN(aliPayRootCertSN)
aliPayPublicCertSN, err := alipay.GetCertSN(cfg.WxappletFilepath.URL + "/" + paySet.PayAlipayrsaPublicKey)
if err != nil {
fmt.Println("publicCertSn_err:>>>>>>>>>>>", err)
return "", err
}
if aliPayPublicCertSN == "" {
fmt.Println(err)
return "", err
}
client.SetAliPayPublicCertSN(aliPayPublicCertSN)
}
//请求参数
body := make(gopay.BodyMap)
body.Set("subject", subject)
body.Set("out_trade_no", orderID)
// quit_url is 用户付款中途退出返回商户网站的地址
body.Set("quit_url", notiURL)
body.Set("total_amount", amount)
// product_code is 销售产品码,商家和支付宝签约的产品码
body.Set("product_code", "QUICK_WAP_WAY")
//手机网站支付请求
payUrl, err := client.TradeWapPay(body)
if err != nil {
return "", logx.Warn(err)

}
return payUrl, nil
}

// TradeAppPay is 支付宝小程序本身支付
func TradeCreate(appID, priKey, subject, orderID, amount, notiURL, RSA, PKCS string, paySet *md.PayData) (*alipay.TradeCreateResponse, error) {
//aliPayPublicKey := "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA1wn1sU/8Q0rYLlZ6sq3enrPZw2ptp6FecHR2bBFLjJ+sKzepROd0bKddgj+Mr1ffr3Ej78mLdWV8IzLfpXUi945DkrQcOUWLY0MHhYVG2jSs/qzFfpzmtut2Cl2TozYpE84zom9ei06u2AXLMBkU6VpznZl+R4qIgnUfByt3Ix5b3h4Cl6gzXMAB1hJrrrCkq+WvWb3Fy0vmk/DUbJEz8i8mQPff2gsHBE1nMPvHVAMw1GMk9ImB4PxucVek4ZbUzVqxZXphaAgUXFK2FSFU+Q+q1SPvHbUsjtIyL+cLA6H/6ybFF9Ffp27Y14AHPw29+243/SpMisbGcj2KD+evBwIDAQAB"
privateKey := priKey
rsa_type := alipay.RSA2
pkcs_type := alipay.PKCS1
if RSA == "1" {
rsa_type = alipay.RSA
}
if PKCS == "1" {
pkcs_type = alipay.PKCS8
}
if paySet.PayAliUseType == "1" {
rsa_type = alipay.RSA2
pkcs_type = alipay.PKCS8
}
//初始化支付宝客户端
// appId:应用ID
// privateKey:应用私钥,支持PKCS1和PKCS8
// isProd:是否是正式环境
client := alipay.NewClient(appID, privateKey, true)
//配置公共参数
client.SetCharset("utf-8").
SetSignType(rsa_type).
SetPrivateKeyType(pkcs_type).
SetNotifyUrl(notiURL)
if paySet.PayAliUseType == "1" {
appCertSN, err := alipay.GetCertSN(cfg.WxappletFilepath.URL + "/" + paySet.PayAppCertSn)
if err != nil {
fmt.Println(err)
return nil, err
}
if appCertSN == "" {
fmt.Println(err)
return nil, err
}
client.SetAppCertSN(appCertSN)
//aliPayRootCertSN, err := alipay.GetCertSN(cfg.WxappletFilepath.URL + "/" + paySet.PayAlipayRootCertSn)
//if err != nil {
// fmt.Println(err)
// return nil, err
//}
//if aliPayRootCertSN == "" {
// fmt.Println(err)
// return nil, err
//}
aliPayRootCertSN := "687b59193f3f462dd5336e5abf83c5d8_02941eef3187dddf3d3b83462e1dfcf6"
client.SetAliPayRootCertSN(aliPayRootCertSN)
aliPayPublicCertSN, err := alipay.GetCertSN(cfg.WxappletFilepath.URL + "/" + paySet.PayAlipayrsaPublicKey)
if err != nil {
fmt.Println(err)
return nil, err
}
if aliPayPublicCertSN == "" {
fmt.Println(err)
return nil, err
}
client.SetAliPayPublicCertSN(aliPayPublicCertSN)
}
//请求参数
body := make(gopay.BodyMap)
body.Set("subject", subject)
// 支付宝小程序支付时 buyer_id 为必传参数,需要提前获取,获取方法如下两种
// 1、alipay.SystemOauthToken() 返回取值:rsp.SystemOauthTokenResponse.UserId
// 2、client.SystemOauthToken() 返回取值:aliRsp.SystemOauthTokenResponse.UserId
buyer_id, err := client.SystemOauthToken(body)
if err != nil {
return nil, logx.Warn(err)
}
body.Set("buyer_id", buyer_id)
body.Set("out_trade_no", orderID)
body.Set("total_amount", amount)
//创建订单
aliRsp, err := client.TradeCreate(body)

if err != nil {
return nil, logx.Warn(err)
}
logx.Warn("aliRsp:", *aliRsp)
logx.Warn("aliRsp.TradeNo:", aliRsp.Response.TradeNo)
return aliRsp, nil

}

+ 74
- 0
app/lib/alipay/init.go View File

@@ -0,0 +1,74 @@
package alipay

import (
"applet/app/db"
"applet/app/utils/cache"
"code.fnuoos.com/EggPlanet/egg_models.git/src/implement"
"code.fnuoos.com/EggPlanet/egg_system_rules.git/enum"
"github.com/go-pay/gopay/alipay"
"github.com/iGoogle-ink/gopay"
"github.com/iGoogle-ink/gopay/pkg/xlog"
)

type InitStruct struct {
IsProd bool `json:"is_prod" label:"是否生产环境"`
AlipayAppId string `json:"alipay_app_id" label:"支付宝商家应用appid"`
AlipayPrivateKey string `json:"alipay_private_key" label:"支付宝商家应用私钥"`
AlipayPublicKey string `json:"alipay_public_key" label:"支付宝商家应用公钥"`
AlipayPublicContentRSA2 []byte `json:"alipay_public_content_rsa_2" label:"支付宝公钥证书"`
AlipayRootContent []byte `json:"alipay_root_content" label:"支付宝根证书"`
AppPublicContent []byte `json:"app_public_content" label:"应用公钥证书"`
}

// Init 初始化支付宝客户端
// appid:应用ID
// privateKey:应用私钥,支持PKCS1和PKCS8
// isProd:是否是正式环境,沙箱环境请选择新版沙箱应用。
func Init(initData *InitStruct) (client *alipay.Client, err error) {
if initData == nil {
sysCfgDb := implement.NewSysCfgDb(db.Db, cache.GetPool().Get())
sysCfgMap := sysCfgDb.SysCfgFindWithDb(enum.AlipayAppId, enum.AlipayPrivateKey, enum.AlipayPublicKey, enum.AlipayPublicContentRSA2, enum.AlipayRootContent, enum.AppPublicContent)
initData = &InitStruct{
IsProd: true,
AlipayAppId: sysCfgMap[enum.AlipayAppId],
AlipayPrivateKey: sysCfgMap[enum.AlipayPrivateKey],
AlipayPublicKey: sysCfgMap[enum.AlipayPublicKey],
AlipayPublicContentRSA2: []byte(sysCfgMap[enum.AlipayPublicContentRSA2]),
AlipayRootContent: []byte(sysCfgMap[enum.AlipayRootContent]),
AppPublicContent: []byte(sysCfgMap[enum.AppPublicContent]),
}
}

client, err = alipay.NewClient(initData.AlipayAppId, initData.AlipayPrivateKey, initData.IsProd)
if err != nil {
xlog.Error(err)
return
}

// 自定义配置http请求接收返回结果body大小,默认 10MB
client.SetBodySize(10) // 没有特殊需求,可忽略此配置

// 打开Debug开关,输出日志,默认关闭
client.DebugSwitch = gopay.DebugOn

client.SetLocation(alipay.LocationShanghai). // 设置时区,不设置或出错均为默认服务器时间
SetCharset(alipay.UTF8). // 设置字符编码,不设置默认 utf-8
SetSignType(alipay.RSA2) // 设置签名类型,不设置默认 RSA2

// SetAppAuthToken("") //授权token

//SetReturnUrl("https://www.fmm.ink"). // 设置返回URL
// SetNotifyUrl("https://www.fmm.ink"). // 设置异步通知URL

// 传入 支付宝公钥证书 alipayPublicCert.crt 内容
client.AutoVerifySign(initData.AlipayPublicContentRSA2)

// 传入证书内容
err = client.SetCertSnByContent(initData.AppPublicContent, initData.AlipayRootContent, initData.AlipayPublicContentRSA2)
if err != nil {
xlog.Debug("SetCertSn:", err)
return
}

return
}

+ 41
- 0
app/lib/alipay/utils_test.go
File diff suppressed because it is too large
View File


+ 0
- 1
app/pkg/pb/business.ext.pb.go View File

@@ -1406,7 +1406,6 @@ func (x *SearchUserResp) ProtoReflect() protoreflect.Message {
return mi.MessageOf(x)
}


// Deprecated: Use SearchUserResp.ProtoReflect.Descriptor instead.
func (*SearchUserResp) Descriptor() ([]byte, []int) {
return file_business_ext_proto_rawDescGZIP(), []int{19}


+ 9
- 4
go.mod View File

@@ -39,6 +39,7 @@ require (
code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git v0.0.5
github.com/360EntSecGroup-Skylar/excelize v1.4.1
github.com/aliyun/aliyun-oss-go-sdk v3.0.2+incompatible
github.com/go-pay/gopay v1.5.106
github.com/go-sql-driver/mysql v1.8.1
github.com/gocolly/colly v1.2.0
github.com/olivere/elastic/v7 v7.0.32
@@ -67,6 +68,9 @@ require (
github.com/go-openapi/jsonreference v0.20.0 // indirect
github.com/go-openapi/spec v0.20.11 // indirect
github.com/go-openapi/swag v0.19.15 // indirect
github.com/go-pay/crypto v0.0.1 // indirect
github.com/go-pay/xlog v0.0.3 // indirect
github.com/go-pay/xtime v0.0.2 // indirect
github.com/gobwas/glob v0.2.3 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
@@ -83,6 +87,7 @@ require (
github.com/leodido/go-urn v1.4.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-sqlite3 v2.0.3+incompatible // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
@@ -100,14 +105,14 @@ require (
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
golang.org/x/arch v0.7.0 // indirect
golang.org/x/crypto v0.22.0 // indirect
golang.org/x/crypto v0.27.0 // indirect
golang.org/x/lint v0.0.0-20200302205851-738671d3881b // indirect
golang.org/x/net v0.24.0 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.26.0 // indirect
golang.org/x/text v0.15.0 // indirect
golang.org/x/text v0.18.0 // indirect
golang.org/x/time v0.0.0-20191024005414-555d28b269f0 // indirect
golang.org/x/tools v0.20.0 // indirect
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
google.golang.org/appengine v1.4.0 // indirect
google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55 // indirect


Loading…
Cancel
Save