2012-03-01 12 views
4

PaypalDeviseユーザー登録プロセス内に統合したいと思います。私が望むのは、標準レールが1つのdevise resourceに基づいていることです。これには、カスタムフィールドがユーザーモデルに属するものもあります。DeviseとPaypalのユーザー登録

ユーザーがこれらのフィールドに記入してサインアップをクリックするとPaypalにリダイレクトされ、彼がpaypalからクリアして私たちのサイトに戻ると、ユーザーデータを作成する必要があります。

ユーザーがpaypalに記入しても同じ時間内に当サイトに戻ってこない場合は、Paypalにリダイレクトする前にユーザーの記録を残す必要があります。

このため、ユーザーモデルにフラグを作成し、Paypal IPNを使用し、通知されたユーザートランザクションがそのフラグを設定したときに使用できます。

しかし、ユーザーがPayPalにリダイレクトしても取引を完了しなかった場合、私が彼が登録して再びサインアップすると、私たちのモデルは電子メールが既にテーブルに存在するとは言いません。

これらのシナリオをすべて処理するにはどうすればいいですか?宝石やプラグインが利用できますか?

答えて

7

ここでは、プロセス全体を実行するための詳細コードを掲載しています。

registration_controller.rb

module Auth 
    class RegistrationController < Devise::RegistrationsController 
    include Auth::RegistrationHelper 

    def create 
     @user = User.new params[:user] 
     if @user.valid? 
     redirect_to get_subscribe_url(@user, request) 
     else 
     super 
     end 
    end 
    end 
end 

registration_helper.rb

module Auth::RegistrationHelper 
    def get_subscribe_url(user, request) 
    url = Rails.env == "production" ? "https://www.paypal.com/cgi-bin/webscr/?" : "https://www.sandbox.paypal.com/cgi-bin/webscr/?" 
    url + { 
     :ip => request.remote_ip, 
     :cmd => '_s-xclick', 
     :hosted_button_id => (Rails.env == "production" ? "ID_FOR_BUTTON" : "ID_FOR_BUTTON"), 
     :return_url => root_url, 
     :cancel_return_url => root_url, 
     :notify_url => payment_notifications_create_url, 
     :allow_note => true, 
     :custom => Base64.encode64("#{user.email}|#{user.organization_type_id}|#{user.password}") 
    }.to_query 
    end 
end 

payment_notification_controller.rb

class PaymentNotificationsController < ApplicationController 
    protect_from_forgery :except => [:create] 
    layout "single_column", :only => :show 

    def create 
    @notification = PaymentNotification.new 
    @notification.transaction_id = params[:ipn_track_id] 
    @notification.params = params 
    @notification.status = "paid" 
    @custom = Base64.decode64(params[:custom]) 
    @custom = @custom.split("|") 
    @user = User.new 
    @user.email = @custom[0] 
    @user.organization_type_id = @custom[1].to_i 
    @user.password = @custom[2] 
    if @user.valid? 
     @user.save 
     @notification.user = @user 
     @notification.save 
     @user.send_confirmation_instructions 
    end 
    render :nothing => true 
    end 

    def show 
    end 
end 
関連する問題