Skip to content

Commit

Permalink
Add support for PayPal NV API which will later replace PaypalGateway
Browse files Browse the repository at this point in the history
git-svn-id: https://activemerchant.googlecode.com/svn/trunk/active_merchant@666 6513ea26-6c20-0410-8a68-89cd7235086d
  • Loading branch information
codyfauser committed May 13, 2008
1 parent 7431980 commit f0026d9
Show file tree
Hide file tree
Showing 10 changed files with 1,269 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG
@@ -1,5 +1,6 @@
= ActiveMerchant CHANGELOG

* Add support for PayPal NV Pair API. Will be used to replace the usage of the PayPal SOAP API in ActiveMerchant in the future [Greg Furmanek, cody]
* Protx does support UK Maestro [cody]
* Add tests for length of UK Maestro cards [cody]
* Return all the error messages and codes from paypal responses [cody]
Expand Down
6 changes: 5 additions & 1 deletion CONTRIBUTERS
Expand Up @@ -66,4 +66,8 @@ HiTRUST Gateway (Dec 10, 2007)

Payflow NV Gateway (Mar 03, 2008)

* Greg Furmanek
* Greg Furmanek

PaypalNVGateway (Apr 12, 2008)

* Greg Furmanek
97 changes: 97 additions & 0 deletions lib/active_merchant/billing/gateways/paypal_express_nv.rb
@@ -0,0 +1,97 @@
require File.dirname(__FILE__) + '/paypal_nv/paypal_nv_common_api'
require File.dirname(__FILE__) + '/paypal_nv/paypal_express_nv_response'

module ActiveMerchant #:nodoc:
module Billing #:nodoc:
class PaypalExpressNvGateway < Gateway
include PaypalNvCommonAPI

self.supported_countries = ['US']

LIVE_REDIRECT_NV_URL = 'https://www.paypal.com/cgibin/webscr?cmd=_express-checkout&token='
TEST_REDIRECT_NV_URL = 'https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token='

self.homepage_url = 'https://www.paypal.com/cgi-bin/webscr?cmd=xpt/merchant/ExpressCheckoutIntro-outside'
self.display_name = 'PayPal Express Checkout'

def redirect_url
test? ? TEST_REDIRECT_NV_URL : LIVE_REDIRECT_NV_URL
end

def redirect_url_for(token, options = {})
options = {:review => true}.update(options)
options[:review] ? "#{redirect_url}#{token}" : "#{redirect_url}#{token}&useraction=commit"
end

def setup_authorization(money, options = {})
requires!(options, :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

private
def build_setup_request(action, money, options)
post = {}
add_amount(post, money, options)
add_pair(post, :returnurl, options[:return_url])
add_pair(post, :cancelurl, options[:cancel_return_url])
add_pair(post, :ipaddress, options[:ip])
add_pair(post, :noshipping, options[:no_shipping] ? '1' : '0')
add_pair(post, :maxamount, options[:max_amount]) if options[:max_amount]
add_pair(post, :paymentaction, action)
add_pair(post, :email, options[:email]) if options[:email]
add_pair(post, :custom, options[:custom_code]) if options[:custom_code]
add_pair(post, :reqconfirmshipping, options[:confirm_shipping] ? "1" : "0") if options[:confirm_shipping]
add_pair(post, :addressoverride, options[:address_override] ? "1" : "0") if options[:address_override]
add_pair(post, :token, options[:token]) if options[:token]
add_pair(post, :locale, options[:locale]) if options[:locale]
add_shipping_address(post, optioins[:shipping_address]) if options[:shipping_address]
post
end

def build_get_details_request(token)
post = {}
add_pair(post, :token, token)
post
end

def build_sale_or_authorization_request(action, money, options)
post = {}
#required
add_pair(post, :paymentaction, action)
add_pair(post, :payerid, options[:payer_id])
add_pair(post, :token, options[:token])
add_amount(post, money, options)
add_pair(post, :buttonsource, application_id)

# optional
add_pair(post, :currencycode, options[:currency] || "USD")
add_pair(post, :token, options[:token]) if options[:token]

post
end

def build_response(success, message, response, options = {})
PaypalExpressNvResponse.new(success, message, response, options)
end
end
end
end
59 changes: 59 additions & 0 deletions lib/active_merchant/billing/gateways/paypal_nv.rb
@@ -0,0 +1,59 @@
require File.dirname(__FILE__) + '/paypal_nv/paypal_nv_common_api'
require File.dirname(__FILE__) + '/paypal_express_nv'

module ActiveMerchant #:nodoc:
module Billing #:nodoc:
class PaypalNvGateway < Gateway
include PaypalNvCommonAPI

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 authorize(money, credit_card, options = {})
requires!(options, :ip)
commit 'DoDirectPayment', build_sale_or_authorization_request('Authorization', money, credit_card, options)
end

def purchase(money, credit_card, options = {})
requires!(options, :ip)
commit 'DoDirectPayment', build_sale_or_authorization_request('Sale', money, credit_card, options)
end

def express
@express ||= PaypalExpressNvGateway.new(@options)
end

private

def build_sale_or_authorization_request(action, money, credit_card_or_reference, options)
post = {}
post[:paymentaction] = action
post[:buttonsource] = application_id.to_s.slice(0,32) unless application_id.blank?
add_addresses(post, options)
add_customer_data(post, options)
add_invoice(post, options)
add_credit_card(post, credit_card_or_reference)
add_amount(post, money, options)
add_subtotals(post, options)
post
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 build_response(success, message, response, options = {})
Response.new(success, message, response, options)
end
end
end
end
@@ -0,0 +1,38 @@
module ActiveMerchant #:nodoc:
module Billing #:nodoc:
class PaypalExpressNvResponse < Response
def email
@params['email']
end

def name
[@params['firstname'], @params['middlename'], @params['lastname']].compact.join(' ')
end

def token
@params['token']
end

def payer_id
@params['payerid']
end

def payer_country
@params['payer_country']
end

def address
{ 'name' => self.name,
'company' => @params['business'],
'address1' => @params['shiptostreet'],
'address2' => @params['shiptostreet2'],
'city' => @params['shiptocity'],
'state' => @params['shiptostate'],
'country' => @params['shiptocountrycode'],
'zip' => @params['shiptozip'],
'phone' => @params['phonenum'],
}
end
end
end
end

0 comments on commit f0026d9

Please sign in to comment.