2017-06-14 58 views
0
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 
} 

我試圖獲取該卡的品牌默認卡的用戶,即我插入到我的數據庫中的客戶,但這總是導致崩潰,因爲空指針。無法從條紋中獲取卡品牌

雖然我不明白爲什麼。在條紋安慰客戶有一個默認的卡顯示「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 
} 

我怎樣才能使它索要卡牌?

+0

什麼指針爲空?你能分享這個錯誤嗎? – smarx

+0

添加日誌,雖然沒有告訴我多少 –

+0

然後我想你應該添加一些'Println's並找出錯誤發生的位置? – smarx

回答

0

根據the Stripe documentationdefault_card字段默認情況下不會擴展。您可以通過將此行添加到CreateUserID來擴展它:

customerParams.Expand("default_source")