spree_pxpay_paymentmethod 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in spree_pxpay_paymentmethod.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,36 @@
1
+ # Spree PxPay Gateway
2
+
3
+ This is a gem for Spree > 1.1 which adds PXPay (paymentexpress.com - a NZ and
4
+ Australian Payment Processor) as a Payment Method.
5
+
6
+ This is does not use Active Merchant - this is not the version of the gateway
7
+ with a POST API (see ActiveMerchant PXPost docs), PXPay is the service hosted
8
+ on the Payment Express servers and credit card information is never touched by
9
+ your Spree implementation (sort of like PayPal's Website Payments system).
10
+
11
+ This extension will completely replace the payment page of the cart, and will
12
+ return the customer to the completed order page when their payment has been
13
+ processed.
14
+
15
+ As such, you can not use multiple payment methods (on the front end) alongside
16
+ this. You can manually create payments on the back-end as per usual.
17
+
18
+ ## Installation
19
+
20
+ 1. Add the gem to your Gemfile
21
+ 2. `bundle install`
22
+ 3. Add a new Payment Method in the admin section using the `Spree::Gateway::PxPay`
23
+ 4. Set the relevant configuration details
24
+
25
+ ## TODO
26
+
27
+ The DPS documentation talks of being able to embed as an iframe. I'd like to
28
+ see if it is possible within Spree's checkout process somewhere. Have to look
29
+ into the states within the state machine, I imagine.
30
+
31
+ Specs, obviously ;)
32
+
33
+ ## THANKS
34
+
35
+ Inspired by the [spree-hosted-gateway gem](https://github.com/joshmcarthur/spree-hosted-gateway) and the work done by
36
+ @bradleypriest who wrote the [PXPay gem](https://github.com/bradleypriest/pxpay).
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,51 @@
1
+ Spree::CheckoutController.class_eval do
2
+ skip_before_filter :verify_authenticity_token, :only => [:dps_callback]
3
+ skip_before_filter :load_order, :only => :px_pay_callback
4
+
5
+ # Handles the response from PxPay (success or failure) and updates the
6
+ # relevant Payment record.
7
+ def px_pay_callback
8
+ response = Pxpay::Response.new(params).response.to_hash
9
+
10
+ payment = Spree::Payment.find(response[:merchant_reference])
11
+
12
+ if payment then
13
+ if response[:success] == '1'
14
+ payment.started_processing
15
+ payment.response_code = response[:auth_code]
16
+ payment.save
17
+ payment.complete
18
+ @order = payment.order
19
+ @order.next
20
+
21
+ state_callback(:after)
22
+ if @order.state == "complete" || @order.completed?
23
+ state_callback(:before)
24
+ flash.notice = t(:order_processed_successfully)
25
+ flash[:commerce_tracking] = "nothing special"
26
+ redirect_to completion_route
27
+ else
28
+ respond_with(@order, :location => checkout_state_path(@order.state))
29
+ end
30
+ else
31
+ payment.void
32
+ redirect_to cart_path, :notice => 'Your credit card details were declined. Please check your details and try again.'
33
+ end
34
+ else
35
+ # Bad Payment!
36
+ raise Spree::Core::GatewayError, "Unknown merchant_reference: #{response[:merchant_reference]}"
37
+ end
38
+ end
39
+
40
+ private
41
+
42
+ alias :before_payment_without_px_pay_redirection :before_payment
43
+ def before_payment
44
+ before_payment_without_px_pay_redirection
45
+ redirect_to px_pay_gateway.url(@order, request)
46
+ end
47
+
48
+ def px_pay_gateway
49
+ @order.available_payment_methods.find { |x| x.is_a?(Spree::Gateway::PxPay) }
50
+ end
51
+ end
@@ -0,0 +1,64 @@
1
+ require 'pxpay'
2
+
3
+ module Spree
4
+ # The PxPay Gateway allows Spree to communicate with the PxPay system,
5
+ # including generating the correct URL to forward the customer to.
6
+ #
7
+ # It differs from most Spree::Gateway implementations in that it does not
8
+ # perform any actions directly via an API (as none exists), but just
9
+ # abstracts access to the pxpay gem.
10
+ class Gateway::PxPay < PaymentMethod
11
+
12
+ include Rails.application.routes.url_helpers
13
+
14
+ preference :user_id, :string
15
+ preference :key, :string
16
+ preference :currency_input, :string, :default => 'AUD', :description => "3 digit currency code from #{Pxpay::Base.currency_types.join(' ')}"
17
+
18
+ attr_accessible :preferred_user_id, :preferred_key, :preferred_currency_input
19
+
20
+ def source_required?
21
+ false
22
+ end
23
+
24
+ def actions
25
+ %w{}
26
+ end
27
+
28
+ # Indicates whether its possible to void the payment.
29
+ def can_void?(payment)
30
+ false
31
+ end
32
+
33
+ def url(order, request)
34
+ callback = callback_url(request.host, request.protocol)
35
+ px_pay_request(payment(order), callback).url
36
+ end
37
+
38
+ private
39
+
40
+ # Finds the pending payment or creates a new one.
41
+ def payment(order)
42
+ payment = order.payments.pending.first
43
+ return payment if payment.present?
44
+
45
+ payment = order.payments.new
46
+ payment.amount = order.total
47
+ payment.payment_method = self
48
+ payment.save!
49
+ payment
50
+ end
51
+
52
+ # Creates a new Pxpay::Request with all the relevant data.
53
+ def px_pay_request(payment, callback_url)
54
+ Pxpay::Base.pxpay_user_id = self.preferred_user_id
55
+ Pxpay::Base.pxpay_key = self.preferred_key
56
+ Pxpay::Request.new(payment.id, payment.amount, { :url_success => callback_url, :url_failure => callback_url, :currency_input => self.preferred_currency_input })
57
+ end
58
+
59
+ # Calculates the url to return to after the PxPay process completes
60
+ def callback_url(host, protocol)
61
+ url_for(:controller => 'spree/checkout', :action => 'px_pay_callback', :only_path => false, :host => host, :protocol => protocol)
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,10 @@
1
+ <fieldset data-hook="creditcard">
2
+ <legend>PxPay</legend>
3
+
4
+ <table class="index">
5
+ <tr>
6
+ <td><%= label_tag nil, 'Authorisation' %></td>
7
+ <td><%= payment.response_code %></td>
8
+ </tr>
9
+ </table>
10
+ </fieldset>
data/config/routes.rb ADDED
@@ -0,0 +1,3 @@
1
+ Rails.application.routes.draw do
2
+ match '/checkout/pay/callback' => 'spree/checkout#px_pay_callback'
3
+ end
@@ -0,0 +1,27 @@
1
+ require 'spree_core'
2
+ require "spree_pxpay_paymentmethod/version"
3
+
4
+ module SpreePxpayPaymentmethod
5
+ class Engine < Rails::Engine
6
+ railtie_name "spree_pxpay_paymentmethod"
7
+
8
+ config.autoload_paths += %W(#{config.root}/lib)
9
+
10
+ def self.activate
11
+ Dir.glob(File.join(File.dirname(__FILE__), "../app/overrides/*.rb")) do |c|
12
+ Rails.application.config.cache_classes ? require(c) : load(c)
13
+ end
14
+ Dir.glob(File.join(File.dirname(__FILE__), "../app/**/*_decorator*.rb")) do |c|
15
+ Rails.configuration.cache_classes ? require(c) : load(c)
16
+ end
17
+ end
18
+
19
+ initializer "spree_pxpay_paymentmethod.register.payment_methods" do |app|
20
+ app.config.spree.payment_methods += [
21
+ Spree::Gateway::PxPay
22
+ ]
23
+ end
24
+
25
+ config.to_prepare &method(:activate).to_proc
26
+ end
27
+ end
@@ -0,0 +1,3 @@
1
+ module SpreePxpayPaymentmethod
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "spree_pxpay_paymentmethod/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "spree_pxpay_paymentmethod"
7
+ s.version = SpreePxpayPaymentmethod::VERSION
8
+ s.authors = ["tuttinator"]
9
+ s.email = ["caleb.tutty@gmail.com"]
10
+ s.homepage = ""
11
+ s.summary = ""
12
+ s.description = "DPS PXPay payment method for Spree"
13
+
14
+ s.rubyforge_project = "spree_pxpay_paymentmethod"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_dependency('spree_core', '~> 1.1.1')
22
+ s.add_dependency('pxpay', '~> 0.2.6')
23
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: spree_pxpay_paymentmethod
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 1.0.0
6
+ platform: ruby
7
+ authors:
8
+ - tuttinator
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2012-09-10 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: spree_core
17
+ prerelease: false
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ~>
22
+ - !ruby/object:Gem::Version
23
+ version: 1.1.1
24
+ type: :runtime
25
+ version_requirements: *id001
26
+ - !ruby/object:Gem::Dependency
27
+ name: pxpay
28
+ prerelease: false
29
+ requirement: &id002 !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ~>
33
+ - !ruby/object:Gem::Version
34
+ version: 0.2.6
35
+ type: :runtime
36
+ version_requirements: *id002
37
+ description: DPS PXPay payment method for Spree
38
+ email:
39
+ - caleb.tutty@gmail.com
40
+ executables: []
41
+
42
+ extensions: []
43
+
44
+ extra_rdoc_files: []
45
+
46
+ files:
47
+ - .gitignore
48
+ - Gemfile
49
+ - README.md
50
+ - Rakefile
51
+ - app/controllers/checkout_controller_decorator.rb
52
+ - app/models/spree/gateway/px_pay.rb
53
+ - app/views/spree/admin/payments/source_views/_pxpay.html.erb
54
+ - config/routes.rb
55
+ - lib/spree_pxpay_paymentmethod.rb
56
+ - lib/spree_pxpay_paymentmethod/version.rb
57
+ - spree_pxpay_paymentmethod.gemspec
58
+ homepage: ""
59
+ licenses: []
60
+
61
+ post_install_message:
62
+ rdoc_options: []
63
+
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: "0"
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: "0"
78
+ requirements: []
79
+
80
+ rubyforge_project: spree_pxpay_paymentmethod
81
+ rubygems_version: 1.8.17
82
+ signing_key:
83
+ specification_version: 3
84
+ summary: ""
85
+ test_files: []
86
+