VRFS-2480 : Incremental

This commit is contained in:
Steven Miers 2014-11-17 21:24:39 -06:00
parent 2d3bbd2a47
commit 7f88a7550c
4 changed files with 96 additions and 63 deletions

View File

@ -1341,16 +1341,17 @@
function updateBillingInfo(options) {
return $.ajax({
type: "PUT",
url: '/api/recurly/update_billing_info?' + $.param(options),
type: "POST",
url: '/api/recurly/update_billing_info?' + $.param({billing_info: options}),
dataType: "json",
//data: JSON.stringify({"billing_info": $.param(options)}),
contentType: 'application/json'
});
}
function placeOrder(options) {
return $.ajax({
type: "PUT",
type: "POST", lll
url: '/api/recurly/place_order?' + $.param(options),
dataType: "json",
contentType: 'application/json'

View File

@ -1,38 +1,25 @@
class ApiRecurlyController < ApiController
before_filter :api_signed_in_user
before_filter :obtain_account, only: [:create_account, :update_account, :get_subscription, :update_billing_info]
respond_to :json
# create Recurly account
def create_account
#logger.debug(params[:billing_info])
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.errors.any?
response.status = 404
else
current_user.recurly_code = @account.account_code
current_user.save
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
#logger.debug @account.to_json
end
render :json=>@account.to_json
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
end
@ -42,7 +29,7 @@ class ApiRecurlyController < ApiController
if @account.present?
@account.destroy
end
render :json=>{}, :status=>200
render json: {}, status: 200
rescue Recurly::Error, NoMethodError => e
render json: { message: ValidationMessages::RECURLY_ERROR, error: e}, :status => 404
end
@ -50,26 +37,25 @@ class ApiRecurlyController < ApiController
# get Recurly account
def get_account
@account = Recurly::Account.find(current_user.recurly_code)
render :json=>@account.to_json
render json: @account.to_json
rescue Recurly::Error, NoMethodError => e
render json: { message: ValidationMessages::RECURLY_ERROR, error: e}, :status => 404
end
# update Recurly account
def update_account
safe_retrieve_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?
response.status = 404
if @account.errors.any?
render status: 404, json: { message: ValidationMessages::RECURLY_ACCOUNT_ERROR}
else
current_user.recurly_code = @account.account_code
current_user.save
end
render :json=>@account.to_json
render json: @account.to_json
end
rescue Recurly::Error, NoMethodError => e
raise e
render json: { message: ValidationMessages::RECURLY_ERROR, error: e}, :status => 404
@ -77,8 +63,7 @@ class ApiRecurlyController < ApiController
# get subscription
def get_subscription
safe_retrieve_account()
render :json=>@account.subscriptions.last.to_json
render json: @account.subscriptions.last.to_json
rescue Recurly::Error, NoMethodError => e
render json: { message: ValidationMessages::RECURLY_ACCOUNT_ERROR, error: e}, :status => 404
end
@ -89,53 +74,66 @@ class ApiRecurlyController < ApiController
# get Billing Information
def billing_info
puts "billing_info1"
if current_user.recurly_code.nil?
render json: { message: ValidationMessages::RECURLY_ACCOUNT_ERROR }, :status => 404 and return
puts "billing_info2"
render json: { message: ValidationMessages::RECURLY_ACCOUNT_ERROR }, :status => 404
else
puts "billing_info3"
@account = Recurly::Account.find(current_user.recurly_code)
#logger.debug @account
render json: @account.billing_info
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
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 and return
render json: { message: ValidationMessages::RECURLY_ACCOUNT_ERROR }, :status => 404
else
if params[:first_name].blank? or params[:last_name].blank? or params[:number].blank? or params[:year].blank? or params[:month].blank? or params[:verification_value].blank?
render json: { message: ValidationMessages::RECURLY_PARAMETER_ERROR }, :status => 404 and return
end
@account = Recurly::Account.find(current_user.recurly_code)
@account.billing_info = params
@account.billing_info.save
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.errors.any?
response.status = :unprocessable_entity
end
if @account.nil? || @account.errors.any?
response.status = :unprocessable_entity
end
render json: @account
render json: @account
end
end
rescue Recurly::Error, NoMethodError => e
raise e
render json: { message: ValidationMessages::RECURLY_ERROR, error: e}, :status => 404
end
def place_order
if current_user.recurly_code.nil?
render json: { message: ValidationMessages::RECURLY_ACCOUNT_ERROR }, :status => 404 and return
render json: { message: ValidationMessages::RECURLY_ACCOUNT_ERROR }, :status => 404
else
if params[:first_name].blank? or params[:last_name].blank? or params[:number].blank? or params[:year].blank? or params[:month].blank? or params[:verification_value].blank?
render json: { message: ValidationMessages::RECURLY_PARAMETER_ERROR }, :status => 404 and return
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 = params
@account.billing_info=billing_params
@account.billing_info.save
# create subscription.
if @account.errors.any?
if @account.nil? || @account.errors.any?
response.status = :unprocessable_entity
end
@ -146,7 +144,7 @@ class ApiRecurlyController < ApiController
end
private
def safe_retrieve_account
def obtain_account
if current_user.recurly_code.nil?
@account = Recurly::Account.create(
account_code: current_user.id,
@ -162,5 +160,11 @@ private
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
end
end

View File

@ -239,7 +239,7 @@ SampleApp::Application.routes.draw do
match '/recurly/update_account' => 'api_recurly#update_account', :via => :post
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 => :put
match '/recurly/place_order' => 'api_recurly#place_order', :via => :post
# login/logout
match '/auth_session' => 'api_users#auth_session_create', :via => :post

View File

@ -1,5 +1,6 @@
require 'spec_helper'
require 'recurly/account'
#require 'recurly/account'
describe ApiRecurlyController do
render_views
@ -33,15 +34,18 @@ describe ApiRecurlyController do
end
it "should create account" do
post :create_account, { :format => 'json', :billing_info => @billing_info}
response.should be_success
post :create_account, {:format => 'json', :billing_info=>@billing_info}
response.should be_success
body = JSON.parse(response.body)
body[:billing_info].should_not be_nil
puts "BODY: #{body}"
end
it "should retrieve account" do
post :create_account, { :format => 'json', :billing_info => @billing_info}
post :create_account, {:format => 'json', :billing_info=>@billing_info}
response.should be_success
get :get_account, { :format => 'json'}
get :get_account
body = JSON.parse(response.body)
response.should be_success
puts "body: #{body}"
@ -49,7 +53,7 @@ describe ApiRecurlyController do
end
it "should update account" do
post :create_account, { :format => 'json', :billing_info => @billing_info}
post :create_account
response.should be_success
body = JSON.parse(response.body)
body['attributes']['first_name'].should eq("Person")
@ -74,4 +78,28 @@ describe ApiRecurlyController do
puts body['attributes']
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")
@billing_info[:state] = "NE"
put :update_billing_info, {:format => 'json', :billing_info=>@billing_info}
response.should be_success
body = JSON.parse(response.body)
puts "BILLING_ BODY: #{body}"
get :billing_info
response.should be_success
puts "response.body: #{response.body}"
body = JSON.parse(response.body)
puts "body: #{body}"
end
it "should place order" do
put :place_order
end
end