🧾 fix: persist epay orders before purchase

Create the subscription order before initiating epay payment and expire it if the provider call fails, preventing orphaned transactions and improving reconciliation.
This commit is contained in:
t0ng7u
2026-02-03 01:24:25 +08:00
parent 49ac355357
commit 0486a5d83b

View File

@@ -81,20 +81,6 @@ func SubscriptionRequestEpay(c *gin.Context) {
return
}
uri, params, err := client.Purchase(&epay.PurchaseArgs{
Type: req.PaymentMethod,
ServiceTradeNo: tradeNo,
Name: fmt.Sprintf("SUB:%s", plan.Title),
Money: strconv.FormatFloat(plan.PriceAmount, 'f', 2, 64),
Device: epay.PC,
NotifyUrl: notifyUrl,
ReturnUrl: returnUrl,
})
if err != nil {
common.ApiErrorMsg(c, "拉起支付失败")
return
}
order := &model.SubscriptionOrder{
UserId: userId,
PlanId: plan.Id,
@@ -108,6 +94,20 @@ func SubscriptionRequestEpay(c *gin.Context) {
common.ApiErrorMsg(c, "创建订单失败")
return
}
uri, params, err := client.Purchase(&epay.PurchaseArgs{
Type: req.PaymentMethod,
ServiceTradeNo: tradeNo,
Name: fmt.Sprintf("SUB:%s", plan.Title),
Money: strconv.FormatFloat(plan.PriceAmount, 'f', 2, 64),
Device: epay.PC,
NotifyUrl: notifyUrl,
ReturnUrl: returnUrl,
})
if err != nil {
_ = model.ExpireSubscriptionOrder(tradeNo)
common.ApiErrorMsg(c, "拉起支付失败")
return
}
c.JSON(http.StatusOK, gin.H{"message": "success", "data": params, "url": uri})
}