Created
January 20, 2009 19:37
-
-
Save neilmock/49623 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# NOTE: set pem_file when loading | |
module ActiveMerchant #:nodoc: | |
module Billing #:nodoc: | |
class PaypalRecurringGateway < Gateway | |
include PaypalCommonAPI | |
self.supported_cardtypes = [:visa, :master, :american_express, :discover] | |
self.supported_countries = ['US'] | |
self.homepage_url = 'https://www.paypal.com/cgi-bin/webscr?cmd=_wp-pro-overview-outside' | |
self.display_name = 'PayPal Website Payments Pro (US)' | |
def create_profile(token, description, period, cycles, amount) | |
#commit 'CreateRecurringPaymentsProfile', build_create_profile_request(token, description, period, cycles, amount) | |
end | |
def get_profile_details(profile_id) | |
#commit 'GetRecurringPaymentsProfileDetails', build_get_profile_details_request(profile_id) | |
end | |
def recurring(money, credit_card, options = {}) | |
requires!(options, :ip) | |
puts build_recurring_request(money, credit_card, options) | |
commit 'CreateRecurringPaymentsProfile', build_recurring_request(money, credit_card, options) | |
end | |
private | |
def build_recurring_request(money, credit_card, options) | |
billing_address = options[:billing_address] || options[:address] | |
currency_code = options[:currency] || currency(money) | |
xml = Builder::XmlMarkup.new :indent => 2 | |
xml.tag! 'CreateRecurringPaymentsProfileReq', 'xmlns' => PAYPAL_NAMESPACE do | |
xml.tag! 'CreateRecurringPaymentsProfileRequest' do | |
xml.tag! 'Version', '50.0', 'xmlns' => EBAY_NAMESPACE | |
xml.tag! 'CreateRecurringPaymentsProfileRequestDetails', 'xmlns' => EBAY_NAMESPACE do | |
add_credit_card(xml, credit_card, billing_address, options) | |
xml.tag! 'RecurringPaymentsProfileDetails' do | |
xml.tag! 'BillingStartDate', options[:billing_start_date] | |
xml.tag! 'ProfileReference', options[:profile_reference] | |
end | |
xml.tag! 'ScheduleDetails' do | |
xml.tag! 'Description', options[:description] | |
xml.tag! 'PaymentPeriod' do | |
xml.tag! 'BillingPeriod', 'Month' | |
xml.tag! 'BillingFrequency', 1 | |
xml.tag! 'Amount', amount(options[:amount]), 'currencyID' => currency_code | |
end | |
xml.tag! 'ActivationDetails' do | |
xml.tag! 'FailedInitialAmountAction', 'CancelOnFailure' | |
end | |
xml.tag! 'AutoBillOutstandingAmount', 'NoAutoBill' | |
end | |
end | |
end | |
end | |
end | |
def add_credit_card(xml, credit_card, address, options) | |
xml.tag! 'CreditCard' do | |
xml.tag! 'CreditCardType', credit_card_type(card_brand(credit_card)) | |
xml.tag! 'CreditCardNumber', credit_card.number | |
xml.tag! 'ExpMonth', format(credit_card.month, :two_digits) | |
xml.tag! 'ExpYear', format(credit_card.year, :four_digits) | |
xml.tag! 'CVV2', credit_card.verification_value | |
if [ 'switch', 'solo' ].include?(card_brand(credit_card).to_s) | |
xml.tag! 'StartMonth', format(credit_card.start_month, :two_digits) unless credit_card.start_month.blank? | |
xml.tag! 'StartYear', format(credit_card.start_year, :four_digits) unless credit_card.start_year.blank? | |
xml.tag! 'IssueNumber', format(credit_card.issue_number, :two_digits) unless credit_card.issue_number.blank? | |
end | |
xml.tag! 'CardOwner' do | |
xml.tag! 'PayerName' do | |
xml.tag! 'FirstName', credit_card.first_name | |
xml.tag! 'LastName', credit_card.last_name | |
end | |
#xml.tag! 'Payer', options[:email] | |
add_address(xml, 'Address', address) | |
end | |
end | |
end | |
def credit_card_type(type) | |
case type | |
when 'visa' then 'Visa' | |
when 'master' then 'MasterCard' | |
when 'discover' then 'Discover' | |
when 'american_express' then 'Amex' | |
when 'switch' then 'Switch' | |
when 'solo' then 'Solo' | |
end | |
end | |
def add_address(xml, element, address) | |
return if address.nil? | |
xml.tag! element do | |
xml.tag! 'Name', address[:name] | |
xml.tag! 'Street1', address[:address1] | |
xml.tag! 'Street2', address[:address2] | |
xml.tag! 'CityName', address[:city] | |
xml.tag! 'StateOrProvince', address[:state].blank? ? 'N/A' : address[:state] | |
xml.tag! 'Country', address[:country] | |
xml.tag! 'PostalCode', address[:zip] | |
xml.tag! 'Phone', address[:phone] | |
end | |
end | |
def build_response(success, message, response, options = {}) | |
Response.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