douchuose2514 2017-06-14 03:55
浏览 58
已采纳

无法从Stripe获得卡牌

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
}

I'm trying to get the card's brand of the default card for the user that customer that I'm inserting into my database but this always lead to a crash because of a null pointer.

I don't get why though. In the Stripe console the customer has a default card displaying "VISA"

Added logs:

[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

This is the CreateUserID function:

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
}

How can I make it ask for the card's brand?

  • 写回答

1条回答 默认 最新

  • doob0526 2017-06-14 05:53
    关注

    Per the Stripe documentation, the default_card field is not expanded by default. You can expand it by adding this line to CreateUserID:

    customerParams.Expand("default_source")
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 算法题:数的划分,用记忆化DFS做WA求调
  • ¥15 chatglm-6b应用到django项目中,模型加载失败
  • ¥15 武汉岩海低应变分析软件,导数据库里不显示波形图
  • ¥15 CreateBitmapFromWicBitmap内存释放问题。
  • ¥30 win c++ socket
  • ¥30 CanMv K210开发板实现功能
  • ¥15 C# datagridview 栏位进度
  • ¥15 vue3页面el-table页面数据过多
  • ¥100 vue3中融入gRPC-web
  • ¥15 kali环境运行volatility分析android内存文件,缺profile