Class: Upi::Generator
- Inherits:
-
Object
- Object
- Upi::Generator
- 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',
amount: 100,
note: 'Test Description'
)
svg_content = generator.generate_qr(mode: :svg)
png_content = generator.generate_qr(mode: :png)
payment_url = generator.generate_url
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.
Instance Attribute Summary collapse
-
#params ⇒ Object
readonly
Returns the value of attribute params.
Instance Method Summary collapse
- #generate_qr(mode: :svg) ⇒ Object
-
#initialize(upi_id:, name:, amount: nil, currency: 'INR', note: '', merchant_code: nil, transaction_ref_id: nil, transaction_id: nil, url: nil) ⇒ Generator
constructor
A new instance of Generator.
- #upi_content ⇒ Object
Constructor Details
#initialize(upi_id:, name:, amount: nil, currency: 'INR', note: '', merchant_code: nil, transaction_ref_id: nil, transaction_id: nil, url: nil) ⇒ Generator
Returns a new instance of Generator.
51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/upi.rb', line 51 def initialize(upi_id:, name:, amount: nil, currency: 'INR', note: '', merchant_code: nil, transaction_ref_id: nil, transaction_id: nil, url: nil) @params = { pa: upi_id, pn: name, am: amount, cu: currency, tn: note, mc: merchant_code, tr: transaction_ref_id, tid: transaction_id, url: url }.compact end |
Instance Attribute Details
#params ⇒ Object (readonly)
Returns the value of attribute params.
49 50 51 |
# File 'lib/upi.rb', line 49 def params @params end |
Instance Method Details
#generate_qr(mode: :svg) ⇒ Object
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 93 |
# File 'lib/upi.rb', line 65 def generate_qr(mode: :svg) content = upi_content qrcode = RQRCode::QRCode.new(content) 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 ⇒ Object
95 96 97 98 99 |
# File 'lib/upi.rb', line 95 def upi_content # Manually construct the UPI URI string without URI::UPI query_string = URI.encode_www_form(params) "upi://pay?#{query_string}" end |