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.0.2'

Class Method Summary collapse

Class Method Details

.api_keyObject



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

def api_key
  Shoppe.config['stripe']['api_key']
end

.publishable_keyObject



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

def publishable_key
  Shoppe.config['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
61
62
# File 'lib/shoppe/stripe.rb', line 17

def setup
  # Validate the configuration file to ensure stripe has been configured.
  Shoppe.validate_live_config({'stripe' => ['api_key', 'publishable_key', 'currency']})
  
  require 'stripe'
  ::Stripe.api_key = self.api_key

  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.config['stripe']['currency'], :capture => false)
        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