[英]Ruby on Rails - params from Paypal : invalid byte sequence in utf-8
I'm following this tutorial to integrate Paypal to my Rails app but I have a problem with the hook. 我正在按照本教程将Paypal集成到我的Rails应用程序中,但我遇到了问题。 Paypal returns me the url via POST but I keep having the error : Paypal通过POST返回我的网址,但我一直有错误:
ArgumentError (invalid byte sequence in UTF-8):
app/controllers/purchases_controller.rb:24:in `hook'
Here is my 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
I tried multiple solutions such as notification_params: params.encoding(xxx)
or force_encode(xxx)
without success. 我尝试了多种解决方案,例如notification_params: params.encoding(xxx)
或force_encode(xxx)
但没有成功。 I cannot use encode
or whatever because params is an ActiveRecord::Parameters
, not a String
... 我不能使用encode
或其他因为params是ActiveRecord::Parameters
,而不是String
...
The problem seems to be that, in Paypal my name is "Stéphane"
but it returns "St\\xE9phane"
. 问题似乎是,在Paypal我的名字是"Stéphane"
但它返回"St\\xE9phane"
。
Here is the complete log from the server : 这是服务器的完整日志:
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'
Thanks 谢谢
经过一些更深入的搜索后,我找到了正确的解决方案: https : //stackoverflow.com/a/13504253/1805275
After multiple tries without any success I decided to bypass the issue. 经过多次尝试没有任何成功,我决定绕过这个问题。 Since by database field is a text column, what I did is to create a String of all my parameters. 由于数据库字段是一个文本列,我所做的是创建一个包含所有参数的字符串。 After that, I was able to store my String into my text field database column. 之后,我能够将我的String存储到我的文本字段数据库列中。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.