Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class ChargesController# < ApplicationController
- def create
- interactor = CheckoutInteractor.call({params: {currency: 'GRN', amount: '2', customer: 'customer',
- description: 'long decriptioin'}})
- if interactor.success?
- p '----Success----'
- p '---------------'
- # redirect_to charges_path
- else
- p '-----Error-----'
- p interactor.error
- p '---------------'
- # redirect_to new_charge_path
- end
- end
- end
- class CheckoutInteractor
- def self.call(context)
- interactor = new(context)
- p interactor
- interactor.run
- interactor
- end
- attr_reader :error
- def initialize(context)
- @context = context
- end
- def success?
- fail!('exception.message - Get lost!!!')
- @error.nil?
- end
- def run
- CheckoutService.new(context[:params])
- # rescue Stripe::CardError => exception
- # fail!(exception.message)
- end
- private
- attr_reader :context
- def fail!(error)
- @error = error
- end
- end
- class CheckoutService
- DEFAULT_CURRENCY = 'USD'.freeze
- def initialize(options = {})
- options.each do |key, value|
- instance_variable_set("@#{key}", value)
- p key, value
- p '-----------------------------------'
- end
- end
- def call
- # Stripe::Charge.create(charge_attributes)
- p 'Create Stripe Charge================='
- end
- private
- attr_reader :email, :source, :amount, :description
- def currency
- @currency || DEFAULT_CURRENCY
- end
- def amount
- @amount.to_i * 100
- end
- def customer
- @customer # ||= Stripe::Customer.create(customer_attributes)
- end
- #
- # def customer_attributes
- # {
- # email: email,
- # source: source
- # }
- # end
- def charge_attributes
- {
- customer: customer.id,
- amount: amount,
- description: description,
- currency: currency
- }
- end
- end
- ChargesController.new.create
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement