无法从Stripe获取卡片品牌。

huangapple go评论76阅读模式
英文:

Can't get card brand from 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 column 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) {
// 从配置中获取密钥并分配给Stripe
stripe.Key = a.cnf.Stripe.SecretKey

// 创建一个客户:
customerParams := &stripe.CustomerParams{
    Desc: userID,
}
customerParams.SetSource(stripeToken)

customer, _ := customer.New(customerParams)

return customer, nil

}

我该如何让它询问卡的品牌?

英文:
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

得分: 0

根据Stripe文档的说明,default_card字段默认情况下不会展开。你可以通过在CreateUserID中添加以下代码来展开它:

customerParams.Expand("default_source")
英文:

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")

huangapple
  • 本文由 发表于 2017年6月14日 11:55:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/44535143.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定