【问题标题】:Can't get card brand from Stripe无法从 Stripe 获得卡品牌
【发布时间】:2017-11-16 00:53:57
【问题描述】:
func (s *Service) CreateNewCreditCard(user *models.User, creditCardRequest *CreditCardRequest) (error) {

    userID := string(user.ID)
    customer, err := s.stripe_a.CreateUserID(creditCardRequest.StripeToken, userID)

    if err != nil {
        return err
    }

    //TO DO - NOT BEING FILLED WITH DATA --> this needs to be added to the brand colom in the database
    //customer.DefaultSource.Card.Brand

    // Save to the DB
    stripeCustomer := &models.StripeCustomer{
        UserID:         util.IntOrNull(int64(user.ID)),
        CustomerID:     util.StringOrNull(customer.ID),
        Last4:          util.StringOrNull(creditCardRequest.Last4),
        Brand:          util.StringOrNull("VISA"),
    }
    if err := s.db.Create(stripeCustomer).Error; err != nil {
        return err
    }

    return nil
}

我正在尝试为我插入数据库的客户获取默认卡的卡品牌,但这总是会因为空指针而导致崩溃。

我不明白为什么。在 Stripe 控制台中,客户有一张显示“VISA”的默认卡片

添加日志:

[2017-06-14 04:26:40]  [18.51ms]  SELECT * FROM "orders"  WHERE "orders"."deleted_at" IS NULL AND (("user_id" IN ('2'))) ORDER BY "orders"."id" ASC
2017/06/14 04:26:40 Requesting POST api.stripe.com/v1/customers
2017/06/14 04:26:41 [C] [asm_amd64.s:514] the request url is  /v1/credit-cards
2017/06/14 04:26:41 [C] [asm_amd64.s:514] Handler crashed with error runtime error: invalid memory address or nil pointer dereference
2017/06/14 04:26:41 [C] [asm_amd64.s:514] /usr/local/go/src/runtime/asm_amd64.s:514
2017/06/14 04:26:41 [C] [asm_amd64.s:514] /usr/local/go/src/runtime/panic.go:489
2017/06/14 04:26:41 [C] [asm_amd64.s:514] /usr/local/go/src/runtime/panic.go:63
2017/06/14 04:26:41 [C] [asm_amd64.s:514] /usr/local/go/src/runtime/signal_unix.go:290

这是 CreateUserID 函数:

func (a *Adapter) CreateUserID(stripeToken string, userID string) (*stripe.Customer, error) {
    // Assign secret key from configuration to Stripe
    stripe.Key = a.cnf.Stripe.SecretKey

    // Create a Customer:
    customerParams := &stripe.CustomerParams{
        Desc: userID,
    }
    customerParams.SetSource(stripeToken)

    customer, _ := customer.New(customerParams)

    return customer, nil
}

如何让它询问卡的品牌?

【问题讨论】:

  • 什么指针是空的?你能分享一下错误吗?
  • 添加了日志,但并没有告诉我太多
  • 那我想你应该添加一些Printlns 并找出错误发生在哪里?
  • 当我在 customer.DefaultSource.Card.Brand 周围放置一个 Println 时发生错误
  • 所以customer.DefaultSource 可能是nil?或者customer.DefaultSource.Card

标签: go stripe-payments


【解决方案1】:

根据the Stripe documentation,默认情况下不扩展default_card 字段。您可以通过将此行添加到 CreateUserID 来扩展它:

customerParams.Expand("default_source")

【讨论】:

    猜你喜欢
    • 2017-11-16
    • 2015-08-21
    • 2018-09-01
    • 2020-12-06
    • 2017-07-04
    • 1970-01-01
    • 2020-08-26
    • 1970-01-01
    • 2018-11-26
    相关资源
    最近更新 更多