require 'oauth'
require 'oauth/consumer'
gem 'herestomwiththeweather-oscurrency-ruby-api-wrapper'
require 'oscurrency'
class PaymentsController < ApplicationController
before_filter :find_article
before_filter :setup
# GET /payments
# GET /payments.xml
def index
@payments = Payment.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @payments }
end
end
# GET /payments/1
# GET /payments/1.xml
def show
@payment = Payment.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @payment }
end
end
# GET /payments/new
# GET /payments/new.xml
def new
@payment = Payment.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @payment }
end
end
# GET /payments/1/edit
def edit
@payment = Payment.find(params[:id])
@request_token = OAuth::RequestToken.new(@consumer,session[:req_token],session[:req_token_secret])
hr = @payment.wire_transfer(@@oscurrency_my_id, @@oscurrency_server, @request_token.get_access_token)
if hr
flash[:notice] = 'Payment succeeded.'
else
flash[:error] = 'Payment failed.'
end
redirect_to(@article)
end
# POST /payments
def create
@payment = Payment.new
@payment.article = @article
@payment.amount = 1.00
if @payment.save
@request_token = @consumer.get_request_token
session[:req_token] = @request_token.token
session[:req_token_secret] = @request_token.secret
callback_url = edit_article_payment_url(@article,@payment)
redirect_to "#{@@oscurrency_server}/oauth/authorize?oauth_token=#{@request_token.token}&oauth_callback=#{callback_url}"
#redirect_to @request_token.authorize_url
else
flash[:notice] = 'Payment failed.'
redirect_to(@article)
end
end
# PUT /payments/1
# PUT /payments/1.xml
def update
@payment = Payment.find(params[:id])
respond_to do |format|
if @payments.update_attributes(params[:payment])
flash[:notice] = 'Payments was successfully updated.'
format.html { redirect_to(@payment) }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @payment.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /payments/1
# DELETE /payments/1.xml
def destroy
@payment = Payment.find(params[:id])
@payment.destroy
respond_to do |format|
format.html { redirect_to(payment_url) }
format.xml { head :ok }
end
end
def find_article
article_id = params[:article_id]
@article = Article.find(article_id)
end
def setup
@consumer = Oscurrency::consumer(@@oscurrency_consumer_token,@@oscurrency_consumer_secret,@@oscurrency_server)
end
end