damoiser / qr-bills

Ruby Gem to generate QR-Bills for Swiss payments

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

undefined method `create_creditor_reference' for QRBills:Class

Texicitys opened this issue · comments

Hello,
I just tried ton add this gem to one of my rails project.

In the controller, when i try QRBills.create_creditor_reference("MTR81UUWZYO48NY55NP3") , il recieve this message :

NoMethodError in MainController#index
undefined method `create_creditor_reference' for QRBills:Class 

Thanks for the help !
Yves

Hi @Texicitys
it seems that the gem was not properly loaded, which version are you using?

Edit:
did you have as well require 'qr-bills' in your controller? I usually use the qr-bills gem in models and I would expect to have a similar behavior in a controller too. If you get the same error again, please provide more details so I can try to bette help you (part of your source code, gemfile, binding.pry debugging,. etc)

Hello !

Thank you for your answer and sorry for the delay (I will answer more quickly now).
My project is on :

  • ruby 2.7.2p137
  • Rails 6.0.3.7

Here is the simple code :

class MainController < ApplicationController
  skip_authorization_check
  require 'qr-bills'

  def index
    QRBills.create_creditor_reference("MTR81UUWZYO48NY55NP3")
  end
end

And the error :

Completed 500 Internal Server Error in 5ms (ActiveRecord: 0.0ms | Allocations: 1771)

NoMethodError (undefined method `create_creditor_reference' for QRBills:Class):
 
app/controllers/main_controller.rb:9:in `index'

And my gemfile :

# frozen_string_literal: true

source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '2.7.2'

gem 'rails_admin', '~> 2.0'

# Users
gem 'devise' # Gestion des utilisateurs
gem 'devise-i18n' # traduction
# Gérer permissions
gem 'cancancan'
# Attributs avec plusieurs choix
gem 'enumerize'
# Connexion avec Facebook
gem 'omniauth-apple'
gem 'omniauth-facebook'
gem 'omniauth-google-oauth2'
gem 'omniauth-rails_csrf_protection'

# Pour le versionning
# gem 'paper_trail'
gem 'public_activity'

# Google Calendar
# gem 'google-api-client', require: 'google/apis/calendar_v3'

# Twitter Bootstrap 4
gem 'bootstrap', '~> 4.3.1'
gem 'bootstrap-email'

# Pour les pdf
# gem 'wicked_pdf'
# gem 'wkhtmltopdf-binary' # si Wkhtmltopdf n'est pas déjà installé

# Pour le deployement !
gem 'capistrano', '~> 3.16'
gem 'capistrano-passenger', '~> 0.2'
gem 'capistrano-rails', '~> 1.4'
gem 'capistrano-rbenv', '~> 2.1', '>= 2.1.4'
# Pour avoir accès à la db
gem 'capistrano-rails-db'
# https://gitlab.com/ydkn/capistrano-rails-console
gem 'capistrano-rails-console', require: false

# Pour les alerts JavaScript jolies
# gem 'alertifyjs-rails'
# gem "therubyracer"
# gem "less-rails" #Sprockets (what Rails 3.1 uses for its asset pipeline) supports LESS
# gem "twitter-bootstrap-rails"
# Pour les formulaires de date et temps
# gem 'bootstrap4-datetime-picker-rails'
# gem 'pickadate-rails'

# Pagination
gem 'kaminari'
# Pour les formulaires avec bootstrap
gem 'simple_form'
# Selection des pays dans un formulaire
gem 'country_select'
# Pour les formulaires imbriqués
gem 'cocoon'

# Pour copier un contenu en JS
gem 'clipboard-rails'

# Intégrer QR code
gem 'rqrcode'

# Pour envoyer des emails
gem 'mailjet'

# Pour les textarea
# gem "wysiwyg-rails"
gem 'tinymce-rails'
# gem 'tinymce-rails-langs'

# Pour avoir un beau visuel des textarea
gem 'sanitize'

# Calendrier base de données
gem 'icalendar'

# Notifications
# gem 'active_delivery'
# gem "active_delivery", github: "palkan/active_delivery"
# Breadcrump
gem 'loaf'

# Haml pour des pages views plus propres
gem 'haml-rails', '~> 2.0'

# Jquery
gem 'jquery-rails'

# Icons Fontawesome
gem 'font-awesome-sass', '~> 5.11.2'

# Geolocalisation
gem 'geocoder'
# Avoir le timezone en fonction du la Geolocalisation
gem 'figaro'
gem 'timezone', '~> 1.0'
# Selection facile
gem 'select2-rails'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 6.0.0'
# Use sqlite3 as the database for Active Record
gem 'sqlite3', '~> 1.4'
# Use Puma as the app server
gem 'puma', '~> 3.11'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5'
# Transpile app-like JavaScript. Read more: https://github.com/rails/webpacker
gem 'webpacker', '~> 4.0'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.7'
# Traduction des trucs de base
gem 'rails-i18n', '~> 6.0.0' # For 6.0.0 or higher
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 4.0'
# Use Active Model has_secure_password
# gem 'bcrypt', '~> 3.1.7'
gem 'premailer-rails'

# Pour le delay job
gem 'sidekiq'

# gem 'sinatra', :require => nil

# Pour intégrer React
# gem 'react-rails'
# Use Active Storage variant
# gem 'image_processing', '~> 1.2'

# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.4.2', require: false

# For uploading files
gem 'carrierwave', '~> 2.0'

# FORM API React
# gem 'devise_token_auth'
# gem 'grape_devise_token_auth'
# gem 'grape-jwt-authentication'
# gem 'jwt'
gem 'doorkeeper'
gem 'doorkeeper-i18n'
# gem 'grape'
# gem 'grape-active_model_serializers'
# gem 'grape-swagger'
gem 'rack-cors'
# gem 'wine_bouncer', git: 'git@github.com:antek-drzewiecki/wine_bouncer.git', ref: 'c82b88f73c7adc43a8e89ca73f7e18952ec30de4'

# https://gist.github.com/stevecondylios/e819a296167a31578d82fac881963789
gem 'mail_form'

# Pour le parse JSON optimisé au max
gem 'fast_jsonapi'
gem 'oj'
gem 'active_model_serializers', '~> 0.10.0'

# Pour les recherches
# gem 'searchkick'

# Pour l'authentication des membres google depuis l'app
# https://github.com/google/google-id-token
gem 'apple_id'
gem 'google-id-token'
# gem 'google-api-client', '~> 0.34'

gem 'qr-bills'

# Traduction UI
# gem 'lit'

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platforms: %i[mri mingw x64_mingw]

  # Pour les bases de données test
  gem 'factory_bot_rails'
  gem 'rspec-rails'
end

group :development do
  # Access an interactive console on exception pages or by calling 'console' anywhere in the code.
  gem 'listen', '>= 3.0.5', '< 3.2'
  gem 'web-console', '>= 3.3.0'
  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
end

group :test do
  # Adds support for Capybara system testing and selenium driver
  gem 'capybara', '>= 2.15'
  gem 'selenium-webdriver'
  # Easy installation and use of web drivers to run system tests with browsers
  gem 'database_cleaner-active_record'
  gem 'webdrivers'
end

group :production do
  gem 'pg'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: %i[mingw mswin x64_mingw jruby]

gem 'redis'

In my gemfile.lock it is writen that I have the version qr-bills (0.0.1) of you gem. Is that correct ?

Thanks

Hello, v0.0.1 was super old :-)

