Permalink
Browse files

Initial import

  • Loading branch information...
0 parents commit bd714228aa3fb63b6bb508367966d678a234fa2c Paul Springett committed May 3, 2011
Showing with 153 additions and 0 deletions.
  1. +3 −0 .gitignore
  2. +4 −0 Gemfile
  3. +2 −0 Rakefile
  4. +21 −0 lib/worldpay-rails.rb
  5. +34 −0 lib/worldpay-rails/helpers.rb
  6. +62 −0 lib/worldpay-rails/response.rb
  7. +5 −0 lib/worldpay-rails/version.rb
  8. +22 −0 worldpay-rails.gemspec
@@ -0,0 +1,3 @@
+pkg/*
+*.gem
+.bundle
@@ -0,0 +1,4 @@
+source :gemcutter
+
+# Specify your gem's dependencies in worldpay-rails.gemspec
+gemspec
@@ -0,0 +1,2 @@
+require 'bundler'
+Bundler::GemHelper.install_tasks
@@ -0,0 +1,21 @@
+module Worldpay
+ module Rails
+
+ def self.uri
+ Worldpay.in_production? ? "https://secure.wp3.rbsworldpay.com/wcc/purchase" : "https://select-test.wp3.rbsworldpay.com/wcc/purchase"
+ end
+
+ def self.test?
+ not Worldpay.in_production?
+ end
+
+ def self.in_production?
+ Rails.env.production?
+ end
+
+ end
+end
+
+class ActionView::Base
+ include Worldpay::Rails::Helpers
+end
@@ -0,0 +1,34 @@
+module Worldpay
+ module Rails
+ module Helpers
+
+ def uri
+ Worldpay::Rails.uri
+ end
+
+ #generate html output for form tag
+ def worldpay_form_tag(installation_id, cart_id, amount, options = {}, &block)
+
+ params = {
+ :instId => "#{installation_id}",
+ :cartId => "#{cart_id}",
+ :amount => "#{amount}",
+ :currency => "GBP",
+ :desc => "Purchase"
+ }.merge(options)
+
+ params.merge!({ :testMode => 100 }) if Worldpay::Rails.test?
+
+ form_tag(uri) do
+ hidden_fields = []
+ params.each_pair do |name, value|
+ hidden_fields << hidden_field_tag(name, value)
+ end
+ hidden_fields.join("\n") + capture(&block)
+ end
+
+ end
+
+ end
+ end
+end
@@ -0,0 +1,62 @@
+module Worldpay
+ module Rails
+ class Response
+
+ def initialize(params, raw_post)
+ # Thanks to Peter Cooper http://www.petercooper.co.uk/
+ # http://snippets.dzone.com/posts/show/2191
+ # for this line to merge the parameters from WorldPay
+ @params = params.merge!(Hash[*raw_post.scan(/(\w+)\=(.+?)&/).flatten])
+ end
+
+ # checker methods
+
+ def is_authorized_by_callback_password?(password = nil)
+ if password.present?
+ password == @params['callbackPW']
+ else
+ true
+ end
+ end
+
+ def success?
+ transaction_result == 'Y'
+ end
+
+ def amounts_match?(order_total)
+ order_total == total
+ end
+
+ def currencies_match?(order_currency = 'GBP')
+ order_currency == currency
+ end
+
+ # get details of order
+
+ def order_ref
+ @params['cartId'].to_i
+ end
+
+ def total
+ @params['authAmount'].to_f
+ end
+
+ def transaction_ref
+ @params['transId']
+ end
+
+ def transaction_result
+ @params['transStatus']
+ end
+
+ def transaction_at
+ Time.parse(@params['transTime'].to_i / 1000)
+ end
+
+ def currency
+ @params['authCurrency']
+ end
+
+ end
+ end
+end
@@ -0,0 +1,5 @@
+module Worldpay
+ module Rails
+ VERSION = "0.0.1"
+ end
+end
@@ -0,0 +1,22 @@
+# -*- encoding: utf-8 -*-
+require File.expand_path("../lib/worldpay-rails/version", __FILE__)
+
+Gem::Specification.new do |s|
+ s.name = "worldpay-rails"
+ s.version = Worldpay::Rails::VERSION
+ s.platform = Gem::Platform::RUBY
+ s.authors = ['Paul Springett']
+ s.email = ['paul@paulspringett.name']
+ s.homepage = "http://rubygems.org/gems/worldpay-rails"
+ s.summary = "TODO: Write a gem summary"
+ s.description = "TODO: Write a gem description"
+
+ s.required_rubygems_version = ">= 1.3.6"
+ s.rubyforge_project = "worldpay-rails"
+
+ s.add_development_dependency "bundler", ">= 1.0.0"
+
+ s.files = `git ls-files`.split("\n")
+ s.executables = `git ls-files`.split("\n").map{|f| f =~ /^bin\/(.*)/ ? $1 : nil}.compact
+ s.require_path = 'lib'
+end

0 comments on commit bd71422

Please sign in to comment.