Skip to content

Instantly share code, notes, and snippets.

@tobi
Created December 29, 2010 19:18
Show Gist options
  • Save tobi/758920 to your computer and use it in GitHub Desktop.
Save tobi/758920 to your computer and use it in GitHub Desktop.
# simple extension to ActiveMerchant for basic support of recurring payments with Express Checkout API
#
# See http://http://clockobj.co.uk/2008/09/07/ruby-on-rails-paypal-express-recurring-payments-using-active-merchant/
# for details on getting started with this gateway
#
#
module ActiveMerchant #:nodoc:
module Billing #:nodoc:
class PaypalExpressRecurringNvGateway < Gateway
include PaypalNvCommonAPI
LIVE_REDIRECT_URL = 'https://www.paypal.com/cgibin/webscr?cmd=_customer-billing-agreement&token='
TEST_REDIRECT_URL = 'https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_customer-billing-agreement&token='
def redirect_url
test? ? TEST_REDIRECT_URL : LIVE_REDIRECT_URL
end
def redirect_url_for(token)
"#{redirect_url}#{token}"
end
def setup_agreement(description, options = {})
requires!(options, :return_url, :cancel_return_url)
commit 'SetCustomerBillingAgreement', build_setup_agreement(description, options)
end
def get_agreement(token)
commit 'GetBillingAgreementCustomerDetails', build_get_agreement(token)
end
def create_profile(money, token, options = {})
commit 'CreateRecurringPaymentsProfile', build_create_profile(money, token, options)
end
def get_profile_details(profile_id)
commit 'GetRecurringPaymentsProfileDetails', build_get_profile_details(profile_id)
end
def manage_profile(profile_id, action, options = {})
commit 'ManageRecurringPaymentsProfileStatus', build_manage_profile(profile_id, action, options)
end
def update_profile(profile_id, options = {})
commit 'UpdateRecurringPaymentsProfile', build_update_profile(profile_id, options)
end
def bill_outstanding(profile_id, money = nil, options = {})
commit 'BillOutstandingAmount', build_bill_outstanding(profile_id, money, options)
end
private
def build_setup_agreement(description, options)
post = {}
add_pair(post, :billingagreementdescription, description)
add_pair(post, :returnurl, options[:return_url])
add_pair(post, :cancelurl, options[:cancel_return_url])
add_pair(post, :billingtype, "RecurringPayments")
add_pair(post, :paymenttype, options[:payment_type]) if options[:payment_type]
add_pair(post, :localecode, options[:locale]) if options[:locale]
add_pair(post, :billingagreementcustom, options[:custom_code]) if options[:custom_code]
post
end
def build_get_agreement(token)
post = {}
add_pair(post, :token, token)
post
end
def build_create_profile(money, token, options)
post = {}
add_pair(post, :token, token)
add_amount(post, money, options)
add_pair(post, :subscribername, options[:subscriber_name]) if options[:subscriber_name]
add_pair(post, :initamt, options[:initamt]) if options[:initamt]
add_pair(post, :failedinitamtaction, options[:failedinitamtaction]) if options[:failedinitamtaction]
add_pair(post, :profilestartdate, (options[:start_at] || Time.now).utc.iso8601)
add_pair(post, :billingperiod, options[:billing_period] ? options[:billing_period] : "Month")
add_pair(post, :billingfrequency, options[:billing_frequency] ? options[:billing_frequency] : 1)
add_pair(post, :totalbillingcycles, options[:billing_cycles]) if options[:billing_cycles]
add_pair(post, :trialbillingperiod, options[:trial_billing_period]) if options[:trial_billing_period]
add_pair(post, :trialbillingfrequency, options[:trial_billing_frequency]) if options[:trial_billing_frequency]
add_pair(post, :trialamt, options[:trial_amount]) if options[:trial_amount]
add_pair(post, :trialtotalbillingcycles, options[:trial_billing_cycles]) if options[:trial_billing_cycles]
add_pair(post, :profilereference, options[:reference]) if options[:reference]
add_pair(post, :autobillamt, options[:auto_bill_amt]) if options[:auto_bill_amt]
add_pair(post, :maxfailedpayments, options[:max_failed_payments]) if options[:max_failed_payments]
add_pair(post, :shippingamt, amount(options[:shipping_amount]), :allow_blank => false) if options[:shipping_amount]
add_pair(post, :taxamt, amount(options[:tax_amount]), :allow_blank => false) if options[:tax_amount]
add_shipping_address(post, options[:shipping_address]) if options[:shipping_address]
post
end
def build_get_profile_details(profile_id)
post = {}
add_pair(post, :profileid, profile_id)
post
end
def build_manage_profile(profile_id, action, options)
post = {}
add_pair(post, :profileid, profile_id)
add_pair(post, :action, action)
add_pair(post, :note, options[:note]) if options[:note]
post
end
def build_update_profile(profile_id, options)
post = {}
add_pair(post, :profileid, profile_id)
add_pair(post, :note, options[:note]) if options[:note]
add_pair(post, :desc, options[:description]) if options[:description]
add_pair(post, :subscribername, options[:subscriber_name]) if options[:subscriber_name]
add_pair(post, :profilereference, options[:reference]) if options[:reference]
add_pair(post, :additionalbillingcycles, options[:additional_billing_cycles]) if options[:additional_billing_cycles]
add_amount(post, options[:money], options)
add_pair(post, :shippingamt, amount(options[:shipping_amount]), :allow_blank => false) if options[:shipping_amount]
add_pair(post, :autobillamt, options[:auto_bill_amt]) if options[:auto_bill_amt]
add_pair(post, :maxfailedpayments, options[:max_failed_payments]) if options[:max_failed_payments]
add_pair(post, :taxamt, amount(options[:tax_amount]), :allow_blank => false) if options[:tax_amount]
add_shipping_address(post, options[:shipping_address]) if options[:shipping_address]
post
end
def build_bill_outstanding(profile_id, money = nil, options = {})
post = {}
add_pair(post, :profileid, profile_id)
add_amount(post, money, options) if money
add_pair(post, :note, options[:note]) if options[:note]
post
end
def build_response(success, message, response, options = {})
PaypalExpressNvResponse.new(success, message, response, options)
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment