亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我如何從條帶化訂閱響應對象中正確獲取結構項?

我如何從條帶化訂閱響應對象中正確獲取結構項?

Go
郎朗坤 2023-02-06 10:29:26
我正在嘗試從條帶響應對象的結構中獲取一些數據以進行訂閱。這是響應對象stribe 訂閱對象的結構的鏈接這是我所擁有的以及我正在嘗試做的type SubscriptionDetails struct {    CustomerId             string  `json:"customer_id"`    SubscritpionId         string  `json:"subscritpion_id"`    StartAt                time.Time  `json:"start_at"`    EndAt                  time.Time  `json:"end_at"`    Interval               string  `json:"interval"`    Plan                   string  `json:"plan"`    PlanId                 string  `json:"plan_id"`    SeatCount              uint8  `json:"seat_count"`    PricePerSeat           float64  `json:"price_per_seat"`}func CreateStripeSubscription(CustomerId string, planId string) (*SubscriptionDetails, error) {    stripe.Key = StripeKey    params := &stripe.SubscriptionParams{    Customer: stripe.String(CustomerId),    Items: []*stripe.SubscriptionItemsParams{        &stripe.SubscriptionItemsParams{        Price: stripe.String(planId),        },    },    }    result, err := sub.New(params)    if err != nil {        return nil, err    }    data := &SubscriptionDetails{}    data.CustomerId           = result.Customer    data.SubscritpionId       =  result.ID    data.StartAt              =  result.CurrentPeriodStart    data.EndAt                =  result.CurrentPeriodEnd    data.Interval             =  result.Items.Data.Price.Recurring.Interval    data.Plan                 =  result.Items.Data.Price.Nickname    data.SeatCount            =  result.Items.Data.Quantity    data.PricePerSeat         =  result.Items.Data.Price.UnitAmount    return data, nil    }有些項目很容易直接獲得,就像ID我很容易獲得的字段一樣result.ID,沒有任何投訴,但對于其他項目,這里是錯誤消息cannot use result.CurrentPeriodStart (type int64) as type time.Time in assignment...cannot use result.Customer (type *stripe.Customer) as type string in assignment...result.Items.Data.price undefined (type []*stripe.SubscriptionItem has no field or method price)那么我如何獲取和的數據data.CustomerId呢data.PricePerSeat?
查看完整描述

3 回答

?
心有法竹

TA貢獻1866條經驗 獲得超5個贊

讓我們首先回顧一下幕后工作的代碼,返回的是什么,然后我們一次一個地查看問題。

當我們用它調用sub.New()方法params時返回Subscription類型

注意:我只會顯示類型的有限定義,因為添加完整的結構會使答案變大,而不是特定于問題上下文

讓我們看看Subscription類型

type Subscription struct {

  ...

  // Start of the current period that the subscription has been invoiced for.

  CurrentPeriodStart int64 `json:"current_period_start"`

  // ID of the customer who owns the subscription.

  Customer *Customer `json:"customer"`

  ...

  // List of subscription items, each with an attached price.

  Items *SubscriptionItemList `json:"items"`

  ...

}

讓我們看一下第一個錯誤

cannot use result.CurrentPeriodStart (type int64) as type time.Time in assignment

根據Subscription我們可以看到CurrentPeriodStart的類型是類型int64,而你試圖將它設置為類型的StartAt字段,因為類型不同,一種類型不能分配給其他類型,為了解決這個問題,我們需要明確地將它轉換為可以這樣做:SubscriptionDetailstime.Timetime.Time

data.StartAt = time.Unix(result.CurrentPeriodStart, 0)

time.Unixtime.Time方法從傳遞的值創建類型int64并返回我們可以分配給StartAt字段的類型

現在讓我們繼續第二個錯誤

cannot use result.Customer (type *stripe.Customer) as type string in assignment

