-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
196 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,4 @@ source "https://rubygems.org" | |
gemspec | ||
|
||
gem "rake", "~> 13.0" | ||
gem 'activesupport', '~> 7.0' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
require_relative 'simple_invoice' | ||
|
||
module MonopayRuby | ||
module Invoices | ||
class AdvancedInvoice < MonopayRuby::Invoices::SimpleInvoice | ||
attr_reader :additional_params, :amount | ||
|
||
# Create invoice for Monobank API | ||
# | ||
# This method sets up the required instance variables and then calls the `create` | ||
# method from the parent class with the relevant parameters. | ||
# | ||
# @param amount [Numeric] The amount of the payment. | ||
# @param additional_params [Hash] (optional) Additional parameters for the payment. | ||
# - :merchantPaymInfo [Hash] Information about the merchant payment. | ||
# - :destination [String] The destination of the payment. | ||
# - :reference [String] A reference for the payment. | ||
# | ||
# @return [Boolean] The result of the `create` method in the parent class, | ||
# which returns true if invoice was created successfully, false otherwise | ||
# | ||
# @example Create a payment with amount and additional parameters | ||
# create(100, additional_params: { merchantPaymInfo: { destination: "Happy payment", reference: "ref123" } }) | ||
def create(amount, additional_params: {}) | ||
@amount = amount | ||
@additional_params = additional_params | ||
@destination = @additional_params&.dig(:merchantPaymInfo, :destination) | ||
@reference = @additional_params&.dig(:merchantPaymInfo, :reference) | ||
|
||
super(amount, destination: @destination, reference: @reference) | ||
end | ||
|
||
protected | ||
|
||
def request_body | ||
current_params = default_params | ||
|
||
return current_params.to_json if additional_params.blank? | ||
|
||
unless additional_params[:merchantPaymInfo].blank? | ||
current_params[:merchantPaymInfo] = { | ||
reference: @reference, | ||
destination: @destination | ||
}.merge!(additional_params[:merchantPaymInfo].except(:reference, :destination)) | ||
end | ||
|
||
current_params.merge!(additional_params.except(:merchantPaymInfo)) | ||
|
||
# TODO: add and modify sum and qty params of merchantPaymInfo[basketOrder] parameters if it is present | ||
# TODO: add and modify sum and qty params of items parameters if it is present | ||
set_sum_and_qty_params(current_params&.dig(:merchantPaymInfo, :basketOrder)) | ||
set_sum_and_qty_params(current_params[:items]) | ||
|
||
current_params.to_json | ||
end | ||
|
||
# Set sum and qty params | ||
# @param current_param [Array] The current parameter to set sum and qty | ||
# It sets the converted amount or sum parameter as sum and pasted quantity parameter or default value as qty parameters for the current parameter | ||
# @return [Object] It could be Hash or Array or nil. It depends on the current parameter | ||
def set_sum_and_qty_params(current_param) | ||
return if current_param.blank? | ||
|
||
current_param.each do |item| | ||
return if item.blank? | ||
|
||
item[:sum] = convert_to_cents(item[:sum] || amount) | ||
item[:qty] = item[:qty] || 1 | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(:additional_params) { { merchantPaymInfo: { basketOrder: [basket_order] }, 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, additional_params: additional_params)).to be_truthy | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters