fusenapi/server/pay/internal/logic/stripewebhooklogic.go

392 lines
13 KiB
Go
Raw Normal View History

2023-07-26 11:23:16 +00:00
package logic
import (
"encoding/json"
"errors"
"fusenapi/constants"
"fusenapi/model/gmodel"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"time"
"context"
"fusenapi/server/pay/internal/svc"
"fusenapi/server/pay/internal/types"
"github.com/stripe/stripe-go/v74"
2023-07-28 03:15:42 +00:00
"github.com/stripe/stripe-go/v74/webhook"
2023-07-26 11:23:16 +00:00
"github.com/zeromicro/go-zero/core/logx"
"gorm.io/gorm"
)
type StripeWebhookLogic struct {
logx.Logger
2023-07-28 03:15:42 +00:00
ctx context.Context
svcCtx *svc.ServiceContext
2023-07-26 11:23:16 +00:00
}
func NewStripeWebhookLogic(ctx context.Context, svcCtx *svc.ServiceContext) *StripeWebhookLogic {
return &StripeWebhookLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
// 处理进入前逻辑w,r
2023-07-28 03:15:42 +00:00
// func (l *StripeWebhookLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
// }
2023-07-26 11:23:16 +00:00
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
// func (l *StripeWebhookLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
// // httpx.OkJsonCtx(r.Context(), w, resp)
// }
func (l *StripeWebhookLogic) StripeWebhook(req *types.StripeWebhookReq, userinfo *auth.UserInfo) (resp *basic.Response) {
// 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data)
// userinfo 传入值时, 一定不为null
2023-07-28 03:15:42 +00:00
stripe.Key = l.svcCtx.Config.PayConfig.Stripe.Key
2023-07-26 11:23:16 +00:00
event := stripe.Event{}
2023-07-28 03:15:42 +00:00
if err := json.Unmarshal(req.Payload, &event); err != nil {
2023-07-26 11:23:16 +00:00
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeAesCbcDecryptionErr, "pay notify Unmarshal fail")
}
2023-07-28 03:15:42 +00:00
endpointSecret := l.svcCtx.Config.PayConfig.Stripe.EndpointSecret
signatureHeader := req.StripeSignature
event, err := webhook.ConstructEvent(req.Payload, signatureHeader, endpointSecret)
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeAesCbcDecryptionErr, "Webhook signature verification failed")
}
2023-07-28 11:03:36 +00:00
// 新增支付回调事件日志
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)
2023-07-26 11:23:16 +00:00
// Unmarshal the event data into an appropriate struct depending on its Type
switch event.Type {
2023-07-28 03:15:42 +00:00
case "charge.succeeded":
// var charge stripe.Charge
// err := json.Unmarshal(event.Data.Raw, &charge)
// if err != nil {
// logx.Error(err)
// return resp.SetStatusWithMessage(basic.CodeAesCbcDecryptionErr, "pay notify Unmarshal fail event.Type charge.succeeded")
// }
case "checkout.session.completed":
// checkout checkout.session.completed 处理逻辑
// var session stripe.CheckoutSession
// err := json.Unmarshal(event.Data.Raw, &session)
// if err != nil {
// logx.Error(err)
// return resp.SetStatusWithMessage(basic.CodeAesCbcDecryptionErr, "pay notify Unmarshal fail event.Type payment_intent.succeeded")
// }
// fmt.Println("checkout.session.completed")
// err = l.handlePaymentSessionCompleted(session.ID, session.PaymentIntent.ID)
// if err != nil {
// return resp.SetStatusWithMessage(basic.CodeAesCbcDecryptionErr, "checkout.session.completed fail")
// }
2023-07-26 11:23:16 +00:00
case "payment_intent.succeeded":
var paymentIntent stripe.PaymentIntent
err := json.Unmarshal(event.Data.Raw, &paymentIntent)
if err != nil {
2023-07-31 03:32:40 +00:00
logx.Errorf("err%+vdesc%s", err, "pay notify Unmarshal fail event.Type payment_intent.succeeded")
2023-07-26 11:23:16 +00:00
return resp.SetStatusWithMessage(basic.CodeAesCbcDecryptionErr, "pay notify Unmarshal fail event.Type payment_intent.succeeded")
}
2023-07-28 11:03:36 +00:00
err = l.HandlePaymentIntentSucceeded(&paymentIntent)
2023-07-26 11:23:16 +00:00
if err != nil {
2023-07-31 03:32:40 +00:00
logx.Errorf("err%+vdesc%s", err, "pay notify handle payment_intent.succeeded")
return resp.SetStatusWithMessage(basic.CodePaybackNotOk, "pay notify handle payment_intent.succeeded")
2023-07-26 11:23:16 +00:00
}
case "payment_method.attached":
var paymentMethod stripe.PaymentMethod
err := json.Unmarshal(event.Data.Raw, &paymentMethod)
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeAesCbcDecryptionErr, "pay notify Unmarshal fail event.Type payment_method.attached")
}
2023-07-31 03:32:40 +00:00
case "charge.refunded":
var chargeRefunded stripe.Charge
err := json.Unmarshal(event.Data.Raw, &chargeRefunded)
if err != nil {
logx.Errorf("err%+vdesc%s", err, "pay notify Unmarshal fail event.Type charge.refunded")
return resp.SetStatusWithMessage(basic.CodeAesCbcDecryptionErr, "pay notify Unmarshal fail event.Type charge.refunded")
}
err = l.HandleChargeRefunded(&chargeRefunded)
if err != nil {
logx.Errorf("err%+vdesc%s", err, "pay notify handle charge.refunded")
return resp.SetStatusWithMessage(basic.CodeAesCbcDecryptionErr, "pay notify handle charge.refunded")
}
2023-07-26 11:23:16 +00:00
// ... handle other event types
default:
logx.Error("Unhandled event")
return resp.SetStatusWithMessage(basic.CodeAesCbcDecryptionErr, "pay notify Unmarshal fail event.Type Unhandled")
}
return resp.SetStatus(basic.CodeOK)
}
2023-07-28 11:03:36 +00:00
// 回调事件日志
func (l *StripeWebhookLogic) HandlePayEventCreate(fsPayEvent *gmodel.FsPayEvent) error {
_, err := gmodel.NewFsPayEventModel(l.svcCtx.MysqlConn).CreateOrUpdate(l.ctx, fsPayEvent)
return err
}
2023-07-31 03:32:40 +00:00
// 退款成功
func (l *StripeWebhookLogic) HandleChargeRefunded(chargeRefunded *stripe.Charge) (err error) {
// 退款成功
if chargeRefunded.Status == "succeeded" {
2023-08-10 06:58:33 +00:00
ctx := l.ctx
err = l.svcCtx.MysqlConn.Transaction(func(connGorm *gorm.DB) error {
2023-07-31 03:32:40 +00:00
// 查询支付记录
payModelT := gmodel.NewFsPayModel(connGorm)
payModelTRSB := payModelT.RowSelectBuilder(nil)
payModelTRSB1 := payModelTRSB.Where("trade_no = ?", chargeRefunded.PaymentIntent.ID).Where("pay_status = ?", constants.PAYSTATUS_SUCCESS).Where("is_refund = ?", 0)
2023-08-10 06:58:33 +00:00
payInfo, err := payModelT.FindOneByQuery(ctx, payModelTRSB1, nil)
2023-07-31 03:32:40 +00:00
if err != nil {
return err
}
// 更新支付记录
*payInfo.IsRefund = 1
_, err = payModelT.CreateOrUpdate(ctx, payInfo)
if err != nil {
return err
}
// 获取是否还有未退款的数据
payModelTRSB2 := payModelTRSB.Where("order_number = ?", payInfo.OrderNumber).Where("pay_status = ?", constants.PAYSTATUS_SUCCESS).Where("is_refund = ?", 0)
count, err := payModelT.FindCount(l.ctx, payModelTRSB2, nil)
if count == 0 {
// 退款完成更新订单状态
orderModelT := gmodel.NewFsOrderModel(connGorm)
orderModelTRSB := orderModelT.RowSelectBuilder(nil).Where("sn =?", payInfo.OrderNumber)
orderInfoRel, err := orderModelT.FindOneByQuery(ctx, orderModelTRSB, nil)
if err != nil {
return err
}
var isRefunded int64 = 1
var isRefunding int64 = 1
var orderStatus int64 = int64(constants.STATUS_NEW_REFUNDED)
var orderInfo = &gmodel.FsOrder{}
orderInfo.Id = orderInfoRel.Id
orderInfo.IsRefunded = &isRefunded
orderInfo.IsRefunding = &isRefunding
orderInfo.Status = &orderStatus
orderModelT.Update(ctx, orderInfo)
// 记录退款原因
refundReasonModelT := gmodel.NewFsRefundReasonModel(connGorm)
refundReasonModelTRSB := refundReasonModelT.RowSelectBuilder(nil).Where("order_id =?", orderInfoRel.Id)
refundReasonInfo, err := refundReasonModelT.FindOneByQuery(ctx, refundReasonModelTRSB, nil)
if err != nil {
return err
}
*refundReasonInfo.IsRefund = 1
_, err = refundReasonModelT.CreateOrUpdate(ctx, refundReasonInfo)
if err != nil {
return err
}
}
return err
})
}
return err
}
2023-07-28 03:15:42 +00:00
// session完成
// func (l *StripeWebhookLogic) handlePaymentSessionCompleted(sessionId string, tradeNo string) (err error) {
// // 查询支付记录
// payModel := gmodel.NewFsPayModel(l.svcCtx.MysqlConn)
// rsbPay := payModel.RowSelectBuilder(nil)
// rsbPay = rsbPay.Where("session_id = ?", sessionId)
// payInfo, err := payModel.FindOneByQuery(l.ctx, rsbPay, nil)
// if err != nil {
// return err
// }
// if *payInfo.PayStatus == 0 {
// *payInfo.TradeNo = tradeNo
// _, err = payModel.CreateOrUpdate(l.ctx, payInfo)
// if err != nil {
// return err
// }
// } else {
// return errors.New("pay status 1")
// }
// return err
// }
2023-07-31 03:32:40 +00:00
// 付款成功
2023-07-28 11:03:36 +00:00
func (l *StripeWebhookLogic) HandlePaymentIntentSucceeded(paymentIntent *stripe.PaymentIntent) error {
2023-07-28 03:15:42 +00:00
orderSn, ok := paymentIntent.Metadata["order_sn"]
if !ok || orderSn == "" {
return errors.New("order_sn not found")
}
2023-07-26 11:23:16 +00:00
// 查询支付记录
payModel := gmodel.NewFsPayModel(l.svcCtx.MysqlConn)
rsbPay := payModel.RowSelectBuilder(nil)
2023-07-28 11:03:36 +00:00
rsbPay = rsbPay.Where("order_number = ?", orderSn).Where("pay_status = ?", constants.PAYSTATUS_UNSUCCESS)
2023-07-26 11:23:16 +00:00
payInfo, err := payModel.FindOneByQuery(l.ctx, rsbPay, nil)
if err != nil {
return err
}
2023-07-28 11:03:36 +00:00
2023-07-26 11:23:16 +00:00
//订单信息
orderDetailTemplateModel := gmodel.NewFsOrderDetailTemplateModel(l.svcCtx.MysqlConn)
orderModel := gmodel.NewFsOrderModel(l.svcCtx.MysqlConn)
2023-07-28 03:15:42 +00:00
fsOrderDetailModel := gmodel.NewFsOrderDetailModel(l.svcCtx.MysqlConn)
fsProductDesignModel := gmodel.NewFsProductDesignModel(l.svcCtx.MysqlConn)
2023-07-26 11:23:16 +00:00
rsbOrder := orderModel.RowSelectBuilder(nil)
2023-07-28 03:15:42 +00:00
rsbOrder = rsbOrder.Where("sn =?", orderSn).Preload("FsOrderDetails")
2023-07-26 11:23:16 +00:00
rsbOrder = rsbOrder.Preload("FsOrderDetails", func(dbPreload *gorm.DB) *gorm.DB {
2023-07-28 03:15:42 +00:00
return dbPreload.Table(fsOrderDetailModel.TableName()).Preload("FsOrderDetailTemplateInfo", func(dbPreload *gorm.DB) *gorm.DB {
return dbPreload.Table(orderDetailTemplateModel.TableName()).Preload("FsProductDesignInfo", func(dbPreload *gorm.DB) *gorm.DB {
return dbPreload.Table(fsProductDesignModel.TableName())
})
2023-07-26 11:23:16 +00:00
})
})
2023-07-28 03:15:42 +00:00
fsOrderRelInfo, err := orderModel.FindOneByQuery(l.ctx, rsbOrder, nil)
2023-07-26 11:23:16 +00:00
if err != nil {
return err
}
var designIds []int64
var cartIds []int64
2023-07-28 03:15:42 +00:00
if len(fsOrderRelInfo.FsOrderDetails) > 0 {
for _, fsOrderDetail := range fsOrderRelInfo.FsOrderDetails {
2023-07-26 11:23:16 +00:00
if fsOrderDetail.FsOrderDetailTemplateInfo.FsProductDesignInfo.Id != 0 {
designIds = append(designIds, fsOrderDetail.FsOrderDetailTemplateInfo.FsProductDesignInfo.Id)
}
cartIds = append(cartIds, *fsOrderDetail.CartId)
}
}
var nowTime int64 = time.Now().Unix()
// 支付成功
if paymentIntent.Status == "succeeded" {
2023-07-28 03:15:42 +00:00
var card string
var brand string
if paymentIntent.LatestCharge.PaymentMethodDetails != nil {
if paymentIntent.LatestCharge.PaymentMethodDetails.Card != nil {
if paymentIntent.LatestCharge.PaymentMethodDetails.Card.Last4 != "" {
card = paymentIntent.LatestCharge.PaymentMethodDetails.Card.Last4
}
if paymentIntent.LatestCharge.PaymentMethodDetails.Card.Brand != "" {
brand = string(paymentIntent.LatestCharge.PaymentMethodDetails.Card.Brand)
}
}
}
2023-07-26 11:23:16 +00:00
2023-08-10 06:58:33 +00:00
ctx := l.ctx
err = l.svcCtx.MysqlConn.Transaction(func(connGorm *gorm.DB) error {
2023-07-26 11:23:16 +00:00
// 更新支付信息
payModelT := gmodel.NewFsPayModel(connGorm)
*payInfo.PayStatus = 1
*payInfo.PayTime = nowTime
*payInfo.CardNo = card
*payInfo.Brand = brand
2023-07-28 11:03:36 +00:00
*payInfo.TradeNo = paymentIntent.ID
2023-07-26 11:23:16 +00:00
_, err = payModelT.CreateOrUpdate(ctx, payInfo)
if err != nil {
return err
}
// 更新设计数据
productDesignModelT := gmodel.NewFsProductDesignModel(connGorm)
var isPay int64 = 1
err = productDesignModelT.UpdateByIds(ctx, designIds, &gmodel.FsProductDesign{IsPay: &isPay})
if err != nil {
return err
}
var orderInfo = &gmodel.FsOrder{}
2023-07-28 03:15:42 +00:00
var orderStatus int64
var orderIsPartPay int64
var orderPayedAmount int64
var orderIsPayCompleted int64
2023-07-26 11:23:16 +00:00
// 支付记录是首款
if *payInfo.PayStage == int64(constants.PAYSTAGE_DEPOSIT) {
2023-07-28 03:15:42 +00:00
orderStatus = int64(constants.STATUS_NEW_PART_PAY)
orderIsPartPay = 1
orderInfo.IsPartPay = &orderIsPartPay
orderPayedAmount = paymentIntent.Amount
2023-07-26 11:23:16 +00:00
// 删除购物车
cartModelT := gmodel.NewFsCartModel(connGorm)
err = cartModelT.DeleteCartsByIds(ctx, cartIds)
if err != nil {
return err
}
}
// 支付记录是尾款
if *payInfo.PayStage == int64(constants.PAYSTAGE_REMAINING) {
2023-07-28 11:03:36 +00:00
if *fsOrderRelInfo.Status < int64(constants.STATUS_NEW_PAY_COMPLETED) {
2023-07-28 03:15:42 +00:00
orderStatus = int64(constants.STATUS_NEW_PAY_COMPLETED)
2023-07-26 11:23:16 +00:00
}
2023-07-28 03:15:42 +00:00
orderIsPayCompleted = 1
orderInfo.IsPayCompleted = &orderIsPayCompleted
2023-07-28 11:03:36 +00:00
orderPayedAmount = *fsOrderRelInfo.PayedAmount + paymentIntent.Amount
2023-07-26 11:23:16 +00:00
}
// 更新订单信息
2023-07-28 03:15:42 +00:00
orderInfo.Id = fsOrderRelInfo.Id
orderInfo.Status = &orderStatus
orderInfo.Ptime = &nowTime
orderInfo.PayedAmount = &orderPayedAmount
2023-07-26 11:23:16 +00:00
orderModelT := gmodel.NewFsOrderModel(connGorm)
err = orderModelT.Update(ctx, orderInfo)
if err != nil {
return err
}
return err
})
if err != nil {
return err
}
//千人千面的处理
// $renderServer = (new RenderService());
// $renderServer->thousandsFacesV2($order->id);
// //清除用户最新的设计
// $cache = \Yii::$app->cache;
// $cache->delete(CacheConfigHelper::LAST_DESIGN . $order->user_id);
// //缓存最新订单编号
// $cache->set(CacheConfigHelper::USER_ORDERNO . $order->user_id, $order->sn);
// //查询用户邮箱信息
// $user = \api\models\User::find()->where(['id' => $order->user_id])->one();
// $redisData = [
// 'key' => 'receipt_download',
// 'param' => [
// 'email' => $user->email,
// 'order_id' => $order->id,
// 'pay_id' => $pay->id,
// 'type' => 1,//付款成功为1
// ]
// ];
// Email::timely($redisData);
}
// 订单记录
return nil
}