退款
This commit is contained in:
43
server/pay/internal/logic/orderrefundlogic.go
Normal file
43
server/pay/internal/logic/orderrefundlogic.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"fusenapi/utils/auth"
|
||||
"fusenapi/utils/basic"
|
||||
|
||||
"context"
|
||||
|
||||
"fusenapi/server/pay/internal/svc"
|
||||
"fusenapi/server/pay/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type OrderRefundLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewOrderRefundLogic(ctx context.Context, svcCtx *svc.ServiceContext) *OrderRefundLogic {
|
||||
return &OrderRefundLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
// 处理进入前逻辑w,r
|
||||
// func (l *OrderRefundLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
|
||||
// }
|
||||
|
||||
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
|
||||
// func (l *OrderRefundLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
|
||||
// // httpx.OkJsonCtx(r.Context(), w, resp)
|
||||
// }
|
||||
|
||||
func (l *OrderRefundLogic) OrderRefund(req *types.OrderRefundReq, userinfo *auth.UserInfo) (resp *basic.Response) {
|
||||
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
|
||||
// userinfo 传入值时, 一定不为null
|
||||
|
||||
return resp.SetStatus(basic.CodeOK)
|
||||
}
|
||||
@@ -64,6 +64,21 @@ func (l *StripeWebhookLogic) StripeWebhook(req *types.StripeWebhookReq, userinfo
|
||||
return resp.SetStatusWithMessage(basic.CodeAesCbcDecryptionErr, "Webhook signature verification failed")
|
||||
}
|
||||
|
||||
// 新增支付回调事件日志
|
||||
var payMethod = int64(constants.PAYMETHOD_STRIPE)
|
||||
var nowTime = time.Now().Unix()
|
||||
var eventData = string(event.Data.Raw)
|
||||
var fsPayEvent = &gmodel.FsPayEvent{
|
||||
PayMethod: &payMethod,
|
||||
EventId: &event.ID,
|
||||
EventType: &event.Type,
|
||||
EventData: &eventData,
|
||||
EventCreated: &event.Created,
|
||||
Ip: &req.RemoteAddr,
|
||||
CreatedAt: &nowTime,
|
||||
}
|
||||
l.HandlePayEventCreate(fsPayEvent)
|
||||
|
||||
// Unmarshal the event data into an appropriate struct depending on its Type
|
||||
switch event.Type {
|
||||
case "charge.succeeded":
|
||||
@@ -94,7 +109,7 @@ func (l *StripeWebhookLogic) StripeWebhook(req *types.StripeWebhookReq, userinfo
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeAesCbcDecryptionErr, "pay notify Unmarshal fail event.Type payment_intent.succeeded")
|
||||
}
|
||||
err = l.handlePaymentIntentSucceeded(&paymentIntent)
|
||||
err = l.HandlePaymentIntentSucceeded(&paymentIntent)
|
||||
if err != nil {
|
||||
return resp.SetStatusWithMessage(basic.CodeAesCbcDecryptionErr, "pay notify Unmarshal fail event.Type Unhandled")
|
||||
}
|
||||
@@ -114,6 +129,12 @@ func (l *StripeWebhookLogic) StripeWebhook(req *types.StripeWebhookReq, userinfo
|
||||
return resp.SetStatus(basic.CodeOK)
|
||||
}
|
||||
|
||||
// 回调事件日志
|
||||
func (l *StripeWebhookLogic) HandlePayEventCreate(fsPayEvent *gmodel.FsPayEvent) error {
|
||||
_, err := gmodel.NewFsPayEventModel(l.svcCtx.MysqlConn).CreateOrUpdate(l.ctx, fsPayEvent)
|
||||
return err
|
||||
}
|
||||
|
||||
// session完成
|
||||
// func (l *StripeWebhookLogic) handlePaymentSessionCompleted(sessionId string, tradeNo string) (err error) {
|
||||
// // 查询支付记录
|
||||
@@ -137,7 +158,7 @@ func (l *StripeWebhookLogic) StripeWebhook(req *types.StripeWebhookReq, userinfo
|
||||
// }
|
||||
|
||||
// 成功的付款
|
||||
func (l *StripeWebhookLogic) handlePaymentIntentSucceeded(paymentIntent *stripe.PaymentIntent) error {
|
||||
func (l *StripeWebhookLogic) HandlePaymentIntentSucceeded(paymentIntent *stripe.PaymentIntent) error {
|
||||
orderSn, ok := paymentIntent.Metadata["order_sn"]
|
||||
if !ok || orderSn == "" {
|
||||
return errors.New("order_sn not found")
|
||||
@@ -146,14 +167,12 @@ func (l *StripeWebhookLogic) handlePaymentIntentSucceeded(paymentIntent *stripe.
|
||||
// 查询支付记录
|
||||
payModel := gmodel.NewFsPayModel(l.svcCtx.MysqlConn)
|
||||
rsbPay := payModel.RowSelectBuilder(nil)
|
||||
rsbPay = rsbPay.Where("order_number = ?", orderSn)
|
||||
rsbPay = rsbPay.Where("order_number = ?", orderSn).Where("pay_status = ?", constants.PAYSTATUS_UNSUCCESS)
|
||||
payInfo, err := payModel.FindOneByQuery(l.ctx, rsbPay, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if *payInfo.PayStatus == 1 {
|
||||
return errors.New("pay status 1")
|
||||
}
|
||||
|
||||
//订单信息
|
||||
orderDetailTemplateModel := gmodel.NewFsOrderDetailTemplateModel(l.svcCtx.MysqlConn)
|
||||
orderModel := gmodel.NewFsOrderModel(l.svcCtx.MysqlConn)
|
||||
@@ -209,6 +228,7 @@ func (l *StripeWebhookLogic) handlePaymentIntentSucceeded(paymentIntent *stripe.
|
||||
*payInfo.PayTime = nowTime
|
||||
*payInfo.CardNo = card
|
||||
*payInfo.Brand = brand
|
||||
*payInfo.TradeNo = paymentIntent.ID
|
||||
_, err = payModelT.CreateOrUpdate(ctx, payInfo)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -244,12 +264,12 @@ func (l *StripeWebhookLogic) handlePaymentIntentSucceeded(paymentIntent *stripe.
|
||||
|
||||
// 支付记录是尾款
|
||||
if *payInfo.PayStage == int64(constants.PAYSTAGE_REMAINING) {
|
||||
if *orderInfo.Status < int64(constants.STATUS_NEW_PAY_COMPLETED) {
|
||||
if *fsOrderRelInfo.Status < int64(constants.STATUS_NEW_PAY_COMPLETED) {
|
||||
orderStatus = int64(constants.STATUS_NEW_PAY_COMPLETED)
|
||||
}
|
||||
orderIsPayCompleted = 1
|
||||
orderInfo.IsPayCompleted = &orderIsPayCompleted
|
||||
orderPayedAmount = *orderInfo.PayedAmount + paymentIntent.Amount
|
||||
orderPayedAmount = *fsOrderRelInfo.PayedAmount + paymentIntent.Amount
|
||||
}
|
||||
|
||||
// 更新订单信息
|
||||
|
||||
Reference in New Issue
Block a user