SHARE
TWEET

Untitled

a guest Jul 17th, 2019 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class ChargesController# < ApplicationController
  2.   def create
  3.     interactor = CheckoutInteractor.call({params: {currency: 'GRN', amount: '2', customer: 'customer',
  4.                                                           description: 'long decriptioin'}})
  5.  
  6.     if interactor.success?
  7.       p '----Success----'
  8.       p '---------------'
  9.       # redirect_to charges_path
  10.     else
  11.       p '-----Error-----'
  12.       p interactor.error
  13.       p '---------------'
  14.       # redirect_to new_charge_path
  15.     end
  16.   end
  17. end
  18.  
  19. class CheckoutInteractor
  20.   def self.call(context)
  21.     interactor = new(context)
  22.     p interactor
  23.     interactor.run
  24.     interactor
  25.   end
  26.  
  27.   attr_reader :error
  28.  
  29.   def initialize(context)
  30.     @context = context
  31.   end
  32.  
  33.   def success?
  34.     fail!('exception.message - Get lost!!!')
  35.     @error.nil?
  36.   end
  37.  
  38.   def run
  39.     CheckoutService.new(context[:params])
  40.   # rescue Stripe::CardError => exception
  41.   #   fail!(exception.message)
  42.   end
  43.  
  44.   private
  45.  
  46.     attr_reader :context
  47.  
  48.     def fail!(error)
  49.       @error = error
  50.     end
  51. end
  52.  
  53. class CheckoutService
  54.   DEFAULT_CURRENCY = 'USD'.freeze
  55.  
  56.   def initialize(options = {})
  57.     options.each do |key, value|
  58.       instance_variable_set("@#{key}", value)
  59.       p key, value
  60.       p '-----------------------------------'
  61.     end
  62.   end
  63.  
  64.   def call
  65.     # Stripe::Charge.create(charge_attributes)
  66.     p 'Create Stripe Charge================='
  67.   end
  68.  
  69.   private
  70.  
  71.     attr_reader :email, :source, :amount, :description
  72.  
  73.     def currency
  74.       @currency || DEFAULT_CURRENCY
  75.     end
  76.  
  77.     def amount
  78.       @amount.to_i * 100
  79.     end
  80.  
  81.     def customer
  82.       @customer # ||= Stripe::Customer.create(customer_attributes)
  83.     end
  84.     #
  85.     # def customer_attributes
  86.     #   {
  87.     #       email: email,
  88.     #       source: source
  89.     #   }
  90.     # end
  91.  
  92.     def charge_attributes
  93.       {
  94.           customer: customer.id,
  95.           amount: amount,
  96.           description: description,
  97.           currency: currency
  98.       }
  99.     end
  100. end
  101.  
  102. ChargesController.new.create
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top