Class: NimbleshopSplitable::Gateway

Inherits:
Object
  • Object
show all
Includes:
ActiveMerchant::PostsData
Defined in:
lib/nimbleshop_splitable/gateway.rb

Instance Method Summary collapse

Constructor Details

#initialize(splitable) ⇒ Gateway

Returns a new instance of Gateway.



9
10
11
12
13
# File 'lib/nimbleshop_splitable/gateway.rb', line 9

def initialize(splitable)
  @api_key        = splitable.api_key
  @expires_in     = splitable.expires_in
  @mode           = splitable.mode
end

Instance Method Details

#add_order_data(params, order) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/nimbleshop_splitable/gateway.rb', line 43

def add_order_data(params, order)
  params[:total_amount] = order.total_amount_in_cents
  params[:invoice]      = order.number
  params[:shipping]     = Nimbleshop::Util.in_cents(ShippingCostCalculator.new(order).shipping_cost)
  params[:tax]          = Nimbleshop::Util.in_cents(TaxCalculator.new(order).tax)
  params[:description]  = 'See Splitable integrates nicely with nimbleShop'
end

#add_product_data(params, products, request) ⇒ Object



51
52
53
54
55
# File 'lib/nimbleshop_splitable/gateway.rb', line 51

def add_product_data(params, products, request)
  products.each.with_index(1) do | item, index |
    params.update(to_param(item, index, request))
  end
end

#api_notify_url(request) ⇒ Object



15
16
17
# File 'lib/nimbleshop_splitable/gateway.rb', line 15

def api_notify_url(request)
  Nimbleshop::Util.localhost2public_url( '/nimbleshop_splitable/splitable/notify', 'http' )
end

#build_params(order, request) ⇒ Object



36
37
38
39
40
41
# File 'lib/nimbleshop_splitable/gateway.rb', line 36

def build_params(order, request)
  params = {}
  add_order_data(params, order)
  add_product_data(params, order.line_items, request)
  params
end

#create(order, request) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/nimbleshop_splitable/gateway.rb', line 19

def create(order, request)
  params = build_params(order, request)

  params[:api_key]        = @api_key
  params[:api_notify_url] = api_notify_url(request)
  params[:expires_in]     = @expires_in

  unless Rails.env.production?
    Rails.logger.info "params to be posted: #{params.inspect}"
  end

  url     = test? ? self.test_url : self.live_url
  params  = params.collect { |key, value| "#{key}=#{CGI.escape(value.to_s)}" }.join("&")

  ssl_post(url, params)
end

#product_url(item, request) ⇒ Object



57
58
59
# File 'lib/nimbleshop_splitable/gateway.rb', line 57

def product_url(item, request)
  request.protocol + request.host_with_port + "/products/#{item.product_permalink}"
end

#test?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/nimbleshop_splitable/gateway.rb', line 70

def test?
  @mode == 'test'
end

#to_param(item, index, request) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/nimbleshop_splitable/gateway.rb', line 61

def to_param(item, index, request)
  {
    "amount_#{index}"     => (item.price * 100).to_i,
    "item_name_#{index}"  => item.product_name,
    "quantity_#{index}"   => item.quantity,
    "url_#{index}"        => product_url(item, request)
  }
end