diff --git a/app/controllers/api/v1/subscriptions_controller.rb b/app/controllers/api/v1/subscriptions_controller.rb index ad08f6b..0625880 100644 --- a/app/controllers/api/v1/subscriptions_controller.rb +++ b/app/controllers/api/v1/subscriptions_controller.rb @@ -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 diff --git a/app/models/customer.rb b/app/models/customer.rb index 677685f..f5190f4 100644 --- a/app/models/customer.rb +++ b/app/models/customer.rb @@ -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 diff --git a/app/models/subscription.rb b/app/models/subscription.rb index c1950f4..a3075a3 100644 --- a/app/models/subscription.rb +++ b/app/models/subscription.rb @@ -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 diff --git a/app/services/fakepay_service.rb b/app/services/fakepay_service.rb index 931eb66..1e06ad8 100644 --- a/app/services/fakepay_service.rb +++ b/app/services/fakepay_service.rb @@ -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 diff --git a/spec/requests/api/v1/subscriptions_request_spec.rb b/spec/requests/api/v1/subscriptions_request_spec.rb index 708e70b..69d9d41 100644 --- a/spec/requests/api/v1/subscriptions_request_spec.rb +++ b/spec/requests/api/v1/subscriptions_request_spec.rb @@ -23,7 +23,7 @@ } post "/api/v1/subscriptions", {params: subscription_params } - + byebug customer = Customer.last assert_response :success