save stripe custome id

Signed-off-by: wozulong <>
This commit is contained in:
wozulong 2024-03-23 21:59:15 +08:00
parent 71d60eeef7
commit 8537f10412
5 changed files with 19 additions and 6 deletions

View File

@ -44,6 +44,7 @@ func StripeWebhook(c *gin.Context) {
} }
func sessionCompleted(event stripe.Event) { func sessionCompleted(event stripe.Event) {
customerId := event.GetObjectValue("customer")
referenceId := event.GetObjectValue("client_reference_id") referenceId := event.GetObjectValue("client_reference_id")
status := event.GetObjectValue("status") status := event.GetObjectValue("status")
if "complete" != status { if "complete" != status {
@ -51,7 +52,7 @@ func sessionCompleted(event stripe.Event) {
return return
} }
err := model.Recharge(referenceId) err := model.Recharge(referenceId, customerId)
if err != nil { if err != nil {
log.Println(err.Error(), referenceId) log.Println(err.Error(), referenceId)
return return

View File

@ -25,7 +25,7 @@ type AmountRequest struct {
TopUpCode string `json:"top_up_code"` TopUpCode string `json:"top_up_code"`
} }
func genStripeLink(referenceId string, amount int64) (string, error) { func genStripeLink(referenceId string, customerId string, email string, amount int64) (string, error) {
if !strings.HasPrefix(common.StripeApiSecret, "sk_") { if !strings.HasPrefix(common.StripeApiSecret, "sk_") {
return "", fmt.Errorf("无效的Stripe API密钥") return "", fmt.Errorf("无效的Stripe API密钥")
} }
@ -44,6 +44,17 @@ func genStripeLink(referenceId string, amount int64) (string, error) {
}, },
Mode: stripe.String(string(stripe.CheckoutSessionModePayment)), Mode: stripe.String(string(stripe.CheckoutSessionModePayment)),
} }
if "" == customerId {
if "" != email {
params.CustomerEmail = stripe.String(email)
}
params.CustomerCreation = stripe.String(string(stripe.CheckoutSessionCustomerCreationAlways))
} else {
params.Customer = stripe.String(customerId)
}
result, err := session.New(params) result, err := session.New(params)
if err != nil { if err != nil {
return "", err return "", err
@ -96,7 +107,7 @@ func RequestPayLink(c *gin.Context) {
reference := fmt.Sprintf("new-api-ref-%d-%d-%s", user.Id, time.Now().UnixMilli(), common.RandomString(4)) reference := fmt.Sprintf("new-api-ref-%d-%d-%s", user.Id, time.Now().UnixMilli(), common.RandomString(4))
referenceId := "ref_" + common.Sha1(reference) referenceId := "ref_" + common.Sha1(reference)
payLink, err := genStripeLink(referenceId, int64(req.Amount)) payLink, err := genStripeLink(referenceId, user.StripeCustomer, user.Email, int64(req.Amount))
if err != nil { if err != nil {
log.Println("获取Stripe Checkout支付链接失败", err) log.Println("获取Stripe Checkout支付链接失败", err)
c.JSON(200, gin.H{"message": "error", "data": "拉起支付失败"}) c.JSON(200, gin.H{"message": "error", "data": "拉起支付失败"})

View File

@ -50,7 +50,7 @@ func GetTopUpByTradeNo(tradeNo string) *TopUp {
return topUp return topUp
} }
func Recharge(referenceId string) (err error) { func Recharge(referenceId string, customerId string) (err error) {
if referenceId == "" { if referenceId == "" {
return errors.New("未提供支付单号") return errors.New("未提供支付单号")
} }
@ -81,7 +81,7 @@ func Recharge(referenceId string) (err error) {
} }
quota = topUp.Money * common.QuotaPerUnit quota = topUp.Money * common.QuotaPerUnit
err = tx.Model(&User{}).Where("id = ?", topUp.UserId).Update("quota", gorm.Expr("quota + ?", quota)).Error err = tx.Model(&User{}).Where("id = ?", topUp.UserId).Updates(map[string]interface{}{"stripe_customer": customerId, "quota": gorm.Expr("quota + ?", quota)}).Error
if err != nil { if err != nil {
return err return err
} }

View File

@ -36,6 +36,7 @@ type User struct {
AffQuota int `json:"aff_quota" gorm:"type:int;default:0;column:aff_quota"` // 邀请剩余额度 AffQuota int `json:"aff_quota" gorm:"type:int;default:0;column:aff_quota"` // 邀请剩余额度
AffHistoryQuota int `json:"aff_history_quota" gorm:"type:int;default:0;column:aff_history"` // 邀请历史额度 AffHistoryQuota int `json:"aff_history_quota" gorm:"type:int;default:0;column:aff_history"` // 邀请历史额度
InviterId int `json:"inviter_id" gorm:"type:int;column:inviter_id;index"` InviterId int `json:"inviter_id" gorm:"type:int;column:inviter_id;index"`
StripeCustomer string `json:"stripe_customer" gorm:"column:stripe_customer;index"`
DeletedAt gorm.DeletedAt `gorm:"index"` DeletedAt gorm.DeletedAt `gorm:"index"`
} }

View File

@ -262,7 +262,7 @@ const TopUp = () => {
}} }}
/> />
<Space> <Space>
<Button style={{backgroundColor: '#2e75cd'}} <Button style={{backgroundColor: '#b161fe'}}
type={'primary'} type={'primary'}
disabled={isPaying} disabled={isPaying}
theme={'solid'} onClick={ theme={'solid'} onClick={