用户在我的应用程序中注册Stripe帐户后,会将其重定向到我的localhost,并在网址中添加authorization_code
。然后我应该用我的client_secret
和authorization_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。我需要设置路线吗?我可以在后台进行此操作吗?
对/oauth/token
的调用是您在后端/控制器上进行的,以便从Stripe获取授权令牌,以便代表已连接的帐户拨打电话。一旦他们授权您的平台连接到他们的帐户,您的用户就不需要参与该呼叫。
既然你正在使用Ruby,我建议使用stripe-ruby(官方库)。这有使用Oauth和Stripe Connect的built-in methods。
解!把它写成一个模块。它不需要实例化,因此不需要使用类。使用stripe ruby library也很有帮助
我写了一个看起来像这样的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]
作为网址的一部分发送,所以