class SimplePayment::Braintree::Gateway

Constants

FEE_FLAT
FEE_PERCENTAGE

Public Class Methods

new(settings={}) click to toggle source
# File lib/simple_payment/braintree/gateway.rb, line 9
def initialize(settings={})
  @settings = settings
end

Public Instance Methods

calculate_fee(payment) click to toggle source
# File lib/simple_payment/braintree/gateway.rb, line 45
def calculate_fee(payment)
  raise 'payment cannot be nil' unless payment
  (payment.amount * FEE_PERCENTAGE) + FEE_FLAT
end
cancel(payment) click to toggle source
# File lib/simple_payment/braintree/gateway.rb, line 31
def cancel(payment)
  result = gateway.transaction.void(payment.external_id)
  [Request.new(:cancel), Response.new(result)]
rescue StandardError => e
  raise TransmissionError.new Request.new(:cancel), e.message
end
completed?(payment) click to toggle source
# File lib/simple_payment/braintree/gateway.rb, line 24
def completed?(payment)
  transaction = gateway.transaction.find(payment.external_id)
  %w(settling settled).include?(transaction.status)
rescue StandardError => e
  raise TransmissionError.new Request.new(:find), e.message
end
execute(payment, nonce) click to toggle source
# File lib/simple_payment/braintree/gateway.rb, line 17
def execute(payment, nonce)
  result = gateway.transaction.sale(execute_args(payment, nonce))
  [Request.new(:sale), Response.new(result)]
rescue StandardError => e
  raise TransmissionError.new Request.new(:sale), e.message
end
new_token() click to toggle source
# File lib/simple_payment/braintree/gateway.rb, line 13
def new_token
  gateway.client_token.generate
end
refund(payment) click to toggle source
# File lib/simple_payment/braintree/gateway.rb, line 38
def refund(payment)
  result = gateway.transaction.refund(payment.external_id)
  [Request.new(:refund), Response.new(result)]
rescue StandardError => e
  raise TransmissionError.new Request.new(:refund), e.message
end

Private Instance Methods

execute_args(payment, nonce) click to toggle source
# File lib/simple_payment/braintree/gateway.rb, line 56
def execute_args(payment, nonce)
  {
    amount: payment.amount,
    payment_method_nonce: nonce,
    options: {
      submit_for_settlement: true
    }
  }.tap do |a|
    if payment.payable&.respond_to?(:custom_fields) &&
        payment.payable&.custom_fields.present?
      a[:custom_fields] = payment.payable.custom_fields
    end
  end
end
gateway() click to toggle source
# File lib/simple_payment/braintree/gateway.rb, line 52
def gateway
  ::Braintree::Gateway.new @settings
end