104 lines
3.3 KiB
Go
104 lines
3.3 KiB
Go
package weChat
|
||
|
||
import (
|
||
"case-open-api/config"
|
||
"context"
|
||
"errors"
|
||
"github.com/wechatpay-apiv3/wechatpay-go/core"
|
||
"github.com/wechatpay-apiv3/wechatpay-go/core/option"
|
||
"github.com/wechatpay-apiv3/wechatpay-go/services/payments"
|
||
"github.com/wechatpay-apiv3/wechatpay-go/services/refunddomestic"
|
||
"github.com/wechatpay-apiv3/wechatpay-go/utils"
|
||
)
|
||
|
||
// 创建客户端
|
||
func createClient() (*core.Client, error) {
|
||
mchId := config.C.Wechat.Pay1281030301.MchId // 商户号
|
||
mchCertificateSerialNumber := config.C.Wechat.Pay1281030301.MchCertificateSerialNumber // 商户证书序列号
|
||
v3ApiSecret := config.C.Wechat.Pay1281030301.V3ApiSecret // 商户APIv3密钥
|
||
privateKeyPath := config.C.Wechat.Pay1281030301.PrivateKey // 商户私钥文件地址
|
||
|
||
if mchId == "" {
|
||
return nil, errors.New("商户号错误")
|
||
}
|
||
|
||
mchPrivateKey, err := utils.LoadPrivateKeyWithPath(privateKeyPath)
|
||
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
|
||
}
|
||
|
||
// WxPayResult 支付结果
|
||
type WxPayResult struct {
|
||
OrderStatus int `json:"order_status"` // 订单状态(1:待支付 2:已完成 3:已取消)
|
||
PayStatus int `json:"pay_status"` // 支付状态(1:未支付 2:已支付 3:支付中 4:支付失败 5:支付超时 6:支付关闭 7:已撤销 8:转入退款)
|
||
PayTime *string `json:"pay_time"` // 支付时间
|
||
}
|
||
|
||
// WxPayRefundResult 退款结果
|
||
type WxPayRefundResult struct {
|
||
RefundStatus int `json:"refundStatus"` // 订单退款状态(0:无退款 1:申请退款 2:退款中 3:退款成功 4:拒绝退款 5:退款关闭 6:退款异常 7:部分退款)
|
||
SuccessTime *string `json:"successTime"` // 退款成功时间
|
||
}
|
||
|
||
// HandlePayStatus 处理支付状态
|
||
func HandlePayStatus(t *payments.Transaction) (w *WxPayResult, err error) {
|
||
w = &WxPayResult{}
|
||
|
||
switch *t.TradeState {
|
||
case "SUCCESS": // 支付成功
|
||
w.OrderStatus = 2
|
||
w.PayStatus = 2
|
||
w.PayTime = t.SuccessTime
|
||
case "CLOSED": // 已关闭
|
||
w.PayStatus = 6
|
||
case "REVOKED": // 已撤销(付款码支付)
|
||
w.PayStatus = 7
|
||
case "USERPAYING": // 用户支付中(付款码支付)
|
||
w.PayStatus = 3
|
||
case "PAYERROR": // 支付失败(其他原因,如银行返回失败)
|
||
w.PayStatus = 4
|
||
default:
|
||
return nil, errors.New("未知支付状态")
|
||
}
|
||
|
||
return w, nil
|
||
}
|
||
|
||
// HandlePayRefundStatus 处理退款状态
|
||
func HandlePayRefundStatus(r *refunddomestic.Refund) (w *WxPayRefundResult, err error) {
|
||
w = &WxPayRefundResult{}
|
||
|
||
switch *r.Status {
|
||
case "SUCCESS": // 退款成功
|
||
w.RefundStatus = 3
|
||
if r.SuccessTime != nil {
|
||
successTime := r.SuccessTime.Format("2006-01-02 15:04:05")
|
||
w.SuccessTime = &successTime
|
||
}
|
||
case "CLOSED": // 退款关闭
|
||
w.RefundStatus = 5
|
||
case "PROCESSING": // 退款处理中
|
||
w.RefundStatus = 2
|
||
case "ABNORMAL": // 退款异常
|
||
return nil, errors.New("退款状态错误")
|
||
default:
|
||
return nil, errors.New("退款状态错误")
|
||
}
|
||
|
||
return w, nil
|
||
}
|