VRFS-2480 : Refactor and cleanup, incremental.
This commit is contained in:
parent
300426e2c9
commit
7f59067226
|
|
@ -1,170 +1,85 @@
|
|||
require 'recurly_client'
|
||||
class ApiRecurlyController < ApiController
|
||||
before_filter :api_signed_in_user
|
||||
before_filter :obtain_account, only: [:create_account, :update_account, :get_subscription, :update_billing_info]
|
||||
before_filter :create_client
|
||||
respond_to :json
|
||||
|
||||
# create Recurly account
|
||||
def create_account
|
||||
#logger.debug(params[:billing_info])
|
||||
|
||||
|
||||
current_user.recurly_code = @account.account_code
|
||||
current_user.save
|
||||
|
||||
if params[:billing_info] && params[:billing_info].any?
|
||||
@account.billing_info = params[:billing_info]
|
||||
@account.billing_info.save
|
||||
end
|
||||
|
||||
if @account.billing_info.errors.any?
|
||||
render json: { :message => @account.errors.inspect}
|
||||
end
|
||||
|
||||
rescue Recurly::Error, NoMethodError => e
|
||||
render json: { :message => e.inspect, error: e }, :status => 404
|
||||
@account = @client.find_or_create_account(current_user, params[:billing_info])
|
||||
puts "JSON: #{JSON.generate(@account)}"
|
||||
render :json=>account_json(@account)
|
||||
rescue RecurlyClientError => x
|
||||
render json: { :message => x.inspect, errors: x.errors }, :status => 404
|
||||
end
|
||||
|
||||
def delete_account
|
||||
@account = Recurly::Account.find(current_user.recurly_code)
|
||||
if @account.present?
|
||||
@account.destroy
|
||||
end
|
||||
@client.delete_account(current_user)
|
||||
render json: {}, status: 200
|
||||
rescue Recurly::Error, NoMethodError => e
|
||||
render json: { message: ValidationMessages::RECURLY_ERROR, error: e}, :status => 404
|
||||
rescue RecurlyClientError => x
|
||||
render json: { :message => x.inspect, errors: x.errors}, :status => 404
|
||||
end
|
||||
|
||||
# get Recurly account
|
||||
def get_account
|
||||
@account = Recurly::Account.find(current_user.recurly_code)
|
||||
render json: @account.to_json
|
||||
rescue Recurly::Error, NoMethodError => e
|
||||
render json: { message: ValidationMessages::RECURLY_ERROR, error: e}, :status => 404
|
||||
@account=@client.get_account(current_user)
|
||||
|
||||
render :json=>account_json(@account)
|
||||
rescue RecurlyClientError => e
|
||||
render json: { message: x.inspect, errors: x.errors}, :status => 404
|
||||
end
|
||||
|
||||
# update Recurly account
|
||||
def update_account
|
||||
@account.first_name = current_user.first_name
|
||||
@account.last_name = current_user.last_name
|
||||
@account.email = current_user.email
|
||||
@account.save
|
||||
|
||||
if @account.errors.any?
|
||||
render status: 404, json: { message: ValidationMessages::RECURLY_ACCOUNT_ERROR}
|
||||
else
|
||||
current_user.recurly_code = @account.account_code
|
||||
current_user.save
|
||||
render json: @account.to_json
|
||||
end
|
||||
rescue Recurly::Error, NoMethodError => e
|
||||
raise e
|
||||
render json: { message: ValidationMessages::RECURLY_ERROR, error: e}, :status => 404
|
||||
end
|
||||
|
||||
# get subscription
|
||||
def get_subscription
|
||||
render json: @account.subscriptions.last.to_json
|
||||
rescue Recurly::Error, NoMethodError => e
|
||||
render json: { message: ValidationMessages::RECURLY_ACCOUNT_ERROR, error: e}, :status => 404
|
||||
end
|
||||
|
||||
# create subscription
|
||||
def create_subscription
|
||||
@account=@client.update_account(current_user, params[:billing_info])
|
||||
render :json=>account_json(@account)
|
||||
rescue RecurlyClientError => x
|
||||
render json: { message: x.inspect, errors: x.errors}, :status => 404
|
||||
end
|
||||
|
||||
# get Billing Information
|
||||
def billing_info
|
||||
puts "billing_info1"
|
||||
if current_user.recurly_code.nil?
|
||||
puts "billing_info2"
|
||||
render json: { message: ValidationMessages::RECURLY_ACCOUNT_ERROR }, :status => 404
|
||||
else
|
||||
puts "billing_info3"
|
||||
@account = Recurly::Account.find(current_user.recurly_code)
|
||||
puts "billing_info3a"
|
||||
logger.debug("@account:#{@account}")
|
||||
puts "billing_infob#{@account}"
|
||||
billing_json = @account.billing_info.nil? ? {}.to_json : @account.billing_info.to_json
|
||||
render json: billing_json
|
||||
end
|
||||
rescue Recurly::Error, NoMethodError => e
|
||||
puts "billing_info4#{e}"
|
||||
logger.debug("Exception #{e}")
|
||||
render json: { message: ValidationMessages::RECURLY_ERROR, error: e}, :status => 404
|
||||
@account = @client.get_account(current_user)
|
||||
# @billing = @account.billing_info
|
||||
# @billing ||= @account
|
||||
render :json=> account_json(@account)
|
||||
rescue RecurlyClientError => x
|
||||
render json: { message: x.inspect, errors: x.errors}, :status => 404
|
||||
end
|
||||
|
||||
# update Billing Information
|
||||
def update_billing_info
|
||||
puts "update_billing_info with params: #{params.inspect}"
|
||||
if current_user.recurly_code.nil?
|
||||
render json: { message: ValidationMessages::RECURLY_ACCOUNT_ERROR }, :status => 404
|
||||
else
|
||||
billing_params = params[:billing_info]
|
||||
if !billing_params || billing_params[:first_name].blank? || billing_params[:last_name].blank? || billing_params[:number].blank? || billing_params[:year].blank? || billing_params[:month].blank? || billing_params[:verification_value].blank?
|
||||
render json: { message: ValidationMessages::RECURLY_PARAMETER_ERROR }, :status => 404
|
||||
else
|
||||
@account = Recurly::Account.find(current_user.recurly_code)
|
||||
@account.billing_info=billing_params
|
||||
#@account.billing_info.save
|
||||
@account.save
|
||||
|
||||
if @account.nil? || @account.errors.any?
|
||||
response.status = :unprocessable_entity
|
||||
end
|
||||
|
||||
render json: @account
|
||||
end
|
||||
end
|
||||
rescue Recurly::Error, NoMethodError => e
|
||||
raise e
|
||||
render json: { message: ValidationMessages::RECURLY_ERROR, error: e}, :status => 404
|
||||
@account=@client.update_billing_info(current_user, params[:billing_info])
|
||||
puts "Updated billing on account: #{@account}"
|
||||
render :json=> account_json(@account)
|
||||
rescue RecurlyClientError => x
|
||||
render json: { message: x.inspect, errors: x.errors}, :status => 404
|
||||
end
|
||||
|
||||
def place_order
|
||||
if current_user.recurly_code.nil?
|
||||
render json: { message: ValidationMessages::RECURLY_ACCOUNT_ERROR }, :status => 404
|
||||
else
|
||||
if params[:first_name].blank? || params[:last_name].blank? || params[:number].blank? || params[:year].blank? || params[:month].blank? || params[:verification_value].blank?
|
||||
render json: { message: ValidationMessages::RECURLY_PARAMETER_ERROR }, :status => 404
|
||||
end
|
||||
@account = Recurly::Account.find(current_user.recurly_code)
|
||||
@account.billing_info=billing_params
|
||||
@account.billing_info.save
|
||||
|
||||
# create subscription.
|
||||
|
||||
if @account.nil? || @account.errors.any?
|
||||
response.status = :unprocessable_entity
|
||||
end
|
||||
|
||||
render json: @account
|
||||
end
|
||||
rescue Recurly::Error, NoMethodError => e
|
||||
render json: { message: ValidationMessages::RECURLY_ERROR, error: e}, :status => 404
|
||||
@client.update_billing_info(current_user, params[:billing_info])
|
||||
render :json=>{}, :status=>200
|
||||
rescue RecurlyClientError => x
|
||||
render json: { message: x.inspect, errors: x.errors}, :status => 404
|
||||
end
|
||||
|
||||
private
|
||||
def obtain_account
|
||||
if current_user.recurly_code.nil?
|
||||
@account = Recurly::Account.create(
|
||||
account_code: current_user.id,
|
||||
email: current_user.email,
|
||||
first_name: current_user.first_name,
|
||||
last_name: current_user.last_name,
|
||||
address: {
|
||||
city: current_user.city,
|
||||
state: current_user.state,
|
||||
country: current_user.country
|
||||
}
|
||||
)
|
||||
else
|
||||
@account = Recurly::Account.find(current_user.recurly_code)
|
||||
end
|
||||
|
||||
if @account.nil?
|
||||
render json: {message: ValidationMessages::RECURLY_ACCOUNT_ERROR}, status: 404
|
||||
elsif @account.errors.any?
|
||||
render json: {message: @account.errors.inspect}, status: 404
|
||||
end
|
||||
def create_client
|
||||
@client = RecurlyClient.new
|
||||
end
|
||||
end
|
||||
|
||||
def account_json(account)
|
||||
{
|
||||
:first_name => account.first_name,
|
||||
:last_name => account.last_name,
|
||||
:email => account.email,
|
||||
:address1 => account.billing_info ? account.billing_info.address1 : nil,
|
||||
:address2 => account.billing_info ? account.billing_info.address2 : nil,
|
||||
:city => account.billing_info ? account.billing_info.city : nil,
|
||||
:state => account.billing_info ? account.billing_info.state : nil,
|
||||
:zip => account.billing_info ? account.billing_info.zip : nil,
|
||||
:country => account.billing_info ? account.billing_info.country : nil
|
||||
}
|
||||
end
|
||||
|
||||
end # class
|
||||
|
|
@ -57,7 +57,6 @@
|
|||
/ @end score filter
|
||||
-elsif :jamtrack==filter_label
|
||||
/ @begin availability filter
|
||||
%h3 hey
|
||||
=content_tag(:div, 'Availability:', :class => 'filter-element desc')
|
||||
=select_tag("#{filter_label}_availability", options_for_select([['Any', '']].concat(JamRuby::JamTrack::SALES_REGION), 'United States'), {:class => "easydropdown"})
|
||||
/ @end availability filter
|
||||
|
|
|
|||
|
|
@ -236,7 +236,7 @@ SampleApp::Application.routes.draw do
|
|||
match '/recurly/delete_account' => 'api_recurly#delete_account', :via => :delete
|
||||
match '/recurly/get_account' => 'api_recurly#get_account', :via => :get
|
||||
#match '/recurly/get_subscription' => 'api_recurly#get_subscription', :via => :get
|
||||
match '/recurly/update_account' => 'api_recurly#update_account', :via => :post
|
||||
match '/recurly/update_account' => 'api_recurly#update_account', :via => :put
|
||||
match '/recurly/billing_info' => 'api_recurly#billing_info', :via => :get
|
||||
match '/recurly/update_billing_info' => 'api_recurly#update_billing_info', :via => :put
|
||||
match '/recurly/place_order' => 'api_recurly#place_order', :via => :post
|
||||
|
|
|
|||
|
|
@ -6,15 +6,19 @@ module JamRuby
|
|||
|
||||
def create_account(current_user, billing_info=nil)
|
||||
options = account_hash(current_user, billing_info)
|
||||
account = nil
|
||||
begin
|
||||
account = Recurly::Account.create(options)
|
||||
raise RecurlyClientError, account.errors.inspect if account.errors.any?
|
||||
raise RecurlyClientError.new(account.errors) if account.errors.any?
|
||||
rescue Recurly::Error, NoMethodError => x
|
||||
puts "Errorrr: #{x.inspect}"
|
||||
raise RecurlyClientError, x.to_s
|
||||
else
|
||||
current_user.update_attribute(:recurly_code, account.account_code)
|
||||
account
|
||||
end
|
||||
if account
|
||||
current_user.update_attribute(:recurly_code, account.account_code)
|
||||
end
|
||||
end
|
||||
account
|
||||
end
|
||||
|
||||
def delete_account(current_user)
|
||||
|
|
@ -53,12 +57,14 @@ module JamRuby
|
|||
if (account.present?)
|
||||
begin
|
||||
account.billing_info=billing_info
|
||||
puts "Saving..."
|
||||
account.billing_info.save
|
||||
puts "...saved"
|
||||
rescue Recurly::Error, NoMethodError => x
|
||||
raise RecurlyClientError, x.to_s
|
||||
end
|
||||
|
||||
raise RecurlyClientError, account.errors.inspect if account.errors.any?
|
||||
raise RecurlyClientError.new(account.errors) if account.errors.any?
|
||||
else
|
||||
raise RecurlyClientError, "Could not find account to update billing info."
|
||||
end
|
||||
|
|
@ -98,6 +104,15 @@ module JamRuby
|
|||
end # class
|
||||
|
||||
class RecurlyClientError < Exception
|
||||
end
|
||||
attr_accessor :errors
|
||||
def initialize(data)
|
||||
if data.respond_to?('has_key?')
|
||||
self.errors = data
|
||||
else
|
||||
puts "2222"
|
||||
self.errors = {:message=>data.to_s}
|
||||
end
|
||||
end # initialize
|
||||
end # RecurlyClientError
|
||||
end # module
|
||||
|
||||
|
|
@ -1,72 +1,84 @@
|
|||
require 'spec_helper'
|
||||
require 'recurly_client'
|
||||
#require 'recurly/account'
|
||||
|
||||
describe ApiRecurlyController do
|
||||
describe ApiRecurlyController, :type=>:controller do
|
||||
render_views
|
||||
|
||||
let(:user) { FactoryGirl.create(:user) }
|
||||
let(:jamtrack) { FactoryGirl.create(:jam_track) }
|
||||
# let(:user) { FactoryGirl.create(:user) }
|
||||
# let(:jamtrack) { FactoryGirl.create(:jam_track) }
|
||||
|
||||
before(:each) do
|
||||
@user = FactoryGirl.create(:user)
|
||||
#@jamtrack = FactoryGirl.create(:jam_track)
|
||||
@billing_info = {}
|
||||
@billing_info[:first_name] = user.first_name
|
||||
@billing_info[:last_name] = user.last_name
|
||||
@billing_info[:first_name] = @user.first_name
|
||||
@billing_info[:last_name] = @user.last_name
|
||||
@billing_info[:address1] = 'Test Address 1'
|
||||
@billing_info[:address2] = 'Test Address 2'
|
||||
@billing_info[:city] = user.city
|
||||
@billing_info[:state] = user.state
|
||||
@billing_info[:country] = user.country
|
||||
@billing_info[:city] = @user.city
|
||||
@billing_info[:state] = @user.state
|
||||
@billing_info[:country] = @user.country
|
||||
@billing_info[:zip] = '12345'
|
||||
@billing_info[:number] = '4111-1111-1111-1111'
|
||||
@billing_info[:month] = '08'
|
||||
@billing_info[:year] = '2017'
|
||||
@billing_info[:verification_value] = '1111'
|
||||
controller.current_user = user
|
||||
@billing_info[:verification_value] = '111'
|
||||
@billing_info[:vat_number] = ''
|
||||
controller.current_user = @user
|
||||
end
|
||||
|
||||
after(:each) do
|
||||
if (user.recurly_code)
|
||||
@account = Recurly::Account.find(user.recurly_code)
|
||||
if (@user.recurly_code)
|
||||
@account = Recurly::Account.find(@user.recurly_code)
|
||||
if @account.present?
|
||||
@account.destroy
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
it "should create account" do
|
||||
it "should send correct error" do
|
||||
@billing_info[:number]='121'
|
||||
post :create_account, {:format => 'json', :billing_info=>@billing_info}
|
||||
response.should be_success
|
||||
response.status.should == 404
|
||||
body = JSON.parse(response.body)
|
||||
body[:billing_info].should_not be_nil
|
||||
puts "BODY: #{body}"
|
||||
puts "body.inspect: #{body.inspect}"
|
||||
body['errors'].should have(1).items
|
||||
body['errors']['number'].should_not be_nil
|
||||
end
|
||||
|
||||
it "should create account" do
|
||||
post :create_account, {:format => 'json'}
|
||||
response.should be_success
|
||||
end
|
||||
|
||||
it "should retrieve account" do
|
||||
post :create_account, {:format => 'json', :billing_info=>@billing_info}
|
||||
post :create_account, {:format => 'json'}
|
||||
response.should be_success
|
||||
|
||||
get :get_account
|
||||
body = JSON.parse(response.body)
|
||||
response.should be_success
|
||||
puts "body: #{body}"
|
||||
body['attributes']['email'].should eq(user.email)
|
||||
body['email'].should eq(@user.email)
|
||||
end
|
||||
|
||||
it "should update account" do
|
||||
post :create_account
|
||||
response.should be_success
|
||||
body = JSON.parse(response.body)
|
||||
body['attributes']['first_name'].should eq("Person")
|
||||
body['first_name'].should eq("Person")
|
||||
|
||||
user.update_attribute(:first_name, "Thing")
|
||||
post :update_account
|
||||
@user.update_attribute(:first_name, "Thing")
|
||||
controller.current_user = @user
|
||||
put :update_account
|
||||
body = JSON.parse(response.body)
|
||||
body['attributes']['first_name'].should eq("Thing")
|
||||
body['first_name'].should eq("Thing")
|
||||
|
||||
get :get_account, { :format => 'json'}
|
||||
response.should be_success
|
||||
body = JSON.parse(response.body)
|
||||
body['attributes']['first_name'].should eq("Thing")
|
||||
body['first_name'].should eq("Thing")
|
||||
end
|
||||
|
||||
# Note: We don't have any subscriptions yet:
|
||||
|
|
@ -75,17 +87,39 @@ describe ApiRecurlyController do
|
|||
get :get_subscription, { :format => 'json'}
|
||||
response.should be_success
|
||||
body = JSON.parse(response.body)
|
||||
puts body['attributes']
|
||||
puts body
|
||||
end
|
||||
|
||||
it "should update billing info" do
|
||||
post :create_account
|
||||
response.should be_success
|
||||
body = JSON.parse(response.body)
|
||||
body['attributes']['first_name'].should eq("Person")
|
||||
#pending "this fails in the controller spec only, possibly because it contacts an external site."
|
||||
|
||||
$enable_tracing = false
|
||||
$trace_out = open('trace.txt', 'w')
|
||||
|
||||
set_trace_func proc { |event, file, line, id, binding, classname|
|
||||
if $enable_tracing && event == 'call' && !file.start_with?("/Users/tangledpath/.ddrvm/")
|
||||
$trace_out.puts "#{file}:#{line} #{classname}##{id}"
|
||||
end
|
||||
}
|
||||
|
||||
$enable_tracing = true
|
||||
|
||||
post :create_account
|
||||
puts "BACK FROM ACCOUNT: #{response.body}"
|
||||
response.should be_success
|
||||
puts "BACK FROM ACCOUNT 1"
|
||||
body = JSON.parse(response.body)
|
||||
puts "BACK FROM ACCOUNT 2 #{body}"
|
||||
body['first_name'].should eq("Person")
|
||||
|
||||
puts "BACK FROM ACCOUNT 3"
|
||||
|
||||
@billing_info[:state] = "NE"
|
||||
puts "BEFORE BILLING"
|
||||
|
||||
put :update_billing_info, {:format => 'json', :billing_info=>@billing_info}
|
||||
puts "BACK FROM billing 1"
|
||||
|
||||
response.should be_success
|
||||
body = JSON.parse(response.body)
|
||||
puts "BILLING_ BODY: #{body}"
|
||||
|
|
@ -94,12 +128,7 @@ describe ApiRecurlyController do
|
|||
response.should be_success
|
||||
puts "response.body: #{response.body}"
|
||||
body = JSON.parse(response.body)
|
||||
puts "body: #{body}"
|
||||
|
||||
puts "body: #{body}"
|
||||
end
|
||||
|
||||
it "should place order" do
|
||||
put :place_order
|
||||
end
|
||||
|
||||
end
|
||||
end # spec
|
||||
|
|
|
|||
|
|
@ -25,14 +25,14 @@ describe RecurlyClient do
|
|||
@billing_info[:verification_value] = '111'
|
||||
end
|
||||
|
||||
# after(:all) do
|
||||
# if (@user.recurly_code)
|
||||
# account = Recurly::Account.find(@user.recurly_code)
|
||||
# if account.present?
|
||||
# account.destroy
|
||||
# end
|
||||
# end
|
||||
# end
|
||||
after(:each) do
|
||||
if (@user.recurly_code)
|
||||
account = Recurly::Account.find(@user.recurly_code)
|
||||
if account.present?
|
||||
account.destroy
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
it "can create account" do
|
||||
account = @client.create_account(@user, @billing_info)
|
||||
|
|
@ -40,6 +40,11 @@ describe RecurlyClient do
|
|||
@user.recurly_code.should eq(account.account_code)
|
||||
end
|
||||
|
||||
it "can create account with errors" do
|
||||
@billing_info[:verification_value] = '1111'
|
||||
expect {@client.create_account(@user, @billing_info)}.to raise_error(RecurlyClientError)
|
||||
end
|
||||
|
||||
describe "with account" do
|
||||
before :each do
|
||||
@account = @client.find_or_create_account(@user, @billing_info)
|
||||
|
|
@ -67,6 +72,9 @@ describe RecurlyClient do
|
|||
found = @client.get_account(@user)
|
||||
found.billing_info.address1.should eq(@billing_info['address1'])
|
||||
end
|
||||
|
||||
it "purchases jamtrack" do
|
||||
end
|
||||
end
|
||||
|
||||
it "can remove account" do
|
||||
|
|
|
|||
Loading…
Reference in New Issue