From bf9bc3bd284665f67806f55b205714bd41a84de5 Mon Sep 17 00:00:00 2001 From: AndriiAndriyovuch Date: Tue, 11 Jul 2023 23:59:55 +0300 Subject: [PATCH 01/14] add min_value to config, use BigDecimal to send amount to Monobank, remove confictable require 'pry' --- lib/monopay-ruby/configuration.rb | 3 ++- lib/monopay-ruby/invoices/simple_invoice.rb | 9 ++++++--- lib/monopay-ruby/webhooks/public_key.rb | 1 - 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/lib/monopay-ruby/configuration.rb b/lib/monopay-ruby/configuration.rb index ada7feb..effb9e3 100644 --- a/lib/monopay-ruby/configuration.rb +++ b/lib/monopay-ruby/configuration.rb @@ -1,7 +1,8 @@ class MonopayRuby::Configuration - attr_accessor :api_token + attr_accessor :api_token, :min_value def initialize @api_token = ENV["MONOBANK_API_TOKEN"] # note ability to use ENV variable in docs + @min_value = ENV["MIN_VALUE"] end end diff --git a/lib/monopay-ruby/invoices/simple_invoice.rb b/lib/monopay-ruby/invoices/simple_invoice.rb index c8fa0c6..15fc332 100644 --- a/lib/monopay-ruby/invoices/simple_invoice.rb +++ b/lib/monopay-ruby/invoices/simple_invoice.rb @@ -73,10 +73,13 @@ def request_body end def convert_to_cents(amount) - if amount.is_a?(BigDecimal) + begin + amount = amount.to_d + raise Exception if amount < MonopayRuby.configuration.min_value + Money.from_amount(amount, DEFAULT_CURRENCY).cents - else - amount + rescue Exception => e + return end end end diff --git a/lib/monopay-ruby/webhooks/public_key.rb b/lib/monopay-ruby/webhooks/public_key.rb index 199d3d8..cc9bc36 100644 --- a/lib/monopay-ruby/webhooks/public_key.rb +++ b/lib/monopay-ruby/webhooks/public_key.rb @@ -2,7 +2,6 @@ require "base64" require "json" -require "pry" module MonopayRuby module Webhooks class PublicKey < MonopayRuby::Base From 84494c226511319c7c6dd4b0ef6a8da8c282ecb1 Mon Sep 17 00:00:00 2001 From: AndriyAndriyovuch Date: Wed, 12 Jul 2023 17:27:29 +0300 Subject: [PATCH 02/14] added validation to amount type with raise TypeError, added min_value to MonopayRuby.configuration , add ability to make discount --- lib/monopay-ruby/configuration.rb | 2 +- lib/monopay-ruby/invoices/simple_invoice.rb | 21 ++++++++++++++------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/lib/monopay-ruby/configuration.rb b/lib/monopay-ruby/configuration.rb index effb9e3..fc78a61 100644 --- a/lib/monopay-ruby/configuration.rb +++ b/lib/monopay-ruby/configuration.rb @@ -3,6 +3,6 @@ class MonopayRuby::Configuration def initialize @api_token = ENV["MONOBANK_API_TOKEN"] # note ability to use ENV variable in docs - @min_value = ENV["MIN_VALUE"] + @min_value = ENV["MIN_VALUE"] || 1 # 0.01 - minimun valid amount in Monobank end end diff --git a/lib/monopay-ruby/invoices/simple_invoice.rb b/lib/monopay-ruby/invoices/simple_invoice.rb index 15fc332..98b4341 100644 --- a/lib/monopay-ruby/invoices/simple_invoice.rb +++ b/lib/monopay-ruby/invoices/simple_invoice.rb @@ -30,12 +30,14 @@ def initialize(redirect_url = nil, webhook_url = nil) # @param [String] destination - additional info about payment # @param [String] reference - bill number or other reference # @return [Boolean] true if invoice was created successfully, false otherwise - def create(amount, destination: nil, reference: nil) + def create(amount, discount=1, destination: nil, reference: nil) begin @amount = convert_to_cents(amount) @destination = destination @reference = reference + make_discount(discount) if discount < 1 + response = RestClient.post(API_CREATE_INVOICE_URL, request_body, headers) response_body = JSON.parse(response.body) @@ -73,15 +75,20 @@ def request_body end def convert_to_cents(amount) - begin - amount = amount.to_d - raise Exception if amount < MonopayRuby.configuration.min_value - + if amount.is_a?(BigDecimal) Money.from_amount(amount, DEFAULT_CURRENCY).cents - rescue Exception => e - return + elsif amount.is_a?(Integer) + amount + else + raise TypeError, "expected amount will be an Integer or BigDecimal, got #{amount.class}" end end + + def make_discount(discount) + sum = (@amount * (1 - discount)).to_i + + @amount = [sum, MonopayRuby.configuration.min_value].max + end end end end From d0c4a16b0ea5091591c148b4d331e3e129862b63 Mon Sep 17 00:00:00 2001 From: AndriyAndriyovuch Date: Wed, 12 Jul 2023 17:34:53 +0300 Subject: [PATCH 03/14] add check for minimum value if discount is unavailable --- lib/monopay-ruby/invoices/simple_invoice.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/monopay-ruby/invoices/simple_invoice.rb b/lib/monopay-ruby/invoices/simple_invoice.rb index 98b4341..4a4665a 100644 --- a/lib/monopay-ruby/invoices/simple_invoice.rb +++ b/lib/monopay-ruby/invoices/simple_invoice.rb @@ -32,7 +32,7 @@ def initialize(redirect_url = nil, webhook_url = nil) # @return [Boolean] true if invoice was created successfully, false otherwise def create(amount, discount=1, destination: nil, reference: nil) begin - @amount = convert_to_cents(amount) + @amount = [convert_to_cents(amount), MonopayRuby.configuration.min_value].max @destination = destination @reference = reference From 256273a8283847982020042c82fc65824280c320 Mon Sep 17 00:00:00 2001 From: AndriiAndriyovuch Date: Thu, 13 Jul 2023 22:55:09 +0300 Subject: [PATCH 04/14] add ability to make discount by fixed sum or percents --- lib/monopay-ruby/invoices/simple_invoice.rb | 23 +++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/lib/monopay-ruby/invoices/simple_invoice.rb b/lib/monopay-ruby/invoices/simple_invoice.rb index 4a4665a..3956c94 100644 --- a/lib/monopay-ruby/invoices/simple_invoice.rb +++ b/lib/monopay-ruby/invoices/simple_invoice.rb @@ -30,13 +30,16 @@ def initialize(redirect_url = nil, webhook_url = nil) # @param [String] destination - additional info about payment # @param [String] reference - bill number or other reference # @return [Boolean] true if invoice was created successfully, false otherwise - def create(amount, discount=1, destination: nil, reference: nil) + def create(amount, discount=100, discount_is_fixed=false, destination: nil, reference: nil) begin - @amount = [convert_to_cents(amount), MonopayRuby.configuration.min_value].max + @amount = convert_to_cents(amount) @destination = destination @reference = reference + discount = convert_discount(discount, discount_is_fixed) - make_discount(discount) if discount < 1 + if discount_is_fixed || (discount < 1 && discount > 0) + @amount = make_discount(discount, discount_is_fixed) + end response = RestClient.post(API_CREATE_INVOICE_URL, request_body, headers) response_body = JSON.parse(response.body) @@ -84,10 +87,18 @@ def convert_to_cents(amount) end end - def make_discount(discount) - sum = (@amount * (1 - discount)).to_i + def convert_discount(discount, discount_is_fixed) + if discount_is_fixed + convert_to_cents(discount) + else + (1 - (discount.to_f / 100)) + end + end + + def make_discount(discount, discount_is_fixed) + sum = discount_is_fixed ? (@amount - discount) : (@amount * discount) - @amount = [sum, MonopayRuby.configuration.min_value].max + [sum.to_i, MonopayRuby.configuration.min_value].max end end end From fe5c1a892b44aa4c620c3ad1620972336844f02a Mon Sep 17 00:00:00 2001 From: AndriiAndriyovuch Date: Fri, 14 Jul 2023 00:02:38 +0300 Subject: [PATCH 05/14] add ability to use default value as an Bigdecimal, update Readme --- README.md | 52 ++++++++++++++++++++- lib/monopay-ruby/invoices/simple_invoice.rb | 5 +- 2 files changed, 53 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index ccd95bf..9176ecc 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ If bundler is not being used to manage dependencies, install the gem by executin gem install "monopay-ruby" ``` -## Usage +## Confiruration Add API token. There are two ways: First - add to the initializer file: @@ -47,8 +47,48 @@ Production: [https://fop.monobank.ua/](https://fop.monobank.ua/) Just get the token and go to earn moneys! 🚀 +Optional + +You may add a minimumm value to your payment: + +```ruby +# config/initializers/monopay-ruby.rb +MonopayRuby.configure do |config| + config.min_value = 1 +end +``` +* 0.01 UAH - it is a minimal valid value for Monobank +if you use 1 as an Integer it is equal to 0.01 UAH +if you use BigDeciamal(5) it's equal to 5 UAH + +Default value is 1 (0.01 UAH) + + ### Generate payment request +Simple: + +```ruby +# app/controllers/payments_controller.rb +class PaymentsController < ApplicationController + def create + payment = MonopayRuby::Invoices::SimpleInvoice.new( + "https://example.com", + "https://example.com/payments/webhook" + ) + + if payment.create(amount: 100, destination: "Payment description") + # your success code processing + else + # your error code processing + # flash[:error] = payment.error_messages + end + end +end +``` + +With discount: + ```ruby # app/controllers/payments_controller.rb class PaymentsController < ApplicationController @@ -58,7 +98,7 @@ class PaymentsController < ApplicationController "https://example.com/payments/webhook" ) - if payment.create(amount: 100, destination: "Payment description",) + if payment.create(amount: 100, discount: 20, discount_is_fixed: true, destination: "Payment description") # your success code processing else # your error code processing @@ -68,6 +108,14 @@ class PaymentsController < ApplicationController end ``` +Where: +- discount - is an number, which represents a % of discount if discount_is_fixed: false and an amount of discount if discount_is_fixed: true +- discount_is_fixed - a Boolean which set type of discount: + - true if it's with fixed amount, for example a coupon + - false if you need a some percentage of discount +* can be Integer, Float or BigDecimal + + ### Verify transaction ```ruby diff --git a/lib/monopay-ruby/invoices/simple_invoice.rb b/lib/monopay-ruby/invoices/simple_invoice.rb index 3956c94..37cf38e 100644 --- a/lib/monopay-ruby/invoices/simple_invoice.rb +++ b/lib/monopay-ruby/invoices/simple_invoice.rb @@ -83,7 +83,7 @@ def convert_to_cents(amount) elsif amount.is_a?(Integer) amount else - raise TypeError, "expected amount will be an Integer or BigDecimal, got #{amount.class}" + raise TypeError, "allowed to use only a BigDecimal or Integer price" end end @@ -97,8 +97,9 @@ def convert_discount(discount, discount_is_fixed) def make_discount(discount, discount_is_fixed) sum = discount_is_fixed ? (@amount - discount) : (@amount * discount) + puts MonopayRuby.configuration.min_value.class - [sum.to_i, MonopayRuby.configuration.min_value].max + [sum.to_i, convert_to_cents(MonopayRuby.configuration.min_value)].max end end end From f47258d34e1206e514d24a512c8ed70776b23866 Mon Sep 17 00:00:00 2001 From: AndriiAndriyovuch Date: Fri, 14 Jul 2023 00:05:59 +0300 Subject: [PATCH 06/14] update Readme --- README.md | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 9176ecc..4065920 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ If bundler is not being used to manage dependencies, install the gem by executin gem install "monopay-ruby" ``` -## Confiruration +## Configuration Add API token. There are two ways: First - add to the initializer file: @@ -47,9 +47,12 @@ Production: [https://fop.monobank.ua/](https://fop.monobank.ua/) Just get the token and go to earn moneys! 🚀 + +_______________________________________________________________ + Optional -You may add a minimumm value to your payment: +You may add a minimum value to your payment: ```ruby # config/initializers/monopay-ruby.rb @@ -57,9 +60,9 @@ MonopayRuby.configure do |config| config.min_value = 1 end ``` -* 0.01 UAH - it is a minimal valid value for Monobank -if you use 1 as an Integer it is equal to 0.01 UAH -if you use BigDeciamal(5) it's equal to 5 UAH +* 0.01 UAH - it is a minimal valid value for Monobank: + - if you use 1 as an Integer it is equal to 0.01 UAH + - if you use BigDeciamal(5) it's equal to 5 UAH Default value is 1 (0.01 UAH) @@ -111,8 +114,8 @@ end Where: - discount - is an number, which represents a % of discount if discount_is_fixed: false and an amount of discount if discount_is_fixed: true - discount_is_fixed - a Boolean which set type of discount: - - true if it's with fixed amount, for example a coupon - - false if you need a some percentage of discount + - ```true``` if it's with fixed amount, for example a coupon + - ```false``` if you need a some percentage of discount * can be Integer, Float or BigDecimal From 91e2a6619dd36ee482cad4efa87d6fe174a44c53 Mon Sep 17 00:00:00 2001 From: AndriyAndriyovuch Date: Fri, 14 Jul 2023 15:29:39 +0300 Subject: [PATCH 07/14] move discount, covertaion and validation to services --- lib/monopay-ruby/configuration.rb | 4 +-- lib/monopay-ruby/invoices/simple_invoice.rb | 39 +++++---------------- lib/monopay-ruby/services/base_service.rb | 9 +++++ lib/monopay-ruby/services/convert_amount.rb | 25 +++++++++++++ lib/monopay-ruby/services/discount.rb | 24 +++++++++++++ lib/monopay-ruby/services/validate_value.rb | 37 +++++++++++++++++++ 6 files changed, 106 insertions(+), 32 deletions(-) create mode 100644 lib/monopay-ruby/services/base_service.rb create mode 100644 lib/monopay-ruby/services/convert_amount.rb create mode 100644 lib/monopay-ruby/services/discount.rb create mode 100644 lib/monopay-ruby/services/validate_value.rb diff --git a/lib/monopay-ruby/configuration.rb b/lib/monopay-ruby/configuration.rb index fc78a61..12100c4 100644 --- a/lib/monopay-ruby/configuration.rb +++ b/lib/monopay-ruby/configuration.rb @@ -2,7 +2,7 @@ class MonopayRuby::Configuration attr_accessor :api_token, :min_value def initialize - @api_token = ENV["MONOBANK_API_TOKEN"] # note ability to use ENV variable in docs - @min_value = ENV["MIN_VALUE"] || 1 # 0.01 - minimun valid amount in Monobank + @api_token = ENV["MONOBANK_API_TOKEN"] + @min_value = ENV["MIN_VALUE"] || 1 end end diff --git a/lib/monopay-ruby/invoices/simple_invoice.rb b/lib/monopay-ruby/invoices/simple_invoice.rb index 37cf38e..5a5c839 100644 --- a/lib/monopay-ruby/invoices/simple_invoice.rb +++ b/lib/monopay-ruby/invoices/simple_invoice.rb @@ -30,15 +30,19 @@ def initialize(redirect_url = nil, webhook_url = nil) # @param [String] destination - additional info about payment # @param [String] reference - bill number or other reference # @return [Boolean] true if invoice was created successfully, false otherwise - def create(amount, discount=100, discount_is_fixed=false, destination: nil, reference: nil) + def create(amount, discount=nil, discount_is_fixed=false, destination: nil, reference: nil) + begin - @amount = convert_to_cents(amount) + @min_amount = MonopayRuby::Services::ValidateValue.call(MonopayRuby.configuration.min_value, DEFAULT_CURRENCY, "Minimal amount") + @amount = MonopayRuby::Services::ValidateValue.call(amount, DEFAULT_CURRENCY) + @destination = destination @reference = reference - discount = convert_discount(discount, discount_is_fixed) - if discount_is_fixed || (discount < 1 && discount > 0) - @amount = make_discount(discount, discount_is_fixed) + if discount.present? + discount = MonopayRuby::Services::ConvertAmount.call(discount, DEFAULT_CURRENCY) + + @amount = MonopayRuby::Services::Discount.call(@amount, discount, discount_is_fixed, @min_amount) end response = RestClient.post(API_CREATE_INVOICE_URL, request_body, headers) @@ -76,31 +80,6 @@ def request_body } }.to_json end - - def convert_to_cents(amount) - if amount.is_a?(BigDecimal) - Money.from_amount(amount, DEFAULT_CURRENCY).cents - elsif amount.is_a?(Integer) - amount - else - raise TypeError, "allowed to use only a BigDecimal or Integer price" - end - end - - def convert_discount(discount, discount_is_fixed) - if discount_is_fixed - convert_to_cents(discount) - else - (1 - (discount.to_f / 100)) - end - end - - def make_discount(discount, discount_is_fixed) - sum = discount_is_fixed ? (@amount - discount) : (@amount * discount) - puts MonopayRuby.configuration.min_value.class - - [sum.to_i, convert_to_cents(MonopayRuby.configuration.min_value)].max - end end end end diff --git a/lib/monopay-ruby/services/base_service.rb b/lib/monopay-ruby/services/base_service.rb new file mode 100644 index 0000000..49c93d0 --- /dev/null +++ b/lib/monopay-ruby/services/base_service.rb @@ -0,0 +1,9 @@ +module MonopayRuby + module Services + class BaseService + def self.call(*args) + new(*args).call + end + end + end +end \ No newline at end of file diff --git a/lib/monopay-ruby/services/convert_amount.rb b/lib/monopay-ruby/services/convert_amount.rb new file mode 100644 index 0000000..a026c18 --- /dev/null +++ b/lib/monopay-ruby/services/convert_amount.rb @@ -0,0 +1,25 @@ +require "money" +require "bigdecimal" + +module MonopayRuby + module Services + class ConvertAmount < BaseService + attr_reader :amount, :currency + + def initialize(amount, currency) + @amount = amount + @currency = currency + end + + def call + if amount.is_a?(BigDecimal) + Money.from_amount(amount, currency).cents + elsif amount.is_a?(Integer) + amount + else + raise TypeError, "allowed to use only a BigDecimal or Integer type" + end + end + end + end +end \ No newline at end of file diff --git a/lib/monopay-ruby/services/discount.rb b/lib/monopay-ruby/services/discount.rb new file mode 100644 index 0000000..f8a2176 --- /dev/null +++ b/lib/monopay-ruby/services/discount.rb @@ -0,0 +1,24 @@ +module MonopayRuby + module Services + class Discount < BaseService + attr_reader :amount, :discount, :discount_is_fixed, :min_amount + + def initialize(amount, discount, discount_is_fixed, min_amount) + @amount = amount + @discount = discount + @discount_is_fixed = discount_is_fixed + @min_amount = min_amount + end + + def call + if discount_is_fixed + sum = amount - discount + else + sum = amount * (1 - (discount.to_f / 100)) + end + + [sum.to_i, min_amount].max + end + end + end +end \ No newline at end of file diff --git a/lib/monopay-ruby/services/validate_value.rb b/lib/monopay-ruby/services/validate_value.rb new file mode 100644 index 0000000..4c18404 --- /dev/null +++ b/lib/monopay-ruby/services/validate_value.rb @@ -0,0 +1,37 @@ +require "bigdecimal" + +module MonopayRuby + module Services + class ValidateValue < BaseService + attr_reader :amount, :currency, :type + + def initialize(amount, currency, type='Amount') + @amount = amount + @currency = currency + @type = type + end + + def call + if amount.is_a?(Integer) || amount.is_a?(BigDecimal) + if amount > 0 + MonopayRuby::Services::ConvertAmount.call(amount, currency) + else + raise ValueError, "#{type} must be greater than 0" + end + else + raise TypeError, "#{type} is allowed to be Integer or BigDecimal, got #{amount.class}" unless amount.is_a?(Integer) || amount.is_a?(BigDecimal) + end + end + + private + + def name(var) + binding.local_variables.each do |name| + value = binding.local_variable_get(name) + return name.to_s if value == var + end + nil + end + end + end +end \ No newline at end of file From 23c59c3b923c6848c1a002e4e95a59131b542570 Mon Sep 17 00:00:00 2001 From: AndriyAndriyovuch Date: Fri, 14 Jul 2023 16:22:13 +0300 Subject: [PATCH 08/14] add empty lines --- lib/monopay-ruby/services/base_service.rb | 2 +- lib/monopay-ruby/services/convert_amount.rb | 2 +- lib/monopay-ruby/services/discount.rb | 2 +- lib/monopay-ruby/services/validate_value.rb | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/monopay-ruby/services/base_service.rb b/lib/monopay-ruby/services/base_service.rb index 49c93d0..129b371 100644 --- a/lib/monopay-ruby/services/base_service.rb +++ b/lib/monopay-ruby/services/base_service.rb @@ -6,4 +6,4 @@ def self.call(*args) end end end -end \ No newline at end of file +end diff --git a/lib/monopay-ruby/services/convert_amount.rb b/lib/monopay-ruby/services/convert_amount.rb index a026c18..3d3b80e 100644 --- a/lib/monopay-ruby/services/convert_amount.rb +++ b/lib/monopay-ruby/services/convert_amount.rb @@ -22,4 +22,4 @@ def call end end end -end \ No newline at end of file +end diff --git a/lib/monopay-ruby/services/discount.rb b/lib/monopay-ruby/services/discount.rb index f8a2176..cffb194 100644 --- a/lib/monopay-ruby/services/discount.rb +++ b/lib/monopay-ruby/services/discount.rb @@ -21,4 +21,4 @@ def call end end end -end \ No newline at end of file +end diff --git a/lib/monopay-ruby/services/validate_value.rb b/lib/monopay-ruby/services/validate_value.rb index 4c18404..c8762e6 100644 --- a/lib/monopay-ruby/services/validate_value.rb +++ b/lib/monopay-ruby/services/validate_value.rb @@ -34,4 +34,4 @@ def name(var) end end end -end \ No newline at end of file +end From e2725155ed0323a87a55d233c9b8ca27be507acb Mon Sep 17 00:00:00 2001 From: AndriiAndriyovuch Date: Sat, 16 Sep 2023 13:59:45 +0300 Subject: [PATCH 09/14] used named params in create payment method, fix README with Grammarly --- README.md | 24 ++++++++++----------- lib/monopay-ruby/configuration.rb | 4 ++-- lib/monopay-ruby/invoices/simple_invoice.rb | 4 ++-- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index e68401e..7189314 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ Install the gem and add to the application's Gemfile by executing: bundle add "monopay-ruby" ``` -If bundler is not being used to manage dependencies, install the gem by executing: +If bundler is not being used to manage dependencies, install the gem by executing the: ```ruby gem install "monopay-ruby" @@ -46,7 +46,7 @@ Development: [https://api.monobank.ua/](https://api.monobank.ua/) Production: [https://fop.monobank.ua/](https://fop.monobank.ua/) -Just get the token and go to earn moneys! 🚀 +Just get the token and go to earn money! 🚀 _______________________________________________________________ @@ -58,14 +58,14 @@ You may add a minimum value to your payment: ```ruby # config/initializers/monopay-ruby.rb MonopayRuby.configure do |config| - config.min_value = 1 + config.min_price = 1 end ``` * 0.01 UAH - it is a minimal valid value for Monobank: - if you use 1 as an Integer it is equal to 0.01 UAH - - if you use BigDeciamal(5) it's equal to 5 UAH + - if you use BigDecimal(1) it's equal to 1 UAH -Default value is 1 (0.01 UAH) +The default value is 1 (0.01 UAH) ### Generate payment request @@ -82,7 +82,7 @@ class PaymentsController < ApplicationController ) if payment.create(amount: 100, destination: "Payment description") - # your success code processing + # your successful code processing else # your error code processing # flash[:error] = payment.error_messages @@ -103,7 +103,7 @@ class PaymentsController < ApplicationController ) if payment.create(amount: 100, discount: 20, discount_is_fixed: true, destination: "Payment description") - # your success code processing + # your successful code processing else # your error code processing # flash[:error] = payment.error_messages @@ -112,11 +112,11 @@ class PaymentsController < ApplicationController end ``` -Where: +Options: - discount - is an number, which represents a % of discount if discount_is_fixed: false and an amount of discount if discount_is_fixed: true -- discount_is_fixed - a Boolean which set type of discount: - - ```true``` if it's with fixed amount, for example a coupon - - ```false``` if you need a some percentage of discount +- discount_is_fixed - a Boolean which sets the type of discount: + - ```true``` fixed amount of discount will be applied, for example a coupon + - ```false``` discount amount will be preceded by %. * can be Integer, Float or BigDecimal @@ -131,7 +131,7 @@ class PaymentsController < ApplicationController webhook_validator = MonopayRuby::Webhooks::Validator.new(request) if webhook_validator.valid? - # your success code processing + # your successful code processing else # your error code processing # flash[:error] = webhook_validator.error_messages diff --git a/lib/monopay-ruby/configuration.rb b/lib/monopay-ruby/configuration.rb index 12100c4..326b850 100644 --- a/lib/monopay-ruby/configuration.rb +++ b/lib/monopay-ruby/configuration.rb @@ -1,8 +1,8 @@ class MonopayRuby::Configuration - attr_accessor :api_token, :min_value + attr_accessor :api_token, :min_price def initialize @api_token = ENV["MONOBANK_API_TOKEN"] - @min_value = ENV["MIN_VALUE"] || 1 + @min_price = ENV["MIN_PRICE"] || 1 end end diff --git a/lib/monopay-ruby/invoices/simple_invoice.rb b/lib/monopay-ruby/invoices/simple_invoice.rb index ebef5a5..3efdf22 100644 --- a/lib/monopay-ruby/invoices/simple_invoice.rb +++ b/lib/monopay-ruby/invoices/simple_invoice.rb @@ -30,10 +30,10 @@ def initialize(redirect_url: nil, webhook_url: nil) # @param [String] destination - additional info about payment # @param [String] reference - bill number or other reference # @return [Boolean] true if invoice was created successfully, false otherwise - def create(amount, discount=nil, discount_is_fixed=false, destination: nil, reference: nil) + def create(amount, discount: nil, discount_is_fixed: false, destination: nil, reference: nil) begin - @min_amount = MonopayRuby::Services::ValidateValue.call(MonopayRuby.configuration.min_value, DEFAULT_CURRENCY, "Minimal amount") + @min_amount = MonopayRuby::Services::ValidateValue.call(MonopayRuby.configuration.min_price, DEFAULT_CURRENCY, "Minimal amount") @amount = MonopayRuby::Services::ValidateValue.call(amount, DEFAULT_CURRENCY) @destination = destination From b26feb0cedb7f6c6360435b8d5d175acb7d4bcd0 Mon Sep 17 00:00:00 2001 From: AndriiAndriyovuch Date: Mon, 23 Oct 2023 23:18:24 +0300 Subject: [PATCH 10/14] added named attributes --- lib/monopay-ruby/invoices/simple_invoice.rb | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/monopay-ruby/invoices/simple_invoice.rb b/lib/monopay-ruby/invoices/simple_invoice.rb index 3efdf22..03ace77 100644 --- a/lib/monopay-ruby/invoices/simple_invoice.rb +++ b/lib/monopay-ruby/invoices/simple_invoice.rb @@ -30,15 +30,17 @@ def initialize(redirect_url: nil, webhook_url: nil) # @param [String] destination - additional info about payment # @param [String] reference - bill number or other reference # @return [Boolean] true if invoice was created successfully, false otherwise - def create(amount, discount: nil, discount_is_fixed: false, destination: nil, reference: nil) + def create(amount:, options: {}) + @amount = amount + discount = options[:discount] + discount_is_fixed = options[:discount_is_fixed] + @destination = options[:destination] + @reference = options[:reference] begin @min_amount = MonopayRuby::Services::ValidateValue.call(MonopayRuby.configuration.min_price, DEFAULT_CURRENCY, "Minimal amount") @amount = MonopayRuby::Services::ValidateValue.call(amount, DEFAULT_CURRENCY) - @destination = destination - @reference = reference - if discount.present? discount = MonopayRuby::Services::ConvertAmount.call(discount, DEFAULT_CURRENCY) From 8f92a53ae47dceb6e1cec19b430b2af33b2ca118 Mon Sep 17 00:00:00 2001 From: AndriyAndriyovuch Date: Tue, 24 Oct 2023 17:42:38 +0300 Subject: [PATCH 11/14] added named params to tests --- lib/monopay-ruby/invoices/simple_invoice.rb | 2 +- spec/lib/invoices/simple_invoice_spec.rb | 18 +++++++++--------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/monopay-ruby/invoices/simple_invoice.rb b/lib/monopay-ruby/invoices/simple_invoice.rb index 03ace77..8bde484 100644 --- a/lib/monopay-ruby/invoices/simple_invoice.rb +++ b/lib/monopay-ruby/invoices/simple_invoice.rb @@ -41,7 +41,7 @@ def create(amount:, options: {}) @min_amount = MonopayRuby::Services::ValidateValue.call(MonopayRuby.configuration.min_price, DEFAULT_CURRENCY, "Minimal amount") @amount = MonopayRuby::Services::ValidateValue.call(amount, DEFAULT_CURRENCY) - if discount.present? + if discount discount = MonopayRuby::Services::ConvertAmount.call(discount, DEFAULT_CURRENCY) @amount = MonopayRuby::Services::Discount.call(@amount, discount, discount_is_fixed, @min_amount) diff --git a/spec/lib/invoices/simple_invoice_spec.rb b/spec/lib/invoices/simple_invoice_spec.rb index 676c5e4..f2ce3f7 100644 --- a/spec/lib/invoices/simple_invoice_spec.rb +++ b/spec/lib/invoices/simple_invoice_spec.rb @@ -44,25 +44,25 @@ end it "returns true" do - expect(simple_invoice_instance.create(2000)).to be_truthy + expect(simple_invoice_instance.create(amount: 2000)).to be_truthy end it "sets invoice_id" do expect do - simple_invoice_instance.create(2000) + simple_invoice_instance.create(amount: 2000) end.to change(simple_invoice_instance, :invoice_id).from(nil).to(invoice_id) end it "sets page_url" do expect do - simple_invoice_instance.create(2000) + simple_invoice_instance.create(amount: 2000) end.to change(simple_invoice_instance, :page_url).from(nil).to(page_url) end context "when amount is BigDecimal" do it "sets amount" do expect do - simple_invoice_instance.create(BigDecimal("20")) + simple_invoice_instance.create(amount: BigDecimal("20")) end.to change(simple_invoice_instance, :amount).from(nil).to(2000) end end @@ -87,11 +87,11 @@ end it "returns false" do - expect(subject.create(2000)).to be_falsey + expect(subject.create(amount: 2000)).to be_falsey end it "has error message" do - subject.create(2000) + subject.create(amount: 2000) expect(subject.error_messages).to include(missing_x_token_header_error_message) end @@ -114,17 +114,17 @@ end it "returns false" do - expect(subject.create(2000)).to be_falsey + expect(subject.create(amount: 2000)).to be_falsey end it "has error message" do - subject.create(2000) + subject.create(amount: 2000) expect(subject.error_messages).to include(invalid_token_error_message) end end - context "with invalid params" do + xcontext "with invalid params" do let(:invalid_amount_server_error_message) { { "errCode" => "BAD_REQUEST", "errText" => "json unmarshal: : json: cannot unmarshal string into Go struct field InvoiceCreateRequest.amount of type int64" } } let(:error_code) { "400 Bad Request" } let(:invalid_amount_error_message) do From 444133486725c5cd4de9c8e2d4e4b28cea1ff356 Mon Sep 17 00:00:00 2001 From: AndriiAndriyovuch Date: Tue, 24 Oct 2023 21:00:16 +0300 Subject: [PATCH 12/14] added tests for invoice --- lib/monopay-ruby/invoices/simple_invoice.rb | 6 +- lib/monopay-ruby/services/validate_value.rb | 4 +- spec/lib/invoices/simple_invoice_spec.rb | 88 ++++++++++++++------- 3 files changed, 62 insertions(+), 36 deletions(-) diff --git a/lib/monopay-ruby/invoices/simple_invoice.rb b/lib/monopay-ruby/invoices/simple_invoice.rb index 8bde484..f0c06fe 100644 --- a/lib/monopay-ruby/invoices/simple_invoice.rb +++ b/lib/monopay-ruby/invoices/simple_invoice.rb @@ -31,16 +31,14 @@ def initialize(redirect_url: nil, webhook_url: nil) # @param [String] reference - bill number or other reference # @return [Boolean] true if invoice was created successfully, false otherwise def create(amount:, options: {}) - @amount = amount + @amount = MonopayRuby::Services::ValidateValue.call(amount, DEFAULT_CURRENCY) discount = options[:discount] discount_is_fixed = options[:discount_is_fixed] @destination = options[:destination] @reference = options[:reference] + @min_amount = MonopayRuby::Services::ValidateValue.call(MonopayRuby.configuration.min_price, DEFAULT_CURRENCY, "Minimal amount") begin - @min_amount = MonopayRuby::Services::ValidateValue.call(MonopayRuby.configuration.min_price, DEFAULT_CURRENCY, "Minimal amount") - @amount = MonopayRuby::Services::ValidateValue.call(amount, DEFAULT_CURRENCY) - if discount discount = MonopayRuby::Services::ConvertAmount.call(discount, DEFAULT_CURRENCY) diff --git a/lib/monopay-ruby/services/validate_value.rb b/lib/monopay-ruby/services/validate_value.rb index c8762e6..8873d37 100644 --- a/lib/monopay-ruby/services/validate_value.rb +++ b/lib/monopay-ruby/services/validate_value.rb @@ -1,6 +1,6 @@ require "bigdecimal" -module MonopayRuby +module MonopayRuby module Services class ValidateValue < BaseService attr_reader :amount, :currency, :type @@ -16,7 +16,7 @@ def call if amount > 0 MonopayRuby::Services::ConvertAmount.call(amount, currency) else - raise ValueError, "#{type} must be greater than 0" + raise ArgumentError, "#{type} must be greater than 0" end else raise TypeError, "#{type} is allowed to be Integer or BigDecimal, got #{amount.class}" unless amount.is_a?(Integer) || amount.is_a?(BigDecimal) diff --git a/spec/lib/invoices/simple_invoice_spec.rb b/spec/lib/invoices/simple_invoice_spec.rb index f2ce3f7..0163f54 100644 --- a/spec/lib/invoices/simple_invoice_spec.rb +++ b/spec/lib/invoices/simple_invoice_spec.rb @@ -43,20 +43,58 @@ allow(RestClient).to receive(:post).and_return(double(body: response_example.to_json)) end - it "returns true" do - expect(simple_invoice_instance.create(amount: 2000)).to be_truthy + context "only with amount" do + it "returns true" do + expect(simple_invoice_instance.create(amount: 2000)).to be_truthy + end + + it "sets invoice_id" do + expect do + simple_invoice_instance.create(amount: 2000) + end.to change(simple_invoice_instance, :invoice_id).from(nil).to(invoice_id) + end + + it "sets page_url" do + expect do + simple_invoice_instance.create(amount: 2000) + end.to change(simple_invoice_instance, :page_url).from(nil).to(page_url) + end end - it "sets invoice_id" do - expect do - simple_invoice_instance.create(amount: 2000) - end.to change(simple_invoice_instance, :invoice_id).from(nil).to(invoice_id) + context "with amount and discount (fixed)" do + it "returns true" do + expect(simple_invoice_instance.create(amount: 2000, options: { discount: 20.to_d, discount_is_fixed: true }) ).to be_truthy + end + + it "sets invoice_id" do + expect do + simple_invoice_instance.create(amount: 2000, options: { discount: 20.to_d, discount_is_fixed: true }) + end.to change(simple_invoice_instance, :invoice_id).from(nil).to(invoice_id) + end + + it "sets page_url" do + expect do + simple_invoice_instance.create(amount: 2000, options: { discount: 20.to_d, discount_is_fixed: true }) + end.to change(simple_invoice_instance, :page_url).from(nil).to(page_url) + end end - it "sets page_url" do - expect do - simple_invoice_instance.create(amount: 2000) - end.to change(simple_invoice_instance, :page_url).from(nil).to(page_url) + context "with amount and discount (not fixed)" do + it "returns true" do + expect(simple_invoice_instance.create(amount: 2000, options: { discount: 20, discount_is_fixed: false }) ).to be_truthy + end + + it "sets invoice_id" do + expect do + simple_invoice_instance.create(amount: 2000, options: { discount: 20, discount_is_fixed: false }) + end.to change(simple_invoice_instance, :invoice_id).from(nil).to(invoice_id) + end + + it "sets page_url" do + expect do + simple_invoice_instance.create(amount: 2000, options: { discount: 20, discount_is_fixed: false }) + end.to change(simple_invoice_instance, :page_url).from(nil).to(page_url) + end end context "when amount is BigDecimal" do @@ -124,30 +162,20 @@ end end - xcontext "with invalid params" do - let(:invalid_amount_server_error_message) { { "errCode" => "BAD_REQUEST", "errText" => "json unmarshal: : json: cannot unmarshal string into Go struct field InvoiceCreateRequest.amount of type int64" } } - let(:error_code) { "400 Bad Request" } - let(:invalid_amount_error_message) do - [error_code, invalid_amount_server_error_message].join(", ") - end - let(:exception_instance) do - RestClient::ExceptionWithResponse.new(double(body: invalid_amount_server_error_message.to_json)) - end - - before do - exception_instance.message = error_code - - allow(RestClient).to receive(:post).and_raise(exception_instance) + context 'when amount is a invalid' do + it 'raises a TypeError if amount type is a string' do + expect { subject.create(amount: '13') } + .to raise_error(TypeError, 'Amount is allowed to be Integer or BigDecimal, got String') end - it "returns false" do - expect(subject.create("")).to be_falsey + it 'raises a TypeError if amount type is a float' do + expect { subject.create(amount: 666.0) } + .to raise_error(TypeError, 'Amount is allowed to be Integer or BigDecimal, got Float') end - it "has error message" do - subject.create("") - - expect(subject.error_messages).to include(invalid_amount_error_message) + it 'raises a ArgumentError if amount negative' do + expect { subject.create(amount: -1) } + .to raise_error(ArgumentError, 'Amount must be greater than 0') end end end From 18519520726e414dd6d223305e6194c6c2bfece7 Mon Sep 17 00:00:00 2001 From: Andrii <95449986+AndriyAndriyovuch@users.noreply.github.com> Date: Tue, 24 Oct 2023 21:51:28 +0300 Subject: [PATCH 13/14] Update README.md --- README.md | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 7189314..0b15eff 100644 --- a/README.md +++ b/README.md @@ -115,9 +115,8 @@ end Options: - discount - is an number, which represents a % of discount if discount_is_fixed: false and an amount of discount if discount_is_fixed: true - discount_is_fixed - a Boolean which sets the type of discount: - - ```true``` fixed amount of discount will be applied, for example a coupon - - ```false``` discount amount will be preceded by %. -* can be Integer, Float or BigDecimal + - ```true``` fixed amount of discount will be applied, for example a coupon (use Integer or BigDecimal in ```discount``` ) + - ```false``` discount amount will be preceded by % (use Integer in ```discount``` for corrent counting of 0-100 %) ### Verify transaction From 9a02c77b54bb7f31e656e1a9b006c4c24cc42ef1 Mon Sep 17 00:00:00 2001 From: Andrii <95449986+AndriyAndriyovuch@users.noreply.github.com> Date: Tue, 24 Oct 2023 21:51:58 +0300 Subject: [PATCH 14/14] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0b15eff..123076f 100644 --- a/README.md +++ b/README.md @@ -116,7 +116,7 @@ Options: - discount - is an number, which represents a % of discount if discount_is_fixed: false and an amount of discount if discount_is_fixed: true - discount_is_fixed - a Boolean which sets the type of discount: - ```true``` fixed amount of discount will be applied, for example a coupon (use Integer or BigDecimal in ```discount``` ) - - ```false``` discount amount will be preceded by % (use Integer in ```discount``` for corrent counting of 0-100 %) + - ```false``` discount amount will be preceded by % (use Integer in ```discount``` for correct counting of 0-100 %) ### Verify transaction