Skip to content

Commit

Permalink
add advanced invoive
Browse files Browse the repository at this point in the history
  • Loading branch information
dhodov committed Aug 13, 2024
1 parent 397d6ad commit e04bf70
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## [1.1.0] - 2024-08-12
- add class `MonopayRuby::Invoices::AdvancedInvoice` where in method `MonopayRuby::Invoices::AdvancedInvoice::create` you can add any necessary params for your app
- change private method `request_body` to public
- add more details about `MonopayRuby::Invoices::AdvancedInvoice` into `README.md`
## [1.0.0] - 2023-06-27

### Changed
Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
monopay-ruby (1.0.0)
monopay-ruby (1.1.0)
base64 (~> 0.1.1)
json (~> 2.5)
money (~> 6.13)
Expand Down
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,30 @@ class PaymentsController < ApplicationController
end
```

## You can add more params:

```ruby
# app/controllers/payments_controller.rb
class PaymentsController < ApplicationController
def create
payment = MonopayRuby::Invoices::AdvancedInvoice.new(
"https://example.com", # your redirect url
"https://example.com/payments/webhook" # your webhook url
)

if payment.create(amount, destination: nil, reference: nil, other_merchant_paymant_info_params: {}, other_params: {})
# your success code processing
else
# your error code processing
# flash[:error] = payment.error_messages
end
end
end
```

other_merchant_paymant_info_params - details about additional merchant paymant info params for example - basketOrder to add information about bought itesm
other_params - you can add any additional params you need at your project, for instance - googlePay for payment via google wallet

### Verify transaction

```ruby
Expand Down
49 changes: 49 additions & 0 deletions lib/monopay-ruby/invoices/advanced_invoice.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
require_relative 'simple_invoice'

module MonopayRuby
module Invoices
class AdvancedInvoice < MonopayRuby::Invoices::SimpleInvoice
attr_reader :merchant_payment_info, :other_params

def create(amount, destination: nil, reference: nil, other_merchant_paymant_info_params: {}, other_params: {})
@other_merchant_paymant_info_params = other_merchant_paymant_info_params
@other_params = other_params

super(amount, destination: destination, reference: reference)
end

# Request body required for Monobank API
#
# @return [Hash] request body
def request_body
# TODO: add "ccy" and another missing params
# TODO: remove nil valued params
request_body = {
amount: amount,
redirectUrl: redirect_url,
webHookUrl: webhook_url,
merchantPaymInfo: {
reference: reference,
destination: destination
}
}

request_body[:merchantPaymInfo].merge!(merchant_paymant_info) # add additional merchant_payment_info params if they are present
request_body.merge!(other_params) # add additional params if they are present

# TODO: add sum params into present additional params
if request_body[:merchantPaymInfo].has_key?(:basketOrder)
request_body[:merchantPaymInfo][:basketOrder].first[:sum] = convert_to_cents(amount)
elsif request_body.has_key?(:items)
request_body[:items].first[:sum] = convert_to_cents(amount)
end
end
end
end

private

def sum_param_into_basket
request_body[:merchantPaymInfo][:basketOrder][:qty] * amount
end
end
2 changes: 0 additions & 2 deletions lib/monopay-ruby/invoices/simple_invoice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ def create(amount, destination: nil, reference: nil)
end
end

private

# Request body required for Monobank API
#
# @return [Hash] request body
Expand Down
2 changes: 1 addition & 1 deletion lib/monopay-ruby/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module MonopayRuby
VERSION = "1.0.0"
VERSION = "1.1.0"
end
53 changes: 53 additions & 0 deletions spec/lib/invoices/advanced_invoice_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# frozen_string_literal: true

RSpec.describe MonopayRuby::Invoices::AdvancedInvoice do
describe "#new" do
let!(:redirect_url) { "https://redirect.example.com" }
let!(:webhook_url) { "https://webhook.example.com" }

context "with keyword parameters" do
subject { described_class.new(redirect_url: redirect_url, webhook_url: webhook_url) }

it "initializes with correct redirect_url" do
expect(subject.redirect_url).to eq(redirect_url)
end

it "initializes with correct webhook_url" do
expect(subject.webhook_url).to eq(webhook_url)
end
end

context "without keyword parameters" do
subject { described_class.new(redirect_url, webhook_url) }

it "raises an ArgumentError" do
expect { subject }.to raise_error(ArgumentError)
end
end

context "without parameters" do
subject { described_class.new }

it { is_expected.to be_a(described_class) }
end
end

describe "#create" do
context "when request is successful" do
let(:simple_invoice_instance) { described_class.new }
let(:invoice_id) { "p2_9ZgpZVsl3" }
let(:page_url) { "https://pay.mbnk.biz/p2_9ZgpZVsl3" }
let(:basket_order) { { name: "product", qty: 1 } }
let(:other_params) { { ccy: 9 } }
let(:response_example) { { "invoiceId": invoice_id, "pageUrl": page_url } }

before do
allow(RestClient).to receive(:post).and_return(double(body: response_example.to_json))
end

it "returns true" do
expect(simple_invoice_instance.create(2000, basket_order: basket_order, other_params: other_params)).to be_truthy
end
end
end
end
8 changes: 7 additions & 1 deletion spec/lib/invoices/simple_invoice_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,13 @@
context "when request is failed" do
context "with missing token" do
# move this code to shared example or mb shared context
let(:missing_x_token_server_message) { { "errorDescription" => "Missing required header 'X-Token'" } }
let(:missing_x_token_server_message) {
{
"errCode" => "BAD_REQUEST",
"errText" => "Missing required header 'X-Token'",
"errorDescription" => "Missing required header 'X-Token'"
}
}
let(:error_code) { "400 Bad Request" }
let(:missing_x_token_header_error_message) do
[error_code, missing_x_token_server_message].join(", ")
Expand Down

0 comments on commit e04bf70

Please sign in to comment.