Skip to content
Browse files

Fixed gem on load into Rails properly

  • Loading branch information...
1 parent 0968bf7 commit 2df8dfd61daf23264df051186068807f6c885a5f Paul Springett committed May 10, 2011
View
26 lib/worldpay-rails.rb
@@ -1,26 +0,0 @@
-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?
- @@test_mode || not Worldpay.in_production?
- end
-
- # can set this in your environment files
- def self.test_mode=(value = false)
- @@test_mode = value
- end
-
- def self.in_production?
- Rails.env.production?
- end
-
- end
-end
-
-class ActionView::Base
- include Worldpay::Rails::Helpers
-end
View
34 lib/worldpay-rails/helpers.rb
@@ -1,34 +0,0 @@
-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 => "USD",
- :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
View
62 lib/worldpay-rails/response.rb
@@ -1,62 +0,0 @@
-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
View
5 lib/worldpay-rails/version.rb
@@ -1,5 +0,0 @@
-module Worldpay
- module Rails
- VERSION = "0.0.1"
- end
-end
View
31 lib/worldpay.rb
@@ -0,0 +1,31 @@
+puts "loading lib/worldpay.rb"
+require File.join(File.dirname(__FILE__), "worldpay/railtie.rb")
+
+module Worldpay
+ autoload :Response, File.join(File.dirname(__FILE__), "worldpay/response")
+ autoload :InstanceMethods, File.join(File.dirname(__FILE__), "worldpay/helpers")
+
+ @@test_mode = false
+
+ def self.uri
+ if Worldpay.test?
+ "https://select-test.wp3.rbsworldpay.com/wcc/purchase"
+ else
+ "https://secure.wp3.rbsworldpay.com/wcc/purchase"
+ end
+ end
+
+ def self.test?
+ @@test_mode || (not Worldpay.in_production?)
+ end
+
+ # can set this in your environment files
+ def self.test_mode=(value = false)
+ @@test_mode = value
+ end
+
+ def self.in_production?
+ Rails.env.production?
+ end
+
+end
View
30 lib/worldpay/helpers.rb
@@ -0,0 +1,30 @@
+module Worldpay::InstanceMethods
+ def uri
+ Worldpay.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 => "USD",
+ :desc => "Purchase"
+ }.merge(options)
+
+ params.merge!({ :testMode => 100 }) if Worldpay.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").html_safe + capture(&block)
+ end
+
+ end
+end
+
+ActionView::Base.send(:include, Worldpay::InstanceMethods) if defined?(ActionView::Base)
View
18 lib/worldpay/railtie.rb
@@ -0,0 +1,18 @@
+require 'rails'
+require 'worldpay'
+
+p "Railtie loaded"
+
+begin
+module Worldpay
+ class Railtie < Rails::Railtie
+ config.to_prepare do
+ p "hook added"
+ ActionView::Base.send(:include, Worldpay::InstanceMethods)
+ end
+ end
+end
+rescue
+p $!, $!.message
+raise $!
+end
View
60 lib/worldpay/response.rb
@@ -0,0 +1,60 @@
+module Worldpay
+ 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
View
3 lib/worldpay/version.rb
@@ -0,0 +1,3 @@
+module Worldpay
+ VERSION = "0.0.9"
+end
View
10 worldpay-rails.gemspec → worldpay.gemspec
@@ -1,18 +1,18 @@
# -*- encoding: utf-8 -*-
-require File.expand_path("../lib/worldpay-rails/version", __FILE__)
+require File.expand_path("../lib/worldpay/version", __FILE__)
Gem::Specification.new do |s|
- s.name = "worldpay-rails"
- s.version = Worldpay::Rails::VERSION
+ s.name = "worldpay"
+ s.version = Worldpay::VERSION
s.platform = Gem::Platform::RUBY
s.authors = ['Paul Springett']
s.email = ['paul@paulspringett.name']
- s.homepage = "http://rubygems.org/gems/worldpay-rails"
+ s.homepage = "http://rubygems.org/gems/worldpay"
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.rubyforge_project = "worldpay"
s.add_development_dependency "bundler", ">= 1.0.0"

0 comments on commit 2df8dfd

Please sign in to comment.
Something went wrong with that request. Please try again.