From 1004640331d48403b134838248c039169ca5f2a5 Mon Sep 17 00:00:00 2001 From: Timothy Joo Date: Fri, 7 Sep 2018 16:08:16 -0500 Subject: [PATCH 1/8] Swap out puts error messages for render json responsej --- app/controllers/api/v1/subscriptions_controller.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/app/controllers/api/v1/subscriptions_controller.rb b/app/controllers/api/v1/subscriptions_controller.rb index ad08f6b..31f1ba6 100644 --- a/app/controllers/api/v1/subscriptions_controller.rb +++ b/app/controllers/api/v1/subscriptions_controller.rb @@ -10,9 +10,10 @@ 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: {"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!" + render json: {"Your transaction was successfully posted!"} Customer.store_token(formatted, params, customer_params) else puts formatted From 15720e056526488824c0ae5d3f7dbabf4530dd2b Mon Sep 17 00:00:00 2001 From: Tyjoo26 Date: Sun, 9 Sep 2018 10:25:33 -0500 Subject: [PATCH 2/8] swap all puts error handling to render json error handling --- app/controllers/api/v1/subscriptions_controller.rb | 2 +- app/models/customer.rb | 4 ++-- app/services/fakepay_service.rb | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/app/controllers/api/v1/subscriptions_controller.rb b/app/controllers/api/v1/subscriptions_controller.rb index 31f1ba6..99feef9 100644 --- a/app/controllers/api/v1/subscriptions_controller.rb +++ b/app/controllers/api/v1/subscriptions_controller.rb @@ -16,7 +16,7 @@ def check_error(object) render json: {"Your transaction was successfully posted!"} Customer.store_token(formatted, params, customer_params) else - puts formatted + render json: {formatted} end end diff --git a/app/models/customer.rb b/app/models/customer.rb index 677685f..5567a6a 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" + render json: {"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" + render json: { "There was an issue with customer details and we were unable to save the information in our database"} end end diff --git a/app/services/fakepay_service.rb b/app/services/fakepay_service.rb index 931eb66..6102eee 100644 --- a/app/services/fakepay_service.rb +++ b/app/services/fakepay_service.rb @@ -24,13 +24,13 @@ 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" + render json: { "The payment amount does not align with the price of the selected plan. Please check your POST request"} exit end end From 1b2289c2405353f25479c68a73ac9adf73dd6b99 Mon Sep 17 00:00:00 2001 From: Tyjoo26 Date: Sun, 9 Sep 2018 11:36:33 -0500 Subject: [PATCH 3/8] Format json properly --- app/controllers/api/v1/subscriptions_controller.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/controllers/api/v1/subscriptions_controller.rb b/app/controllers/api/v1/subscriptions_controller.rb index 99feef9..11974e2 100644 --- a/app/controllers/api/v1/subscriptions_controller.rb +++ b/app/controllers/api/v1/subscriptions_controller.rb @@ -10,13 +10,13 @@ def check_error(object) FakepayService.check_amount_with_plan(params) formatted = FakepayService.parse_json(object) if formatted[:success] == false - render json: {"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 - render json: {"Your transaction was successfully posted!"} - Customer.store_token(formatted, params, customer_params) + render json: { :success => "Your transaction was successfully posted!"} + Customer.store_token(formatted, params, customer_params) else - render json: {formatted} + render json: {:payload => formatted} end end From 8ca0b122f5a47409d13d78e4d4c039b6b498f61a Mon Sep 17 00:00:00 2001 From: Tyjoo26 Date: Sun, 9 Sep 2018 11:36:54 -0500 Subject: [PATCH 4/8] Render method not applicable in class, put return for now" --- app/models/customer.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/models/customer.rb b/app/models/customer.rb index 5567a6a..d4c0108 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 - render json: {"You've successfully loaded a customer into the database"} + return { :success => "You've successfully loaded a customer into the database"} else - render json: { "There was an issue with customer details and we were unable to save the information in our database"} + return { :error => "There was an issue with customer details and we were unable to save the information in our database"} end end From 2bf855c3dc8e487cdc191ac95e9e8dfc7b2b196d Mon Sep 17 00:00:00 2001 From: Tyjoo26 Date: Sun, 9 Sep 2018 11:37:20 -0500 Subject: [PATCH 5/8] Change key in plan pricing to string because json response is not parsed before plan pricing method is called ' --- app/services/fakepay_service.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/services/fakepay_service.rb b/app/services/fakepay_service.rb index 6102eee..6c212d1 100644 --- a/app/services/fakepay_service.rb +++ b/app/services/fakepay_service.rb @@ -30,13 +30,13 @@ def self.check_amount_with_plan(object) plan = object[:plan_id] amount = object[:amount] if self.plan_pricing[plan].include?(amount) != true - render json: { "The payment amount does not align with the price of the selected plan. Please check your POST request"} + return { :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]} + { "1" => [1999, "1999"], "2" => ["4900", 4900], "3" => ["9900", 9900]} end From 0418f60e67e20b0e287349683a249901acea18b5 Mon Sep 17 00:00:00 2001 From: Tyjoo26 Date: Sun, 9 Sep 2018 16:55:55 -0500 Subject: [PATCH 6/8] Return JSON for error handling in class --- app/controllers/api/v1/subscriptions_controller.rb | 2 +- app/models/customer.rb | 4 ++-- app/services/fakepay_service.rb | 2 +- spec/requests/api/v1/subscriptions_request_spec.rb | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/app/controllers/api/v1/subscriptions_controller.rb b/app/controllers/api/v1/subscriptions_controller.rb index 11974e2..599a644 100644 --- a/app/controllers/api/v1/subscriptions_controller.rb +++ b/app/controllers/api/v1/subscriptions_controller.rb @@ -14,7 +14,7 @@ def check_error(object) # puts "Your transaction failed due to error code: #{formatted[:error_code]}. Please check the error code description" elsif formatted[:success] == true render json: { :success => "Your transaction was successfully posted!"} - Customer.store_token(formatted, params, customer_params) + render json: Customer.store_token(formatted, params, customer_params) else render json: {:payload => formatted} end diff --git a/app/models/customer.rb b/app/models/customer.rb index d4c0108..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 - return { :success => "You've successfully loaded a customer into the database"} + return json: { :success => "You've successfully loaded a customer into the database"} else - return { :error => "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/services/fakepay_service.rb b/app/services/fakepay_service.rb index 6c212d1..8840b80 100644 --- a/app/services/fakepay_service.rb +++ b/app/services/fakepay_service.rb @@ -30,7 +30,7 @@ def self.check_amount_with_plan(object) plan = object[:plan_id] amount = object[:amount] if self.plan_pricing[plan].include?(amount) != true - return { :error => "The payment amount does not align with the price of the selected plan. Please check your POST request"} + return json: { :error => "The payment amount does not align with the price of the selected plan. Please check your POST request"} exit 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 From 457e479a0f1974ecb6b02ae605571e678a2c65dd Mon Sep 17 00:00:00 2001 From: Tyjoo26 Date: Sun, 9 Sep 2018 16:59:06 -0500 Subject: [PATCH 7/8] clean up extra spacing --- app/services/fakepay_service.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/app/services/fakepay_service.rb b/app/services/fakepay_service.rb index 8840b80..93e44cf 100644 --- a/app/services/fakepay_service.rb +++ b/app/services/fakepay_service.rb @@ -24,8 +24,6 @@ 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] From 0bb9f583a9520d6d3855723f4e335bb5a4375d33 Mon Sep 17 00:00:00 2001 From: Tyjoo26 Date: Wed, 23 Jan 2019 10:47:49 -0600 Subject: [PATCH 8/8] Place pricing validation in Subscription class instead of fakepayservice for separation of concerns --- app/controllers/api/v1/subscriptions_controller.rb | 4 ++-- app/models/subscription.rb | 4 ++++ app/services/fakepay_service.rb | 8 +------- 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/app/controllers/api/v1/subscriptions_controller.rb b/app/controllers/api/v1/subscriptions_controller.rb index 599a644..0625880 100644 --- a/app/controllers/api/v1/subscriptions_controller.rb +++ b/app/controllers/api/v1/subscriptions_controller.rb @@ -10,11 +10,11 @@ def check_error(object) FakepayService.check_amount_with_plan(params) formatted = FakepayService.parse_json(object) if formatted[:success] == false - render json: { :errors => "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 render json: { :success => "Your transaction was successfully posted!"} - render json: Customer.store_token(formatted, params, customer_params) + render json: Customer.store_token(formatted, params, customer_params) else render json: {:payload => formatted} 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 93e44cf..1e06ad8 100644 --- a/app/services/fakepay_service.rb +++ b/app/services/fakepay_service.rb @@ -27,15 +27,9 @@ def self.parse_json(payload) def self.check_amount_with_plan(object) plan = object[:plan_id] amount = object[:amount] - if self.plan_pricing[plan].include?(amount) != true + 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