Try to update to the latest v1.0.1 and let me know if you are facing the same issue.

How... i tried to bundle update but doesn t work..

well, this is a standard R&R command. The latest gem version is already published on the RubyGems repo:

You can do like that and run bundle install / bundle update

gem 'qr-bills', '~> 1.0.1'

Thanks,

It is very strange that by default it install the version 0.0.1... I think I had a version conflict or... ?
Thanks for your help, upgrading the app worked ! I can now generate a bill. Thank you !

May I just ask you a last noob question and abuse of your help ?

I tried generating a standard bill following your manual. Everything seems to work perfectly, the QR code is generated in my tmp/ folder as wanted. But at the output, the path of the QR code is not working :

My controller :

class MainController < ApplicationController
  skip_authorization_check

  def index
    QRBills.create_creditor_reference("MTR81UUWZYO48NY55NP3")

    # get the QR params, so you will get the full hash structure and as well some default values
    params = QRBills.get_qr_params

    # fill the params, for example
    params[:bill_type]                                      = QRBills.get_qrbill_with_creditor_reference_type
    params[:qrcode_filepath]                                = "#{Dir.pwd}/tmp/qrcode-html.png"
    params[:output_params][:format]                         = "html"
    params[:bill_params][:creditor][:iban]                  = "CH93 0076 2011 6238 5295 7"
    params[:bill_params][:creditor][:address][:type]        = "S"
    params[:bill_params][:creditor][:address][:name]        = "Compagnia di assicurazione forma & scalciante"
    params[:bill_params][:creditor][:address][:line1]       = "Via cantonale"
    params[:bill_params][:creditor][:address][:line2]       = "24"
    params[:bill_params][:creditor][:address][:postal_code] = "3000"
    params[:bill_params][:creditor][:address][:town]        = "Lugano"
    params[:bill_params][:creditor][:address][:country]     = "CH"
    params[:bill_params][:amount]                           = 12345.15
    params[:bill_params][:currency]                         = "CHF"
    params[:bill_params][:debtor][:address][:type]          = "S"
    params[:bill_params][:debtor][:address][:name]          = "Foobar Barfoot"
    params[:bill_params][:debtor][:address][:line1]         = "Via cantonale"
    params[:bill_params][:debtor][:address][:line2]         = "25"
    params[:bill_params][:debtor][:address][:postal_code]   = "3001"
    params[:bill_params][:debtor][:address][:town]          = "Comano"
    params[:bill_params][:debtor][:address][:country]       = "CH"
    # you can get the new creditor reference using QRBills.create_creditor_reference("your_reference")
    params[:bill_params][:reference]                        = "RF89MTR81UUWZYO48NY55NP3"
    params[:bill_params][:reference_type]                   = "SCOR"
    params[:bill_params][:additionally_information]         = "pagamento riparazione monopattino"

    # generate the QR Bill
    @bill = QRBills.generate(params)
  end
end

My view :

=@bill[:output].html_safe

And here is the result :
image

Am I using it the correct way ? How can I correct this QR code path ?
Thank you very much for your gem !

are you printing on standard html or via a pdf? Pdf prints in R&R may use a different approach to get the assets.

The path shown in the browser inspector does contain the qr-code image /home/yves/calmanager/tmp/qrcode-html.png? If yes, then check the web console for possible loading errors, if not, then there is a possible mismatch in the configuration you provided.

Here is the full error on the console :

Started GET "/home/yves/calmanager/tmp/qrcode-html.png" for 127.0.0.1 at 2021-09-24 20:44:28 +0200
  
ActionController::RoutingError (No route matches [GET] "/home/yves/calmanager/tmp/qrcode-html.png"):
  
actionpack (6.0.4.1) lib/action_dispatch/middleware/debug_exceptions.rb:36:in `call'
web-console (4.1.0) lib/web_console/middleware.rb:132:in `call_app'
web-console (4.1.0) lib/web_console/middleware.rb:28:in `block in call'
web-console (4.1.0) lib/web_console/middleware.rb:17:in `catch'
web-console (4.1.0) lib/web_console/middleware.rb:17:in `call'
actionpack (6.0.4.1) lib/action_dispatch/middleware/show_exceptions.rb:33:in `call'
railties (6.0.4.1) lib/rails/rack/logger.rb:37:in `call_app'
railties (6.0.4.1) lib/rails/rack/logger.rb:26:in `block in call'
activesupport (6.0.4.1) lib/active_support/tagged_logging.rb:80:in `block in tagged'
activesupport (6.0.4.1) lib/active_support/tagged_logging.rb:28:in `tagged'
activesupport (6.0.4.1) lib/active_support/tagged_logging.rb:80:in `tagged'
railties (6.0.4.1) lib/rails/rack/logger.rb:26:in `call'
sprockets-rails (3.2.2) lib/sprockets/rails/quiet_assets.rb:13:in `call'
actionpack (6.0.4.1) lib/action_dispatch/middleware/remote_ip.rb:81:in `call'
actionpack (6.0.4.1) lib/action_dispatch/middleware/request_id.rb:27:in `call'
rack (2.2.3) lib/rack/method_override.rb:24:in `call'
rack (2.2.3) lib/rack/runtime.rb:22:in `call'
activesupport (6.0.4.1) lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call'
actionpack (6.0.4.1) lib/action_dispatch/middleware/executor.rb:14:in `call'
actionpack (6.0.4.1) lib/action_dispatch/middleware/static.rb:126:in `call'
rack (2.2.3) lib/rack/sendfile.rb:110:in `call'
actionpack (6.0.4.1) lib/action_dispatch/middleware/host_authorization.rb:82:in `call'
rack-cors (1.1.1) lib/rack/cors.rb:100:in `call'
webpacker (4.3.0) lib/webpacker/dev_server_proxy.rb:23:in `perform_request'
rack-proxy (0.7.0) lib/rack/proxy.rb:63:in `call'
railties (6.0.4.1) lib/rails/engine.rb:527:in `call'
puma (3.12.6) lib/puma/configuration.rb:227:in `call'
puma (3.12.6) lib/puma/server.rb:706:in `handle_request'
puma (3.12.6) lib/puma/server.rb:476:in `process_client'
puma (3.12.6) lib/puma/server.rb:334:in `block in run'
puma (3.12.6) lib/puma/thread_pool.rb:135:in `block in spawn_thread'
Started GET "/home/yves/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/qr-bills-1.0.1/web/assets/fonts/LiberationSans-Regular.woff" for 127.0.0.1 at 2021-09-24 20:44:28 +0200
  
ActionController::RoutingError (No route matches [GET] "/home/yves/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/qr-bills-1.0.1/web/assets/fonts/LiberationSans-Regular.woff"):
  
actionpack (6.0.4.1) lib/action_dispatch/middleware/debug_exceptions.rb:36:in `call'
web-console (4.1.0) lib/web_console/middleware.rb:132:in `call_app'
web-console (4.1.0) lib/web_console/middleware.rb:28:in `block in call'
web-console (4.1.0) lib/web_console/middleware.rb:17:in `catch'
web-console (4.1.0) lib/web_console/middleware.rb:17:in `call'
actionpack (6.0.4.1) lib/action_dispatch/middleware/show_exceptions.rb:33:in `call'
railties (6.0.4.1) lib/rails/rack/logger.rb:37:in `call_app'
railties (6.0.4.1) lib/rails/rack/logger.rb:26:in `block in call'
activesupport (6.0.4.1) lib/active_support/tagged_logging.rb:80:in `block in tagged'
activesupport (6.0.4.1) lib/active_support/tagged_logging.rb:28:in `tagged'
activesupport (6.0.4.1) lib/active_support/tagged_logging.rb:80:in `tagged'
railties (6.0.4.1) lib/rails/rack/logger.rb:26:in `call'
sprockets-rails (3.2.2) lib/sprockets/rails/quiet_assets.rb:13:in `call'
actionpack (6.0.4.1) lib/action_dispatch/middleware/remote_ip.rb:81:in `call'
actionpack (6.0.4.1) lib/action_dispatch/middleware/request_id.rb:27:in `call'
rack (2.2.3) lib/rack/method_override.rb:24:in `call'
rack (2.2.3) lib/rack/runtime.rb:22:in `call'
activesupport (6.0.4.1) lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call'
actionpack (6.0.4.1) lib/action_dispatch/middleware/executor.rb:14:in `call'
actionpack (6.0.4.1) lib/action_dispatch/middleware/static.rb:126:in `call'
rack (2.2.3) lib/rack/sendfile.rb:110:in `call'
actionpack (6.0.4.1) lib/action_dispatch/middleware/host_authorization.rb:82:in `call'
rack-cors (1.1.1) lib/rack/cors.rb:100:in `call'
webpacker (4.3.0) lib/webpacker/dev_server_proxy.rb:23:in `perform_request'
rack-proxy (0.7.0) lib/rack/proxy.rb:63:in `call'
railties (6.0.4.1) lib/rails/engine.rb:527:in `call'
puma (3.12.6) lib/puma/configuration.rb:227:in `call'
puma (3.12.6) lib/puma/server.rb:706:in `handle_request'
puma (3.12.6) lib/puma/server.rb:476:in `process_client'
puma (3.12.6) lib/puma/server.rb:334:in `block in run'
puma (3.12.6) lib/puma/thread_pool.rb:135:in `block in spawn_thread'
Started GET "/home/yves/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/qr-bills-1.0.1/web/assets/fonts/LiberationSans-Regular.ttf" for 127.0.0.1 at 2021-09-24 20:44:28 +0200
  
ActionController::RoutingError (No route matches [GET] "/home/yves/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/qr-bills-1.0.1/web/assets/fonts/LiberationSans-Regular.ttf"):
  
