pet-house/business/api/pay.go

170 lines
7.0 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 `json:"payOrderId"`
PayType int `json:"payType"` //支付类型 0在线支付 1会员余额支付
}
// 充值
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.ParseFloat(rechargeInfo.Price, 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(int64(price * 100.0)),
},
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,
RechargeId: rechargeInfo.Id,
PayId: "",
Uid: headerBaseInfo.Uid,
}
database.Instance().Model(&models.PayOrder{}).Create(&payOrder)
Success(ctx, toPayRequest, ToPayResponse{Content: resp, PayOrderId: orderId, PayType: 0})
})
}}
}
// 充值通知
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) {
var cert = `-----BEGIN CERTIFICATE-----
MIIEFDCCAvygAwIBAgIUXfrnoWdj8YwsOsPDsHsB8Exd/nQwDQYJKoZIhvcNAQEL
BQAwXjELMAkGA1UEBhMCQ04xEzARBgNVBAoTClRlbnBheS5jb20xHTAbBgNVBAsT
FFRlbnBheS5jb20gQ0EgQ2VudGVyMRswGQYDVQQDExJUZW5wYXkuY29tIFJvb3Qg
Q0EwHhcNMjQwOTEwMDc0NDUyWhcNMjkwOTA5MDc0NDUyWjBuMRgwFgYDVQQ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jkemIO27X1j1ZdtA314xZpPrO4uqrVFiLahoZ9y6az0RhNpPadYcT6D24G
2boO3JsJZ1X/MNiW78pfDUra7a+yEoH+JjiPNtaw9ztbtKtDcLIgWQ==
-----END CERTIFICATE-----`
wechatPayCert, err1 := utils2.LoadCertificate(cert)
if err1 != nil {
panic(err1)
}
// 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)
payOrderD := map[string]interface{}{
"OrderStatus": 1,
"PayId": *transaction.TransactionId,
}
database.Instance().Model(&payOrder).Updates(&payOrderD)
if payOrder.RechargeId > 1 {
var userInfo *models.User
database.Instance().Model(&models.User{}).Where("id = ?", payOrder.Uid).Find(&userInfo)
var rechargeInfo *models.RechargeInfo
database.Instance().Model(&models.RechargeInfo{}).Where("id = ?", payOrder.RechargeId).Find(&rechargeInfo)
rechargePrice, _ := strconv.ParseInt(rechargeInfo.RechargePrice, 10, 64)
//充值
userAmountRecord := models.UserAmountRecord{
UserId: payOrder.Uid,
Type: 1,
OriginAmount: userInfo.Amount,
ProAmount: int(rechargePrice),
CurrAmount: userInfo.Amount + int(rechargePrice),
OrderId: payOrder.OrderId,
ProScene: "充值",
Status: 1,
}
database.Instance().Model(&models.UserAmountRecord{}).Create(&userAmountRecord)
userInfo.Amount = userInfo.Amount + (int(rechargePrice) * 10)
updateValues := map[string]interface{}{
"Amount": userInfo.Amount,
}
database.Instance().Model(&userInfo).Updates(&updateValues)
}
})
}}
}