Ruby on Rails - 来自 Paypal 的参数:utf-8 中的无效字节序列

Posted

技术标签:

【中文标题】Ruby on Rails - 来自 Paypal 的参数:utf-8 中的无效字节序列【英文标题】:Ruby on Rails - params from Paypal : invalid byte sequence in utf-8 【发布时间】:2014-12-21 17:30:15 【问题描述】:

我正在关注 this tutorial to integrate Paypal 到我的 Rails 应用程序,但我的钩子有问题。 Paypal 通过 POST 将网址返回给我,但我一直有错误:

ArgumentError (invalid byte sequence in UTF-8):
  app/controllers/purchases_controller.rb:24:in `hook'

这是我的钩子:

protect_from_forgery except: [:hook]
    def hook
        params.permit! # Permit all Paypal input params
        status = params[:payment_status]
        if status == "Completed"
          @purchase = Purchase.find params[:invoice]
          Line 24 --> @purchase.update_attributes notification_params: params, status: status, transaction_id: params[:txn_id], date: Time.now
        end
        render nothing: true
    end

我尝试了多种解决方案,例如notification_params: params.encoding(xxx)force_encode(xxx),但均未成功。我不能使用encode 或其他什么,因为params 是ActiveRecord::Parameters,而不是String...

问题似乎是,在 Paypal 中我的名字是 "Stéphane",但它返回 "St\xE9phane"

这是来自服务器的完整日志:

Started POST "/hook" for 127.0.0.1 at 2014-10-26 11:59:51 +0100
Processing by PurchasesController#hook as html
  Parameters: "mc_gross"=>"1.00", "invoice"=>"19", "protection_eligibility"=>"Eligible", "address_status"=>"unconfirmed", "payer_id"=>"FSXBUQDGG6KWN", "tax"=>"0.00", "address_street"=>"Av. de la Pelouse, 87648672 Mayet", "payment_date"=>"03:57:09 Oct 26, 2014 PDT", "payment_status"=>"Completed", "charset"=>"windows-1252", "address_zip"=>"75002", "first_name"=>"St\xE9phane", "mc_fee"=>"0.34", "address_country_code"=>"FR", "address_name"=>"St\xE9phane Xxxxx", "notify_version"=>"3.8", "custom"=>"", "payer_status"=>"verified", "business"=>"stephanexxxxx@gmail.com", "address_country"=>"France", "address_city"=>"Paris", "quantity"=>"1", "verify_sign"=>"AOLXbVgQrAtqa0Lllz6erhuaVkd-ADHLMH5k6uuEypyAZ7WCQUuOpfxY", "payer_email"=>"xxxxxxx@outlook.com", "txn_id"=>"5SD166511T176754U", "payment_type"=>"instant", "last_name"=>"Xxxxx", "address_state"=>"Alsace", "receiver_email"=>"stephanexxxxxxx@gmail.com", "payment_fee"=>"0.34", "receiver_id"=>"ZNER97N82WKY2", "txn_type"=>"web_accept", "item_name"=>"XXXX", "mc_currency"=>"USD", "item_number"=>"1", "residence_country"=>"FR", "test_ipn"=>"1", "handling_amount"=>"0.00", "transaction_subject"=>"", "payment_gross"=>"1.00", "shipping"=>"0.00", "ipn_track_id"=>"d4e0d603abd89"
  User Load (0.4ms)  SELECT  "users".* FROM "users"  WHERE "users"."remember_token" = 'da39a3ee5e6b4b0d3255bfef95601890afd80709' LIMIT 1
  Purchase Load (0.2ms)  SELECT  "purchases".* FROM "purchases"  WHERE "purchases"."id" = ?  ORDER BY created_at DESC LIMIT 1  [["id", 19]]
   (0.1ms)  begin transaction
   (0.1ms)  rollback transaction
Completed 500 Internal Server Error in 11ms

ArgumentError (invalid byte sequence in UTF-8):
  app/controllers/purchases_controller.rb:24:in `hook'

谢谢

【问题讨论】:

你试过rack-utf8_sanitizer吗? 是的@papirtiger,我遇到了同样的错误 【参考方案1】:

经过更深入的搜索,我找到了正确的解决方案:https://***.com/a/13504253/1805275

【讨论】:

【参考方案2】:

在多次尝试都没有成功后,我决定绕过这个问题。由于数据库字段是一个文本列,我所做的是创建一个包含所有参数的字符串。之后,我能够将我的字符串存储到我的文本字段数据库列中。

protect_from_forgery except: [:hook]
def hook
  params.permit! # Permit all Paypal input params
  status = params[:payment_status]
  if status == "Completed"

    @purchase = Purchase.find params[:invoice]
    parameters = String.new
    params.each do |key, value|
      parameters += key + " = " + value + " | "
    end

    @purchase.update_attributes notification_params: parameters, status: status, transaction_id: params[:txn_id], date: Time.now
  end

  render nothing: true
end

【讨论】:

以上是关于Ruby on Rails - 来自 Paypal 的参数:utf-8 中的无效字节序列的主要内容,如果未能解决你的问题,请参考以下文章

paypal gem 多个帐户 ruby​​ on rails

Paypal 经常性问题 ruby​​ on rails

如何在 Ruby on Rails 中集成 Paypal 集成

Ruby on Rails Paypal REST API 来宾结账

美国以外的 Ruby on Rails 和 Paypal

如何将 paypal 与 ruby​​ on rails 应用程序集成?