paypal-checkout-sdk breaks in ruby 3.0.1

578 Views Asked by At

Ruby 3.0.1

Rails 6.1.3.2

Executing a Paypal OrdersCreateRequest fails with this error

NoMethodError (undefined method `escape' for URI:Module):

The backtrace points to .rvm/gems/ruby-3.0.1/gems/paypalhttp-1.0.0/lib/paypalhttp/serializers/form_encoded.rb:8:in `block in encode' which contains this line as of paypalhttp v1.0.0

encoded_params.push("#{URI.escape(k.to_s)}=#{URI.escape(v.to_s)}")

Apparently the URI.escape method was removed in ruby 3 as stated here https://github.com/ruby/uri/issues/14

Is there a workaround for this or I have to just wait for Paypal to update the gem? I do not want to downgrade to ruby 2.x

1

There are 1 best solutions below

2
On

You could monkeypatch it:

# /config/initializers/functions_overrides.rb

require 'uri'

module URI
  def self.escape(*args)
    URI.encode_www_form_component(*args)
  end
end

Update: Added require 'uri' and comment with initializer path. Thanks for the hint @prd-rsd.