-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feature/ability to apply coupon for price #2
base: master
Are you sure you want to change the base?
Changes from 8 commits
bf9bc3b
84494c2
d0c4a16
256273a
fe5c1a8
f47258d
91e2a66
23c59c3
c9cf3ba
e272515
b26feb0
8f92a53
4441334
1851952
9a02c77
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,7 @@ If bundler is not being used to manage dependencies, install the gem by executin | |
gem install "monopay-ruby" | ||
``` | ||
|
||
## Usage | ||
## Configuration | ||
|
||
Add API token. There are two ways: | ||
First - add to the initializer file: | ||
|
@@ -47,8 +47,51 @@ Production: [https://fop.monobank.ua/](https://fop.monobank.ua/) | |
|
||
Just get the token and go to earn moneys! 🚀 | ||
|
||
|
||
_______________________________________________________________ | ||
|
||
Optional | ||
|
||
You may add a minimum 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. чому не 1? давай теж 1, щоб було зрозуміло різницю |
||
|
||
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 +101,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 +111,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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Options: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. щось таке |
||
|
||
|
||
### Verify transaction | ||
|
||
```ruby | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
@api_token = ENV["MONOBANK_API_TOKEN"] | ||
@min_value = ENV["MIN_VALUE"] || 1 | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,12 +30,21 @@ 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=nil, discount_is_fixed=false, destination: nil, reference: nil) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. нє, ну це ж кпц), юзай неймед параметри |
||
|
||
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 | ||
|
||
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) | ||
response_body = JSON.parse(response.body) | ||
|
||
|
@@ -71,14 +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 | ||
else | ||
amount | ||
end | ||
end | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module MonopayRuby | ||
module Services | ||
class BaseService | ||
def self.call(*args) | ||
new(*args).call | ||
end | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
краще не
min_value
, аmin_price