shoppe-stripe 1.1.0 → 1.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 08f8f38e5bd7c52b3f57149ec46d6b71640a4a35
4
+ data.tar.gz: 42ffb84822be3f45d177f397534e4a5fdfbc8b4a
5
+ SHA512:
6
+ metadata.gz: 49ef76694bf16501dc9b88ff4af93341f39f6ba391b6bb279fe8a71eab2e2dc59399a4fbcab2a23f5ef43a725556c2c93671c3b549ad22cde56ec0be5cacd489
7
+ data.tar.gz: 271370bfb461b2ffa2231926ec798d44b2426d625cc09e27d2f8a08b46fc71fb5bfff0f83333708ed7fc26de168db29e8dd23a0fac9a6ea3e4e1d5e5e253604a
@@ -1,5 +1,5 @@
1
1
  require 'shoppe/stripe/version'
2
- require 'shoppe/stripe/railtie'
2
+ require 'shoppe/stripe/engine'
3
3
 
4
4
  module Shoppe
5
5
  module Stripe
@@ -15,21 +15,25 @@ module Shoppe
15
15
  end
16
16
 
17
17
  def setup
18
+ # Set the configuration which we would like
18
19
  Shoppe.add_settings_group :stripe, [:stripe_api_key, :stripe_publishable_key, :stripe_currency]
19
20
 
21
+ # Require the external Stripe library
20
22
  require 'stripe'
21
-
23
+
24
+ # Extend Shoppe Order & Payment classess
22
25
  require 'shoppe/stripe/order_extensions'
23
26
  Shoppe::Order.send :include, Shoppe::Stripe::OrderExtensions
27
+
28
+ require 'shoppe/stripe/payment_extensions'
29
+ Shoppe::Payment.send :include, Shoppe::Stripe::PaymentExtensions
24
30
 
25
31
  # When an order is confirmed, attempt to authorise the payment
26
32
  Shoppe::Order.before_confirmation do
27
33
  if self.properties['stripe_customer_token']
28
34
  begin
29
- charge = ::Stripe::Charge.create({:customer => self.properties['stripe_customer_token'], :amount => self.total_in_pence, :currency => Shoppe.settings.stripe_currency, :capture => false}, Shoppe.settings.stripe_api_key)
30
- self.paid_at = Time.now
31
- self.payment_method = 'Stripe'
32
- self.payment_reference = charge.id
35
+ charge = ::Stripe::Charge.create({:customer => self.properties['stripe_customer_token'], :amount => (self.total * BigDecimal(100)).round, :currency => Shoppe.settings.stripe_currency, :capture => false}, Shoppe.settings.stripe_api_key)
36
+ self.payments.create(:amount => self.total, :method => 'Stripe', :reference => charge.id, :refundable => true, :confirmed => false)
33
37
  rescue ::Stripe::CardError
34
38
  raise Shoppe::Errors::PaymentDeclined, "Payment was declined by the payment processor."
35
39
  end
@@ -38,27 +42,50 @@ module Shoppe
38
42
 
39
43
  # When an order is accepted, attempt to capture the payment
40
44
  Shoppe::Order.before_acceptance do
41
- if stripe_charge
45
+ self.payments.where(:confirmed => false, :method => 'Stripe').each do |payment|
42
46
  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."
47
+ payment.stripe_charge.capture
48
+ payment.update_attribute(:confirmed, true)
49
+ rescue ::Stripe::CardError
50
+ raise Shoppe::Errors::PaymentDeclined, "Payment ##{payment.id} could not be captured by Stripe. Investigate with Stripe. Do not accept the order."
46
51
  end
47
52
  end
48
53
  end
49
54
 
50
- # When an order is rejected, attempt to refund the payment
55
+ # When an order is rejected, attempt to refund all the payments which have been
56
+ # created with Stripe and are not confirmed.
51
57
  Shoppe::Order.before_rejection do
52
- if stripe_charge
58
+ self.payments.where(:confirmed => false, :method => 'Stripe').each do |payment|
59
+ payment.refund!(payment.refundable_amount)
60
+ end
61
+ end
62
+
63
+ # When a new payment is added which is a refund and associated with another Stripe method,
64
+ # attempt to refund it automatically.
65
+ Shoppe::Payment.before_create do
66
+ if self.refund? && self.parent && self.parent.method == 'Stripe'
53
67
  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."
68
+ options = {}
69
+ if self.parent.confirmed?
70
+ options[:amount] = (self.amount * BigDecimal(100)).round.abs
71
+ else
72
+ # If the original item hasn't been captured and the amount refunded isn't the
73
+ # same as the orignal value, raise an error.
74
+ if self.amount.abs != self.parent.refundable_amount
75
+ raise Shoppe::Errors::RefundFailed, :message => "Refund could not be processed because charge hasn't been captured and the amount is not the same as the original payment."
76
+ end
77
+ end
78
+ refund = self.parent.stripe_charge.refund(options)
79
+ self.method = 'Stripe'
80
+ self.reference = refund.id
81
+ true
82
+ rescue ::Stripe::CardError, ::Stripe::InvalidRequestError => e
83
+ raise Shoppe::Errors::RefundFailed, :message => "Refund could not be processed with Stripe (#{e.class}: #{e.message}). Please investigate with Stripe."
57
84
  end
