Class: Plugins::Ecommerce::CartService
- Inherits:
-
Object
- Object
- Plugins::Ecommerce::CartService
- Defined in:
- app/services/plugins/ecommerce/cart_service.rb
Instance Attribute Summary collapse
-
#cart ⇒ Object
readonly
Returns the value of attribute cart.
-
#site ⇒ Object
readonly
Returns the value of attribute site.
Instance Method Summary collapse
-
#initialize(site, cart) ⇒ CartService
constructor
A new instance of CartService.
- #pay_with_authorize_net(options = {}) ⇒ Object
- #pay_with_paypal(options = {}) ⇒ Object
- #pay_with_stripe(options) ⇒ Object
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
#cart ⇒ Object (readonly)
Returns the value of attribute cart.
7 8 9 |
# File 'app/services/plugins/ecommerce/cart_service.rb', line 7 def cart @cart end |
#site ⇒ Object (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 (={}) payment_method = [:payment_method] || site.payment_method('authorize_net') billing_address = cart.("billing_address") details = cart.("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: [:ip] } if [:ip] payment_params[:ip] = [:ip] end = { login: payment_method.[:authorize_net_login_id], password: payment_method.[:authorize_net_transaction_key] } ActiveMerchant::Billing::Base.mode = payment_method.[:authorize_net_sandbox].to_s.to_bool ? :test : :production credit_card = ActiveMerchant::Billing::CreditCard.new( first_name: [:first_name], last_name: [:last_name], number: [:number], month: [:exp_month], year: "20#{[:exp_year]}", verification_value: [:cvc] ) if credit_card.validate.empty? gateway = ActiveMerchant::Billing::AuthorizeNetGateway.new() response = gateway.purchase(amount, credit_card, payment_params) if response.success? cart.('pay_authorize_net', payment_params) return {} else return {error: response.} 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(={}) billing_address = cart.("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: [:ip], return_url: [:return_url], cancel_return_url: [:cancel_return_url] } if [:ip] gateway_request[:ip] = [: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() require 'stripe' payment_method = [:payment_method] || site.payment_method('stripe') Stripe.api_key = payment_method.[:stripe_id] customer = Stripe::Customer.create( email: [:email], source: [: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: [:email], customer: customer.id, charge: charge.id, } cart.("payment_data", payment_data) {} rescue Stripe::CardError => e {error: e., payment_error: true} rescue => e {error: e.} end end |