Skip to content

Instantly share code, notes, and snippets.

@Ank13
Last active December 28, 2015 08:19
Show Gist options
  • Save Ank13/7470845 to your computer and use it in GitHub Desktop.
Save Ank13/7470845 to your computer and use it in GitHub Desktop.
Trying out the Dwolla API. Please see the second file below, 'issues.md', for a list of issues and questions for Dwolla regarding the API.
require 'rubygems'
require 'dwolla'
class User
attr_accessor :dwolla_oauth_token, :encrypted_dwolla_pin
def initialize(dwolla_oauth_token, pin)
@dwolla_oauth_token = dwolla_oauth_token
@pin = pin
end
end
module Payment
# SCOPE: transactions
def self.charge_money(user, amount)
Dwolla::token = user.dwolla_oauth_token
Dwolla::debug = true
destination = '812-713-9234' # Hardcoded to Dwolla reflector account for testing purposes
Dwolla::Transactions.send({:destinationId => destination, :amount => amount, :pin => user.pin})
end
# SCOPE: balance
def self.check_dwolla_balance(user)
Dwolla::token = user.dwolla_oauth_token
Dwolla::Balance.get
end
# SCOPE: funding sources
def self.get_dwolla_funding_sources(user)
Dwolla::token = user.dwolla_oauth_token
Dwolla::FundingSources.get
end
def self.fund_dwolla_from_funding_source(user, amount, funding_source_id)
Dwolla::debug = true
Dwolla::token = user.dwolla_oauth_token
pin = user.pin
Dwolla::FundingSources.deposit(funding_source_id, {:amount => amount, :pin => pin})
end
end
# *********************************************************
# *** DRIVER CODE FOR DEBUGGING / EXPOLORATION ************
# *********************************************************
user = User.new("put_oauth_dwolla_token_here", pin) # REPLACE WITH DWOLLA OAUTH TOKEN AND PIN
balance = Payment.check_dwolla_balance(user) # => 0
sources = Payment.get_dwolla_funding_sources(user) # see below for return values
viable_sources = sources.select{ |src| src["Verified"] == true && src["Name"] == "Credit"}
charge_amount = 1.00
# sources is an array of three hashes:
# {"Id"=>"Balance", "Name"=>"My Dwolla Balance", "Type"=>"", "Verified"=>true, "ProcessingType"=>""},
# {"Id"=>"xxxxxxxxxxxxxxxxxxx", "Name"=>"bankxxxxxxxxxxxxx", "Type"=>"Checking", "Verified"=>false, "ProcessingType"=>"ACH"},
# {"Id"=>"Credit", "Name"=>"Credit", "Type"=>"", "Verified"=>true, "ProcessingType"=>""}]
if balance > charge_amount
puts Payment.charge_money(user, charge_amount)
elsif !viable_sources.empty?
fund_id = viable_sources.first["Id"]
amount = charge_amount - balance
Payment.fund_dwolla_from_funding_source(user, amount, fund_id)
puts Payment.charge_money(user, charge_amount)
else
puts "Sorry, you don't have any verified accounts with Dwolla that can be used"
end
# FAILS if try to fund from "Credit" funding source
# FAILS if Dwolla balance is 0 and try to fund from an "ACH" funding source, because it takes up to five days for the transfer to clear before it can be used.

Issues and Questions with this process:

  1. Using the API I cannot charge a user's Dwolla credit card. Is there a way to use the Dwolla API and oAuth to charge the user's Dwolla credit account? If not, is this planned?
  2. If the user does have a linked and verified bank account for ACH transfer, it can take up to five days for the transfer to clear. This means that if the user's Dwolla balance is currently 0, it is not viable to fund their Dwolla from their checking account and make a payment instantaneosly, or even on the same day. Is there a way to do this?
  3. There is no built-in way to receive a pre-authorization from the user to charge them for future expenses. As a workaround, Dwolla permits the app to solicit and save the user's Dwolla PIN. What's the recommended way to secure their pin and transmit it? (see StackOverflow: http://stackoverflow.com/questions/20005612/how-to-encrypt-a-pin-that-must-be-transmitted)
  4. Are their any future plans to enable pre-authorizations, like PayPal does?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment