首页 > 解决方案 > 如何将 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

标签: ruby-on-railsrubystripe-payments

解决方案


state将参数作为链接的一部分传递/oauth/authorize

从条纹的参考

我们将传回给您的任意字符串值,对 CSRF 保护很有用。

然后,当 Stripe 重定向到您的 时,这将包含在查询字符串中redirect_uri,您可以使用它在您的应用程序中进一步重定向或处理。


推荐阅读