Skip to content

Instantly share code, notes, and snippets.

@hoffmanc
Created December 7, 2012 21:00
Show Gist options
  • Save hoffmanc/4236467 to your computer and use it in GitHub Desktop.
Save hoffmanc/4236467 to your computer and use it in GitHub Desktop.
Dwolla Active Merchant Integration Example
class DwollaController < ApplicationController
include ActiveMerchant::Billing::Integrations
def cancel
backer = Backer.find params[:id]
flash[:failure] = t('projects.backers.checkout.dwolla_cancel')
redirect_to new_project_backer_path(backer.project)
end
def confirm
backer = Backer.find(params[:id])
notify = ActiveMerchant::Billing::Integrations::Dwolla::Notification.new(params.to_json)
if notify.complete?
backer.update_attributes(
:key => checkout.payment_info.first.transaction_id,
:payment_method => 'Dwolla',
:payment_token => params[:token]
)
backer.build_payment_detail.update_from_service
backer.confirm!
flash[:success] = t('projects.backers.checkout.success')
redirect_to thank_you_path
else
#flash[:failure] = t('projects.backers.checkout.dwolla_error')
flash[:failure] = "#{t('projects.backers.checkout.error_header')} #{params[:error_description]}"
return redirect_to new_project_backer_path(backer.project)
end
end
end
// snipped out some bits, but this is what was added
#dwolla_payment
- payment_service_for("backer-#{@backer.id}",
Configuration.find_by_name('dwolla_destination_id').value,
:credential2 => Configuration.find_by_name('dwolla_key').value,
:credential3 => Configuration.find_by_name('dwolla_secret').value,
:amount => @backer.display_value.gsub(/[^0-9.]/,'').to_i,
:currency => 'USD',
:service => :dwolla,
:html => { :id => 'payment-form' }) do |service|
- bu = @backer.user
- service.customer(:first_name => bu.first_name, :last_name => bu.last_name, :phone => bu.phone_number, :email => bu.email)
- service.billing_address(:city => bu.address_city, :address1 => bu.address_street, :address2 => bu.address_number, :state => bu.address_state, :country => 'USA', :zip => bu.address_zip_code)
- service.shipping '0.00'
- service.tax '0.00'
- service.notify_url url_for(done_dwolla_path(@backer, :only_path => false))
- service.redirect_url url_for(thank_you_path(@backer, :only_path => false))
- service.return_url url_for(confirm_dwolla_path(@backer, :only_path => false))
%h1
%input(type='submit' value="Pay with Dwolla")
# Load the rails application
require File.expand_path('../application', __FILE__)
# Initialize the rails application
Catarse::Application.initialize!
require 'money'
require 'active_merchant'
require 'active_merchant/billing/integrations/action_view_helper'
ActionView::Base.send(:include, ActiveMerchant::Billing::Integrations::ActionViewHelper)
Catarse::Application.routes.draw do
# this is what was added
resources :dwolla do
member do
get :done # er
get :cancel
get :confirm
end
end
end
class DwollaController < ApplicationController
include ActiveMerchant::Billing::Integrations
def cancel
backer = Backer.find params[:id]
flash[:failure] = t('projects.backers.checkout.dwolla_cancel')
redirect_to new_project_backer_path(backer.project)
end
def confirm
backer = Backer.find(params[:id])
notify = ActiveMerchant::Billing::Integrations::Dwolla::Notification.new(params.to_json)
if notify.complete?
backer.update_attributes(
:key => checkout.payment_info.first.transaction_id,
:payment_method => 'Dwolla',
:payment_token => params[:token]
)
backer.build_payment_detail.update_from_service
backer.confirm!
flash[:success] = t('projects.backers.checkout.success')
redirect_to thank_you_path
else
#flash[:failure] = t('projects.backers.checkout.dwolla_error')
flash[:failure] = "#{t('projects.backers.checkout.error_header')} #{params[:error_description]}"
return redirect_to new_project_backer_path(backer.project)
end
end
end
# Load the rails application
require File.expand_path('../application', __FILE__)
# Initialize the rails application
Catarse::Application.initialize!
require 'money'
require 'active_merchant'
require 'active_merchant/billing/integrations/action_view_helper'
ActionView::Base.send(:include, ActiveMerchant::Billing::Integrations::ActionViewHelper)
# added
gem 'activemerchant'
// snipped out some bits, but this is what was added
#dwolla_payment
- payment_service_for("backer-#{@backer.id}",
Configuration.find_by_name('dwolla_destination_id').value,
:credential2 => Configuration.find_by_name('dwolla_key').value,
:credential3 => Configuration.find_by_name('dwolla_secret').value,
:amount => @backer.display_value.gsub(/[^0-9.]/,'').to_i,
:currency => 'USD',
:service => :dwolla,
:html => { :id => 'payment-form' }) do |service|
- bu = @backer.user
- service.customer(:first_name => bu.first_name, :last_name => bu.last_name, :phone => bu.phone_number, :email => bu.email)
- service.billing_address(:city => bu.address_city, :address1 => bu.address_street, :address2 => bu.address_number, :state => bu.address_state, :country => 'USA', :zip => bu.address_zip_code)
- service.shipping '0.00'
- service.tax '0.00'
- service.notify_url url_for(done_dwolla_path(@backer, :only_path => false))
- service.redirect_url url_for(thank_you_path(@backer, :only_path => false))
- service.return_url url_for(confirm_dwolla_path(@backer, :only_path => false))
%h1
%input(type='submit' value="Pay with Dwolla")
Catarse::Application.routes.draw do
# this is what was added
resources :dwolla do
member do
get :done # er
get :cancel
get :confirm
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment