Permalink
Browse files

Merge pull request #26 from lvxn0va/ft-preapprovals

Ft preapprovals
  • Loading branch information...
2 parents fcfc91a + 63dcc28 commit 0b1243f4d39a195719006eeba4533955b9171d75 @adamthedeveloper committed Feb 23, 2013
View
BIN .DS_Store
Binary file not shown.
View
@@ -8,6 +8,7 @@ gem 'httparty'
# Include everything needed to run rake, tests, features, etc.
group :development do
gem "rails", "3.1.0"
+ gem "rake"
gem 'turn', '0.8.2', :require => false
gem 'thor'
gem "bundler"
View
@@ -110,6 +110,7 @@ DEPENDENCIES
httparty
jeweler (~> 1.6.4)
rails (= 3.1.0)
+ rake
rcov
thor
turn (= 0.8.2)
View
@@ -6,6 +6,7 @@ Wepay-Rails allows your rails app to accept payments with Wepay (http://www.wepa
= Features
+* Added PREAPPROVAL and DELAYED CHARGE ability 09/2012
* Built in IPN that listens to push notifications from wepay and updates saved checkout records for you
* Allows you to fetch your access token easily through a web interface
* Built in API tools to allow you to make all wepay-api calls easily
View
Binary file not shown.
@@ -0,0 +1,43 @@
+class Wepay::ChargeController < Wepay::ApplicationController
+ include WepayRails::Payments
+
+ def index
+ record = WepayCheckoutRecord.find_by_checkout_id_and_security_token(params[:checkout_id],params[:security_token])
+
+ if record.present?
+ wepay_gateway = WepayRails::Payments::Gateway.new( record.access_token )
+ charge = wepay_gateway.lookup_preapproval(record.preapproval_id)
+
+ # remove unnecessary attributes
+ charge.delete_if {|k,v| !record.attributes.include? k.to_s}
+
+ record.update_attributes(charge)
+ redirect_to "#{wepay_gateway.configuration[:after_charge_redirect_uri]}?checkout_id=#{params[:checkout_id]}"
+ else
+ raise StandardError.new("Wepay IPN: No record found for checkout_id #{params[:checkout_id]} and security_token #{params[:security_token]}")
+ end
+ end
+
+ def success
+ response = WepayCheckoutRecord.find(:last)
+ wepay_gateway = WepayRails::Payments::Gateway.new( response.access_token )
+ charge = wepay_gateway.lookup_checkout(response.checkout_id)
+
+ response.update_attributes(charge)
+ logger.info params
+ render :text => "Wepay charge OK from #{response.payer_email} with Checkout ID # #{response.checkout_id} from the Pre-approval ID # #{response.preapproval_id}."
+ end
+
+ def new
+ # The following is from the Wepay API sample checkout call
+ # create the checkout
+ charge_params = {
+ :amount => '50.00',
+ :short_description => 'A Checkout on the Wepay PreApproval.',
+ :type => 'GOODS',
+ :preapproval_id => xxxxx # To test, be sure to manually add the preapproval_id from the preapproval response which will skip having to go to Wepay to add CC info.
+ }
+ # Finally, send user to charge on the preapproval.
+ init_charge_and_return_success(charge_params)
+ end
+end
@@ -1,18 +1,32 @@
class Wepay::CheckoutController < Wepay::ApplicationController
+ include WepayRails::Payments
+
def index
- record = WepayCheckoutRecord.find_by_checkout_id_and_security_token(params[:checkout_id],params[:security_token])
+ record = WepayCheckoutRecord.find_by_checkout_id_and_security_token(params[:checkout_id],params[:security_token])
- if record.present?
- wepay_gateway = WepayRails::Payments::Gateway.new( record.access_token )
- checkout = wepay_gateway.lookup_checkout(record.checkout_id)
-
- # remove unnecessary attributes
- checkout.delete_if {|k,v| !record.attributes.include? k.to_s}
-
- record.update_attributes(checkout)
- redirect_to "#{wepay_gateway.configuration[:after_checkout_redirect_uri]}?checkout_id=#{params[:checkout_id]}"
- else
- raise StandardError.new("Wepay IPN: No record found for checkout_id #{params[:checkout_id]} and security_token #{params[:security_token]}")
- end
+ if record.present?
+ wepay_gateway = WepayRails::Payments::Gateway.new( record.access_token )
+ checkout = wepay_gateway.lookup_checkout(record.checkout_id)
+
+ # remove unnecessary attributes
+ checkout.delete_if {|k,v| !record.attributes.include? k.to_s}
+
+ record.update_attributes(checkout)
+ redirect_to "#{wepay_gateway.configuration[:after_checkout_redirect_uri]}?checkout_id=#{params[:checkout_id]}"
+ else
+ raise StandardError.new("Wepay IPN: No record found for checkout_id #{params[:checkout_id]} and security_token #{params[:security_token]}")
+ end
+ end
+
+ def new
+ # create the checkout - This is TEST info from Wepay only
+ checkout_params = {
+ :amount => '255.00',
+ :short_description => 'A transaction for WePay Testing',
+ :type => 'GOODS'
+ }
+ # Finally, send the user off to wepay so you can get paid! - CASH MONEY
+ init_checkout_and_send_user_to_wepay(checkout_params)
end
+
end
@@ -1,20 +1,22 @@
class Wepay::IpnController < Wepay::ApplicationController
- def create
+ include WepayRails::Payments
+ def update
+ record = WepayCheckoutRecord.find_by_checkout_id_and_security_token(params[:checkout_id],params[:security_token])
- record = WepayCheckoutRecord.find_by_checkout_id_and_security_token(params[:checkout_id],params[:security_token])
+ if record.present?
+ wepay_gateway = WepayRails::Payments::Gateway.new
- if record.present?
- wepay_gateway = WepayRails::Payments::Gateway.new
- checkout = wepay_gateway.lookup_checkout(record.checkout_id)
-
- # remove unnecessary attributes
- checkout.delete_if {|k,v| !record.attributes.include? k.to_s}
-
- record.update_attributes(checkout)
- render :text => "ok"
- else
- raise StandardError.new("Wepay IPN: No record found for checkout_id #{params[:checkout_id]} and security_token #{params[:security_token]}")
- end
+ if record.checkout_id.present?
+ checkout = wepay_gateway.lookup_checkout(record.checkout_id)
+ else
+ checkout = wepay_gateway.lookup_preapproval(record.preapproval_id)
+ end
+ checkout.delete_if {|k,v| !record.attributes.include? k.to_s}
+ record.update_attributes(checkout)
+ render :text => "ok"
+ else
+ raise StandardError.new("Wepay IPN: No record found for checkout_id #{params[:checkout_id]} and security_token #{params[:security_token]}")
+ end
- end
+ end
end
@@ -0,0 +1,47 @@
+class Wepay::PreapprovalController < Wepay::ApplicationController
+ include WepayRails::Payments
+
+ def index
+ record = WepayCheckoutRecord.find_by_preapproval_id_and_security_token(params[:preapproval_id],params[:security_token])
+
+
+ if record.present?
+ wepay_gateway = WepayRails::Payments::Gateway.new ( record.access_token )
+ preapproval = wepay_gateway.lookup_preapproval(record.preapproval_id)
+
+ #remove unneccesary attributes
+ preapproval.delete_if {|k,v| !record.attributes.include? k.to_s}
+
+ record.update_attributes(preapproval)
+ redirect_to "#{wepay_gateway.configuration[:after_preapproval_redirect_uri]}?preapproval_id=#{params[:preapproval_id]}"
+ else
+ raise StandardError.new("Wepay IPN: No record found for preapproval_id #{params[:preapproval_id]} and security_token #{params[:security_token]}")
+ end
+ end
+
+ def success
+ response = WepayCheckoutRecord.find(:last)
+ wepay_gateway = WepayRails::Payments::Gateway.new( response.access_token )
+ charge = wepay_gateway.lookup_preapproval(response.preapproval_id)
+
+ response.update_attributes(charge)
+ logger.info params
+ render :text => "PRE-APPROVAL OK from #{response.payer_email} with Pre-approval ID # #{response.preapproval_id}. You can use this Pre-approval Id# to run a charge at a later time."
+ end
+
+ def new
+ # create the preapproval - This is TEST info
+ preapproval_params = {
+ :period => 'once',
+ :end_time => '2013-12-25',
+ :amount => '50.00',
+ :mode => 'regular',
+ :short_description => 'A Preapproval for MyApp.',
+ :app_fee => "10",
+ :fee_payer => 'payee',
+ :payer_email_message => "You just approved MyApp to charge you for a some money later. You have NOT been charged at this time!"
+ }
+ # Finally, send the user off to wepay for the preapproval
+ init_preapproval_and_send_user_to_wepay(preapproval_params)
+ end
+end
View
@@ -0,0 +1,4 @@
+class Authorize < ActiveRecord::Base
+ has_many :wepay_checkout_records
+
+end
View
@@ -0,0 +1,4 @@
+class Checkout < ActiveRecord::Base
+ has_many :wepay_checkout_records
+
+end
View
@@ -0,0 +1,4 @@
+class Ipn < ActiveRecord::Base
+ has_many :wepay_checkout_records
+
+end
@@ -0,0 +1,4 @@
+class Preapproval < ActiveRecord::Base
+ has_many :wepay_checkout_records
+
+end
View
@@ -0,0 +1,113 @@
+require 'digest/sha2'
+module WepayRails
+ module Api
+ module ChargeMethods
+ # Many of the settings you pass in here are already factored in from
+ # the wepay.yml file and only need to be overridden if you insist on doing
+ # so when this method is called. The following list of key values are pulled
+ # in for you from your wepay.yml file:
+ #
+ # Note: @wepay_config is your wepay.yml as a Hash
+ # :callback_uri => @wepay_config[:ipn_callback_uri],
+ # :redirect_uri => @wepay_config[:checkout_redirect_uri],
+ # :fee_payer => @wepay_config[:fee_payer],
+ # :type => @wepay_config[:checkout_type],
+ # :charge_tax => @wepay_config[:charge_tax] ? 1 : 0,
+ # :app_fee => @wepay_config[:app_fee],
+ # :auto_capture => @wepay_config[:auto_capture] ? 1 : 0,
+ # :require_shipping => @wepay_config[:require_shipping] ? 1 : 0,
+ # :shipping_fee => @wepay_config[:shipping_fee],
+ # :charge_tax => @wepay_config[:charge_tax],
+ # :account_id => wepay_user['account_id'] # wepay-rails goes and gets this for you, but you can override it if you want to.
+ #
+ #
+ # params hash key values possibilities are:
+ # Parameter: Required: Description:
+ # :account_id Yes The unique ID of the account you want to create a checkout for.
+ # :short_description Yes A short description of what is being paid for.
+ # :long_description No A long description of what is being paid for.
+ # :type Yes The the checkout type (one of the following: GOODS, SERVICE, DONATION, or PERSONAL)
+ # :reference_id No The unique reference id of the checkout (set by the application in /checkout/create
+ # :amount Yes The amount that the payer will pay.
+ # :app_fee No The amount that the application will receive in fees.
+ # :fee_payer No Who will pay the fees (WePay's fees and any app fees). Set to "Payer" to charge fees to the person paying (Payer will pay amount + fees, payee will receive amount). Set to "Payee" to charge fees to the person receiving money (Payer will pay amount, Payee will receive amount - fees). Defaults to "Payer".
+ # :redirect_uri No The uri the payer will be redirected to after paying.
+ # :callback_uri No The uri that will receive any Instant Payment Notifications sent. Needs to be a full uri (ex https://www.wepay.com )
+ # :auto_capture No A boolean value (0 or 1). Default is 1. If set to 0 then the payment will not automatically be released to the account and will be held by WePay in payment state 'reserved'. To release funds to the account you must call /checkout/capture
+ # :require_shipping No A boolean value (0 or 1). If set to 1 then the payer will be asked to enter a shipping address when they pay. After payment you can retrieve this shipping address by calling /checkout
+ # :shipping_fee No The amount that you want to charge for shipping.
+ # :charge_tax No A boolean value (0 or 1). If set to 1 and the account has a relevant tax entry (see /account/set_tax), then tax will be charged.
+ def perform_charge(params)
+ security_token = Digest::SHA2.hexdigest("#{Rails.env.production? ? rand(4) : 1}#{Time.now.to_i}") # Make less random during tests
+
+ # add the security token to any urls that were passed in from the app
+ if params[:callback_uri]
+ params[:callback_uri] = apply_security_token( params[:callback_uri], security_token )
+ end
+
+ if params[:redirect_uri]
+ params[:redirect_uri] = apply_security_token( params[:redirect_uri], security_token )
+ end
+
+ defaults = {
+ :callback_uri => ipn_callback_uri(security_token),
+ :redirect_uri => checkout_redirect_uri(security_token),
+ :fee_payer => @wepay_config[:fee_payer],
+ :type => @wepay_config[:checkout_type],
+ :charge_tax => @wepay_config[:charge_tax] ? 1 : 0,
+ :app_fee => @wepay_config[:app_fee],
+ :auto_capture => @wepay_config[:auto_capture] ? 1 : 0,
+ :require_shipping => @wepay_config[:require_shipping] ? 1 : 0,
+ :shipping_fee => @wepay_config[:shipping_fee],
+ :account_id => @wepay_config[:account_id]
+ }.merge(params)
+
+ resp = self.call_api("/checkout/create", defaults)
+ resp.merge({:security_token => security_token})
+ end
+
+ def lookup_checkout(checkout_id)
+ co = self.call_api("/checkout", {:checkout_id => checkout_id})
+ co.delete(:type)
+ co
+ end
+
+ def lookup_preapproval(preapproval_id)
+ co = self.call_api("/preapproval", {:preapproval_id => preapproval_id})
+ co.delete("type")
+ co
+ end
+
+ def ipn_callback_uri(security_token)
+ uri = if @wepay_config[:ipn_callback_uri].present?
+ @wepay_config[:ipn_callback_uri]
+ else
+ "#{@wepay_config[:root_callback_uri]}/wepay/ipn"
+ end
+ apply_security_token(uri, security_token)
+ end
+
+ def charge_redirect_uri(security_token)
+ uri = if @wepay_config[:ipn_callback_uri].present?
+ @wepay_config[:charge_redirect_uri]
+ else
+ "#{@wepay_config[:root_callback_uri]}/wepay/charge"
+ end
+ apply_security_token(uri, security_token)
+ end
+
+ def checkout_redirect_uri(security_token)
+ uri = if @wepay_config[:ipn_callback_uri].present?
+ @wepay_config[:checkout_redirect_uri]
+ else
+ "#{@wepay_config[:root_callback_uri]}/wepay/checkout"
+ end
+ apply_security_token(uri, security_token)
+ end
+
+ def apply_security_token(uri, security_token)
+ uri += (uri =~ /\?/ ? '&' : '?') + "security_token=#{security_token}"
+ end
+ end
+ end
+end
@@ -71,6 +71,12 @@ def lookup_checkout(checkout_id)
co.delete(:type)
co
end
+
+ def lookup_preapproval(preapproval_id)
+ co = self.call_api("/preapproval", {:preapproval_id => preapproval_id})
+ co.delete("type")
+ co
+ end
def ipn_callback_uri(security_token)
uri = if @wepay_config[:ipn_callback_uri].present?
Oops, something went wrong.

0 comments on commit 0b1243f

Please sign in to comment.