Sha256: b5807d7df928b87479d7df7bc983622fe900a807803d73e4a37020c647dc6bca

Contents?: true

Size: 1.44 KB

Versions: 2

Compression:

Stored size: 1.44 KB

Contents

require 'uri'
require 'rack/utils'
require 'fake_braintree/helpers'
require 'fake_braintree/credit_card'
require 'fake_braintree/customer'

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.8.0 lib/fake_braintree/redirect.rb
fake_braintree-0.7.0 lib/fake_braintree/redirect.rb