A pretty basic abstractor in Ruby (on Rails) for the Swipe API modelled on examples visible here: https://www.swipehq.co.nz/tools/
This is quick and dirty and is designed to help you out if you don't want to take credit card on your site. If you want take cards on your site and use Swipe as your background processing mechanism go use ActiveMerchant which supports Swipe. If you want people to input their cards directly on the Swipe screen, but still host your own cart, this might help you.
-
Grab a copy of swipe.rb and drop it into your
app/models/
orconfig/initalizers/
directory. -
If you don't use Figaro to manage ENV vars, consider doing so, or edit the following lines to suit your needs:
@merchant_id ||= ENV['SWIPE_ID'] @api_key ||= ENV['SWIPE_API_KEY'] self.callback_url ||= ENV['SWIPE_CALLBACK_URL'] self.lpn_url ||= ENV['SWIPE_LPN_URL']
-
Create yourself a new method that will handle the redirect from your cart to the Swipe payment page, something along the lines of:
def swipe order_id = Time.now.strftime("%y%m%d%H%M%S") swipe_params = { name: "Order ##{order_id}", price: @basket.total_with_shipping, user_data: order_id } @swipe = Swipe.new(swipe_params) # You can pass an optional hash containing any acceptable parameters to the identify command. # For instance, td_email, td_first_name, etc. redirect_to @swipe.identify({td_email: "[email protected]", td_first_name:, @address.name}) end
-
Create another action that will receive the LPN, something like:
def lpn
unless params[:td_user_data].empty?
item_id = params[:td_user_data]
transaction_id = params[:transaction_id]
if Swipe.accepted?(transaction_id)
# ... your sales logic
end
end
end