Class: Refinery::StoreGateway

Inherits:
Object
  • Object
show all
Defined in:
lib/refinery/store_gateway.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(store) ⇒ StoreGateway




21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/refinery/store_gateway.rb', line 21

def initialize( store )
  @store = store    # remember the store
  
  @gateways = %w( stripe )

  # TODO: make this variable by gateway type
  payment_gateway = "stripe"
  @gateway_mode =  ::Refinery::Setting.find_or_set( :payment_gateway_mode, "test" )
     # do some error checking & cleanup
  @gateway_mode = "test" unless @gateway_mode =~ /(test)|(live)/

  # setup the keys for the payment gateway
  payment_settings = "#{payment_gateway}_#{@gateway_mode}"

  @payment_access_url = ::Refinery::Setting.find_or_set( "#{payment_settings}_access_url".to_sym, "access URL missing" )
  @payment_secret_key = ::Refinery::Setting.find_or_set( "#{payment_settings}_secret_key".to_sym, "secret key missing" )
  @payment_api_key    = ::Refinery::Setting.find_or_set( "#{payment_settings}_api_key".to_sym,    "public API key missing" )
end

Instance Attribute Details

#gateway_modeObject (readonly)

######################################################################### ######################################################################### PAYMENT / GATEWAY CONFIGURATION ######################################################################### #########################################################################



12
13
14
# File 'lib/refinery/store_gateway.rb', line 12

def gateway_mode
  @gateway_mode
end

#payment_access_urlObject (readonly)

######################################################################### ######################################################################### PAYMENT / GATEWAY CONFIGURATION ######################################################################### #########################################################################



12
13
14
# File 'lib/refinery/store_gateway.rb', line 12

def payment_access_url
  @payment_access_url
end

#payment_api_keyObject (readonly)

######################################################################### ######################################################################### PAYMENT / GATEWAY CONFIGURATION ######################################################################### #########################################################################



12
13
14
# File 'lib/refinery/store_gateway.rb', line 12

def payment_api_key
  @payment_api_key
end

#payment_secret_keyObject (readonly)

######################################################################### ######################################################################### PAYMENT / GATEWAY CONFIGURATION ######################################################################### #########################################################################



12
13
14
# File 'lib/refinery/store_gateway.rb', line 12

def payment_secret_key
  @payment_secret_key
end

Instance Method Details

#charge_purchase(token, amount, order, email) ⇒ Object




63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/refinery/store_gateway.rb', line 63

def charge_purchase( token, amount, order, email )
begin
    # secret_key for Stripe
  Stripe.api_key = @payment_secret_key
 
    # create the charge on Stripe's servers
    # this will charge the user's card

  charge = Stripe::Charge.create(
    :amount => (amount * 100).to_i, # amount in cents
    :currency => "usd",             # US$
    :card => token,
    :description => purchase_description( order.order_number, email )
  )

  return charge

rescue ::Stripe::StripeError
  order.errors.add($!.class.to_s,  $!.to_s )  # capture errors to base object
  # order.errors.add(:cc_token, $!.class.to_s + ": " + $!.to_s )  # capture errors to base object
  return nil

end  # begin .. end for exceptions


end

#clear_gateways!Object





98
99
100
# File 'lib/refinery/store_gateway.rb', line 98

def clear_gateways!
  @gateways = []
end

#purchase_description(order_nbr, email) ⇒ Object





50
51
52
# File 'lib/refinery/store_gateway.rb', line 50

def purchase_description( order_nbr, email )
  "#{store_name } Order: #{order_nbr.to_s} purchase for: #{email.to_s}"
end

#register_gateway(name) ⇒ Object





92
93
94
# File 'lib/refinery/store_gateway.rb', line 92

def register_gateway(name)
  @gateways << name
end

#store_nameObject





42
43
44
45
46
# File 'lib/refinery/store_gateway.rb', line 42

def store_name
  str = @store.name
  str << " [TEST MODE]" if @gateway_mode == "test"
  return str
end