增加了单项订单的接口

This commit is contained in:
2024-07-23 11:41:00 +08:00
parent dfda2e2425
commit 8c84ed3656
24 changed files with 1181 additions and 165 deletions
+43
View File
@@ -0,0 +1,43 @@
package weChat
import (
"context"
"errors"
"github.com/wechatpay-apiv3/wechatpay-go/core"
"github.com/wechatpay-apiv3/wechatpay-go/core/option"
"github.com/wechatpay-apiv3/wechatpay-go/utils"
"hepa-calc-api/config"
"path/filepath"
)
// 创建客户端
func createClient() (*core.Client, error) {
mchId := config.C.Wechat.Pay1659662936.MchId // 商户号
mchCertificateSerialNumber := config.C.Wechat.Pay1659662936.MchCertificateSerialNumber // 商户证书序列号
v3ApiSecret := config.C.Wechat.Pay1659662936.V3ApiSecret // 商户APIv3密钥
if mchId == "" {
return nil, errors.New("商户号错误")
}
// 使用 utils 提供的函数从本地文件中加载商户私钥,商户私钥会用来生成请求的签名
certsDir := filepath.Join("extend/weChat/certs", mchId, "/apiclient_key.pem")
// certsDir := filepath.Join("extend/weChat/certs", "/1636644248/apiclient_key.pem")
mchPrivateKey, err := utils.LoadPrivateKeyWithPath(certsDir)
if err != nil {
return nil, errors.New("微信签名生成失败")
}
ctx := context.Background()
// 使用商户私钥等初始化 client,并使它具有自动定时获取微信支付平台证书的能力
opts := []core.ClientOption{
option.WithWechatPayAutoAuthCipher(mchId, mchCertificateSerialNumber, mchPrivateKey, v3ApiSecret),
}
client, err := core.NewClient(ctx, opts...)
if err != nil {
return nil, errors.New(err.Error())
}
return client, nil
}
+133
View File
@@ -0,0 +1,133 @@
package weChat
import (
"context"
"errors"
"github.com/wechatpay-apiv3/wechatpay-go/core"
"github.com/wechatpay-apiv3/wechatpay-go/services/payments/app"
"github.com/wechatpay-apiv3/wechatpay-go/services/payments/jsapi"
)
/**
JSAPI下单
APP下单
*/
// JsapiRequest 请求数据-JSAPI下单
type JsapiRequest struct {
AppId string `json:"appid" comment:"公众号ID"`
MchId string `json:"mchid" comment:"直连商户号"`
Description string `json:"description" comment:"商品描述"`
OutTradeNo string `json:"out_trade_no" comment:"商户订单号"`
NotifyUrl string `json:"notify_url" comment:"回调地址"`
Amount JsapiRequestAmountRequest `json:"amount" comment:"订单金额"`
Payer JsapiRequestPayerRequest `json:"payer" comment:"支付者"`
}
// JsapiRequestAmountRequest 订单金额信息
type JsapiRequestAmountRequest struct {
Total int64 `json:"total" comment:"订单总金额"`
Currency string `json:"currency" comment:"货币类型"`
}
// JsapiRequestPayerRequest 支付者信息
type JsapiRequestPayerRequest struct {
OpenId string `json:"openid" comment:"openid"`
}
// AppRequest 请求数据-APP下单
type AppRequest struct {
AppId string `json:"appid" comment:"公众号ID"`
MchId string `json:"mchid" comment:"直连商户号"`
Description string `json:"description" comment:"商品描述"`
OutTradeNo string `json:"out_trade_no" comment:"商户订单号"`
NotifyUrl string `json:"notify_url" comment:"回调地址"`
Amount AppRequestAmountRequest `json:"amount" comment:"订单金额"`
}
// AppRequestAmountRequest 订单金额信息
type AppRequestAmountRequest struct {
Total int64 `json:"total" comment:"订单总金额"`
Currency string `json:"currency" comment:"货币类型"`
}
// GetJsapiPrepay JSAPI下单-获取jsapi预支付交易会话
func (r JsapiRequest) GetJsapiPrepay() (prepay *jsapi.PrepayWithRequestPaymentResponse, err error) {
client, err := createClient()
if err != nil {
return nil, err
}
svc := jsapi.JsapiApiService{Client: client}
// 得到prepay_id,以及调起支付所需的参数和签名
resp, result, err := svc.PrepayWithRequestPayment(context.Background(),
jsapi.PrepayRequest{
Appid: core.String(r.AppId),
Mchid: core.String(r.MchId),
Description: core.String(r.Description),
OutTradeNo: core.String(r.OutTradeNo),
NotifyUrl: core.String(r.NotifyUrl),
Amount: &jsapi.Amount{
Total: core.Int64(r.Amount.Total),
Currency: core.String("CNY"),
},
Payer: &jsapi.Payer{
Openid: core.String(r.Payer.OpenId),
},
},
)
if err != nil {
return nil, err
}
if result.Response.StatusCode != 200 {
return nil, errors.New("发起支付失败")
}
if resp.PrepayId == nil {
return nil, errors.New("发起支付失败")
}
return resp, nil
}
// GetAppPrepay APP下单-获取app预支付交易会话
func (r AppRequest) GetAppPrepay() (prepay *app.PrepayWithRequestPaymentResponse, err error) {
client, err := createClient()
if err != nil {
return nil, err
}
svc := app.AppApiService{Client: client}
// 得到prepay_id,以及调起支付所需的参数和签名
resp, result, err := svc.PrepayWithRequestPayment(context.Background(),
app.PrepayRequest{
Appid: core.String(r.AppId),
Mchid: core.String(r.MchId),
Description: core.String(r.Description),
OutTradeNo: core.String(r.OutTradeNo),
NotifyUrl: core.String(r.NotifyUrl),
Amount: &app.Amount{
Total: core.Int64(r.Amount.Total),
Currency: core.String("CNY"),
},
},
)
if err != nil {
return nil, err
}
if result.Response.StatusCode != 200 {
return nil, errors.New("发起支付失败")
}
if resp.PrepayId == nil {
return nil, errors.New("发起支付失败")
}
return resp, nil
}
+46
View File
@@ -0,0 +1,46 @@
package weChat
import (
"context"
"github.com/wechatpay-apiv3/wechatpay-go/core"
"github.com/wechatpay-apiv3/wechatpay-go/services/refunddomestic"
)
type RefundRequest struct {
TransactionId string `json:"transaction_id" comment:"微信订单号"`
OutTradeNo string `json:"out_trade_no" comment:"商户订单号"`
OutRefundNo string `json:"out_refund_no" comment:"退款订单号"`
Reason string `json:"reason" comment:"退款原因"`
RefundAmount int64 `json:"refund_amount" comment:"退款金额"`
PaymentAmountTotal int64 `json:"payment_amount_total" comment:"支付金额"`
NotifyUrl string `json:"notify_url" comment:"回调地址"`
}
// Refund 退款
func (r RefundRequest) Refund() (*refunddomestic.Refund, error) {
client, err := createClient()
if err != nil {
return nil, err
}
refundRequest := refunddomestic.CreateRequest{
TransactionId: core.String(r.TransactionId),
OutTradeNo: core.String(r.OutTradeNo),
OutRefundNo: core.String(r.OutRefundNo),
Reason: core.String(r.Reason),
NotifyUrl: core.String(r.NotifyUrl),
Amount: &refunddomestic.AmountReq{
Currency: core.String("CNY"),
Refund: core.Int64(r.RefundAmount),
Total: core.Int64(r.PaymentAmountTotal),
},
}
svc := refunddomestic.RefundsApiService{Client: client}
resp, _, err := svc.Create(context.Background(), refundRequest)
if err != nil {
return nil, err
}
return resp, nil
}
-79
View File
@@ -1,79 +0,0 @@
package weChat
import (
"context"
"errors"
"github.com/wechatpay-apiv3/wechatpay-go/core"
"github.com/wechatpay-apiv3/wechatpay-go/core/option"
"github.com/wechatpay-apiv3/wechatpay-go/services/refunddomestic"
"github.com/wechatpay-apiv3/wechatpay-go/utils"
"hepa-calc-api/config"
"path/filepath"
)
type RefundRequest struct {
TransactionId string `json:"transaction_id" comment:"微信订单号"`
OutTradeNo string `json:"out_trade_no" comment:"商户订单号"`
OutRefundNo string `json:"out_refund_no" comment:"退款订单号"`
Reason string `json:"reason" comment:"退款原因"`
RefundAmount int64 `json:"refund_amount" comment:"退款金额"`
PaymentAmountTotal int64 `json:"payment_amount_total" comment:"支付金额"`
NotifyUrl string `json:"notify_url" comment:"回调地址"`
}
// Refund 退款
func (r RefundRequest) Refund() (*refunddomestic.Refund, error) {
mchId := ""
mchCertificateSerialNumber := ""
v3ApiSecret := ""
mchId = config.C.Wechat.Pay1636644248.MchId
mchCertificateSerialNumber = config.C.Wechat.Pay1636644248.MchCertificateSerialNumber
v3ApiSecret = config.C.Wechat.Pay1636644248.V3ApiSecret
if mchId == "" {
return nil, errors.New("商户号错误")
}
// certsDir := "extend/weChat/certs/" + mchId + "/apiclient_key.pem"
// 使用 utils 提供的函数从本地文件中加载商户私钥,商户私钥会用来生成请求的签名
certsDir := filepath.Join("extend/weChat/certs", mchId, "/apiclient_key.pem")
// certsDir := filepath.Join("extend/weChat/certs", "/1636644248/apiclient_key.pem")
mchPrivateKey, err := utils.LoadPrivateKeyWithPath(certsDir)
if err != nil {
return nil, errors.New("微信签名生成失败")
}
ctx := context.Background()
// 使用商户私钥等初始化 client,并使它具有自动定时获取微信支付平台证书的能力
opts := []core.ClientOption{
option.WithWechatPayAutoAuthCipher(mchId, mchCertificateSerialNumber, mchPrivateKey, v3ApiSecret),
}
client, err := core.NewClient(ctx, opts...)
if err != nil {
return nil, errors.New(err.Error())
}
refundRequest := refunddomestic.CreateRequest{
TransactionId: core.String(r.TransactionId),
OutTradeNo: core.String(r.OutTradeNo),
OutRefundNo: core.String(r.OutRefundNo),
Reason: core.String(r.Reason),
NotifyUrl: core.String(r.NotifyUrl),
Amount: &refunddomestic.AmountReq{
Currency: core.String("CNY"),
Refund: core.Int64(r.RefundAmount),
Total: core.Int64(r.PaymentAmountTotal),
},
}
svc := refunddomestic.RefundsApiService{Client: client}
resp, _, err := svc.Create(ctx, refundRequest)
if err != nil {
return nil, errors.New(err.Error())
}
return resp, nil
}