Skip to content

Commit

Permalink
🔖 chore: add user info to Stripe
Browse files Browse the repository at this point in the history
  • Loading branch information
MartialBE committed Sep 29, 2024
1 parent 5387551 commit 3b86b2b
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 9 deletions.
9 changes: 7 additions & 2 deletions controller/order.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ func CreateOrder(c *gin.Context) {
}

userId := c.GetInt("id")
user, err := model.GetUserById(userId, false)
if err != nil {
common.APIRespondWithError(c, http.StatusOK, errors.New("用户不存在"))
return
}

// 关闭用户未完成的订单
go model.CloseUnfinishedOrder()

Expand All @@ -54,10 +60,9 @@ func CreateOrder(c *gin.Context) {
}
// 获取手续费和支付金额
discount, fee, payMoney := calculateOrderAmount(paymentService.Payment, orderReq.Amount)
currency := paymentService.Payment.Currency
// 开始支付
tradeNo := utils.GenerateTradeNo()
payRequest, err := paymentService.Pay(tradeNo, payMoney, currency)
payRequest, err := paymentService.Pay(tradeNo, payMoney, user)
if err != nil {
common.APIRespondWithError(c, http.StatusOK, errors.New("创建支付失败,请稍后再试"))
return
Expand Down
2 changes: 1 addition & 1 deletion payment/gateway/alipay/payment.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,6 @@ func getAlipayConfig(gatewayConfig string) (*AlipayConfig, error) {
return &alipayConfig, nil
}

func (a *Alipay) CreatedPay(notifyURL string, gatewayConfig *model.Payment) error {
func (a *Alipay) CreatedPay(_ string, _ *model.Payment) error {
return nil
}
2 changes: 1 addition & 1 deletion payment/gateway/epay/payment.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,6 @@ func getEpayConfig(gatewayConfig string) (*EpayConfig, error) {

return &epayConfig, nil
}
func (e *Epay) CreatedPay(notifyURL string, gatewayConfig *model.Payment) error {
func (e *Epay) CreatedPay(_ string, _ *model.Payment) error {
return nil
}
13 changes: 11 additions & 2 deletions payment/gateway/stripe/payment.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (e *Stripe) Pay(config *types.PayConfig, gatewayConfig string) (*types.PayR
currency = stripe.String("CNY")
}

result, err := sc.CheckoutSessions.New(&stripe.CheckoutSessionParams{
params := &stripe.CheckoutSessionParams{
Mode: stripe.String(string(stripe.CheckoutSessionModePayment)),
SuccessURL: stripe.String(config.ReturnURL),
ClientReferenceID: stripe.String(config.TradeNo),
Expand All @@ -59,7 +59,16 @@ func (e *Stripe) Pay(config *types.PayConfig, gatewayConfig string) (*types.PayR
Quantity: stripe.Int64(1),
},
},
})
Metadata: map[string]string{
"user_id": fmt.Sprintf("%d", config.User.Id),
},
}

if config.User.Email != "" {
params.CustomerEmail = stripe.String(config.User.Email)
}

result, err := sc.CheckoutSessions.New(params)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion payment/gateway/wxpay/payment.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,6 @@ func getWeChatConfig(gatewayConfig string) (*WeChatConfig, error) {
return &wechatConfig, nil
}

func (w *WeChatPay) CreatedPay(notifyURL string, gatewayConfig *model.Payment) error {
func (w *WeChatPay) CreatedPay(_ string, _ *model.Payment) error {
return nil
}
5 changes: 3 additions & 2 deletions payment/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,14 @@ func (s *PaymentService) CreatedPay() error {
return s.gateway.CreatedPay(notifyURL, s.Payment)
}

func (s *PaymentService) Pay(tradeNo string, amount float64, currency model.CurrencyType) (*types.PayRequest, error) {
func (s *PaymentService) Pay(tradeNo string, amount float64, user *model.User) (*types.PayRequest, error) {
config := &types.PayConfig{
Money: amount,
TradeNo: tradeNo,
NotifyURL: s.getNotifyURL(),
ReturnURL: s.getReturnURL(),
Currency: currency,
Currency: s.Payment.Currency,
User: user,
}
payRequest, err := s.gateway.Pay(config, s.Payment.Config)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions payment/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type PayConfig struct {
TradeNo string `json:"trade_no"`
Money float64 `json:"money"`
Currency model.CurrencyType `json:"currency"`
User *model.User `json:"user"`
}

// 请求支付时的数据结构
Expand Down

0 comments on commit 3b86b2b

Please sign in to comment.