Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class BraintreeClient
- def self.connect_url(redirect_uri, scope)
- oauth_gateway.connect_url({
- redirect_uri: redirect_uri,
- scope: scope
- })
- end
- def self.get_token(code)
- data = oauth_gateway.create_token_from_code(code: code).credentials
- return {
- scope: data.instance_variable_get(:@scope),
- access_token: data.access_token,
- refresh_token: data.refresh_token,
- client_id: config[:client_id]
- }
- end
- def self.get_account(vendor_user_id, access_token)
- Braintree::Customer.find(vendor_user_id, access_token)
- end
- def initialize(access_token)
- @access_token = access_token
- end
- [:customer].each do |m|
- braintree_class = "Braintree::#{m.capitalize}".constantize
- define_method(m) do |*opts|
- braintree_class.find(*opts, @access_token)
- end
- define_method(m.to_s.pluralize) do |opts={}|
- default_options = { limit: 100 }
- braintree_class.all(default_options.merge(opts), @access_token)
- end
- # TODO
- define_method("#{m.to_s.pluralize}_count") do
- return -1 unless @access_token
- braintree_class.all({ include: ['total_count'], limit: 1}, @access_token).total_count rescue -1
- end
- end
- # Cards need customer..
- def cards(customer_id, options)
- Braintree::Customer.find(customer_id, @access_token).credit_cards
- end
- # it's plug, see more http://www.rubydoc.info/github/braintree/braintree_ruby/Braintree/Dispute
- def update_dispute(vendor_dispute_id, new_evidence)
- true
- end
- def source(vendor_source_id)
- Braintree::CreditCard.find(vendor_source_id)
- end
- def user_subscriptions(customer_id)
- customer = self.customer(customer_id)
- customer.credit_cards.map(&:subscriptions).flatten
- end
- # it's plug
- def refunds_by_charge (vendor_charge_id, options = {})
- true
- end
- # it's plug
- def upcoming_invoice(vendor_customer_id)
- true
- end
- def create_customer(email)
- Braintree::Customer.create({email: email}, @access_token)
- end
- def create_card(customer_id, params)
- card = Braintree::CreditCard.create(
- customer_id: customer_id,
- number: params[:number],
- expiration_month: params[:expiration_month],
- expiration_year: params[:expiration_year],
- cvv: params[:cvv],
- options: {:verify_card => true}
- )
- end
- # TODO string-89 method first! (should be defoult, but we can not add default card)
- def create_subscription_for_plan (customer_id, plan_id)
- customer = self.customer(customer_id)
- Braintree::Subscription.create(
- plan_id: plan_id,
- payment_method_nonce: customer.payment_methods.first.token
- )
- end
- # it's plug
- def ping
- true
- end
- private
- def self.oauth_gateway
- @gateway ||= Braintree::Gateway.new({
- client_id: config[:client_id],
- client_secret: config[:client_secret],
- client_public: config[:client_public],
- environment: config[:environment]
- }).oauth
- end
- def self.config
- Rails.application.secrets.braintree
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment