You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
79 lines
1.9 KiB
79 lines
1.9 KiB
package handler
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"io"
|
|
"mime"
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/gin-gonic/gin/binding"
|
|
|
|
"laic-backend/common"
|
|
"laic-backend/model"
|
|
"laic-backend/service"
|
|
"laic-backend/vo"
|
|
)
|
|
|
|
func CreateWechatPayment(c *gin.Context) {
|
|
var req vo.PaymentCreateReq
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
common.FailWithBindError(c, common.ErrParam, err)
|
|
return
|
|
}
|
|
data, e := service.DefaultPaymentService.CreateWechatPayment(common.GetUserId(c), &req)
|
|
if e != nil {
|
|
common.FailWithBusiError(c, e)
|
|
return
|
|
}
|
|
common.OKWithData(c, data)
|
|
}
|
|
|
|
func GetPaymentTransaction(c *gin.Context) {
|
|
id, e := parseID(c)
|
|
if e != nil {
|
|
common.FailWithBusiError(c, e)
|
|
return
|
|
}
|
|
data, e := service.DefaultPaymentService.GetTransaction(common.GetUserId(c), id)
|
|
if e != nil {
|
|
common.FailWithBusiError(c, e)
|
|
return
|
|
}
|
|
common.OKWithData(c, data)
|
|
}
|
|
|
|
func AGPayCallback(c *gin.Context) {
|
|
contentType, _, err := mime.ParseMediaType(c.GetHeader("Content-Type"))
|
|
if err != nil || contentType != "application/json" {
|
|
c.Status(http.StatusBadRequest)
|
|
return
|
|
}
|
|
c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, 1<<10)
|
|
body, err := c.GetRawData()
|
|
if err != nil {
|
|
c.Status(http.StatusBadRequest)
|
|
return
|
|
}
|
|
var callback model.AGPayCallback
|
|
decoder := json.NewDecoder(bytes.NewReader(body))
|
|
decoder.DisallowUnknownFields()
|
|
if err := decoder.Decode(&callback); err != nil || decoder.Decode(&struct{}{}) != io.EOF {
|
|
c.Status(http.StatusBadRequest)
|
|
return
|
|
}
|
|
if err := binding.Validator.ValidateStruct(&callback); err != nil {
|
|
c.Status(http.StatusBadRequest)
|
|
return
|
|
}
|
|
if err := service.DefaultPaymentService.ConfirmAGPayCallback(callback.TradeNo, body); err != nil {
|
|
if err == common.ErrInternal {
|
|
c.Status(http.StatusInternalServerError)
|
|
return
|
|
}
|
|
c.Status(http.StatusBadRequest)
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{"code": "SUCCESS"})
|
|
}
|
|
|