package api import ( "crypto/x509" "encoding/json" "fmt" "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 OrderId string //订单号 } 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) price := 0.0 desc := "充值" id := 0 if len(toPayRequest.OrderId) == 0 { var rechargeInfo *models.RechargeInfo database.Instance().Model(&models.RechargeInfo{}).Where("id = ?", toPayRequest.PayId).Find(&rechargeInfo) price1, _ := strconv.ParseFloat(rechargeInfo.Price, 64) price = price1 desc = rechargeInfo.Name id = rechargeInfo.Id } svc := jsapi.JsapiApiService{Client: utils.GetWxPayService()} var payPrice = core.Int64(int64(price * 100.0)) orderId := NextId.Generate().String() //支付订单 if len(toPayRequest.OrderId) > 0 { var orderSubList []models.OrderSub database.Instance().Model(&models.OrderSub{}).Where("main_order_id = ? ", toPayRequest.OrderId).Find(&orderSubList) if len(orderSubList) == 0 { OrderExistError.Fail(ctx, toPayRequest) return } //取出折扣价 price1 := 0.0 for _, orderSub := range orderSubList { price1 = price1 + float64(orderSub.PayAmount) } var orderMain models.OrderMain database.Instance().Model(&models.OrderMain{}).Where("order_id = ? and status = 1", toPayRequest.OrderId).Find(&orderMain) if len(orderMain.PayOrderId) > 0 { var payOrder *models.PayOrder database.Instance().Model(&models.PayOrder{}).Where("order_id = ?", orderMain.PayOrderId).Find(&payOrder) if payOrder.OrderStatus == 1 { //去除定金扣除金额 price1 = price1 - (30.0 * 10) } } payPrice = core.Int64(int64(price1 * 10.0)) desc = "宠物洗护付款-" + toPayRequest.OrderId orderId = toPayRequest.OrderId price = price1 / 10.0 } // 得到prepay_id,以及调起支付所需的参数和签名 resp, _, err := svc.PrepayWithRequestPayment(ctx, jsapi.PrepayRequest{ Appid: core.String(utils.CONFIG.AppId), Mchid: core.String(utils.CONFIG.MchId), Description: core.String(desc), OutTradeNo: core.String(orderId), NotifyUrl: core.String(web.CONFIG.System.Domain + "/pet-house/pay/payNotify"), Amount: &jsapi.Amount{ Total: payPrice, }, Payer: &jsapi.Payer{ Openid: core.String(userInfo.OpenId), }, }, ) if err != nil { PayError.DefFail(ctx, toPayRequest, nil) return } payOrder := models.PayOrder{ OrderId: orderId, OrderName: desc, OrderPrice: fmt.Sprintf("%.1f", price), RechargeId: 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 Q0EwHhcNMjQwOTEwMDc0NDUyWhcNMjkwOTA5MDc0NDUyWjBuMRgwFgYDVQQDDA9U ZW5wYXkuY29tIHNpZ24xEzARBgNVBAoMClRlbnBheS5jb20xHTAbBgNVBAsMFFRl bnBheS5jb20gQ0EgQ2VudGVyMQswCQYDVQQGEwJDTjERMA8GA1UEBwwIU2hlblpo ZW4wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCnZzIY5opDpRJQU6KX xtwVtyvq3u20pQgjUSYy4HxbEWbyTujrgEm+d6CrJrey0wLa9RBOwQMOuyYKbx3Y t+WNxYnrkBDv5bbJtSTv6wA2/sINbFFSIP9GAiIAdGP/ii0BXjxG1pxGhrPDHW1Q vSzJphyHO9s3gBDBNUW8BT/XxoLhxlOrbYNV+kv4Mr/oh9U+/KsNT1Ba1Pt8s4As AQOJ7oE7xHzzUOgoFSyU+RvuYhIx6ZV0gTkZDmzu5cwj+FlCaxYFYnKPtxm5m9hj eM39rTUl5jmsZXRQUyI9Ez1AkwVOjolbrJ3f5kpkpXoJvesfowOL53DfS/th+x3I 12QzAgMBAAGjgbkwgbYwCQYDVR0TBAIwADALBgNVHQ8EBAMCA/gwgZsGA1UdHwSB kzCBkDCBjaCBiqCBh4aBhGh0dHA6Ly9ldmNhLml0cnVzLmNvbS5jbi9wdWJsaWMv aXRydXNjcmw/Q0E9MUJENDIyMEU1MERCQzA0QjA2QUQzOTc1NDk4NDZDMDFDM0U4 RUJEMiZzZz1IQUNDNDcxQjY1NDIyRTEyQjI3QTlEMzNBODdBRDFDREY1OTI2RTE0 MDM3MTANBgkqhkiG9w0BAQsFAAOCAQEAgL/Bfpy6kS6qCBZycuGvwdqYWU9exf1a ZfbG8eop1ADUD5j6LjZcBrLyaVZDNZMRafqDiWI1OBypEtAxC3MScN3B0rxiJ3uQ LHn/YOel6DKKJdMjANC36NpseC/PXZrdMMdCFLzQuc2FcG2ObOhRKcfTRnR/zdcU Tw88WZqCW0thBcOXcQ9e7R2fIk+W4YqQ7C1O1/17qDpViPOTGWWnJBnUYpi5lTlX JIhayMjkemIO27X1j1ZdtA314xZpPrO4uqrVFiLahoZ9y6az0RhNpPadYcT6D24G 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)) state := *transaction.TradeState OrderStatus := 0 if "SUCCESS" == state { OrderStatus = 1 } else if "REFUND" == state { OrderStatus = 2 } else { zap_server.ZAPLOG.Info("payNotify", zap.Any("notifyReq", notifyReq), zap.Any("transaction", transaction), zap.Any("state", state)) return } var payOrder *models.PayOrder database.Instance().Model(&models.PayOrder{}).Where("order_id = ?", transaction.OutTradeNo).Find(&payOrder) payOrderD := map[string]interface{}{ "OrderStatus": OrderStatus, "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) } else { var orderMain models.OrderMain database.Instance().Model(&models.OrderMain{}).Where("order_id = ? and status = 1", payOrder.OrderId).Find(&orderMain) //设置订单状态 orderMain.PayStatus = 1 database.Instance().Save(&orderMain) } }) }} }