From 882cf4f9e4e17427c6fd020a92e3d0361223aac2 Mon Sep 17 00:00:00 2001 From: dhodov_sftsrv Date: Tue, 13 Aug 2024 10:37:57 +0300 Subject: [PATCH] add advanced invoive --- CHANGELOG.md | 4 ++ Gemfile.lock | 2 +- README.md | 24 +++++++++ lib/monopay-ruby/invoices/advanced_invoice.rb | 36 +++++++++++++ lib/monopay-ruby/invoices/simple_invoice.rb | 2 - lib/monopay-ruby/version.rb | 2 +- spec/lib/invoices/advanced_invoice_spec.rb | 53 +++++++++++++++++++ spec/lib/invoices/simple_invoice_spec.rb | 8 ++- 8 files changed, 126 insertions(+), 5 deletions(-) create mode 100644 lib/monopay-ruby/invoices/advanced_invoice.rb create mode 100644 spec/lib/invoices/advanced_invoice_spec.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index bfa6a99..3c42c88 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## [1.1.0] - 2024-08-12 +- add `MonopayRuby::Invoices::AdvancedInvoice` +- change private method `request_body` to private +- modify `README.md` ## [1.0.0] - 2023-06-27 ### Changed diff --git a/Gemfile.lock b/Gemfile.lock index 863fd18..3ef5035 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) diff --git a/README.md b/README.md index 61117a1..f1361fe 100644 --- a/README.md +++ b/README.md @@ -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", + "https://example.com/payments/webhook" + ) + + if payment.create(amount: 100, destination: "Payment description", basketOrder: {}, otherParams: {}) + # your success code processing + else + # your error code processing + # flash[:error] = payment.error_messages + end + end +end +``` + +basketOrder - details about order items +otherParams - you can add any params you need at your project + ### Verify transaction ```ruby diff --git a/lib/monopay-ruby/invoices/advanced_invoice.rb b/lib/monopay-ruby/invoices/advanced_invoice.rb new file mode 100644 index 0000000..8d259b8 --- /dev/null +++ b/lib/monopay-ruby/invoices/advanced_invoice.rb @@ -0,0 +1,36 @@ +require_relative 'simple_invoice' +module MonopayRuby + module Invoices + class AdvancedInvoice < MonopayRuby::Invoices::SimpleInvoice + attr_reader :basketOrder, :otherParams + + def create(amount, destination: nil, reference: nil, basketOrder: {}, otherParams: {}) + @basketOrder = basketOrder + @otherParams = otherParams + + super(amount, destination: destination, reference: reference) + end + + # private + + # 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 + { + amount: amount, + redirectUrl: redirect_url, + webHookUrl: webhook_url, + merchantPaymInfo: { + reference: reference, + destination: destination, + basketOrder: basketOrder, + + } + }.merge(otherParams).to_json + end + end + end +end diff --git a/lib/monopay-ruby/invoices/simple_invoice.rb b/lib/monopay-ruby/invoices/simple_invoice.rb index 9d6cf8a..7a8b302 100644 --- a/lib/monopay-ruby/invoices/simple_invoice.rb +++ b/lib/monopay-ruby/invoices/simple_invoice.rb @@ -53,8 +53,6 @@ def create(amount, destination: nil, reference: nil) end end - private - # Request body required for Monobank API # # @return [Hash] request body diff --git a/lib/monopay-ruby/version.rb b/lib/monopay-ruby/version.rb index e55885d..68dab9d 100644 --- a/lib/monopay-ruby/version.rb +++ b/lib/monopay-ruby/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module MonopayRuby - VERSION = "1.0.0" + VERSION = "1.1.0" end diff --git a/spec/lib/invoices/advanced_invoice_spec.rb b/spec/lib/invoices/advanced_invoice_spec.rb new file mode 100644 index 0000000..97a57ac --- /dev/null +++ b/spec/lib/invoices/advanced_invoice_spec.rb @@ -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, basketOrder: basket_order, otherParams: other_params)).to be_truthy + end + end + end +end diff --git a/spec/lib/invoices/simple_invoice_spec.rb b/spec/lib/invoices/simple_invoice_spec.rb index 676c5e4..1157095 100644 --- a/spec/lib/invoices/simple_invoice_spec.rb +++ b/spec/lib/invoices/simple_invoice_spec.rb @@ -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(", ")