获取Stripe在Golang中的所有计划数组。

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

Get array of all plans of Stripe in Golang

问题

我正在尝试使用Stripe的Golang API获取我Stripe账户中存在的所有计划列表。根据这里提供的文档:https://stripe.com/docs/api/go#list_plans,它应该返回所有计划的列表。但是它只返回了一个计划的详细信息。

以下是我的代码:

package main

import (
	"github.com/gin-gonic/gin"
	"github.com/stripe/stripe-go"
	"github.com/stripe/stripe-go/plan"
)

func main() {
	router := gin.Default()
	stripe.Key = "stripe_api_key"

	router.GET("/plans", func(c *gin.Context) {
		plans := GetAllPlans()
		c.JSON(200, gin.H{"plans": plans})
	})
	router.Run(":8080")
}

func GetAllPlans() (plans *stripe.Plan) {
	plans = &stripe.Plan{}
	params := &stripe.PlanListParams{}

	it := plan.List(params)
	for it.Next() {
		plans = it.Plan()
	}
	return
}

有趣的是,我发现文档中提供的Golang的响应示例与其他语言(如PHP、Ruby等)有些不同。对于除了Go和.NET之外的语言,它返回一个计划数组,但对于Go和.NET,它只返回一个计划。所以我不确定这是API的默认行为还是某个bug。

任何形式的帮助都将不胜感激。

谢谢!

英文:

I am trying to get list of all plans that exist in my Stripe account using Stripes's Golang API. As per documentation provided here: <https://stripe.com/docs/api/go#list_plans> it should return a list of all plans. But its returning me only a single plan details.

Here is my code:

package main

import (
    &quot;github.com/gin-gonic/gin&quot;
    &quot;github.com/stripe/stripe-go&quot;
    &quot;github.com/stripe/stripe-go/plan&quot;

)
func main(){
    router := gin.Default()
    stripe.Key = &quot;stripe_api_key&quot;

    router.GET(&quot;/plans&quot;, func(c *gin.Context) {
	    plans := GetAllPlans()
	    c.JSON(200, gin.H{	&quot;plans&quot;: plans, })
    })
    router.Run(&quot;:8080&quot;)
}

func GetAllPlans() (plans *stripe.Plan){
    plans = &amp;stripe.Plan{}
    params := &amp;stripe.PlanListParams{}

    it := plan.List(params)
    for it.Next() {		    
        plans = it.Plan()
    }
    return 
}

What's interesting I have found is the response example provided in the documentation for Golang is somewhat different from other languages like PHP, Ruby etc. For languages other than Go and .NET its returning an array of plans but for Go and .NET its returning a single plan. So I am not sure it is api's default behaviour or some bug.

Any kind of help will be appreciated.

Thanks!

答案1

得分: 1

嘿,我找到了我的问题的答案:

我应该使用接口而不是结构实例:

func GetAllPlans() (interface{}) {
    var plans []interface{}
    params := &stripe.PlanListParams{}

    it := plan.List(params)
    for it.Next() {
        plans = append(plans, it.Plan())
    }
    return plans
}

感谢 @u_mulder 提供的有用提示!

英文:

Hey I found the answer to my question:

I should use a interface instead of structure instance:

func GetAllPlans() (interface{}){
	var plans []interface{}
	params := &amp;stripe.PlanListParams{}

	it := plan.List(params)
	for it.Next() {		    
	    plans = append(plans, it.Plan())
	}
	return plans
}

Thanks @u_mulder for a useful hint!

huangapple
  • 本文由 发表于 2017年8月23日 15:34:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/45833413.html
匿名

发表评论

匿名网友

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

确定