0

在用户使用我的应用注册Stripe帐户后,他们会被重定向到我的本地主机,并且authorization_code会在 url 中添加一个。然后我应该向我的client_secretauthorization_code他们的 API 端点发出 POST 请求。文档提供的代码说要做这样的事情:

curl https://connect.stripe.com/oauth/token \
 -d client_secret=blahblah \
 -d code="{AUTHORIZATION_CODE}" \
 -d grant_type=authorization_code

但是......我在哪里做这个,究竟是什么?在控制器中?像这样?

def post_to_endpoint(endpoint)
 require 'json'

 begin
  uri = URI.parse(endpoint)

  post_params = {
    client_secret: "client_secret",
    code: "{AUTHORIZATION_CODE}",
    grant_type: authorization_code
  }

  req = Net::HTTP::Post.new(uri.path)
  req.body = JSON.generate(post_params)
  req["Content-Type"] = "application/json"
  http = Net::HTTP.new(uri.host, uri.port)
  response = http.start { |htt| htt.request(req) }
 rescue => e
  puts "failed #{e}"
 end
end

在第 3 步结束时,用户被重定向到我的应用程序上的 GET 路由,然后我的应用程序应该向 Stripe 端点发出 POST。我需要设置路线吗?我可以让这个动作在后台发生吗?

4

2 回答 2

1

调用/oauth/token是您在后端/控制器上进行的,以从 Stripe 获取授权令牌,您需要该令牌才能代表连接的帐户进行调用。一旦他们授权您的平台连接到他们的帐户,您的用户就不需要参与该呼叫。

由于您使用的是 Ruby,我建议您使用stripe-ruby(官方库)。它具有将 Oauth 与 Stripe Connect 结合使用的内置方法

于 2019-04-17T19:10:55.650 回答
0

解决方案!把它写成一个模块。它不需要持久化到数据库。使用stripe ruby​​ 库也很有帮助

我写了一个如下所示的 stripe_oauth.rb:

module StripeOauth
 def self.connect(code)
  Stripe.api_key = ENV["STRIPE_SECRET_KEY"]
  Stripe::OAuth.token( {code: code, grant_type: "authorization_code" } )
 end
end

然后我从控制器动作中调用它,Stripe 将我重定向到:

def welcome
 StripeOauth.connect(params[:code])
end

params[:code]作为 url 的一部分发送,所以

于 2019-04-19T23:53:17.300 回答