Skip to content

Commit

Permalink
tests for paypal recurring gteway
Browse files Browse the repository at this point in the history
  • Loading branch information
ginettev committed Dec 6, 2011
1 parent aefafb7 commit 007afdb
Show file tree
Hide file tree
Showing 3 changed files with 563 additions and 31 deletions.
58 changes: 28 additions & 30 deletions lib/active_merchant/billing/gateways/paypal_recurring.rb
Expand Up @@ -2,47 +2,45 @@
require File.dirname(__FILE__) + '/paypal/paypal_express_response'
require File.dirname(__FILE__) + '/paypal_express_common'

module ActiveMerchant #:nodoc:
module Billing #:nodoc:
module ActiveMerchant
module Billing
class PaypalRecurringGateway < Gateway
include PaypalCommonAPI
include PaypalExpressCommon

self.test_redirect_url = 'https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token='
self.live_redirect_url = 'https://www.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token='
# self.supported_countries = ['US']
# self.homepage_url = 'https://www.paypal.com/cgi-bin/webscr?cmd=xpt/merchant/ExpressCheckoutIntro-outside'
# self.display_name = 'PayPal Express Checkout Recurring'

API_VERSION = '59.0'

def setup_authorization(money, options = {})
requires!(options, :description, :return_url, :cancel_return_url)

commit 'SetExpressCheckout', build_setup_request('Authorization', money, options)
end

def setup_purchase(money, options = {})
requires!(options, :return_url, :cancel_return_url)

commit 'SetExpressCheckout', build_setup_request('Sale', money, options)
end

def details_for(token)
commit 'GetExpressCheckoutDetails', build_get_details_request(token)
end

def authorize(money, options = {})
requires!(options, :token, :payer_id)

commit 'DoExpressCheckoutPayment', build_sale_or_authorization_request('Authorization', money, options)
end

def purchase(money, options = {})
requires!(options, :token, :payer_id)

commit 'DoExpressCheckoutPayment', build_sale_or_authorization_request('Sale', money, options)
end

def get_agreement(token)
commit 'GetBillingAgreementCustomerDetails', build_get_agreement_request(token)
end
Expand Down Expand Up @@ -84,7 +82,7 @@ def build_setup_request(action, money, options)
xml.tag! 'SetExpressCheckoutReq', 'xmlns' => PAYPAL_NAMESPACE do
xml.tag! 'SetExpressCheckoutRequest', 'xmlns:n2' => EBAY_NAMESPACE do
xml.tag! 'n2:Version', API_VERSION
xml.tag! 'n2:SetExpressCheckoutRequestDetails' do
xml.tag! 'n2:SetExpressCheckoutRequestDetails' do
xml.tag! 'n2:BillingAgreementDetails' do
xml.tag! 'n2:BillingType', 'RecurringPayments'
xml.tag! 'n2:BillingAgreementDescription', options[:description]
Expand All @@ -98,19 +96,19 @@ def build_setup_request(action, money, options)
xml.tag! 'n2:IPAddress', options[:ip] unless options[:ip].blank?
# IMPORTANT: If you want to use Invoice ID, it has to be unique!
# xml.tag! 'n2:InvoiceID', 'InvoiceID' + Time.now.to_i.to_s

xml.tag! 'n2:AllowNote', '0'
xml.tag! 'n2:LocaleCode', options[:locale] unless options[:locale].blank?
end
end
end

xml.target!
end

def build_sale_or_authorization_request(action, money, options)
currency_code = options[:currency] || currency(money)

xml = Builder::XmlMarkup.new :indent => 2
xml.tag! 'DoExpressCheckoutPaymentReq', 'xmlns' => PAYPAL_NAMESPACE do
xml.tag! 'DoExpressCheckoutPaymentRequest', 'xmlns:n2' => EBAY_NAMESPACE do
Expand All @@ -121,15 +119,15 @@ def build_sale_or_authorization_request(action, money, options)
xml.tag! 'n2:PayerID', options[:payer_id]
xml.tag! 'n2:PaymentDetails' do
xml.tag! 'n2:OrderTotal', amount(money), 'currencyID' => currency_code