正如我們從Subscription定義Customer字段中看到的那樣,*Customer它不是字符串類型,因為您試圖將類型分配給*Customer字符串CustomerId類型的字段,這不可能導致上述錯誤,引用的數據不正確那么正確的數據在哪里正確的數據在帶有字段的*Customer類型中可用ID,可以按如下方式檢索

data.CustomerId = result.Customer.ID

讓我們回顧一下最后一個錯誤

result.Items.Data.price undefined (type []*stripe.SubscriptionItem has no field or method price)

同樣,如果我們查看Subscription定義,我們可以看到Items字段是類型*SubscriptionItemList類型,如果我們查看*SubscriptionItemList定義

type SubscriptionItemList struct {
    APIResource
    ListMeta
    Data []*SubscriptionItem `json:"data"`}

它包含一個字段名稱DataData類型[]*SubscriptionItem注意它是 slice []of *SubscriptionItem,這是錯誤的原因,因為Data字段是 slice of*SubscriptionItem我們可以解決問題如下:

data.PricePerSeat = result.Items.Data[0].price.UnitAmount

我想指出的可能發生的錯誤很少,請繼續閱讀下面的內容以解決這些問題

現在讓我們看看*SubscriptionItem定義

type SubscriptionItem struct {
  ...
  Price *Price `json:"price"`
  ...
}

它包含Price名稱以大寫字母開頭的字段通知,在共享代碼中它以小寫字母引用,這可能會導致另一個問題,最后如果我們查看Price定義

type Price struct {
  ...  // The unit amount in %s to be charged, represented as a whole integer if possible. Only set if `billing_scheme=per_unit`.
  UnitAmount int64 `json:"unit_amount"`

  // The unit amount in %s to be charged, represented as a decimal string with at most 12 decimal places. Only set if `billing_scheme=per_unit`.
  UnitAmountDecimal float64 `json:"unit_amount_decimal,string"`
  ...
}

它包含UnitAmount我們正在使用的字段,但這里有一個問題UnitAmount是類型,int64但類型PricePerSeat是類型,float64將不同的類型分配給彼此會再次導致錯誤,因此您可以轉換int64float64或者更好的是您可以使用包含的類型中UnitAmountDecimal可用的字段格式Price相同的數據float64將減少我們在使用UnitAmount字段時必須進行的顯式轉換,因此根據我們得到以下解決方案的解釋

data.PricePerSeat = result.Items.Data[0].Price.UnitAmountDecimal


查看完整回答
反對 回復 2023-02-06
?
千巷貓影

TA貢獻1829條經驗 獲得超7個贊

查看您提到的3個錯誤

  1. cannot use result.CurrentPeriodStart (type int64) as type time.Time in assignment

的類型result.CurrentPeriodStart是 int64 并且您試圖將其設置為類型的字段time.Time,這顯然會失敗。 API 以 unix 格式發送時間,您需要對其進行解析以將其轉換為 time.Time。對其他時間字段也執行此操作

data.StartAt = time.Unix(result.CurrentPeriodStart, 0)
  1. cannot use result.Customer (type *stripe.Customer) as type string in assignment

與上述類似的問題,當您嘗試將其設置為 type 的字段時,該字段是 typeresult.Customer的。客戶 ID 是結構中的一個字段*stripe.CustomerstringCustomer

data.CustomerId = result.Customer.ID
  1. result.Items.Data.price undefined (type []*stripe.SubscriptionItem has no field or method price)

stripe.SubscriptionItem結構沒有字段price。我不確定你在這里想要什么。我建議閱讀訂閱對象文檔。


查看完整回答
反對 回復 2023-02-06
?
喵喔喔

TA貢獻1735條經驗 獲得超5個贊

訪問價格result.Items.Data[0].Price.UnitAmount

如果您使用調試器,只需sub.New在行后放置一個斷點并探索結果變量的內容


查看完整回答
反對 回復 2023-02-06
  • 3 回答
  • 0 關注
  • 163 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號