ruby-on-rails - 如何将 id 传递给条带重定向 uri
问题描述
我希望重定向 uri 返回到特定任务的提交路径,但我不知道如何将 id 发送到条带重定向 uri。
我目前已将重定向 uri 设置为http://localhost:3000/stripe/connect。但我希望能够通过任务 ID 发送到条带连接操作,以便我可以重定向到特定的任务提交路径
提交/new.html.erb:
<% if current_user.stripe_user_id %>
<h2>Submit</h2>
<%= form_for [:task, @submission] do |f| %>
<%= f.text_area :description, placeholder: "File description" %>
<%= f.file_field :files, multiple: true %>
<%= f.submit "Submit", class: "btn button" %>
<% end %>
<% else %>
<h6> To Accept Payments for Tasks:</h6>
<%= link_to image_tag("light-on-dark.png"), stripe_button_link, :data => {:task_id => @task.id} %>
<% end %>
用户助手:
def stripe_button_link
stripe_url = "https://connect.stripe.com/oauth/authorize"
redirect_uri = stripe_connect_url
client_id = ENV["STRIPE_CLIENT_ID"]
"#{stripe_url}?response_type=code&redirect_uri=# {redirect_uri}&client_id=#{client_id}&scope=read_write"
end
条纹控制器:
def connect
response = HTTParty.post("https://connect.stripe.com/oauth/token",
query: {
client_secret: ENV["STRIPE_SECRET_KEY"],
code: params[:code],
grant_type: "authorization_code"
}
)
@task = Task.find(params[:task_id])
if response.parsed_response.key?("error")
redirect_to new_task_submission_path(@task),
notice: response.parsed_response["error_description"]
else
stripe_user_id = response.parsed_response["stripe_user_id"]
current_user.update_attribute(:stripe_user_id, stripe_user_id)
redirect_to new_task_submission_path(@task),
notice: 'User successfully connected with Stripe!'
end
end
配置路线:
get "stripe/connect", to: "stripe#connect", as: :stripe_connect
解决方案
state
将参数作为链接的一部分传递/oauth/authorize
。
从条纹的参考:
我们将传回给您的任意字符串值,对 CSRF 保护很有用。
然后,当 Stripe 重定向到您的 时,这将包含在查询字符串中redirect_uri
,您可以使用它在您的应用程序中进一步重定向或处理。
推荐阅读
- simulation - AnyLogic - 如何用产品建模盒子
- css - CSS 网格布局:是否可以使项目之一在网格上 100% 宽度?
- sql - 查询未在 MS Access 中运行。(Join 和 From 语句)
- performance - Azure REST API 和高容量问题
- scrapy - 如何最好地处理 Scrapy GeneratorExits 并重试失败的请求?
- .net-core - EFCore 将 2 个实体映射到同一个表 - 无法添加新条目
- sql - 如果至少一个值属于不同的分组,则标记是或否
- amazon-web-services - Route53 名称服务器的 DNS_PROBE_FINISHED_NXDOMAIN 错误
- node.js - 错误:元素类型无效:需要字符串(对于内置组件)或类/函数(对于复合组件)
- grails - 重用没有继承的域类的属性、约束和行为