class Refinery::StoreGateway

Attributes

gateway_mode[R]

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

payment_access_url[R]

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

payment_api_key[R]

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

payment_secret_key[R]

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

Public Class Methods

new( store ) click to toggle source

######################################################################### #########################################################################


initialize – creates new StoreGateway obj; args: store object


# 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

Public Instance Methods

charge_purchase( token, amount, order, email ) click to toggle source

charge_purchase – charge customer card for purchase return nil if error; else the JSON charge object from Stripe args:

token -- string for CC token produced earlier
amount -- float amt for charge in dollars.cents
order -- order object
email -- customer email

# 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!() click to toggle source


# File lib/refinery/store_gateway.rb, line 98
def clear_gateways!
  @gateways = []
end
purchase_description( order_nbr, email ) click to toggle source


# 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) click to toggle source


# File lib/refinery/store_gateway.rb, line 92
def register_gateway(name)
  @gateways << name
end
store_name() click to toggle source


# File lib/refinery/store_gateway.rb, line 42
def store_name
  str = @store.name
  str << " [TEST MODE]" if @gateway_mode == "test"
  return str
end