0

Trying to implement Stripe Connect, and am getting the following error when I click the "connect to stripe" button.

The action 'passthru' could not be found for OmniauthCallbacksController

users/omniauth_callbacks_controller.rb

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
    def stripe_connect
        @user = current_user
        if @user.update_attributes({
          provider: request.env["omniauth.auth"].provider,
          uid: request.env["omniauth.auth"].uid,
          access_code: request.env["omniauth.auth"].credentials.token,
          publishable_key: request.env["omniauth.auth"].info.stripe_publishable_key
        })
          # anything else you need to do in response..
          sign_in_and_redirect @user, :event => :authentication
          set_flash_message(:notice, :success, :kind => "Stripe") if is_navigational_format?
        else
          session["devise.stripe_connect_data"] = request.env["omniauth.auth"]
          redirect_to new_user_registration_url
        end
      end
end

models/user.rb

  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable, :omniauthable, :omniauth_providers => [:stripe_connect]

routes.rb

devise_for :users, controllers: { registrations: 'users/registrations', :omniauth_callbacks => "users/omniauth_callbacks" }

gemfile.rb

gem 'omniauth-stripe-connect'

initializers/stripe.rb

Rails.configuration.stripe = {
  :publishable_key => ENV['PUBLISHABLE_KEY'],
  :secret_key      => ENV['SECRET_KEY']
}
Stripe.api_key = Rails.configuration.stripe[:secret_key]

initializers/devise.rb

config.omniauth :stripe_connect,
  ENV['STRIPE_CONNECT_CLIENT_ID'],
  ENV['STRIPE_SECRET_KEY'],
  :scope => 'read_write',
  :stripe_landing => 'register'

button link:

<%= link_to image_tag('blue-on-light.png'), user_stripe_connect_omniauth_authorize_path(:stripe_connect) %>

As I understand it with my noob Ruby mind, I need to define 'passthru'? how do I define it though? when I enter:

def passthru
end

the link doesn't work / the page reloads itself. Haven't been able to find a solution on here. What am I missing?

EDIT:

Changed my connect to stripe link to:

 <%= link_to image_tag('blue-on-light.png'), "/users/auth/stripe_connect" %>

The link takes me to the connect to stripe page, but when I click the "connect to stripe" button, the page cant be found, and doesn't load or redirect.

ChrisJC2017
  • 49
  • 11

1 Answers1

1

Can you try changing

# app/controllers/omniauth_callbacks_controller.rb
class OmniauthCallbacksController < ApplicationController
    def stripe_connect
    ....

to

class OmniauthCallbacksController < Devise::OmniauthCallbacksController 
    def stripe_connect
      @user = User.find_for_stripe_connect(request.env['omniauth.auth'], current_user)
      set_notice_and_redirect
    end

    private

    def set_notice_and_redirect          
      if @user.persisted?
          flash[:notice] = 'Successfully signed in'
          set_flash_message(:notice, :success, :kind => "Stripe") if is_navigational_format?
        else
          session["devise.stripe_connect_data"] = request.env["omniauth.auth"]
          redirect_to new_user_registration_url
      end
    end
end

and in your user model

# Checks if user exists, otherwise create it
def self.find_for_stripe_connect(access_token, _ = nil)
  data = access_token.info
  user = User.where(email: data['email']).first_or_create(
    email: data['email'],
    password: Devise.friendly_token[0, 20],
    provider: request.env["omniauth.auth"].provider,
    uid: request.env["omniauth.auth"].uid,
    access_code: request.env["omniauth.auth"].credentials.token,
    publishable_key: request.env["omniauth.auth"].info.stripe_publishable_key
  )
  user
end

and also sing in path

<%= link_to image_tag('blue-on-light.png'), user_stripe_connect_omniauth_authorize %>

I think you don't need to define a passthru action. If you see the below two in the routes it can work. Authorize path is for redirecting user to stripe and callback is for redirecting user from stripe back to your site

$ rake routes

user_stripe_connect_omniauth_authorize /auth/stripe_connect(.:format)  ....
user_stripe_connect_omniauth_callback /auth/stripe_connect/callback(.:format) ....
Onur Kucukkece
  • 1,708
  • 1
  • 20
  • 46
  • had just made some similar changes as you posted. I changed my route, and user/OmniauthCallbacksController.rb, but now I'm just getting the error: Not found. Authentication passthru. – ChrisJC2017 Feb 04 '17 at 21:40
  • you may also need to set omniauth provider in user model http://stackoverflow.com/a/22246487/522897 , for your provider omniauth_providers: [:stripe_connect] – Onur Kucukkece Feb 04 '17 at 21:43
  • Tried that, and added above in the question details. same error persists. – ChrisJC2017 Feb 04 '17 at 21:46
  • changing to sign in path just brings the user back to the homepage with the notice "you are already signed in" – ChrisJC2017 Feb 04 '17 at 22:10
  • I added another button to the bottom of my question. it works, but i don't think it has any connection between the user and the app? I had this button originally in place before I installed any type of omniauth for stripe. Could be my own confusion at to whether this is for standalone, or managed. – ChrisJC2017 Feb 04 '17 at 22:16
  • updated my answer again, made a little bit improvement, might come in handy – Onur Kucukkece Feb 04 '17 at 22:25
  • Thanks for helping so far. That resulted in the same result, clicking just returned the user to the home page "already signed in". I think i might be confused as to the purpose of omniauth with stripe? Is it a necessary part of stripe connect? Or is it just a "sign in with stripe" type of sign up. As I said above, I had another button link which seemed to work fine, and sent the user through the stripe with process just fine. Some tutorials use omniauth, and some don't. I'm trying to set up a marketplace with standalone. Do I even need to use omniauth? – ChrisJC2017 Feb 04 '17 at 23:21
  • changing my link to: <%= link_to image_tag('blue-on-light.png'), "/users/auth/stripe_connect" %> now goes to the stripe connect account screen, but now when I click the connect my stripe account, it gives me a 404 error, and cant find the page. – ChrisJC2017 Feb 05 '17 at 13:43
  • you have probably set a return url somewhere in stripe account, that must match the one that redirects to after hitting the connect button in stripe screen – Onur Kucukkece Feb 05 '17 at 17:06
  • Updated my answer again with new sign in path. Previous one was an irrelevant path, sorry fır the mislead. – Onur Kucukkece Feb 05 '17 at 17:41
  • no worries, I had assumed that it was an irrelevant path and found the correct one in my rake routes. the issue was my callback in the stripe settings. it had somehow been changed to localhost:3003.... Still though, I'm not sure the proper response when the callback happens. for me it redirects to the correct place, and a flash notification appears which says "you are already signed in". is that correct? – ChrisJC2017 Feb 05 '17 at 22:48
  • Actually, disregard that last comment. The stripe icon is visible on the browser throughout the users current session so it appears to have worked. Thank you very much for you patience and help!! – ChrisJC2017 Feb 05 '17 at 22:59
  • I'm glad it worked and I think you did not put that port 3003 into the URL because that typo is made in the documentation of omniauth-stripe-connect gem. You probably just copied it like everyone else. – Onur Kucukkece Feb 06 '17 at 09:53