How to pass id to stripe redirect uri

I want to the redirect uri to return to the submission path for a specific task but I don't know how to send an id to the stripe redirect uri.

I have currently set the redirect uri to http://localhost:3000/stripe/connect. But I want to be able to send through task id to the stripe connect action so that I can redirect to the specific task submission path

Submissions/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 %>

Users Helper:

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

Stripe Controller:

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

Config Routes:

get "stripe/connect", to: "stripe#connect", as: :stripe_connect

Upvotes: 1

Views: 1313

Answers (2)

duck
duck

Reputation: 5470

Pass a state parameter as part of your /oauth/authorize link.

From Stripe's reference:

An arbitrary string value we will pass back to you, useful for CSRF protection.

This will then be included in the query string when Stripe redirects to your redirect_uri, and you can use it to further redirect or process in your application.

Upvotes: 2

Armand Fardeau
Armand Fardeau

Reputation: 98

If I understand right, you want to send a task_id as a parameter to stripe and get it back from the response. Have you tried Metadata in your request? you could parse the response to find them.

Upvotes: 0

Related Questions