Paypal IPN:UTF-8 中的无效字节序列
Posted
技术标签:
【中文标题】Paypal IPN:UTF-8 中的无效字节序列【英文标题】:Paypal IPN: Invalid byte sequence in UTF-8 【发布时间】:2016-07-19 01:36:35 【问题描述】:我有一个处理 paypal 付款的 ruby 应用程序。最近我们改用ruby 2.3.0,最近出现了这个错误:ArgumentError: invalid byte sequence in UTF-8
我在下面发布了参数(已清理),并且很确定它来自瑞典语字符和字符集(windows)。
我不确定如何解决这个问题,或者以后要注意它。
我也不清楚错误发生在代码的哪一部分。
帮助?
» 30 Mar 2016 10:58:47.729 2016-03-30 17:58:47.428957+00:00 app web.1 - - Started POST "/en/paypal_ipns/cart_purchase_ipn" for 173.0.81.1 at 2016-03-30 17:58:47 +0000
» 30 Mar 2016 10:58:47.731 2016-03-30 17:58:47.435860+00:00 app web.1 - - Processing by PaypalIpnsController#cart_purchase_ipn as html
» 30 Mar 2016 10:58:47.731 2016-03-30 17:58:47.435988+00:00 app web.1 - - Parameters: "mc_gross"=>"10.15", "protection_eligibility"=>"Eligible", "address_status"=>"confirmed", "payer_id"=>"removed", "address_street"=>"Kung\xE4lvsgatan 6 A", "payment_date"=>"08:07:48 Mar 30, 2016 PDT", "payment_status"=>"Completed", "charset"=>"windows-1252", "address_zip"=>"41669", "first_name"=>"Martin", "mc_fee"=>"0.70", "address_country_code"=>"SE", "address_name"=>"Martin removed", "notify_version"=>"3.8", "custom"=>"7173", "payer_status"=>"verified", "business"=>"removed@gmail.com", "address_country"=>"Sweden", "address_city"=>"G\xF6teborg", "quantity"=>"1", "verify_sign"=>"removed", "payer_email"=>"removed@hotmail.com", "txn_id"=>"removed", "payment_type"=>"instant", "last_name"=>"removed", "address_state"=>"", "receiver_email"=>"removed@gmail.com", "payment_fee"=>"0.70", "receiver_id"=>"removed", "txn_type"=>"web_accept", "item_name"=>"removed", "mc_currency"=>"USD", "item_number"=>"13536", "residence_country"=>"SE", "transaction_subject"=>"", "payment_gross"=>"10.15", "ipn_track_id"=>"removed", "locale"=>"en"
» 30 Mar 2016 10:58:47.738 2016-03-30 17:58:47.446401+00:00 app web.1 - - Cart Load (2.2ms) SELECT "carts".* FROM "carts" WHERE "carts"."id" = $1 LIMIT 1 [["id", 13536]]
» 30 Mar 2016 10:58:47.738 2016-03-30 17:58:47.478086+00:00 app web.1 - -
» 30 Mar 2016 10:58:47.738 2016-03-30 17:58:47.478097+00:00 app web.1 - -
» 30 Mar 2016 10:58:47.738 2016-03-30 17:58:47.478098+00:00 app web.1 - - Exception caught, but rendered in pretty page
» 30 Mar 2016 10:58:47.738 2016-03-30 17:58:47.478103+00:00 app web.1 - - #<ArgumentError: invalid byte sequence in UTF-8>
» 30 Mar 2016 10:58:47.738 2016-03-30 17:58:47.478272+00:00 app web.1 - - /app/vendor/ruby-2.3.0/lib/ruby/2.3.0/psych/visitors/yaml_tree.rb:447:in `count'
» 30 Mar 2016 10:58:47.738 2016-03-30 17:58:47.478274+00:00 app web.1 - - /app/vendor/ruby-2.3.0/lib/ruby/2.3.0/psych/visitors/yaml_tree.rb:447:in `binary?'
» 30 Mar 2016 10:58:47.738 2016-03-30 17:58:47.478275+00:00 app web.1 - - /app/vendor/ruby-2.3.0/lib/ruby/2.3.0/psych/visitors/yaml_tree.rb:316:in `visit_String'
» 30 Mar 2016 10:58:47.738 2016-03-30 17:58:47.478275+00:00 app web.1 - - /app/vendor/ruby-2.3.0/lib/ruby/2.3.0/psych/visitors/yaml_tree.rb:163:in `accept'
» 30 Mar 2016 10:58:47.743 2016-03-30 17:58:47.478276+00:00 app web.1 - - /app/vendor/ruby-2.3.0/lib/ruby/2.3.0/psych/visitors/yaml_tree.rb:493:in `block in visit_hash_subclass'
» 30 Mar 2016 10:58:47.743 2016-03-30 17:58:47.478278+00:00 app web.1 - - /app/vendor/bundle/ruby/2.3.0/gems/actionpack-4.2.5.1/lib/action_controller/metal/strong_parameters.rb:185:in `each_pair'
» 30 Mar 2016 10:58:47.743 2016-03-30 17:58:47.478278+00:00 app web.1 - - /app/vendor/bundle/ruby/2.3.0/gems/actionpack-4.2.5.1/lib/action_controller/metal/strong_parameters.rb:185:in `each_pair'
» 30 Mar 2016 10:58:47.743 2016-03-30 17:58:47.478279+00:00 app web.1 - - /app/vendor/ruby-2.3.0/lib/ruby/2.3.0/psych/visitors/yaml_tree.rb:491:in `visit_hash_subclass'
» 30 Mar 2016 10:58:47.743 2016-03-30 17:58:47.478280+00:00 app web.1 - - /app/vendor/ruby-2.3.0/lib/ruby/2.3.0/psych/visitors/yaml_tree.rb:389:in `visit_Hash'
» 30 Mar 2016 10:58:47.743 2016-03-30 17:58:47.478280+00:00 app web.1 - - /app/vendor/ruby-2.3.0/lib/ruby/2.3.0/psych/visitors/yaml_tree.rb:163:in `accept'
» 30 Mar 2016 10:58:47.743 2016-03-30 17:58:47.478281+00:00 app web.1 - - /app/vendor/ruby-2.3.0/lib/ruby/2.3.0/psych/visitors/yaml_tree.rb:127:in `push'
» 30 Mar 2016 10:58:47.748 2016-03-30 17:58:47.478282+00:00 app web.1 - - /app/vendor/ruby-2.3.0/lib/ruby/2.3.0/psych.rb:416:in `dump'
» 30 Mar 2016 10:58:47.748 2016-03-30 17:58:47.478283+00:00 app web.1 - - /app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.5.1/lib/active_record/coders/yaml_column.rb:20:in `dump'
» 30 Mar 2016 10:58:47.748 2016-03-30 17:58:47.478283+00:00 app web.1 - - /app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.5.1/lib/active_record/type/serialized.rb:26:in `type_cast_for_database'
» 30 Mar 2016 10:58:47.748 2016-03-30 17:58:47.478284+00:00 app web.1 - - /app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.5.1/lib/active_record/type/mutable.rb:5:in `type_cast_from_user'
» 30 Mar 2016 10:58:47.748 2016-03-30 17:58:47.478285+00:00 app web.1 - - /app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.5.1/lib/active_record/attribute.rb:110:in `type_cast'
» 30 Mar 2016 10:58:47.748 2016-03-30 17:58:47.478286+00:00 app web.1 - - /app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.5.1/lib/active_record/attribute.rb:42:in `original_value'
» 30 Mar 2016 10:58:47.748 2016-03-30 17:58:47.478286+00:00 app web.1 - - /app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.5.1/lib/active_record/attribute.rb:37:in `value'
» 30 Mar 2016 10:58:47.752 2016-03-30 17:58:47.478287+00:00 app web.1 - - /app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.5.1/lib/active_record/attribute.rb:50:in `changed_from?'
» 30 Mar 2016 10:58:47.752 2016-03-30 17:58:47.478288+00:00 app web.1 - - /app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.5.1/lib/active_record/attribute_methods/dirty.rb:142:in `_field_changed?'
» 30 Mar 2016 10:58:47.752 2016-03-30 17:58:47.478289+00:00 app web.1 - - /app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.5.1/lib/active_record/attribute_methods/dirty.rb:115:in `save_changed_attribute'
» 30 Mar 2016 10:58:47.752 2016-03-30 17:58:47.478289+00:00 app web.1 - - /app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.5.1/lib/active_record/attribute_methods/dirty.rb:98:in `write_attribute'
» 30 Mar 2016 10:58:47.752 2016-03-30 17:58:47.478290+00:00 app web.1 - - /app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.5.1/lib/active_record/attribute_methods.rb:50:in `__temp__07162716d637'
» 30 Mar 2016 10:58:47.752 2016-03-30 17:58:47.478291+00:00 app web.1 - - /app/app/models/paypal_ipn.rb:20:in `params_info='
» 30 Mar 2016 10:58:47.752 2016-03-30 17:58:47.478291+00:00 app web.1 - - /app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.5.1/lib/active_record/attribute_assignment.rb:54:in `public_send'
» 30 Mar 2016 10:58:47.762 2016-03-30 17:58:47.478292+00:00 app web.1 - - /app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.5.1/lib/active_record/attribute_assignment.rb:54:in `_assign_attribute'
» 30 Mar 2016 10:58:47.762 2016-03-30 17:58:47.478293+00:00 app web.1 - - /app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.5.1/lib/active_record/attribute_assignment.rb:65:in `block in assign_nested_parameter_attributes'
» 30 Mar 2016 10:58:47.762 2016-03-30 17:58:47.478293+00:00 app web.1 - - /app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.5.1/lib/active_record/attribute_assignment.rb:65:in `each'
» 30 Mar 2016 10:58:47.762 2016-03-30 17:58:47.478294+00:00 app web.1 - - /app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.5.1/lib/active_record/attribute_assignment.rb:65:in `assign_nested_parameter_attributes'
» 30 Mar 2016 10:58:47.762 2016-03-30 17:58:47.478295+00:00 app web.1 - - /app/vendor/bundle/ruby/2.3.0/gems/activerecord-4.2.5.1/lib/active_record/attribute_assignment.rb:45:in `assign_attributes'
» 30 Mar 2016 10:58:47.762 2016-03-30 17:58:47.485285+00:00 app web.1 - - Rendered main/message.html.haml within layouts/application (1.4ms)
【问题讨论】:
【参考方案1】:你有带有扩展 ascii 的字符串,它们被解释为 UTF-8。您需要强制将它们的编码更改为支持那些高字节的东西,我的建议是“ISO-8859-1”,然后转换为 UTF-8:
str.force_encoding("ISO-8859-1").encode("UTF-8")
【讨论】:
以上是关于Paypal IPN:UTF-8 中的无效字节序列的主要内容,如果未能解决你的问题,请参考以下文章
PayPal IPN:已完成 500 内部服务器 JSON::GeneratorError(源序列非法/格式错误的 utf-8):