Module: Shoppe::Stripe

Defined in:
lib/shoppe/stripe.rb,
lib/shoppe/stripe/railtie.rb,
lib/shoppe/stripe/version.rb,
lib/shoppe/stripe/view_helpers.rb,
lib/shoppe/stripe/order_extensions.rb

Defined Under Namespace

Modules: OrderExtensions, ViewHelpers Classes: Railtie

Constant Summary collapse

VERSION =
'1.1.0'

Class Method Summary collapse

Class Method Details

.api_keyObject



9
10
11
# File 'lib/shoppe/stripe.rb', line 9

def api_key
  Shoppe.settings.stripe_api_key
end

.publishable_keyObject



13
14
15
# File 'lib/shoppe/stripe.rb', line 13

def publishable_key
  Shoppe.settings.stripe_publishable_key
end

.setupObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/shoppe/stripe.rb', line 17

def setup
  Shoppe.add_settings_group :stripe, [:stripe_api_key, :stripe_publishable_key, :stripe_currency]
  
  require 'stripe'

  require 'shoppe/stripe/order_extensions'
  Shoppe::Order.send :include, Shoppe::Stripe::OrderExtensions
  
  # When an order is confirmed, attempt to authorise the payment
  Shoppe::Order.before_confirmation do
    if self.properties['stripe_customer_token']
      begin
        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)
        self.paid_at = Time.now
        self.payment_method = 'Stripe'
        self.payment_reference = charge.id
      rescue ::Stripe::CardError
        raise Shoppe::Errors::PaymentDeclined, "Payment was declined by the payment processor."
      end
    end
  end
  
  # When an order is accepted, attempt to capture the payment
  Shoppe::Order.before_acceptance do
    if stripe_charge
      begin
        stripe_charge.capture
      rescue ::Stripe::Error
        raise Shoppe::Errors::PaymentDeclined, "Payment could not be captured by Stripe. Investigate with Stripe. Do not accept the order."
      end
    end
  end
  
  # When an order is rejected, attempt to refund the payment
  Shoppe::Order.before_rejection do
    if stripe_charge
      begin
        stripe_charge.refund
      rescue ::Stripe::Error
        raise Shoppe::Errors::PaymentDeclined, "Payment could not be captured by Stripe. Investigate with Stripe. Do not accept the order."
      end
    end
  end
end