103 lines
2.7 KiB
Go
103 lines
2.7 KiB
Go
package services
|
||
|
||
import (
|
||
"PaymentCenter/app/constants/common"
|
||
"PaymentCenter/app/constants/errorcode"
|
||
"PaymentCenter/app/data"
|
||
"context"
|
||
"errors"
|
||
"github.com/go-pay/gopay"
|
||
"github.com/go-pay/gopay/wechat/v3"
|
||
"github.com/go-pay/xlog"
|
||
"time"
|
||
)
|
||
|
||
func ActivityGoodsCreateService(orderId int64) (err error) {
|
||
// 获取订单信息
|
||
orderInfo, err := data.GetOrderInfoById(orderId)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
if orderInfo.PayId == 0 {
|
||
return errors.New("缺少订单信息,无法进行支付")
|
||
}
|
||
// 获取支付渠道
|
||
payChannelInfo, err := data.GetPayChannelById(orderInfo.PayId)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
|
||
switch payChannelInfo.ChannelType {
|
||
case common.PAYCHANNEL_WX_H5:
|
||
//todo 微信H5支付
|
||
wxH5PayInfo()
|
||
break
|
||
case common.PAYCHANNEL_ALI_H5:
|
||
//todo 支付宝H5支付
|
||
aLiH5PayInfo()
|
||
break
|
||
default:
|
||
return errors.New("暂不支持该支付渠道,请后续再使用")
|
||
}
|
||
return nil
|
||
}
|
||
|
||
func wxH5PayInfo(c context.Context) {
|
||
// NewClientV3 初始化微信客户端 v3
|
||
// mchid:商户ID 或者服务商模式的 sp_mchid
|
||
// serialNo:商户证书的证书序列号
|
||
// apiV3Key:apiV3Key,商户平台获取
|
||
// privateKey:私钥 apiclient_key.pem 读取后的内容
|
||
client, err := wechat.NewClientV3(MchId, SerialNo, APIv3Key, PrivateKey)
|
||
if err != nil {
|
||
return
|
||
}
|
||
|
||
// 设置微信平台API证书和序列号(推荐开启自动验签,无需手动设置证书公钥等信息)
|
||
//client.SetPlatformCert([]byte(""), "")
|
||
|
||
// 启用自动同步返回验签,并定时更新微信平台API证书(开启自动验签时,无需单独设置微信平台API证书和序列号)
|
||
err = client.AutoVerifySign()
|
||
if err != nil {
|
||
return
|
||
}
|
||
|
||
// 自定义配置http请求接收返回结果body大小,默认 10MB
|
||
client.SetBodySize(10) // 没有特殊需求,可忽略此配置
|
||
|
||
// 打开Debug开关,输出日志,默认是关闭的
|
||
client.DebugSwitch = gopay.DebugOn
|
||
expire := time.Now().Add(10 * time.Minute).Format(time.RFC3339)
|
||
// 初始化 BodyMap
|
||
bm := make(gopay.BodyMap)
|
||
bm.Set("sp_appid", "sp_appid").
|
||
Set("sp_mchid", "sp_mchid").
|
||
Set("sub_mchid", "sub_mchid").
|
||
Set("description", "测试Jsapi支付商品").
|
||
Set("out_trade_no", tradeNo).
|
||
Set("time_expire", expire).
|
||
Set("notify_url", "https://www.fmm.ink").
|
||
SetBodyMap("amount", func(bm gopay.BodyMap) {
|
||
bm.Set("total", 1).
|
||
Set("currency", "CNY")
|
||
}).
|
||
SetBodyMap("payer", func(bm gopay.BodyMap) {
|
||
bm.Set("sp_openid", "asdas")
|
||
})
|
||
|
||
wxRsp, err := client.V3TransactionH5(c, bm)
|
||
if err != nil {
|
||
xlog.Error(err)
|
||
return
|
||
}
|
||
if wxRsp.Code == errorcode.Success {
|
||
xlog.Debugf("wxRsp: %#v", wxRsp.Response)
|
||
return
|
||
}
|
||
xlog.Errorf("wxRsp:%s", wxRsp.Error)
|
||
}
|
||
|
||
func aLiH5PayInfo() {
|
||
|
||
}
|