【问题标题】:Cannot get stripe charge work with app engine Golang无法使用应用引擎 Golang 进行条带充电工作
【发布时间】:2017-11-15 09:08:39
【问题描述】:

所以这是我的问题,我一直在关注应用程序引擎用户的 github 自述文件,以便在我的应用程序中实现条带化,但问题是我无法让它工作,因为似乎 http.DefaultTransport 和 http .DefaultClient 在 App Engine 中不可用。

我已经看到,在自述文件中,您向我们展示了如何使用应用引擎初始化 Stripe 客户端,但我找不到任何收费卡示例,所以这就是我进行此实现的原因。

我已经习惯了这个问题,因为我已经使用应用引擎很长时间了,但由于某种原因,我仍然得到这个错误的错误:

cannot use stripe.BackendConfiguration literal (type stripe.BackendConfiguration) as type stripe.Backend in assignment: stripe.BackendConfiguration does not implement stripe.Backend (Call method has pointer receiver)

代码如下:

func setStripeChargeClient(context context.Context, key string) *charge.Client {
    c := &charge.Client{}

    var b stripe.Backend
    b = stripe.BackendConfiguration{
        stripe.APIBackend, 
        "https://api.stripe.com/v1",
        urlfetch.Client(context),
    }

    c.Key = key
    c.B = b

    return c
}

得到那个 b 分配的错误......

我想不通的是为什么这个例子似乎在整个网络上都可以工作并且在我的应用程序中不起作用,如果你能告诉我这个,我会欠你的哈哈哈

然后这样称呼它

stripeClient := setStripeChargeClient(context, "sk_live_key »)

【问题讨论】:

    标签: google-app-engine go stripe-payments


    【解决方案1】:

    错误消息告诉您问题所在。 stripe.BackendConfiguration 类型没有实现 stripe.Backend 接口。该错误消息还提供了有用的提示,即缺少的 Call 方法位于指针接收器上。

    解决方法是使用指针值:

    b = &stripe.BackendConfiguration{  // note the &
        stripe.APIBackend, 
        "https://api.stripe.com/v1",
        urlfetch.Client(context),
    }
    

    请参阅specification regarding method sets。值接收器的方法集不包括指针接收器方法。

    【讨论】:

    【解决方案2】:

    好的,这样终于得到了解决方案:

    func setStripeChargeClient(context context.Context, key string) *charge.Client {
           c := &charge.Client{}
    
           //SETTING THE CLIENT WITH URL FETCH PROPERLY
           fetch := &http.Client{
                  Transport: &urlfetch.Transport{
                  Context: context,
            },
           }
    
         //USE SetHTTPClient method to switch httpClient
         stripe.SetHTTPClient(fetch)
    
         var b stripe.Backend
         b = &stripe.BackendConfiguration{stripe.APIBackend, 
         "https://api.stripe.com/v1",fetch}
    
         c.Key = key
         c.B = b
    
         return c
    }
    

    【讨论】:

      【解决方案3】:

      这是我根据 Stripe 的 GAE 建议的解决方案:

      func myHandler(w http.ResponseWriter, r *http.Request) {
          c := appengine.NewContext(r)
          myStripeKey := "pk_test_ABCDEF..."
          httpClient := urlfetch.Client(c)
          stripeClient := client.New(myStripeKey, stripe.NewBackends(httpClient))
          chargeParams := &stripe.ChargeParams{
              Amount:    uint64(totalCents),
              Currency:  "usd",
              Desc:      description,
              Email:     email,
              Statement: "MyCompany",
          }
          chargeParams.SetSource(token)
          charge, chargeErr := stripeClient.Charges.New(chargeParams)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-16
        • 2013-09-16
        • 1970-01-01
        • 1970-01-01
        • 2015-05-25
        • 1970-01-01
        相关资源
        最近更新 更多