# All of the values must be included together and add up to the order total
if [:subtotal, :shipping, :handling, :tax].all?{ |o| options.has_key?(o) }
xml.tag! 'n2:ItemTotal', amount(options[:subtotal]), 'currencyID' => currency_code
xml.tag! 'n2:ShippingTotal', amount(options[:shipping]),'currencyID' => currency_code
xml.tag! 'n2:HandlingTotal', amount(options[:handling]),'currencyID' => currency_code
xml.tag! 'n2:TaxTotal', amount(options[:tax]), 'currencyID' => currency_code
end

xml.tag! 'n2:NotifyURL', options[:notify_url]
xml.tag! 'n2:ButtonSource', application_id.to_s.slice(0,32) unless application_id.blank?
end
Expand Down Expand Up @@ -195,13 +193,13 @@ def build_create_profile_request(token, options)
xml.tag! 'n2:BillingFrequency', options[:trialfrequency]
xml.tag! 'n2:TotalBillingCycles', options[:trialcycles] || 1
xml.tag! 'n2:Amount', amount(options[:trialamount]), 'currencyID' => options[:currency] || 'USD'
end
end
end
if !options[:initial_amount].blank?
xml.tag! 'n2:ActivationDetails' do
xml.tag! 'n2:InitialAmount', amount(options[:initial_amount]), 'currencyID' => options[:currency] || 'USD'
xml.tag! 'n2:FailedInitialAmountAction', options[:continue_on_failure] ? 'ContinueOnFailure' : 'CancelOnFailure'
end
end
end
# xml.tag! 'n2:MaxFailedPayments', options[:max_failed_payments] unless options[:max_failed_payments].blank?
xml.tag! 'n2:AutoBillOutstandingAmount', options[:auto_bill_outstanding] ? 'AddToNextBilling' : 'NoAutoBill'
Expand Down Expand Up @@ -255,7 +253,7 @@ def build_change_profile_request(profile_id, options)

xml.target!
end

def build_manage_profile_request(profile_id, action, options)
xml = Builder::XmlMarkup.new :indent => 2
xml.tag! 'ManageRecurringPaymentsProfileStatusReq', 'xmlns' => PAYPAL_NAMESPACE do
Expand Down Expand Up @@ -295,19 +293,19 @@ def add_credit_card(xml, credit_card, address, options)
xml.tag! 'n2:ExpMonth', format(credit_card.month, :two_digits)
xml.tag! 'n2:ExpYear', format(credit_card.year, :four_digits)
xml.tag! 'n2:CVV2', credit_card.verification_value

if [ 'switch', 'solo' ].include?(card_brand(credit_card).to_s)
xml.tag! 'n2:StartMonth', format(credit_card.start_month, :two_digits) unless credit_card.start_month.blank?
xml.tag! 'n2:StartYear', format(credit_card.start_year, :four_digits) unless credit_card.start_year.blank?
xml.tag! 'n2:IssueNumber', format(credit_card.issue_number, :two_digits) unless credit_card.issue_number.blank?
end

xml.tag! 'n2:CardOwner' do
xml.tag! 'n2:PayerName' do
xml.tag! 'n2:FirstName', credit_card.first_name
xml.tag! 'n2:LastName', credit_card.last_name
end

xml.tag! 'n2:Payer', options[:email]
add_address(xml, 'n2:Address', address) if address
end
Expand All @@ -330,4 +328,4 @@ def build_response(success, message, response, options = {})
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/active_merchant/common/validateable.rb
Expand Up @@ -47,7 +47,7 @@ def empty?
# if more than one error is available we will only return the first. If no error is available
# we return an empty string
def on(field)
self[field].to_a.first if self[:field].present?
self[field].to_a.first if self[field].present?
end

def add(field, error)
Expand Down

0 comments on commit 007afdb

Please sign in to comment.