actionpack (6.0.4.1) lib/action_dispatch/middleware/debug_exceptions.rb:36:in `call'
web-console (4.1.0) lib/web_console/middleware.rb:132:in `call_app'
web-console (4.1.0) lib/web_console/middleware.rb:28:in `block in call'
web-console (4.1.0) lib/web_console/middleware.rb:17:in `catch'
web-console (4.1.0) lib/web_console/middleware.rb:17:in `call'
actionpack (6.0.4.1) lib/action_dispatch/middleware/show_exceptions.rb:33:in `call'
railties (6.0.4.1) lib/rails/rack/logger.rb:37:in `call_app'
railties (6.0.4.1) lib/rails/rack/logger.rb:26:in `block in call'
activesupport (6.0.4.1) lib/active_support/tagged_logging.rb:80:in `block in tagged'
activesupport (6.0.4.1) lib/active_support/tagged_logging.rb:28:in `tagged'
activesupport (6.0.4.1) lib/active_support/tagged_logging.rb:80:in `tagged'
railties (6.0.4.1) lib/rails/rack/logger.rb:26:in `call'
sprockets-rails (3.2.2) lib/sprockets/rails/quiet_assets.rb:13:in `call'
actionpack (6.0.4.1) lib/action_dispatch/middleware/remote_ip.rb:81:in `call'
actionpack (6.0.4.1) lib/action_dispatch/middleware/request_id.rb:27:in `call'
rack (2.2.3) lib/rack/method_override.rb:24:in `call'
rack (2.2.3) lib/rack/runtime.rb:22:in `call'
activesupport (6.0.4.1) lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call'
actionpack (6.0.4.1) lib/action_dispatch/middleware/executor.rb:14:in `call'
actionpack (6.0.4.1) lib/action_dispatch/middleware/static.rb:126:in `call'
rack (2.2.3) lib/rack/sendfile.rb:110:in `call'
actionpack (6.0.4.1) lib/action_dispatch/middleware/host_authorization.rb:82:in `call'
rack-cors (1.1.1) lib/rack/cors.rb:100:in `call'
webpacker (4.3.0) lib/webpacker/dev_server_proxy.rb:23:in `perform_request'
rack-proxy (0.7.0) lib/rack/proxy.rb:63:in `call'
railties (6.0.4.1) lib/rails/engine.rb:527:in `call'
puma (3.12.6) lib/puma/configuration.rb:227:in `call'
puma (3.12.6) lib/puma/server.rb:706:in `handle_request'
puma (3.12.6) lib/puma/server.rb:476:in `process_client'
puma (3.12.6) lib/puma/server.rb:334:in `block in run'
puma (3.12.6) lib/puma/thread_pool.rb:135:in `block in spawn_thread'

I'm printing on standard html. The path shown the absolut path of the qr-code image in my computer.
But normaly my image assets are not added like that..
For exemple if I want to add an image, I will give the relative path like that :
=image_tag "image.jpg"

and the output will be like this :
<img src="/assets/image-1ad7cbc55ecc41b999106bed18262269b1935743ab8fa268e727d1e148922e39.jpg">

Or if the image is in the public folder, I will call it like that : =image_tag "#{root_path}image.jpg"
and the output will be like : <img src="image.jpg" >.

With the config that you showed on the manual, the image is created in the tmp/ folder in the root path... I read that this folder won't be accessible to your web server, for security reasons.

So I tried to modify the configuration to this :
params[:qrcode_filepath] = "#{root_path}/tmp/qrcode-html.png"

But I got this error :

Errno::EACCES in MainController#index
Permission denied @ rb_sysopen - /qrcode-html.png 

So what would be the correct way ?

Thanks !

Hello,

yes, if you are printing the exact output in a HTML page then the image may be not shown because the image path is not correctly passed through the rails assets.

I usually (and I guess the other as well) print this in a PDF, then it works as expected (you get the image).

If you need to print the QR bill only in HTML, then I suggest you to do a on-the-fly replace of the path or try to use the parameters qrcode_filepath with an image path already passed by the asset function. Not sure if this will work but it is worth a try. Otherwise the last chance would be to try to integrate the salt path inside the gem, but this will add more dependencies (rails) that I would like to avoid.

Or maybe there are other gems doing exactly that? i.e. replacing automatically absolute images paths into assets_path?

For reference here you can find all the params you can pass to the gem when you do the creation:

Do you have an empty project where you use your gem to generate a QR-bill ? So I could just have a look and it will probably help other persons :)
Thank you very much !

Hey :)

Sorry to insist, but is it possible to just have an example to how to print a QR Bill ?

I used

=@bill[:output].html_safe

in a html.haml file or in a pdf.haml file.. In both it didn't work... So I just think I m not doing correct.

Can you please just copy / paste here a simple code exemple (controller and view) to how you print a bill ?

Thanks for your help.

Hello @Texicitys, sure I can do a demo-page, however what is on the main readme seems to be enough for others developers (others are already using the gem based on the current docs).

I will close this because this request is not related to the original thread, please open a new issue - so I will not forget to do it - asking for a demo-page and as soon I will have some spare time and based on priority I will do it.

Thanks

Thanks @damoiser ! Sure it looks to be enough, that's the reason I'm not understanding what I'm doing wrong. If you could just copy/past a sample of a controller / view just to check what's wrong in mine, would be very very kind !

You are right to close the issue, the first message has no link with my last question. ;-)