Class: TradoStripeModule::Striper

Inherits:
Object
  • Object
show all
Defined in:
lib/trado_stripe_module/striper.rb

Class Method Summary collapse

Class Method Details

.complete(order, session, ip_address) ⇒ Object

Creates a charge for the Stripe API; receives a response and in turn creates the relevant transaction records, sends a confirmation email and redirects the user.

Parameters:

  • order (Object)
  • session (Object)

    ession [Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/trado_stripe_module/striper.rb', line 13

def self.complete order, session, ip_address
    order.transfer(order.cart)
    charge = Stripe::Charge.create(
        amount: Store::Price.new(price: order.gross_amount, tax_type: 'net').singularize,
        currency: Store.settings.currency_code,
        customer: order.stripe_customer_id,
        description: "Order ID ##{order.id} | #{order.billing_address.full_name} | #{order.email}",
        metadata: Hash[ *order.order_items.collect { |item| [ "order_item_#{item.id}", "#{item.product.name} - #{item.sku.full_sku}" ] }.flatten ],
        statement_descriptor: Store.settings.stripe_statement_descriptor,
        expand: ['balance_transaction']
    )
    if charge.paid
        TradoStripeModule::Striper.successful(charge, order)
        Payatron4000.destroy_cart(session)
        Payatron4000.decommission_order(order)
        order.reload
        Mailatron4000::Orders.confirmation_email(order)
        return Rails.application.routes.url_helpers.success_orders_url(host: Trado::Application.config.action_mailer.default_url_options[:host])
    end
rescue Stripe::CardError => e
    body = e.json_body
    err  = body[:error]
    TradoStripeModule::Striper.failed(err, order)
    order.reload
    Mailatron4000::Orders.confirmation_email(order)
    return Rails.application.routes.url_helpers.failed_orders_url(host: Trado::Application.config.action_mailer.default_url_options[:host])
end

.failed(error, order) ⇒ Object

When an order has failed to complete, a new transaction record is created with a logged status reason

Parameters:

  • error (Object)
  • order (Object)


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/trado_stripe_module/striper.rb', line 65

def self.failed error, order
    Transaction.new(  :fee                        => 0, 
                      :gross_amount               => order.gross_amount, 
                      :order_id                   => order.id, 
                      :payment_status             => 'failed', 
                      :transaction_type           => 'Credit', 
                      :tax_amount                 => order.tax_amount, 
                      :stripe_charge_id           => nil, 
                      :payment_type               => 'stripe',
                      :net_amount                 => order.net_amount,
                      :status_reason              => error[:message],
                      :error_code                 => error[:code].to_i
    ).save(validate: false)
    Payatron4000.increment_product_order_count(order.products)
end

.format_currency(charge) ⇒ Object



81
82
83
# File 'lib/trado_stripe_module/striper.rb', line 81

def self.format_currency charge
  Money.new(charge.balance_transaction.fee, charge.currency.upcase)
end

.successful(charge, order) ⇒ Object

Upon successfully completing an order with a Stripe payment option a new transaction record is created, stock is updated for the relevant SKU

Parameters:

  • charge (Object)
  • order (Object)


45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/trado_stripe_module/striper.rb', line 45

def self.successful charge, order
    Transaction.new(  :fee                      => TradoStripeModule::Striper.format_currency(charge),  
                      :order_id                 => order.id, 
                      :payment_status           => 'completed', 
                      :transaction_type         => 'Credit', 
                      :tax_amount               => order.tax_amount, 
                      :stripe_charge_id         => charge.id, 
                      :payment_type             => 'stripe',
                      :net_amount               => order.net_amount,
                      :gross_amount             => order.gross_amount
    ).save(validate: false)
    Payatron4000.update_stock(order)
    Payatron4000.increment_product_order_count(order.products)
end