【问题标题】:How to calculate the hash for a payment form?如何计算支付表单的哈希值?
【发布时间】:2023-03-07 00:13:01
【问题描述】:

我正在尝试计算付款表单的哈希值,但出现错误:

Fixnum 没有隐式转换为字符串

所以我解释说我正在尝试对什么是文本进行数学计算。但是我应该如何更正我的代码呢?

支付商家的说明是:

hash = SHA1(salt + "|" + description + "|" + amount + "|" + currency + “|” + 交易类型)

所以在我的控制器中我有:

  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

在初始化程序中,我定义了SALT(以及我的merchant_id,它在结帐视图中发布给商家的表单中使用)。

【问题讨论】:

  • 您的金额和描述是浮点数/整数

标签: ruby-on-rails ruby ruby-on-rails-4 hash


【解决方案1】:

这样写会更好。

 hash = SHA1([salt, description, amount.to_s, currency,transaction_type].join("|"))

【讨论】:

  • 这似乎有效,但这确实产生了另一条错误消息:undefined method 'SHA1' for #<OrganizationsController。这是否意味着 SHA1 不是自动可用的方法?我应该在某处添加此方法吗?
  • 在 development.log 中(我希望这就是你的意思?)说: Completed 500 Internal Server Error in 965ms (ActiveRecord: 51.4ms) NoMethodError (undefined method SHA1' for #<OrganizationsController:0x007effbb148658>): app/controllers/organizations_controller.rb:189:in hash' app/controllers /organizations_controller.rb:60:in `checkout'
  • 谢谢,我在组织控制器的开头添加了require 'digest/sha1'。在def has 中,我将SHA1 替换为Digest::SHA1.hexdigest。我现在收到一条新的错误消息,但这似乎解决了这篇文章的主题。
【解决方案2】:

你理解错了,反之亦然。您正在尝试将数字用作字符串。

尝试将数字显式转换为字符串,如下所示:

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

由于我不知道所有变量的类型是什么,您可能需要在非字符串中添加另一个 to_s

【讨论】:

  • 这仍然产生相同的错误,但 pangpang 线似乎工作。
【解决方案3】:

错误表明您正在向字符串添加数字,我认为问题在于“数量”是您要添加到字符串的数字。你只需要改变它:

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

:to_s 方法会将金额转换为字符串。 (如果有其他数字也一样。)

或者你可以用字符串插值来做到这一点:

 @hash = SHA1("#{SALT}|#{description}|#{amount}|#{currency}|#{transaction_type}")

【讨论】:

  • 两者仍然产生相同的错误,但 pangpang 线似乎工作。
猜你喜欢
  • 2017-05-25
  • 2022-01-04
  • 1970-01-01
  • 2014-04-01
  • 2016-06-23
  • 2013-10-30
  • 2011-12-19
  • 2014-09-16
  • 1970-01-01
相关资源
最近更新 更多