Dima_S

Untitled

Sep 29th, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. class BraintreeClient
  2. def self.connect_url(redirect_uri, scope)
  3. oauth_gateway.connect_url({
  4. redirect_uri: redirect_uri,
  5. scope: scope
  6. })
  7. end
  8.  
  9. def self.get_token(code)
  10. data = oauth_gateway.create_token_from_code(code: code).credentials
  11.  
  12. return {
  13. scope: data.instance_variable_get(:@scope),
  14. access_token: data.access_token,
  15. refresh_token: data.refresh_token,
  16. client_id: config[:client_id]
  17. }
  18. end
  19.  
  20. def self.get_account(vendor_user_id, access_token)
  21. Braintree::Customer.find(vendor_user_id, access_token)
  22. end
  23.  
  24. def initialize(access_token)
  25. @access_token = access_token
  26. end
  27.  
  28. [:customer].each do |m|
  29. braintree_class = "Braintree::#{m.capitalize}".constantize
  30. define_method(m) do |*opts|
  31. braintree_class.find(*opts, @access_token)
  32. end
  33. define_method(m.to_s.pluralize) do |opts={}|
  34. default_options = { limit: 100 }
  35. braintree_class.all(default_options.merge(opts), @access_token)
  36. end
  37. # TODO
  38. define_method("#{m.to_s.pluralize}_count") do
  39. return -1 unless @access_token
  40. braintree_class.all({ include: ['total_count'], limit: 1}, @access_token).total_count rescue -1
  41. end
  42. end
  43.  
  44. # Cards need customer..
  45. def cards(customer_id, options)
  46. Braintree::Customer.find(customer_id, @access_token).credit_cards
  47. end
  48.  
  49. # it's plug, see more http://www.rubydoc.info/github/braintree/braintree_ruby/Braintree/Dispute
  50. def update_dispute(vendor_dispute_id, new_evidence)
  51. true
  52. end
  53.  
  54. def source(vendor_source_id)
  55. Braintree::CreditCard.find(vendor_source_id)
  56. end
  57.  
  58. def user_subscriptions(customer_id)
  59. customer = self.customer(customer_id)
  60. customer.credit_cards.map(&:subscriptions).flatten
  61. end
  62.  
  63. # it's plug
  64. def refunds_by_charge (vendor_charge_id, options = {})
  65. true
  66. end
  67.  
  68. # it's plug
  69. def upcoming_invoice(vendor_customer_id)
  70. true
  71. end
  72.  
  73. def create_customer(email)
  74. Braintree::Customer.create({email: email}, @access_token)
  75. end
  76.  
  77. def create_card(customer_id, params)
  78. card = Braintree::CreditCard.create(
  79. customer_id: customer_id,
  80. number: params[:number],
  81. expiration_month: params[:expiration_month],
  82. expiration_year: params[:expiration_year],
  83. cvv: params[:cvv],
  84. options: {:verify_card => true}
  85. )
  86. end
  87.  
  88. # TODO string-89 method first! (should be defoult, but we can not add default card)
  89. def create_subscription_for_plan (customer_id, plan_id)
  90. customer = self.customer(customer_id)
  91. Braintree::Subscription.create(
  92. plan_id: plan_id,
  93. payment_method_nonce: customer.payment_methods.first.token
  94. )
  95. end
  96.  
  97. # it's plug
  98. def ping
  99. true
  100. end
  101.  
  102. private
  103. def self.oauth_gateway
  104. @gateway ||= Braintree::Gateway.new({
  105. client_id: config[:client_id],
  106. client_secret: config[:client_secret],
  107. client_public: config[:client_public],
  108. environment: config[:environment]
  109. }).oauth
  110. end
  111.  
  112. def self.config
  113. Rails.application.secrets.braintree
  114. end
  115. end
Advertisement
Add Comment
Please, Sign In to add comment