Sha256: 218d676cfac9aea070f0150d77b0813affab3fc94b9a0e4fccd8a697ebb1a9c9

Contents?: true

Size: 1.31 KB

Versions: 2

Compression:

Stored size: 1.31 KB

Contents

module FakeBraintree
  class Redirect
    include Helpers

    attr_reader :id

    def initialize(params, merchant_id)
      hash, query = *params[:tr_data].split('|', 2)
      @transparent_data = Rack::Utils.parse_nested_query(query)
      @merchant_id = merchant_id
      @id = create_id(@merchant_id)
      @params = params
      @kind = @transparent_data['kind']
    end

    def url
      uri.to_s
    end

    def confirm
      if @kind == 'create_customer'
        Customer.new(@params['customer'], {:merchant_id => @merchant_id}).create
      elsif @kind == 'create_payment_method'
        credit_card_options = {:merchant_id => @merchant_id}
        credit_card_options.merge!(@transparent_data['credit_card'].fetch('options', {}))

        credit_card_options.symbolize_keys!
        CreditCard.new(@params['credit_card'].merge(@transparent_data['credit_card']), credit_card_options).create
      end
    end

    private

    def uri
      uri = URI.parse(@transparent_data['redirect_url'])
      merged_query = [uri.query, base_query].compact.join('&')
      uri.query = "#{merged_query}&hash=#{hash(merged_query)}"
      uri
    end

    def base_query
      "http_status=200&id=#{@id}&kind=#{@kind}"
    end

    def hash(string)
      Braintree::Digest.hexdigest(Braintree::Configuration.private_key, string)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
fake_braintree-0.4 lib/fake_braintree/redirect.rb
fake_braintree-0.3 lib/fake_braintree/redirect.rb