1

I'm trying to calculate a hash for a payment form but I get the error:

no implicit conversion of Fixnum into String

so I interpret this that I'm trying to do math calculation on what is text. But how should I correct my code?

The instructions from the payment merchant are:

hash = SHA1(salt + "|" + description + "|" + amount + "|" + currency + "|" + transaction_type)

So in my controller I have:

  def checkout
    @organization = Organization.new(organizationnew_params)
    if @organization.save
      @organization.members.each do |single_member|
        single_member.send_activation_email
      end
      @amount = 100.00
      @currency = "EUR"
      @description = @organization.id
      @transaction_description = "My description"
      @transaction_type = "S"

      ### LINE BELOW HAS THE HASH, WHICH REFERS TO THE PRIVATE METHOD BELOW ###
      @hash = hash(@description, @amount, @currency, @transaction_type)
      render 'checkout'
    else                            
      render 'new_premium'
    end
  end

private
  def hash(description, amount, currency, transaction_type)
    @hash = SHA1(SALT + "|" + description + "|" + amount + "|" + currency + "|" + transaction_type)
  end

In an initializer I have defined SALT (as well as my merchant_id which is used in the form that is posted to the merchant in the checkout view).

Nick
  • 3,496
  • 7
  • 42
  • 96

3 Answers3

2

Writing this will be better.

 hash = SHA1([salt, description, amount.to_s, currency,transaction_type].join("|"))
pangpang
  • 8,581
  • 11
  • 60
  • 96
  • This seemed to have worked but this did produce another error message: `undefined method 'SHA1' for # – Nick May 09 '15 at 12:05
  • In development.log (I hope this is what you mean?) is says: Completed 500 Internal Server Error in 965ms (ActiveRecord: 51.4ms) NoMethodError (undefined method `SHA1' for #): app/controllers/organizations_controller.rb:189:in `hash' app/controllers/organizations_controller.rb:60:in `checkout' – Nick May 09 '15 at 12:14
  • @Nick see: http://stackoverflow.com/questions/34490/how-do-i-create-a-sha1-hash-in-ruby – Mohammad May 09 '15 at 12:15
  • Thanks, I added `require 'digest/sha1'` at the beginning of the organizations controller. And in `def has` I replaced `SHA1` with `Digest::SHA1.hexdigest`. I now get a new error message but this seems to have solved the topic of this post. – Nick May 09 '15 at 12:24
0

The error indicates that you are adding a number to a string, I think the problem is that "amount" is a number which you are adding to a string. you just have to change that:

 @hash = SHA1(SALT + "|" + description + "|" + amount.to_s + "|" + currency + "|" + transaction_type)

the :to_s method will convert amount into string. (if there are other numbers just do the same.)

Or you can do this with string interpolation:

 @hash = SHA1("#{SALT}|#{description}|#{amount}|#{currency}|#{transaction_type}")
Mohammad
  • 3,276
  • 2
  • 19
  • 35
0

You interpreted it wrongly, it's the other way round. You're trying to use a number as a string.

Try to explicitely convert the number in to a string, like so:

@hash = SHA1(SALT + "|" + description + "|" + amount.to_s + "|" + currency + "|" + transaction_type)

As I don't know what all the variables' types are, you might have to add another to_s to a non-string.

pgrosslicht
  • 1,552
  • 13
  • 18