paypal / paypalhttp_ruby

MIT License
6 stars 38 forks source link

Support ruby 3.0 with the removal of URI.escape #18

Closed dorianmariecom closed 2 years ago

dorianmariecom commented 2 years ago

Otherwise I get:

/Users/dorianmariefr/.rvm/gems/ruby-3.0.3/gems/paypalhttp-1.0.1/lib/paypalhttp/serializers/form_encoded.rb:8:in `block in encode': undefined method `escape' for URI:Module (NoMethodError)
darkBuddha commented 2 years ago

Excellent work, please merge it!

dorianmariecom commented 2 years ago

thanks, but maybe using activesupport's Hash#to_query would be better, or not doing the gsub or using CGI.escape

Also it needs to be tested on a full set of endpoints, I only tested for fetching an order and creating an order

GregSmith92 commented 2 years ago

Thanks for taking the time to create this. It seems like the gem authors won't be merging these anytime soon.

How would I do a monkey patch to included these changes?? Not done one before and I'm having trouble

dorianmariecom commented 2 years ago

@GregSmith92 You would add this in an initializer:

module PayPalHttp
  class FormEncoded
    def encode(request)
      encoded_params = []
      request.body.each do |k, v|
        encoded_params.push("#{escape(k)}=#{escape(v)}")
      end

      encoded_params.join("&")
    end

    def decode(body)
      raise UnsupportedEncodingError.new("FormEncoded does not support deserialization")
    end

    def content_type
      /^application\/x-www-form-urlencoded/
    end

    private

    def escape(value)
      URI.encode_www_form_component(value.to_s).gsub('+', '%20')
    end
  end
end
GregSmith92 commented 2 years ago

@dorianmariefr Thanks for responding 🙏.

This works perfectly, much appreciated.

hlahlou-pp-dev commented 2 years ago

An issue with this approach might come up when the original "key" that gets encoded contains a + within its value, as that will be translated into %2b with cgi escape, but will it be able to be properly decoded if the space values are replaced from + to %20?

dorianmariecom commented 2 years ago

ruby 3.1:

> URI.encode_www_form_component("Hello world").gsub('+', '%20')
=> "Hello%20world"

ruby 2.7.6

> URI.escape("Hello world")
(irb):3: warning: URI.escape is obsolete
=> "Hello%20world"

I don't know how PayPal's backend handles spacing but at least it's sending the same payload as before

crookedneighbor commented 2 years ago

This is fixed in v2.0.0, which was just released.