elf 6 months ago
parent 6e62d1a84a
commit e91ae2eb70

@ -1,8 +1,10 @@
package h5
import (
"fmt"
"github.com/gin-gonic/gin"
"gold-shop/errors"
"gold-shop/global"
"gold-shop/request"
"gold-shop/service"
"gold-shop/utils/result"
@ -80,6 +82,7 @@ func GetPayments(c *gin.Context) (result.Data, error) {
func Payment(c *gin.Context) (result.Data, error) {
var paymentRequest request.PaymentRequest
err := c.ShouldBind(&paymentRequest)
fmt.Println(paymentRequest)
if err != nil {
return nil, errors.NewBusinessError("参数错误")
}
@ -116,7 +119,13 @@ func Order(c *gin.Context) (result.Data, error) {
}
func GetWxAuthUrl(c *gin.Context) (result.Data, error) {
authUrl := weixin.WxApi.GetAuthUrl("http://mall.wrtcjt.com")
amount := c.DefaultQuery("amount", "")
redirectUri := global.Config.Payment.WxPay.AuthRedirectUri
if amount != "" {
redirectUri += "?amount=" + amount
}
authUrl := weixin.WxApi.GetAuthUrl(redirectUri)
fmt.Println(authUrl)
data := result.Data{"url": authUrl}
return data, nil
}
@ -127,7 +136,7 @@ func GetWxOpenId(c *gin.Context) (result.Data, error) {
if err != nil {
return nil, err
}
data := result.Data{"openid": res.Openid}
data := result.Data{"openId": res.Openid}
return data, nil
}
@ -149,6 +158,18 @@ func TransferPayNotify(c *gin.Context) {
return
}
func PaymentNotify(c *gin.Context) {
var req request.PaymentNotifyRequest
err := c.ShouldBindQuery(&req)
if err != nil {
c.String(http.StatusOK, "fail")
return
}
service.PaymentService.AfterPaymentNotify(req)
c.String(http.StatusOK, "success")
return
}
func TLPay(c *gin.Context) {
param := tlpay.PayParam{}
param.PayType = "B2B"

@ -23,6 +23,7 @@ payment:
api-key: "24114801"
sign-key: "88819264"
wx-pay:
auth-redirect-uri: "http://mall.wrtcjt.com/pages/wx/callback"
sub-app-id: "wx939c26e4e09ce9ee"
sub-app-secret: "0d365a525ec9bf2c055d735ced98a6c1"
jwt:

@ -10,4 +10,5 @@ type Payment struct {
type WxPay struct {
SubAppId string `yaml:"sub-app-id"`
SubAppSecret string `yaml:"sub-app-secret"`
AuthRedirectUri string `yaml:"auth-redirect-uri"`
}

Binary file not shown.

BIN
goshop

Binary file not shown.

@ -0,0 +1,18 @@
package request
type PaymentNotifyRequest struct {
OrderID string `form:"orderid"`
TradeNo string `form:"trade_no"`
DisName string `form:"dis_name"`
PayStatus int `form:"paystatus"`
PayMoney float64 `form:"paymoney"`
PriPayMoney float64 `form:"pri_paymoney"`
OrderTime int64 `form:"order_time"`
PayTime int64 `form:"paytime"`
MchOrderID string `form:"mch_orderid"`
NotifyUrl string `form:"notify_url"`
BuyerAccount string `form:"buyer_account"`
Attach string `form:"attach"`
Sign string `form:"sign"`
SignType string `form:"sign_type"`
}

@ -14,6 +14,7 @@ func h5RouteInit(r *gin.Engine) {
h5Group.Match([]string{"POST", "OPTIONS"}, "/register", result.Json(h5.Register))
h5Group.Match([]string{"GET", "OPTIONS"}, "/get-product-info", result.Json(h5.GetProductInfo))
h5Group.Match([]string{"POST", "OPTIONS"}, "/transfer-pay-notify", h5.TransferPayNotify)
h5Group.Match([]string{"POST", "OPTIONS"}, "/payment-notify", h5.PaymentNotify)
h5Group.Match([]string{"GET", "OPTIONS"}, "/tl-pay", h5.TLPay)
authGroup := h5Group.Group("")

@ -2,6 +2,7 @@ package service
import (
e "errors"
"fmt"
"github.com/xuri/excelize/v2"
"gold-shop/errors"
"gold-shop/global"
@ -25,7 +26,7 @@ func (paymentService) GetUserPayments(userID, page, pageSize int) ([]model.Payme
payments := make([]model.Payment, pageSize)
offset := (page - 1) * pageSize
var total int64
global.DB.Where("user_id", userID).Offset(offset).Limit(pageSize).Find(&payments)
global.DB.Where("user_id", userID).Order("id desc").Offset(offset).Limit(pageSize).Find(&payments)
global.DB.Model(&model.Payment{}).Where("user_id", userID).Count(&total)
return payments, total
}
@ -37,7 +38,7 @@ func (s paymentService) Payment(userID int, paymentRequest request.PaymentReques
}
data := result.Data{}
if paymentRequest.PayType == "wxOfficial" {
if paymentRequest.PayType == "wxgzh" {
data, err = s.wxPayment(paymentRequest, p)
} else if paymentRequest.PayType == "transfer" {
data, err = s.transferPayment(paymentRequest, p)
@ -53,7 +54,7 @@ func (s paymentService) Payment(userID int, paymentRequest request.PaymentReques
p.Status = 1
global.DB.Save(p)
fmt.Println(data)
return data, nil
}
@ -63,7 +64,7 @@ func (s paymentService) wxPayment(paymentRequest request.PaymentRequest, p *mode
param.OrderId = utils.GenerateNo("payment")
param.OpenId = paymentRequest.OpenID
param.Amount = paymentRequest.Amount
param.NotifyUrl = ""
param.NotifyUrl = "http://api.wrtcjt.com/payment-notify"
res, err := payment.PayApi.WxPay(param)
if err != nil {
return nil, err
@ -206,3 +207,21 @@ func (s paymentService) AfterTransferPayNotify(req request.TransferPayNotifyRequ
return err
}
func (s paymentService) AfterPaymentNotify(req request.PaymentNotifyRequest) error {
pm := &model.Payment{}
err := global.DB.Where("payment_no", req.MchOrderID).First(&pm).Error
if err != nil && e.Is(err, gorm.ErrRecordNotFound) {
return errors.NewBusinessError("记录不存在")
}
if req.PayStatus == 1 {
pm.Status = 1
} else {
pm.Status = 2
}
err = global.DB.Save(&pm).Error
return err
}

@ -9,6 +9,7 @@ import (
"io"
"net/http"
"net/url"
"strconv"
"strings"
)
@ -44,6 +45,31 @@ func (api *payApi) WxPay(param WxPayParam) (*WxPayResult, error) {
return &result, nil
}
func (api *payApi) QueryOrder(param QueryOrderParam) (*QueryOrderResult, error) {
payment := global.Config.Payment
data := url.Values{}
data.Set("service", "pay.comm.query_order")
data.Set("apikey", payment.ApiKey)
data.Set("orderid", param.OrderID)
data.Set("mch_orderid", param.MchOrderID)
data.Set("nonce_str", utils.GenerateRandomString(32))
data.Set("order_time", strconv.Itoa(int(param.OrderTime)))
data.Set("dis_name", param.DisName)
sign := api.sign(data, payment.SignKey)
data.Set("sign", sign)
fmt.Println(data.Encode())
res, err := api.post(payment.BaseUrl+"/payapi/pay/query_order", data)
if err != nil {
return nil, err
}
result := QueryOrderResult{}
err = json.Unmarshal(res, &result)
if err != nil {
return nil, errors.NewBusinessError("结果解析错误")
}
return &result, nil
}
func (api *payApi) TransferPay(param TransferPayParam) (*TransferPayResult, error) {
payment := global.Config.Payment
data := url.Values{}

@ -0,0 +1,8 @@
package payment
type QueryOrderParam struct {
OrderID string
DisName string
MchOrderID string
OrderTime int64
}

@ -0,0 +1,25 @@
package payment
type QueryOrderResult struct {
OrderID string `form:"orderid"`
TradeNo string `form:"trade_no"`
DisName string `form:"dis_name"`
PayStatus int `form:"paystatus"`
PayMoney float64 `form:"paymoney"`
PriPayMoney float64 `form:"pri_paymoney"`
OrderTime int64 `form:"order_time"`
PayTime int64 `form:"paytime"`
MchOrderID string `form:"mch_orderid"`
NotifyUrl string `form:"notify_url"`
BuyerAccount string `form:"buyer_account"`
Attach string `form:"attach"`
ReceiptAmount float64 `form:"receipt_amount"`
BuyerPayAmount float64 `form:"buyer_pay_amount"`
CouponFee float64 `form:"coupon_fee"`
Version string `form:"version"`
Charset string `form:"charset"`
Message string `form:"message"`
Status string `form:"status"`
Sign string `form:"sign"`
SignType string `form:"sign_type"`
}

@ -29,10 +29,11 @@ func (api wxApi) GetAccessToken(code string) (*AccessTokenResult, error) {
}
result := AccessTokenResult{}
err = json.Unmarshal(body, &result)
fmt.Println(result)
if err != nil {
return nil, errors.NewBusinessError("数据解析错误")
}
if result.ErrorCode != 200 {
if result.ErrorCode != 0 && result.ErrorCode != 200 {
return nil, errors.NewBusinessError(result.ErrorMessage)
}
return &result, nil

Loading…
Cancel
Save