shoppe-stripe 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,20 @@
1
+ Copyright 2013 Niftyware Limited.
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,64 @@
1
+ require 'stripe'
2
+ require 'shoppe/stripe/version'
3
+ require 'shoppe/stripe/order_extensions'
4
+ require 'shoppe/stripe/railtie'
5
+
6
+ module Shoppe
7
+ module Stripe
8
+
9
+ class << self
10
+
11
+ def api_key
12
+ Shoppe.config['stripe']['api_key']
13
+ end
14
+
15
+ def publishable_key
16
+ Shoppe.config['stripe']['publishable_key']
17
+ end
18
+
19
+ def setup
20
+ require 'stripe'
21
+ ::Stripe.api_key = self.api_key
22
+
23
+ Shoppe::Order.send :include, Shoppe::Stripe::OrderExtensions
24
+
25
+ # When an order is confirmed, attempt to authorise the payment
26
+ Shoppe::Order.before_confirmation do
27
+ if self.properties['stripe_customer_token']
28
+ begin
29
+ charge = ::Stripe::Charge.create(:customer => self.properties['stripe_customer_token'], :amount => self.total_in_pence, :currency => Shoppe.config['stripe']['currency'], :capture => false)
30
+ self.paid_at = Time.now
31
+ self.payment_method = 'Stripe'
32
+ self.payment_reference = charge.id
33
+ rescue ::Stripe::CardError
34
+ raise Shoppe::Errors::PaymentDeclined, "Payment was declined by the payment processor."
35
+ end
36
+ end
37
+ end
38
+
39
+ # When an order is accepted, attempt to capture the payment
40
+ Shoppe::Order.before_acceptance do
41
+ if stripe_charge
42
+ begin
43
+ stripe_charge.capture
44
+ rescue ::Stripe::Error
45
+ raise Shoppe::Errors::PaymentDeclined, "Payment could not be captured by Stripe. Investigate with Stripe. Do not accept the order."
46
+ end
47
+ end
48
+ end
49
+
50
+ # When an order is rejected, attempt to refund the payment
51
+ Shoppe::Order.before_rejection do
52
+ if stripe_charge
53
+ begin
54
+ stripe_charge.refund
55
+ rescue ::Stripe::Error
56
+ raise Shoppe::Errors::PaymentDeclined, "Payment could not be captured by Stripe. Investigate with Stripe. Do not accept the order."
57
+ end
58
+ end
59
+ end
60
+ end
61
+
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,37 @@
1
+ module Shoppe
2
+ module Stripe
3
+ module OrderExtensions
4
+
5
+ def accept_stripe_token(token)
6
+ if token =~ /\Atok/
7
+ customer = ::Stripe::Customer.create(:description => "Customer for order #{number}", :card => token)
8
+ self.properties['stripe_customer_token'] = customer.id
9
+ self.save
10
+ elsif token =~ /\Acus/ && self.properties[:stripe_customer_token] != token
11
+ self.properties['stripe_customer_token'] = token
12
+ self.save
13
+ elsif self.properties['stripe_customer_token'] && self.properties['stripe_customer_token'] =~ /\Acus/
14
+ true
15
+ else
16
+ false
17
+ end
18
+ end
19
+
20
+ private
21
+
22
+ def stripe_customer
23
+ @stripe_customer ||= ::Stripe::Customer.retrieve(self.properties['stripe_customer_token'])
24
+ end
25
+
26
+ def stripe_card
27
+ @stripe_card ||= stripe_customer.cards.last
28
+ end
29
+
30
+ def stripe_charge
31
+ return false unless self.paid? && self.payment_method == 'Stripe'
32
+ @stripe_charge ||= ::Stripe::Charge.retrieve(self.payment_reference)
33
+ end
34
+
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,16 @@
1
+ module Shoppe
2
+ module Stripe
3
+ class Railtie < Rails::Engine
4
+
5
+ initializer "shoppe.stripe.initializer" do
6
+ Shoppe::Stripe.setup
7
+
8
+ ActiveSupport.on_load(:action_view) do
9
+ require 'shoppe/stripe/view_helpers'
10
+ ActionView::Base.send :include, Shoppe::Stripe::ViewHelpers
11
+ end
12
+ end
13
+
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,5 @@
1
+ module Shoppe
2
+ module Stripe
3
+ VERSION = '1.0.0'
4
+ end
5
+ end
@@ -0,0 +1,14 @@
1
+ module Shoppe
2
+ module Stripe
3
+ module ViewHelpers
4
+
5
+ def shoppe_stripe_javascript
6
+ String.new.tap do |s|
7
+ s << javascript_include_tag('https://js.stripe.com/v2/')
8
+ s << "<script type=\"text/javascript\">Stripe.setPublishableKey('#{Shoppe::Stripe.publishable_key}');</script>"
9
+ end.html_safe
10
+ end
11
+
12
+ end
13
+ end
14
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: shoppe-stripe
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Adam Cooke
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-10-16 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: shoppe
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 0.0.8
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: 0.0.8
30
+ - !ruby/object:Gem::Dependency
31
+ name: stripe
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 1.8.7
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 1.8.7
46
+ - !ruby/object:Gem::Dependency
47
+ name: coffee-rails
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 4.0.0w
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 4.0.0w
62
+ description: A Stripe module to assist with the integration of Stripe.
63
+ email:
64
+ - adam@niftyware.io
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - lib/shoppe/stripe/order_extensions.rb
70
+ - lib/shoppe/stripe/railtie.rb
71
+ - lib/shoppe/stripe/version.rb
72
+ - lib/shoppe/stripe/view_helpers.rb
73
+ - lib/shoppe/stripe.rb
74
+ - MIT-LICENSE
75
+ homepage: http://tryshoppe.com
76
+ licenses: []
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ! '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ! '>='
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ requirements: []
94
+ rubyforge_project:
95
+ rubygems_version: 1.8.23
96
+ signing_key:
97
+ specification_version: 3
98
+ summary: A Stripe module for Shoppe.
99
+ test_files: []