pet-house/business/api/pay.go

111 lines
4.2 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 api
import (
"crypto/x509"
"encoding/json"
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/context"
"github.com/wechatpay-apiv3/wechatpay-go/core"
"github.com/wechatpay-apiv3/wechatpay-go/core/auth/verifiers"
"github.com/wechatpay-apiv3/wechatpay-go/core/notify"
"github.com/wechatpay-apiv3/wechatpay-go/services/payments"
"github.com/wechatpay-apiv3/wechatpay-go/services/payments/jsapi"
utils2 "github.com/wechatpay-apiv3/wechatpay-go/utils"
"go.uber.org/zap"
"io"
"pet-house.com/business/models"
"pet-house.com/business/utils"
"pet-house.com/core/server/database"
"pet-house.com/core/server/web"
"pet-house.com/core/server/web/web_iris"
"pet-house.com/core/server/zap_server"
"strconv"
)
// 充值挡位列表
func (p DefParty) rechargeInfoList() web_iris.Party {
return web_iris.Party{Prefix: p.Prefix, PartyFunc: func(index iris.Party) {
index.Post(PayBase+"/rechargeInfoList", func(ctx *context.Context) {
var rechargeInfoList []models.RechargeInfo
database.Instance().Model(&models.RechargeInfo{}).Where("type = 1 and status = 1").Find(&rechargeInfoList)
Success(ctx, nil, rechargeInfoList)
})
}}
}
type ToPayRequest struct {
PayId int //支付ID
}
type ToPayResponse struct {
Content *jsapi.PrepayWithRequestPaymentResponse `json:"content"`
PayOrderId string
}
// 充值
func (p DefParty) toPay() web_iris.Party {
return web_iris.Party{Prefix: p.Prefix, PartyFunc: func(index iris.Party) {
index.Post(PayBase+"/toPay", func(ctx *context.Context) {
headerBaseInfo := GetHeaderBaseInfo(ctx)
body, _ := io.ReadAll(ctx.Request().Body)
var toPayRequest ToPayRequest
json.Unmarshal(body, &toPayRequest)
var userInfo *models.User
database.Instance().Model(&models.User{}).Where("id = ?", headerBaseInfo.Uid).Find(&userInfo)
var rechargeInfo *models.RechargeInfo
database.Instance().Model(&models.RechargeInfo{}).Where("id = ?", toPayRequest.PayId).Find(&rechargeInfo)
svc := jsapi.JsapiApiService{Client: utils.GetWxPayService()}
price, _ := strconv.ParseInt(rechargeInfo.Price, 10, 64)
orderId := NextId.Generate().String()
// 得到prepay_id以及调起支付所需的参数和签名
resp, _, err := svc.PrepayWithRequestPayment(ctx,
jsapi.PrepayRequest{
Appid: core.String(utils.CONFIG.AppId),
Mchid: core.String(utils.CONFIG.MchId),
Description: core.String(rechargeInfo.Name),
OutTradeNo: core.String(orderId),
NotifyUrl: core.String(web.CONFIG.System.Domain + "/pet-house/pay/payNotify"),
Amount: &jsapi.Amount{
Total: core.Int64(price * 100),
},
Payer: &jsapi.Payer{
Openid: core.String(userInfo.OpenId),
},
},
)
if err != nil {
PayError.DefFail(ctx, toPayRequest, nil)
return
}
payOrder := models.PayOrder{
OrderId: orderId,
OrderName: rechargeInfo.Name,
OrderPrice: rechargeInfo.Price,
PayId: "",
}
database.Instance().Model(&models.PayOrder{}).Create(&payOrder)
Success(ctx, toPayRequest, ToPayResponse{Content: resp, PayOrderId: orderId})
})
}}
}
// 充值通知
func (p DefParty) payNotify() web_iris.Party {
return web_iris.Party{Prefix: p.Prefix, PartyFunc: func(index iris.Party) {
index.Post(PayBase+"/payNotify", func(ctx *context.Context) {
wechatPayCert, _ := utils2.LoadCertificate(utils.CONFIG.WechatCert)
// 2. 使用本地管理的微信支付平台证书获取微信支付平台证书访问器
certificateVisitor := core.NewCertificateMapWithList([]*x509.Certificate{wechatPayCert})
// 3. 使用apiv3 key、证书访问器初始化 `notify.Handler`
handler := notify.NewNotifyHandler(utils.CONFIG.MchAPIv3Key, verifiers.NewSHA256WithRSAVerifier(certificateVisitor))
transaction := new(payments.Transaction)
notifyReq, err := handler.ParseNotifyRequest(ctx, ctx.Request(), transaction)
zap_server.ZAPLOG.Info("payNotify", zap.Any("notifyReq", notifyReq), zap.Any("transaction", transaction), zap.Any("err", err))
var payOrder *models.PayOrder
database.Instance().Model(&models.PayOrder{}).Where("order_id = ?", transaction.OutTradeNo).Find(&payOrder)
payOrder.Status = 1
database.Instance().Model(&models.PayOrder{}).Updates(&payOrder)
})
}}
}