goProject/trunk/center/paycenter/internal/pay.go
2025-01-08 17:48:14 +08:00

217 lines
6.9 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 internal
import (
"context"
"errors"
"fmt"
"github.com/wechatpay-apiv3/wechatpay-go/core"
"github.com/wechatpay-apiv3/wechatpay-go/core/option"
"github.com/wechatpay-apiv3/wechatpay-go/services/payments/app"
"github.com/wechatpay-apiv3/wechatpay-go/utils"
"goutil/logUtilPlus"
"log"
_ "paycenter/internal/pay"
"paycenter/internal/wxpaycofnig"
_ "paycenter/internal/wxpaycofnig"
"strconv"
"time"
)
var (
mchID string = wxpaycofnig.GetWxPayConfig().MchID // 商户号
mchCertificateSerialNumber string = wxpaycofnig.GetWxPayConfig().MchCertificateSerialNumber // 商户证书序列号
mchAPIv3Key string = wxpaycofnig.GetWxPayConfig().MchAPIv3Key // 商户APIv3密钥
)
// Prepay 预支付
func Prepay(outTradeNo int64, description string) (string, error) {
// 使用 utils 提供的函数从本地文件中加载商户私钥,商户私钥会用来生成请求的签名
mchPrivateKey, err := utils.LoadPrivateKeyWithPath("/path/to/merchant/apiclient_key.pem")
if err != nil {
logUtilPlus.ErrorLog("load merchant private key error")
return "", err
}
ctx := context.Background()
// 使用商户私钥等初始化 client并使它具有自动定时获取微信支付平台证书的能力
opts := []core.ClientOption{
option.WithWechatPayAutoAuthCipher(mchID, mchCertificateSerialNumber, mchPrivateKey, mchAPIv3Key),
}
client, err := core.NewClient(ctx, opts...)
if err != nil {
logUtilPlus.ErrorLog("new wechat pay client err:%s", err)
return "", err
}
svc := app.AppApiService{Client: client}
resp, result, err := svc.Prepay(ctx,
app.PrepayRequest{
Appid: core.String("wxd678efh567hg6787"),
Mchid: core.String("1230000109"),
Description: core.String(description),
OutTradeNo: core.String(strconv.FormatInt(outTradeNo, 10)),
TimeExpire: core.Time(time.Now()),
Attach: core.String("自定义数据说明"),
NotifyUrl: core.String("https://www.weixin.qq.com/wxpay/pay.php"),
GoodsTag: core.String("WXG"),
LimitPay: []string{"LimitPay_example"},
SupportFapiao: core.Bool(false),
Amount: &app.Amount{
Currency: core.String("CNY"),
Total: core.Int64(100),
},
Detail: &app.Detail{
CostPrice: core.Int64(608800),
GoodsDetail: []app.GoodsDetail{app.GoodsDetail{
GoodsName: core.String("iPhoneX 256G"),
MerchantGoodsId: core.String("ABC"),
Quantity: core.Int64(1),
UnitPrice: core.Int64(828800),
WechatpayGoodsId: core.String("1001"),
}},
InvoiceId: core.String("wx123"),
},
SceneInfo: &app.SceneInfo{
DeviceId: core.String("013467007045764"),
PayerClientIp: core.String("14.23.150.211"),
StoreInfo: &app.StoreInfo{
Address: core.String("广东省深圳市南山区科技中一道10000号"),
AreaCode: core.String("440305"),
Id: core.String("0001"),
Name: core.String("腾讯大厦分店"),
},
},
SettleInfo: &app.SettleInfo{
ProfitSharing: core.Bool(false),
},
},
)
if err != nil {
// 处理错误
logUtilPlus.ErrorLog("call Prepay err:%s", err.Error())
return "", err
}
if result.Response.StatusCode != 200 {
errStr := fmt.Sprintf("status=%d resp=%s", result.Response.StatusCode, resp)
logUtilPlus.ErrorLog(errStr)
return "", errors.New(errStr)
}
return *resp.PrepayId, nil
}
// CloseOrder 关闭订单
func CloseOrder() {
// 使用 utils 提供的函数从本地文件中加载商户私钥,商户私钥会用来生成请求的签名
mchPrivateKey, err := utils.LoadPrivateKeyWithPath("/path/to/merchant/apiclient_key.pem")
if err != nil {
log.Print("加载商家私钥错误")
}
ctx := context.Background()
// 使用商户私钥等初始化 client并使它具有自动定时获取微信支付平台证书的能力
opts := []core.ClientOption{
option.WithWechatPayAutoAuthCipher(mchID, mchCertificateSerialNumber, mchPrivateKey, mchAPIv3Key),
}
client, err := core.NewClient(ctx, opts...)
if err != nil {
log.Printf("新的 WeChat Pay 客户端 Err:%s", err)
}
svc := app.AppApiService{Client: client}
result, err := svc.CloseOrder(ctx,
app.CloseOrderRequest{
//商户系统内部订单号只能是数字、大小写字母_-*且在同一个商户号下唯
OutTradeNo: core.String("OutTradeNo_example"),
//直连商户的商户号,由微信支付生成并下发。
Mchid: core.String("1230000109"),
},
)
if err != nil {
// 处理错误
log.Printf("call CloseOrder err:%s", err)
} else {
// 处理返回结果
log.Printf("status=%d", result.Response.StatusCode)
}
}
// QueryOrderById 根据商户订单号查询订单
func QueryOrderById() {
// 使用 utils 提供的函数从本地文件中加载商户私钥,商户私钥会用来生成请求的签名
mchPrivateKey, err := utils.LoadPrivateKeyWithPath("/path/to/merchant/apiclient_key.pem")
if err != nil {
log.Print("load merchant private key error")
}
ctx := context.Background()
// 使用商户私钥等初始化 client并使它具有自动定时获取微信支付平台证书的能力
opts := []core.ClientOption{
option.WithWechatPayAutoAuthCipher(mchID, mchCertificateSerialNumber, mchPrivateKey, mchAPIv3Key),
}
client, err := core.NewClient(ctx, opts...)
if err != nil {
log.Printf("new wechat pay client err:%s", err)
}
svc := app.AppApiService{Client: client}
resp, result, err := svc.QueryOrderById(ctx,
app.QueryOrderByIdRequest{
TransactionId: core.String("TransactionId_example"),
Mchid: core.String("Mchid_example"),
},
)
if err != nil {
// 处理错误
log.Printf("call QueryOrderById err:%s", err)
} else {
// 处理返回结果
log.Printf("status=%d resp=%s", result.Response.StatusCode, resp)
}
}
// QueryOrderByOutTradeNo 根据商户订单号查询订单
func QueryOrderByOutTradeNo() {
// 使用 utils 提供的函数从本地文件中加载商户私钥,商户私钥会用来生成请求的签名
mchPrivateKey, err := utils.LoadPrivateKeyWithPath("/path/to/merchant/apiclient_key.pem")
if err != nil {
log.Print("load merchant private key error")
}
ctx := context.Background()
// 使用商户私钥等初始化 client并使它具有自动定时获取微信支付平台证书的能力
opts := []core.ClientOption{
option.WithWechatPayAutoAuthCipher(mchID, mchCertificateSerialNumber, mchPrivateKey, mchAPIv3Key),
}
client, err := core.NewClient(ctx, opts...)
if err != nil {
log.Printf("new wechat pay client err:%s", err)
}
svc := app.AppApiService{Client: client}
resp, result, err := svc.QueryOrderByOutTradeNo(ctx,
app.QueryOrderByOutTradeNoRequest{
OutTradeNo: core.String("OutTradeNo_example"),
Mchid: core.String("Mchid_example"),
},
)
if err != nil {
// 处理错误
log.Printf("call QueryOrderByOutTradeNo err:%s", err)
} else {
// 处理返回结果
log.Printf("status=%d resp=%s", result.Response.StatusCode, resp)
}
}