Class: Taproot

Inherits:
Sinatra::Base
  • Object
show all
Includes:
Term::ANSIColor
Defined in:
lib/taproot.rb

Instance Method Summary collapse

Instance Method Details

#_client_token(options = {}) ⇒ Object



275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/taproot.rb', line 275

def _client_token(options = {})
  content_type :json

  raw_client_token = Braintree::ClientToken.generate(params)
  client_token = JSON.parse(Base64.decode64(raw_client_token))
  if Braintree::Configuration.environment == :production
    client_token["venmo"] = "production"
  end

  if options[:decoded]
    client_token
  else
    Base64.strict_encode64(JSON.dump(client_token))
  end
end

#_color_status(status) ⇒ Object



291
292
293
294
295
296
297
298
299
# File 'lib/taproot.rb', line 291

def _color_status(status)
  if status >= 400
    yellow status.to_s
  elsif status >= 500
    red status.to_s
  else
    green status.to_s
  end
end

#log(message) ⇒ Object



220
221
222
# File 'lib/taproot.rb', line 220

def log(message)
  puts "--- [#{CONFIG_MANAGER.current}] #{message}"
end

#nonce_from_paramsObject



224
225
226
227
228
229
230
# File 'lib/taproot.rb', line 224

def nonce_from_params
  server_config[:nonce_param_names].find do |nonce_param_name|
    if params[nonce_param_name]
      return params[nonce_param_name]
    end
  end
end

#sale(nonce, amount) ⇒ Object



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/taproot.rb', line 232

def sale(nonce, amount)
  transaction_params = {
    :amount => amount,
    :payment_method_nonce => nonce,
  }

  if CONFIG_MANAGER.
    transaction_params[:merchant_account_id] = CONFIG_MANAGER.
  end

  log("Creating transaction #{transaction_params.inspect}")

  result = Braintree::Transaction.sale(transaction_params)

  if result.transaction.present?
    void_result = Braintree::Transaction.void(result.transaction.id)
  end

  if result.success? and void_result.present? and void_result.success?
    {:message => "created #{result.transaction.id} #{result.transaction.status}"}
  else
    {:message => result.message || void_result.message}
  end

rescue Exception => e
  {:message => e.message}
end

#server_configObject



214
215
216
217
218
# File 'lib/taproot.rb', line 214

def server_config
  {
    :nonce_param_names => ["nonce", "payment_method_nonce", "paymentMethodNonce"]
  }
end

#vault(nonce, customer_id) ⇒ Object



260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/taproot.rb', line 260

def vault(nonce, customer_id)
  log("Vaulting payment method #{nonce} for customer #{customer_id}")

  result = Braintree::PaymentMethod.create({
    :customer_id => customer_id,
    :payment_method_nonce => nonce
  })

  if result.success?
    {:message => "Vaulted payment method #{result.payment_method.token}"}
  else
    {:message => result.message}
  end
end