Class: Upi::Generator

Inherits:
Object
  • Object
show all
Defined in:
lib/upi.rb

Overview

The Generator class is responsible for creating UPI QR codes and payment URLs.

You can generate a QR code for a UPI payment in either SVG or PNG format, and also create a UPI payment URL for use in HTML links.

Example usage:

generator = Upi::Generator.new(
  upi_id: 'test@upi',
  name: 'Test Name'
)

svg_content = generator.generate_qr(100, 'Personal Payment', mode: :svg)
png_content = generator.generate_qr(100, 'Personal Payment', mode: :png)
payment_url = generator.upi_content(100, 'Personal Payment')

Parameters:

  • ‘upi_id`: The UPI ID of the recipient.

  • ‘name`: The name of the recipient.

  • ‘amount`: The payment amount (required for UPI transactions).

  • ‘currency`: The currency code (default is ’INR’).

  • ‘note`: An optional note or description for the payment.

  • ‘merchant_code`: An optional merchant code.

  • ‘transaction_ref_id`: An optional transaction reference ID.

  • ‘transaction_id`: An optional transaction ID.

  • ‘url`: An optional URL for additional information or payment redirect.

Examples:

generator = Upi::Generator.new(
  upi_id: 'test@upi',
  name: 'Test Name'
)
svg_content = generator.generate_qr(100, 'Personal Payment', mode: :svg)
File.write('qr_code.svg', svg_content)

See Also:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(upi_id:, name:, currency: 'INR', merchant_code: nil, no_url_parse: true) ⇒ Generator

Returns a new instance of Generator.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/upi.rb', line 48

def initialize(upi_id:, name:, currency: 'INR', merchant_code: nil, no_url_parse: true)
  @params = {
    pa: upi_id,
    pn: name,
    am: '',
    cu: currency,
    tn: '',
    mc: merchant_code,
    tr: nil,
    tid: nil,
    url: nil
  }.compact
  @no_url_parse = no_url_parse
end

Instance Attribute Details

#no_url_parseObject (readonly)

Returns the value of attribute no_url_parse.



46
47
48
# File 'lib/upi.rb', line 46

def no_url_parse
  @no_url_parse
end

#paramsObject (readonly)

Returns the value of attribute params.



46
47
48
# File 'lib/upi.rb', line 46

def params
  @params
end

Instance Method Details

#generate_qr(amount = '0', note = '', transaction_ref_id: nil, transaction_id: nil, url: nil, mode: :svg) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/upi.rb', line 63

def generate_qr(amount = '0', note = '', transaction_ref_id: nil, transaction_id: nil, url: nil, mode: :svg)
  content = upi_content(amount, note, transaction_ref_id: transaction_ref_id, transaction_id: transaction_id, url: url)
  qrcode = RQRCode::QRCode.new(content)
  generate_transaction(amount, note, transaction_id, transaction_ref_id, url)

  case mode
  when :svg
    qrcode.as_svg(
      color: '000',
      shape_rendering: 'crispEdges',
      module_size: 11
    )
  when :png
    png = qrcode.as_png(
      bit_depth: 1,
      border_modules: 4,
      color_mode: ChunkyPNG::COLOR_GRAYSCALE,
      color: 'black',
      file: nil,
      fill: 'white',
      module_px_size: 6, # Adjust size as needed
      resize_exactly_to: false,
      resize_gte_to: false,
      size: 300 # Adjust size as needed
    )
    png.to_s
  else
    raise ArgumentError, "Unsupported mode: #{mode}. Use :svg or :png."
  end
end

#upi_content(amount = '0', note = '', transaction_ref_id: nil, transaction_id: nil, url: nil) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/upi.rb', line 94

def upi_content(amount = '0', note = '', transaction_ref_id: nil, transaction_id: nil, url: nil)
  generate_transaction(amount, note, transaction_id, transaction_ref_id, url)

  # Manually construct the UPI URI string without URI::UPI
  query_string = if no_url_parse
                   URI.encode_www_form(params.reject { |k, v| v.nil? || k == :pa })
                 else
                   params.reject { |k, v| v.nil? || k == :pa }.map { |k, v| "#{k}=#{v}" }.join('&')
                 end
  "upi://pay?pa=#{params[:pa]}&#{query_string}"
end