Class: Plugins::Ecommerce::CartService

Inherits:
Object
  • Object
show all
Defined in:
app/services/plugins/ecommerce/cart_service.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site, cart) ⇒ CartService

Returns a new instance of CartService.



2
3
4
5
# File 'app/services/plugins/ecommerce/cart_service.rb', line 2

def initialize(site, cart)
  @site = site
  @cart = cart
end

Instance Attribute Details

#cartObject (readonly)

Returns the value of attribute cart.



7
8
9
# File 'app/services/plugins/ecommerce/cart_service.rb', line 7

def cart
  @cart
end

#siteObject (readonly)

Returns the value of attribute site.



7
8
9
# File 'app/services/plugins/ecommerce/cart_service.rb', line 7

def site
  @site
end

Instance Method Details

#pay_with_authorize_net(options = {}) ⇒ Object



9
10
11
12
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/services/plugins/ecommerce/cart_service.rb', line 9

def pay_with_authorize_net(options={})
  payment_method = options[:payment_method] || site.payment_method('authorize_net')
  billing_address = cart.get_meta("billing_address")
  details = cart.get_meta("details")
  amount = Plugins::Ecommerce::UtilService.ecommerce_money_to_cents(cart.total_amount)
  payment_params = {
    order_id: cart.slug,
    currency: site.currency_code,
    email: cart.user.email,
    billing_address: {name: "#{cart.user.fullname}",
                         address1: billing_address[:address1],
                         address2: billing_address[:address2],
                         city: billing_address[:city],
                         state: billing_address[:state],
                         country: billing_address[:country],
                         zip: billing_address[:zip]
    },
    description: 'Buy Products',
    ip: options[:ip]
  }

  if options[:ip]
    payment_params[:ip] = options[:ip]
  end

  authorize_net_options = {
    login: payment_method.options[:authorize_net_login_id],
    password: payment_method.options[:authorize_net_transaction_key]
  }

  ActiveMerchant::Billing::Base.mode = payment_method.options[:authorize_net_sandbox].to_s.to_bool ? :test : :production

  credit_card = ActiveMerchant::Billing::CreditCard.new(
    first_name: options[:first_name],
    last_name: options[:last_name],
    number: options[:number],
    month: options[:exp_month],
    year: "20#{options[:exp_year]}",
    verification_value: options[:cvc]
  )
  if credit_card.validate.empty?
    gateway = ActiveMerchant::Billing::AuthorizeNetGateway.new(authorize_net_options)
    response = gateway.purchase(amount, credit_card, payment_params)
    if response.success?
      cart.set_meta('pay_authorize_net', payment_params)
      return {}
    else
      return {error: response.message}
    end
  else
    return {error: credit_card.validate.map{|k, v| "#{k}: #{v.join(', ')}"}.join('<br>')}
  end
end

#pay_with_paypal(options = {}) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'app/services/plugins/ecommerce/cart_service.rb', line 63

def pay_with_paypal(options={})
  billing_address = cart.get_meta("billing_address")
  gateway = cart.paypal_gateway
  amount_in_cents = Plugins::Ecommerce::UtilService.ecommerce_money_to_cents(cart.total_amount)
  gateway_request = {
    brand_name: site.name,
    items: [{
      number: cart.slug,
      name: "Buy Products from #{site.the_title}: #{cart.products_title}",
      amount: amount_in_cents,
    }],
    order_id: cart.slug,
    currency: site.currency_code,
    email: cart.user.email,
    billing_address: {name: "#{billing_address[:first_name]} #{billing_address[:last_name]}",
                         address1: billing_address[:address1],
                         address2: billing_address[:address2],
                         city: billing_address[:city],
                         state: billing_address[:state],
                         country: billing_address[:country],
                         zip: billing_address[:zip]
    },
    description: "Buy Products from #{site.the_title}: #{cart.total_amount}",
    ip: options[:ip],
    return_url: options[:return_url],
    cancel_return_url: options[:cancel_return_url]
  }

  if options[:ip]
    gateway_request[:ip] = options[:ip]
  end

  response = gateway.setup_purchase(amount_in_cents, gateway_request)
  # TODO handle errors
  {redirect_url: gateway.redirect_url_for(response.token)}
end

#pay_with_stripe(options) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'app/services/plugins/ecommerce/cart_service.rb', line 100

def pay_with_stripe(options)
  require 'stripe'
  payment_method = options[:payment_method] || site.payment_method('stripe')
  Stripe.api_key = payment_method.options[:stripe_id]
  customer = Stripe::Customer.create(
    email: options[:email], source: options[:stripe_token])
  amount_in_cents = Plugins::Ecommerce::UtilService.ecommerce_money_to_cents(cart.total_amount)
  begin
    charge = Stripe::Charge.create(
      customer: customer.id,
      amount: amount_in_cents,
      description: "Payment Products: #{cart.products_title}",
      currency: site.currency_code,
    )
    payment_data = {
      email: options[:email],
      customer: customer.id,
      charge: charge.id,
    }
    cart.set_meta("payment_data", payment_data)
    {}
  rescue Stripe::CardError => e
    {error: e.message, payment_error: true}
  rescue => e
    {error: e.message}
  end
end