Reputation: 3272
I'm following this tutorial to integrate Paypal to my Rails app but I have a problem with the hook. Paypal returns me the url via POST but I keep having the error :
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. I cannot use encode
or whatever because params is an ActiveRecord::Parameters
, not a String
...
The problem seems to be that, in Paypal my name is "Stéphane"
but it returns "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"=>"[email protected]", "address_country"=>"France", "address_city"=>"Paris", "quantity"=>"1", "verify_sign"=>"AOLXbVgQrAtqa0Lllz6erhuaVkd-ADHLMH5k6uuEypyAZ7WCQUuOpfxY", "payer_email"=>"[email protected]", "txn_id"=>"5SD166511T176754U", "payment_type"=>"instant", "last_name"=>"Xxxxx", "address_state"=>"Alsace", "receiver_email"=>"[email protected]", "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
Upvotes: 3
Views: 438
Reputation: 3272
After some more extra deeply searches, I found the right solution : https://stackoverflow.com/a/13504253/1805275
Upvotes: 1
Reputation: 3272
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.
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
Upvotes: 0