Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Creating PayPal recurring payments profile with activemerchant



   1  
   2  # simple extension to ActiveMerchant for basic support of recurring payments with Express Checkout API
   3  # 
   4  # NOTE: set pem_file when loading
   5  module ActiveMerchant #:nodoc:
   6    module Billing #:nodoc:
   7      class PaypalExpressRecurringGateway < Gateway
   8        include PaypalCommonAPI
   9  
  10        LIVE_REDIRECT_URL = 'https://www.paypal.com/cgibin/webscr?cmd=_customer-billing-agreement&token='
  11        TEST_REDIRECT_URL = 'https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_customer-billing-agreement&token='
  12  
  13        def redirect_url
  14          test? ? TEST_REDIRECT_URL : LIVE_REDIRECT_URL
  15        end
  16  
  17        def redirect_url_for(token)
  18          "#{redirect_url}#{token}"
  19        end
  20  
  21        def setup_agreement(description, return_url, cancel_url)
  22          commit 'SetCustomerBillingAgreement', build_setup_request(description, return_url, cancel_url)
  23        end
  24  
  25        def create_profile(token, description, period, cycles, amount)
  26          commit 'CreateRecurringPaymentsProfile', build_create_profile_request(token, description, period, cycles, amount)
  27        end
  28  
  29        def get_profile_details(profile_id)
  30          commit 'GetRecurringPaymentsProfileDetails', build_get_profile_details_request(profile_id)
  31        end
  32  
  33      private
  34        def build_setup_request(description, return_url, cancel_url)
  35          xml = Builder::XmlMarkup.new :indent => 2
  36          xml.tag! 'SetCustomerBillingAgreementReq', 'xmlns' => PAYPAL_NAMESPACE do
  37            xml.tag! 'SetCustomerBillingAgreementRequest', 'xmlns:n2' => EBAY_NAMESPACE do
  38              xml.tag! 'n2:Version', 50
  39              xml.tag! 'n2:SetCustomerBillingAgreementRequestDetails' do
  40                xml.tag! 'n2:BillingAgreementDetails' do
  41                  xml.tag! 'n2:BillingType', 'RecurringPayments'
  42                  xml.tag! 'n2:BillingAgreementDescription', description
  43                end
  44                xml.tag! 'n2:ReturnURL', return_url
  45                xml.tag! 'n2:CancelURL', cancel_url
  46              end
  47            end
  48          end
  49          xml.target!
  50        end
  51  
  52        def build_create_profile_request(token, description, period, cycles, money)
  53          xml = Builder::XmlMarkup.new :indent => 2
  54          xml.tag! 'CreateRecurringPaymentsProfileReq', 'xmlns' => PAYPAL_NAMESPACE do
  55            xml.tag! 'CreateRecurringPaymentsProfileRequest', 'xmlns:n2' => EBAY_NAMESPACE do
  56              xml.tag! 'n2:Version', 50
  57              xml.tag! 'n2:CreateRecurringPaymentsProfileRequestDetails' do
  58                xml.tag! 'Token', token
  59                xml.tag! 'n2:RecurringPaymentsProfileDetails' do
  60                  xml.tag! 'n2:BillingStartDate', Time.now.utc.iso8601
  61                end
  62                xml.tag! 'n2:ScheduleDetails' do
  63                  xml.tag! 'n2:Description', description
  64                  xml.tag! 'n2:PaymentPeriod' do
  65                    xml.tag! 'n2:BillingPeriod', 'Day'
  66                    xml.tag! 'n2:BillingFrequency', period
  67                    xml.tag! 'n2:TotalBillingCycles', cycles
  68                    xml.tag! 'n2:Amount', amount(money), 'currencyID' => currency(money)
  69                  end
  70                end
  71              end
  72            end
  73          end
  74  
  75          xml.target!
  76        end
  77  
  78        def build_get_profile_details_request(profile_id)
  79          xml = Builder::XmlMarkup.new :indent => 2
  80          xml.tag! 'GetRecurringPaymentsProfileDetailsReq', 'xmlns' => PAYPAL_NAMESPACE do
  81            xml.tag! 'GetRecurringPaymentsProfileDetailsRequest', 'xmlns:n2' => EBAY_NAMESPACE do
  82              xml.tag! 'n2:Version', 50
  83              xml.tag! 'ProfileID', profile_id
  84            end
  85          end
  86  
  87          xml.target!
  88        end
  89  
  90        def build_response(success, message, response, options = {})
  91          PaypalExpressResponse.new(success, message, response, options)
  92        end
  93  
  94      end
  95    end
  96  end
  97  
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS