class Dwolla::OffsiteGateway

Attributes

additional_funding_sources[W]
allow_funding_sources[W]
callback[W]
facilitator_amount[W]
notes[W]
order_id[W]
redirect[W]
shipping[W]
tax[W]
test_mode[W]

Public Class Methods

add_product(name=nil, description=nil, price=nil, quantity=1) click to toggle source
# File lib/dwolla/offsite_gateway.rb, line 40
def self.add_product(name=nil, description=nil, price=nil, quantity=1)
    @products.push({
        :name => name,
        :description => description,
        :price => price,
        :quantity => quantity
    })
end
clear_session() click to toggle source
# File lib/dwolla/offsite_gateway.rb, line 14
def self.clear_session
    @products = []
    @discount = 0
    @tax = 0
    @shipping = 0
    @notes = nil
    @facilitator_amount = nil
    @test_mode = false
    @allow_funding_sources = true
    @additional_funding_sources = true
    @order_id = nil
end
discount=(discount) click to toggle source
# File lib/dwolla/offsite_gateway.rb, line 60
def self.discount=(discount)
    @discount = -(discount.abs)
end
get_checkout_url(destinationId) click to toggle source
# File lib/dwolla/offsite_gateway.rb, line 64
def self.get_checkout_url(destinationId)
    params = {
        :key => Dwolla::api_key,
        :secret => Dwolla::api_secret,
        :allowFundingSources => @allow_funding_sources,
        :additionalFundingSources => @additional_funding_sources,
        :test => @test_mode,
        :callback => @callback,
        :redirect => @redirect,
        :orderId => @order_id,
        :notes => @notes,
        :purchaseOrder => {
            :customerInfo => @customerInfo,
            :destinationId => destinationId,
            :orderItems => @products,
            :facilitatorAmount => @facilitator_amount,
            :discount => @discount,
            :shipping => @shipping,
            :tax => @tax,
            :total => self.calculate_total
        }
    }

    resp = Dwolla.request(:post, request_url, params, {}, false, false, true)

    return "No data received." unless resp.is_a?(Hash)
    raise APIError.new(resp['Message']) unless resp.has_key?('Result') and resp['Result'] == 'Success'

    return checkout_url + resp['CheckoutId']
end
read_callback(body) click to toggle source
# File lib/dwolla/offsite_gateway.rb, line 95
def self.read_callback(body)
    data = JSON.load(body)
    verify_callback_signature(data['signature'], data['checkoutId'], data['amount'])

    return data
end
set_customer_info(first_name=nil, last_name=nil, email=nil, city=nil, state=nil, zip=nil) click to toggle source
# File lib/dwolla/offsite_gateway.rb, line 49
def self.set_customer_info(first_name=nil, last_name=nil, email=nil, city=nil, state=nil, zip=nil)
    @customerInfo = {
        :firstName => first_name,
        :lastName => last_name,
        :email => email,
        :city => city,
        :state => state,
        :zip => zip
    }
end
validate_webhook(signature, body) click to toggle source
# File lib/dwolla/offsite_gateway.rb, line 102
def self.validate_webhook(signature, body)
    verify_webhook_signature(signature, body)
end

Private Class Methods

calculate_total() click to toggle source
# File lib/dwolla/offsite_gateway.rb, line 138
def self.calculate_total
    total = 0.0

    @products.each { |product|
        total += product[:price] * product[:quantity]
    }

    total += @shipping
    total += @tax
    total += @discount

    return total.round(2)
end
checkout_url() click to toggle source
# File lib/dwolla/offsite_gateway.rb, line 130
def self.checkout_url
    if Dwolla::sandbox
        return 'https://sandbox.dwolla.com/payment/checkout/'
    else
        return 'https://www.dwolla.com/payment/checkout/'
    end
end
request_url() click to toggle source
# File lib/dwolla/offsite_gateway.rb, line 122
def self.request_url
    if Dwolla::sandbox
        return 'https://sandbox.dwolla.com/payment/request'
    else
        return 'https://www.dwolla.com/payment/request'
    end
end
verify_callback_signature(candidate=nil, checkout_id=nil, amount=nil) click to toggle source
# File lib/dwolla/offsite_gateway.rb, line 107
def self.verify_callback_signature(candidate=nil, checkout_id=nil, amount=nil)
    key = "#{checkout_id}&#{amount}"
    digest  = OpenSSL::Digest.new('sha1')
    signature = OpenSSL::HMAC.hexdigest(digest, Dwolla::api_secret, key)

    raise APIError.new("Invalid callback signature (#{candidate} vs #{signature})") unless candidate == signature
end
verify_webhook_signature(candidate=nil, body=nil) click to toggle source
# File lib/dwolla/offsite_gateway.rb, line 115
def self.verify_webhook_signature(candidate=nil, body=nil)
    digest  = OpenSSL::Digest.new('sha1')
    signature = OpenSSL::HMAC.hexdigest(digest, Dwolla::api_secret, body)

    raise APIError.new("Invalid Webhook signature (#{candidate} vs #{signature})") unless candidate == signature
end