|
| 1 | +require_relative 'simple_invoice' |
| 2 | + |
| 3 | +module MonopayRuby |
| 4 | + module Invoices |
| 5 | + class AdvancedInvoice < MonopayRuby::Invoices::SimpleInvoice |
| 6 | + attr_reader :additional_params, :amount |
| 7 | + |
| 8 | + # Create invoice for Monobank API |
| 9 | + # |
| 10 | + # This method sets up the required instance variables and then calls the `create` |
| 11 | + # method from the parent class with the relevant parameters. |
| 12 | + # |
| 13 | + # @param amount [Numeric] The amount of the payment. |
| 14 | + # @param additional_params [Hash] (optional) Additional parameters for the payment. |
| 15 | + # - :merchantPaymInfo [Hash] Information about the merchant payment. |
| 16 | + # - :destination [String] The destination of the payment. |
| 17 | + # - :reference [String] A reference for the payment. |
| 18 | + # |
| 19 | + # @return [Boolean] The result of the `create` method in the parent class, |
| 20 | + # which returns true if invoice was created successfully, false otherwise |
| 21 | + # |
| 22 | + # @example Create a payment with amount and additional parameters |
| 23 | + # create(100, additional_params: { merchantPaymInfo: { destination: "Happy payment", reference: "ref123" } }) |
| 24 | + def create(amount, additional_params: {}) |
| 25 | + @amount = amount |
| 26 | + @additional_params = additional_params |
| 27 | + @destination = @additional_params&.dig(:merchantPaymInfo, :destination) |
| 28 | + @reference = @additional_params&.dig(:merchantPaymInfo, :reference) |
| 29 | + |
| 30 | + super(amount, destination: @destination, reference: @reference) |
| 31 | + end |
| 32 | + |
| 33 | + protected |
| 34 | + |
| 35 | + def request_body |
| 36 | + current_params = default_params |
| 37 | + |
| 38 | + return current_params.to_json if additional_params.blank? |
| 39 | + |
| 40 | + unless additional_params[:merchantPaymInfo].blank? |
| 41 | + current_params[:merchantPaymInfo] = { |
| 42 | + reference: @reference, |
| 43 | + destination: @destination |
| 44 | + }.merge!(additional_params[:merchantPaymInfo].except(:reference, :destination)) |
| 45 | + end |
| 46 | + |
| 47 | + current_params.merge!(additional_params.except(:merchantPaymInfo)) |
| 48 | + |
| 49 | + # TODO: add and modify sum and qty params of merchantPaymInfo[basketOrder] parameters if it is present |
| 50 | + # TODO: add and modify sum and qty params of items parameters if it is present |
| 51 | + set_sum_and_qty_params(current_params&.dig(:merchantPaymInfo, :basketOrder)) |
| 52 | + set_sum_and_qty_params(current_params[:items]) |
| 53 | + |
| 54 | + current_params.to_json |
| 55 | + end |
| 56 | + |
| 57 | + # Set sum and qty params |
| 58 | + # @param current_param [Array] The current parameter to set sum and qty |
| 59 | + # It sets the converted amount or sum parameter as sum and pasted quantity parameter or default value as qty parameters for the current parameter |
| 60 | + # @return [Object] It could be Hash or Array or nil. It depends on the current parameter |
| 61 | + def set_sum_and_qty_params(current_param) |
| 62 | + return if current_param.blank? |
| 63 | + |
| 64 | + current_param.each do |item| |
| 65 | + return if item.blank? |
| 66 | + |
| 67 | + item[:sum] = convert_to_cents(item[:sum] || amount) |
| 68 | + item[:qty] = item[:qty] || 1 |
| 69 | + end |
| 70 | + end |
| 71 | + end |
| 72 | + end |
| 73 | +end |
0 commit comments