Browse Source

add 支付包

master
dengbiao 5 days ago
parent
commit
38e6619ff7
3 changed files with 118 additions and 2 deletions
  1. +73
    -0
      app/lib/gopay/init_alipay.go
  2. +41
    -0
      app/lib/gopay/utils_test.go
  3. +4
    -2
      go.mod

+ 73
- 0
app/lib/gopay/init_alipay.go View File

@@ -0,0 +1,73 @@
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"
"github.com/go-pay/gopay/alipay"
"github.com/go-pay/xlog"
)

type InitAlipayStruct 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 InitAlipay(initData *InitAlipayStruct) (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 = &InitAlipayStruct{
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/gopay/utils_test.go
File diff suppressed because it is too large
View File


+ 4
- 2
go.mod View File

@@ -1,10 +1,11 @@
module applet

go 1.19
go 1.21

toolchain go1.23.2

//replace code.fnuoos.com/EggPlanet/egg_models.git => E:/company/Egg/egg_models

//
//replace code.fnuoos.com/EggPlanet/egg_system_rules.git => E:/company/Egg/egg_system_rules

require (
@@ -41,6 +42,7 @@ require (
github.com/gin-contrib/sessions v1.0.1
github.com/go-pay/crypto v0.0.1
github.com/go-pay/gopay v1.5.98
github.com/go-pay/xlog v0.0.3
github.com/go-pay/xtime v0.0.2
github.com/go-sql-driver/mysql v1.8.1
github.com/gocolly/colly v1.2.0


Loading…
Cancel
Save