70 lines
2.1 KiB
Go
70 lines
2.1 KiB
Go
package controller
|
|
|
|
import (
|
|
"context"
|
|
"video-factory/shortdrama/middleware"
|
|
"video-factory/shortdrama/model/dto"
|
|
"video-factory/shortdrama/service"
|
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
)
|
|
|
|
type payment struct{}
|
|
|
|
var Payment = new(payment)
|
|
|
|
func (c *payment) Prepay(ctx context.Context, req *dto.PrepayReq) (res *dto.PrepayRes, err error) {
|
|
r := g.RequestFromCtx(ctx)
|
|
userId := middleware.GetUserId(r)
|
|
|
|
orderType := req.OrderType
|
|
if orderType == "" {
|
|
orderType = "recharge"
|
|
}
|
|
order, codeUrl, prepayJson, redirectUrl, err := service.PaymentService.Prepay(ctx, userId, req.Amount, req.Channel, orderType, req.TierId, req.Duration)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &dto.PrepayRes{
|
|
OrderNo: order.OrderNo,
|
|
Channel: order.Channel,
|
|
CodeUrl: codeUrl,
|
|
PrepayJson: prepayJson,
|
|
RedirectUrl: redirectUrl,
|
|
}, nil
|
|
}
|
|
|
|
func (c *payment) Status(ctx context.Context, req *dto.PaymentStatusReq) (res *dto.PaymentStatusRes, err error) {
|
|
status, amount, err := service.PaymentService.GetStatus(ctx, req.OrderNo)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &dto.PaymentStatusRes{OrderNo: req.OrderNo, Status: status, Amount: amount}, nil
|
|
}
|
|
|
|
func (c *payment) ConfirmOffline(ctx context.Context, req *dto.ConfirmOfflineReq) (res *dto.ConfirmOfflineRes, err error) {
|
|
if err := service.PaymentService.ConfirmOffline(ctx, req.OrderNo); err != nil {
|
|
return nil, err
|
|
}
|
|
status, _, _ := service.PaymentService.GetStatus(ctx, req.OrderNo)
|
|
return &dto.ConfirmOfflineRes{OrderNo: req.OrderNo, Status: status}, nil
|
|
}
|
|
|
|
func (c *payment) NotifyWechat(ctx context.Context, req *struct{}) (res *struct{}, err error) {
|
|
r := g.RequestFromCtx(ctx)
|
|
body := r.GetBody()
|
|
_ = service.PaymentService.HandleNotify(ctx, "wechat", body)
|
|
r.Response.WriteString("<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>")
|
|
r.Exit()
|
|
return nil, nil
|
|
}
|
|
|
|
func (c *payment) NotifyAlipay(ctx context.Context, req *struct{}) (res *struct{}, err error) {
|
|
r := g.RequestFromCtx(ctx)
|
|
body := r.GetBody()
|
|
_ = service.PaymentService.HandleNotify(ctx, "alipay", body)
|
|
r.Response.WriteString("success")
|
|
r.Exit()
|
|
return nil, nil
|
|
}
|