Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions app/controllers/api/v1/subscriptions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ def check_error(object)
FakepayService.check_amount_with_plan(params)
formatted = FakepayService.parse_json(object)
if formatted[:success] == false
puts "Your transaction failed due to error code: #{formatted[:error_code]}. Please check the error code description"
render json: { :errors => "Your transaction failed due to error code: #{formatted[:error_code]}. Please check the error code description"}
# puts "Your transaction failed due to error code: #{formatted[:error_code]}. Please check the error code description"
elsif formatted[:success] == true
puts "Your transaction was successfully posted!"
Customer.store_token(formatted, params, customer_params)
render json: { :success => "Your transaction was successfully posted!"}
render json: Customer.store_token(formatted, params, customer_params)
else
puts formatted
render json: {:payload => formatted}
end
end

Expand Down
4 changes: 2 additions & 2 deletions app/models/customer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ def self.store_token(object, params, customer_data)
@customer.plans << plan
@customer.token = object[:token]
@customer.save
puts "You've successfully loaded a customer into the database"
return json: { :success => "You've successfully loaded a customer into the database"}
else
puts "There was an issue with customer details and we were unable to save the information in our database"
return json: { :error => "There was an issue with customer details and we were unable to save the information in our database"}
end
end

Expand Down
4 changes: 4 additions & 0 deletions app/models/subscription.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@ class Subscription < ApplicationRecord
belongs_to :plan



def self.validate_pricing
{ 1 => [1999, "1999"], 2 => ["4900", 4900], 3 => ["9900", 9900]}
end
end
12 changes: 2 additions & 10 deletions app/services/fakepay_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,12 @@ def self.parse_json(payload)
JSON.parse(payload, symbolize_names: true)
end



def self.check_amount_with_plan(object)
plan = object[:plan_id]
amount = object[:amount]
if self.plan_pricing[plan].include?(amount) != true
puts "The payment amount does not align with the price of the selected plan. Please check your POST request"
if Subscription.plan_pricing[plan].include?(amount) != true
return json: { :error => "The payment amount does not align with the price of the selected plan. Please check your POST request"}
exit
end
end

def self.plan_pricing
{ 1 => [1999, "1999"], 2 => ["4900", 4900], 3 => ["9900", 9900]}
end


end
2 changes: 1 addition & 1 deletion spec/requests/api/v1/subscriptions_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
}

post "/api/v1/subscriptions", {params: subscription_params }

byebug
customer = Customer.last

assert_response :success
Expand Down