Skip to content

Commit 827aeab

Browse files
committed
add advanced invoive
1 parent 397d6ad commit 827aeab

File tree

8 files changed

+128
-5
lines changed

8 files changed

+128
-5
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## [1.1.0] - 2024-08-12
2+
- add `MonopayRuby::Invoices::AdvancedInvoice`
3+
- change private method `request_body` to private
4+
- modify `README.md`
15
## [1.0.0] - 2023-06-27
26

37
### Changed

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
monopay-ruby (1.0.0)
4+
monopay-ruby (1.1.0)
55
base64 (~> 0.1.1)
66
json (~> 2.5)
77
money (~> 6.13)

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,30 @@ class PaymentsController < ApplicationController
6969
end
7070
```
7171

72+
## You can add more params:
73+
74+
```ruby
75+
# app/controllers/payments_controller.rb
76+
class PaymentsController < ApplicationController
77+
def create
78+
payment = MonopayRuby::Invoices::AdvancedInvoice.new(
79+
"https://example.com",
80+
"https://example.com/payments/webhook"
81+
)
82+
83+
if payment.create(amount: 100, destination: "Payment description", basketOrder: {}, otherParams: {})
84+
# your success code processing
85+
else
86+
# your error code processing
87+
# flash[:error] = payment.error_messages
88+
end
89+
end
90+
end
91+
```
92+
93+
basketOrder - details about order items
94+
otherParams - you can add any params you need at your project
95+
7296
### Verify transaction
7397

7498
```ruby
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
require_relative 'simple_invoice'
2+
module MonopayRuby
3+
module Invoices
4+
class AdvancedInvoice < MonopayRuby::Invoices::SimpleInvoice
5+
attr_reader :basketOrder, :otherParams
6+
7+
def create(amount, destination: nil, reference: nil, basketOrder: {}, otherParams: {})
8+
@basketOrder = basketOrder
9+
@otherParams = otherParams
10+
11+
super(amount, destination: destination, reference: reference)
12+
end
13+
14+
# Request body required for Monobank API
15+
#
16+
# @return [Hash] request body
17+
def request_body
18+
# TODO: add "ccy" and another missing params
19+
# TODO: remove nil valued params
20+
request_body = {
21+
amount: amount,
22+
redirectUrl: redirect_url,
23+
webHookUrl: webhook_url,
24+
merchantPaymInfo: {
25+
reference: reference,
26+
destination: destination
27+
28+
}
29+
}
30+
31+
request_body[:merchantPaymInfo][:basketOrder] = basketOrder if basketOrder.any?
32+
request_body.merge!(otherParams).to_json
33+
end
34+
end
35+
end
36+
end

lib/monopay-ruby/invoices/simple_invoice.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@ def create(amount, destination: nil, reference: nil)
5353
end
5454
end
5555

56-
private
57-
5856
# Request body required for Monobank API
5957
#
6058
# @return [Hash] request body
@@ -72,6 +70,8 @@ def request_body
7270
}.to_json
7371
end
7472

73+
private
74+
7575
def convert_to_cents(amount)
7676
if amount.is_a?(BigDecimal)
7777
Money.from_amount(amount, DEFAULT_CURRENCY).cents

lib/monopay-ruby/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module MonopayRuby
4-
VERSION = "1.0.0"
4+
VERSION = "1.1.0"
55
end
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# frozen_string_literal: true
2+
3+
RSpec.describe MonopayRuby::Invoices::AdvancedInvoice do
4+
describe "#new" do
5+
let!(:redirect_url) { "https://redirect.example.com" }
6+
let!(:webhook_url) { "https://webhook.example.com" }
7+
8+
context "with keyword parameters" do
9+
subject { described_class.new(redirect_url: redirect_url, webhook_url: webhook_url) }
10+
11+
it "initializes with correct redirect_url" do
12+
expect(subject.redirect_url).to eq(redirect_url)
13+
end
14+
15+
it "initializes with correct webhook_url" do
16+
expect(subject.webhook_url).to eq(webhook_url)
17+
end
18+
end
19+
20+
context "without keyword parameters" do
21+
subject { described_class.new(redirect_url, webhook_url) }
22+
23+
it "raises an ArgumentError" do
24+
expect { subject }.to raise_error(ArgumentError)
25+
end
26+
end
27+
28+
context "without parameters" do
29+
subject { described_class.new }
30+
31+
it { is_expected.to be_a(described_class) }
32+
end
33+
end
34+
35+
describe "#create" do
36+
context "when request is successful" do
37+
let(:simple_invoice_instance) { described_class.new }
38+
let(:invoice_id) { "p2_9ZgpZVsl3" }
39+
let(:page_url) { "https://pay.mbnk.biz/p2_9ZgpZVsl3" }
40+
let(:basket_order) { { name: "product", qty: 1 } }
41+
let(:other_params) { { ccy: 9 } }
42+
let(:response_example) { { "invoiceId": invoice_id, "pageUrl": page_url } }
43+
44+
before do
45+
allow(RestClient).to receive(:post).and_return(double(body: response_example.to_json))
46+
end
47+
48+
it "returns true" do
49+
expect(simple_invoice_instance.create(2000, basketOrder: basket_order, otherParams: other_params)).to be_truthy
50+
end
51+
end
52+
end
53+
end

spec/lib/invoices/simple_invoice_spec.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,13 @@
7171
context "when request is failed" do
7272
context "with missing token" do
7373
# move this code to shared example or mb shared context
74-
let(:missing_x_token_server_message) { { "errorDescription" => "Missing required header 'X-Token'" } }
74+
let(:missing_x_token_server_message) {
75+
{
76+
"errCode" => "BAD_REQUEST",
77+
"errText" => "Missing required header 'X-Token'",
78+
"errorDescription" => "Missing required header 'X-Token'"
79+
}
80+
}
7581
let(:error_code) { "400 Bad Request" }
7682
let(:missing_x_token_header_error_message) do
7783
[error_code, missing_x_token_server_message].join(", ")

0 commit comments

Comments
 (0)