58
85
  end
59
86
  end
87
+
60
88
  end
61
-
62
89
  end
63
90
  end
64
91
  end
@@ -1,10 +1,9 @@
1
1
  module Shoppe
2
2
  module Stripe
3
- class Railtie < Rails::Engine
3
+ class Engine < Rails::Engine
4
4
 
5
5
  initializer "shoppe.stripe.initializer" do
6
6
  Shoppe::Stripe.setup
7
-
8
7
  ActiveSupport.on_load(:action_view) do
9
8
  require 'shoppe/stripe/view_helpers'
10
9
  ActionView::Base.send :include, Shoppe::Stripe::ViewHelpers
@@ -27,11 +27,6 @@ module Shoppe
27
27
  @stripe_card ||= stripe_customer.cards.last
28
28
  end
29
29
 
30
- def stripe_charge
31
- return false unless self.paid? && self.payment_method == 'Stripe'
32
- @stripe_charge ||= ::Stripe::Charge.retrieve(self.payment_reference, Shoppe.settings.stripe_api_key)
33
- end
34
-
35
30
  end
36
31
  end
37
32
  end
@@ -0,0 +1,16 @@
1
+ module Shoppe
2
+ module Stripe
3
+ module PaymentExtensions
4
+
5
+ def stripe_charge
6
+ return false unless self.method == 'Stripe'
7
+ @stripe_charge ||= ::Stripe::Charge.retrieve(self.reference, Shoppe.settings.stripe_api_key)
8
+ end
9
+
10
+ def transaction_url
11
+ "https://manage.stripe.com/#{Rails.env.production? ? '/' : 'test/'}payments/#{reference}"
12
+ end
13
+
14
+ end
15
+ end
16
+ end
@@ -1,5 +1,5 @@
1
1
  module Shoppe
2
2
  module Stripe
3
- VERSION = '1.1.0'
3
+ VERSION = '1.2.1'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,20 +1,18 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shoppe-stripe
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
5
- prerelease:
4
+ version: 1.2.1
6
5
  platform: ruby
7
6
  authors:
8
7
  - Adam Cooke
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-10-24 00:00:00.000000000 Z
11
+ date: 2013-11-12 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: shoppe
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
17
  - - ~>
20
18
  - !ruby/object:Gem::Version
@@ -22,7 +20,6 @@ dependencies:
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
24
  - - ~>
28
25
  - !ruby/object:Gem::Version
@@ -30,7 +27,6 @@ dependencies:
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: stripe
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
31
  - - ~>
36
32
  - !ruby/object:Gem::Version
@@ -38,7 +34,6 @@ dependencies:
38
34
  type: :runtime
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
38
  - - ~>
44
39
  - !ruby/object:Gem::Version
@@ -46,19 +41,17 @@ dependencies:
46
41
  - !ruby/object:Gem::Dependency
47
42
  name: coffee-rails
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
45
  - - ~>
52
46
  - !ruby/object:Gem::Version
53
- version: 4.0.0w
47
+ version: 4.0.0
54
48
  type: :runtime
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
52
  - - ~>
60
53
  - !ruby/object:Gem::Version
61
- version: 4.0.0w
54
+ version: 4.0.0
62
55
  description: A Stripe module to assist with the integration of Stripe.
63
56
  email:
64
57
  - adam@niftyware.io
@@ -66,8 +59,9 @@ executables: []
66
59
  extensions: []
67
60
  extra_rdoc_files: []
68
61
  files:
62
+ - lib/shoppe/stripe/engine.rb
69
63
  - lib/shoppe/stripe/order_extensions.rb
70
- - lib/shoppe/stripe/railtie.rb
64
+ - lib/shoppe/stripe/payment_extensions.rb
71
65
  - lib/shoppe/stripe/version.rb
72
66
  - lib/shoppe/stripe/view_helpers.rb
73
67
  - lib/shoppe/stripe.rb
@@ -75,26 +69,26 @@ files:
75
69
  - MIT-LICENSE
76
70
  homepage: http://tryshoppe.com
77
71
  licenses: []
72
+ metadata: {}
78
73
  post_install_message:
79
74
  rdoc_options: []
80
75
  require_paths:
81
76
  - lib
82
77
  required_ruby_version: !ruby/object:Gem::Requirement
83
- none: false
84
78
  requirements:
85
- - - ! '>='
79
+ - - '>='
86
80
  - !ruby/object:Gem::Version
87
81
  version: '0'
88
82
  required_rubygems_version: !ruby/object:Gem::Requirement
89
- none: false
90
83
  requirements:
91
- - - ! '>='
84
+ - - '>='
92
85
  - !ruby/object:Gem::Version
93
86
  version: '0'
94
87
  requirements: []
95
88
  rubyforge_project:
96
- rubygems_version: 1.8.23
89
+ rubygems_version: 2.0.3
97
90
  signing_key:
98
- specification_version: 3
91
+ specification_version: 4
99
92
  summary: A Stripe module for Shoppe.
100
93
  test_files: []
94
+ has_rdoc: