require "stripe_wrapper/engine" module StripeWrapper require "stripe" Stripe.api_key = ENV["STRIPE_KEY"] # Source is either a customer or a token def self.create_charge(customer=nil,source=nil,amount=0,currency='clp',metadata={},description='') #Metadata must be a hash. Example: :metadata => {'order_id' => '6735'} Stripe::Charge.create({ amount: amount, currency: currency, customer: customer, source: source, # obtained with Stripe.js description: description, metadata: metadata } # , { idempotency_key: "oSTGoYvkC1kaczHw"} ) end def self.create_customer(token,user,description='') Stripe::Customer.create( :description => description, :email => user.email, :source => token # obtained with Stripe.js ) end def self.get_customer(user) customer = StripeCustomer.find_by_user_id(user.id) customer = Customer.parse(Stripe::Customer.retrieve(customer.stripe_id)) rescue nil #TODO implement customer model end def self.update_customer(user,customer_params) customer = StripeCustomer.find_by_user_id(user.id) stripe_customer = Stripe::Customer.retrieve(customer.stripe_id) customer_params.each do |param| stripe_customer.send("#{param[0]}=",param[1]) end stripe_customer.save end end