Class Index [+]

Quicksearch

ActiveMerchant::Billing::PaypalCommonAPI

This module is included in both PaypalGateway and PaypalExpressGateway

Constants

API_VERSION
URLS
PAYPAL_NAMESPACE
EBAY_NAMESPACE
ENVELOPE_NAMESPACES
CREDENTIALS_NAMESPACES
AUSTRALIAN_STATES
SUCCESS_CODES
FRAUD_REVIEW_CODE

Public Class Methods

included(base) click to toggle source
   # File lib/active_merchant/billing/gateways/paypal/paypal_common_api.rb, line 5
5:       def self.included(base)
6:         base.default_currency = 'USD'
7:         base.cattr_accessor :pem_file
8:         base.cattr_accessor :signature
9:       end
new(options = {}) click to toggle source

The gateway must be configured with either your PayPal PEM file or your PayPal API Signature. Only one is required.

:pem The text of your PayPal PEM file. Note

                      this is not the path to file, but its
                      contents. If you are only using one PEM
                      file on your site you can declare it
                      globally and then you won't need to
                      include this option

:signature The text of your PayPal signature.

                      If you are only using one API Signature
                      on your site you can declare it
                      globally and then you won't need to
                      include this option
    # File lib/active_merchant/billing/gateways/paypal/paypal_common_api.rb, line 63
63:       def initialize(options = {})
64:         requires!(options, :login, :password)
65:         
66:         headers = {'X-PP-AUTHORIZATION' => options.delete(:auth_signature), 'X-PAYPAL-MESSAGE-PROTOCOL' => 'SOAP11'} if options[:auth_signature]
67:         @options = {
68:           :pem => pem_file,
69:           :signature => signature,
70:           :headers => headers || {}
71:         }.update(options)
72: 
73:         
74:         if @options[:pem].blank? && @options[:signature].blank?
75:           raise ArgumentError, "An API Certificate or API Signature is required to make requests to PayPal" 
76:         end
77:         
78:         super
79:       end

Public Instance Methods

capture(money, authorization, options = {}) click to toggle source
    # File lib/active_merchant/billing/gateways/paypal/paypal_common_api.rb, line 89
89:       def capture(money, authorization, options = {})
90:         commit 'DoCapture', build_capture_request(money, authorization, options)
91:       end
credit(money, identification, options = {}) click to toggle source
     # File lib/active_merchant/billing/gateways/paypal/paypal_common_api.rb, line 115
115:       def credit(money, identification, options = {})
116:         deprecated Gateway::CREDIT_DEPRECATION_MESSAGE
117:         refund(money, identification, options)
118:       end
reauthorize(money, authorization, options = {}) click to toggle source
    # File lib/active_merchant/billing/gateways/paypal/paypal_common_api.rb, line 85
85:       def reauthorize(money, authorization, options = {})
86:         commit 'DoReauthorization', build_reauthorize_request(money, authorization, options)
87:       end
refund(money, identification, options = {}) click to toggle source
     # File lib/active_merchant/billing/gateways/paypal/paypal_common_api.rb, line 111
111:       def refund(money, identification, options = {})
112:         commit 'RefundTransaction', build_refund_request(money, identification, options)
113:       end
test?() click to toggle source
    # File lib/active_merchant/billing/gateways/paypal/paypal_common_api.rb, line 81
81:       def test?
82:         @options[:test] || Base.gateway_mode == :test
83:       end
transfer(*args) click to toggle source

Transfer money to one or more recipients.

  gateway.transfer 1000, 'bob@example.com',
    :subject => "The money I owe you", :note => "Sorry it's so late"

  gateway.transfer [1000, 'fred@example.com'],
    [2450, 'wilma@example.com', :note => 'You will receive another payment on 3/24'],
    [2000, 'barney@example.com'],
    :subject => "Your Earnings", :note => "Thanks for your business."
     # File lib/active_merchant/billing/gateways/paypal/paypal_common_api.rb, line 103
103:       def transfer(*args)
104:         commit 'MassPay', build_mass_pay_request(*args)
105:       end
void(authorization, options = {}) click to toggle source
     # File lib/active_merchant/billing/gateways/paypal/paypal_common_api.rb, line 107
107:       def void(authorization, options = {})
108:         commit 'DoVoid', build_void_request(authorization, options)
109:       end

Private Instance Methods

add_address(xml, element, address) click to toggle source
     # File lib/active_merchant/billing/gateways/paypal/paypal_common_api.rb, line 307
307:       def add_address(xml, element, address)
308:         return if address.nil?
309:         xml.tag! element do
310:           xml.tag! 'n2:Name', address[:name]
311:           xml.tag! 'n2:Street1', address[:address1]
312:           xml.tag! 'n2:Street2', address[:address2]
313:           xml.tag! 'n2:CityName', address[:city]
314:           xml.tag! 'n2:StateOrProvince', address[:state].blank? ? 'N/A' : address[:state]
315:           xml.tag! 'n2:Country', address[:country]
316:           xml.tag! 'n2:PostalCode', address[:zip]
317:           xml.tag! 'n2:Phone', address[:phone]
318:         end
319:       end
add_credentials(xml) click to toggle source
     # File lib/active_merchant/billing/gateways/paypal/paypal_common_api.rb, line 296
296:       def add_credentials(xml)
297:         xml.tag! 'RequesterCredentials', CREDENTIALS_NAMESPACES do
298:           xml.tag! 'n1:Credentials' do
299:             xml.tag! 'Username', @options[:login]
300:             xml.tag! 'Password', @options[:password]
301:             xml.tag! 'Subject', @options[:subject]
302:             xml.tag! 'Signature', @options[:signature] unless @options[:signature].blank?
303:           end
304:         end
305:       end
authorization_from(response) click to toggle source
     # File lib/active_merchant/billing/gateways/paypal/paypal_common_api.rb, line 341
341:       def authorization_from(response)
342:         response[:transaction_id] || response[:authorization_id] || response[:refund_transaction_id] # middle one is from reauthorization
343:       end
build_capture_request(money, authorization, options) click to toggle source
     # File lib/active_merchant/billing/gateways/paypal/paypal_common_api.rb, line 135
135:       def build_capture_request(money, authorization, options)   
136:         xml = Builder::XmlMarkup.new
137:         
138:         xml.tag! 'DoCaptureReq', 'xmlns' => PAYPAL_NAMESPACE do
139:           xml.tag! 'DoCaptureRequest', 'xmlns:n2' => EBAY_NAMESPACE do
140:             xml.tag! 'n2:Version', API_VERSION
141:             xml.tag! 'AuthorizationID', authorization
142:             xml.tag! 'Amount', amount(money), 'currencyID' => options[:currency] || currency(money)
143:             xml.tag! 'CompleteType', 'Complete'
144:             xml.tag! 'InvoiceID', options[:order_id] unless options[:order_id].blank?
145:             xml.tag! 'Note', options[:description]
146:           end
147:         end
148: 
149:         xml.target!        
150:       end
build_mass_pay_request(*args) click to toggle source
     # File lib/active_merchant/billing/gateways/paypal/paypal_common_api.rb, line 182
182:       def build_mass_pay_request(*args)   
183:         default_options = args.last.is_a?(Hash) ? args.pop : {}
184:         recipients = args.first.is_a?(Array) ? args : [args]
185:         
186:         xml = Builder::XmlMarkup.new
187:         
188:         xml.tag! 'MassPayReq', 'xmlns' => PAYPAL_NAMESPACE do
189:           xml.tag! 'MassPayRequest', 'xmlns:n2' => EBAY_NAMESPACE do
190:             xml.tag! 'n2:Version', API_VERSION
191:             xml.tag! 'EmailSubject', default_options[:subject] if default_options[:subject]
192:             recipients.each do |money, recipient, options|
193:               options ||= default_options
194:               xml.tag! 'MassPayItem' do
195:                 xml.tag! 'ReceiverEmail', recipient
196:                 xml.tag! 'Amount', amount(money), 'currencyID' => options[:currency] || currency(money)
197:                 xml.tag! 'Note', options[:note] if options[:note]
198:                 xml.tag! 'UniqueId', options[:unique_id] if options[:unique_id]
199:               end
200:             end
201:           end
202:         end
203:         
204:         xml.target!
205:       end
build_reauthorize_request(money, authorization, options) click to toggle source
     # File lib/active_merchant/billing/gateways/paypal/paypal_common_api.rb, line 121
121:       def build_reauthorize_request(money, authorization, options)
122:         xml = Builder::XmlMarkup.new
123:         
124:         xml.tag! 'DoReauthorizationReq', 'xmlns' => PAYPAL_NAMESPACE do
125:           xml.tag! 'DoReauthorizationRequest', 'xmlns:n2' => EBAY_NAMESPACE do
126:             xml.tag! 'n2:Version', API_VERSION
127:             xml.tag! 'AuthorizationID', authorization
128:             xml.tag! 'Amount', amount(money), 'currencyID' => options[:currency] || currency(money)
129:           end
130:         end
131: 
132:         xml.target!        
133:       end
build_refund_request(money, identification, options) click to toggle source
     # File lib/active_merchant/billing/gateways/paypal/paypal_common_api.rb, line 152
152:       def build_refund_request(money, identification, options)
153:         xml = Builder::XmlMarkup.new
154:             
155:         xml.tag! 'RefundTransactionReq', 'xmlns' => PAYPAL_NAMESPACE do
156:           xml.tag! 'RefundTransactionRequest', 'xmlns:n2' => EBAY_NAMESPACE do
157:             xml.tag! 'n2:Version', API_VERSION
158:             xml.tag! 'TransactionID', identification
159:             xml.tag! 'Amount', amount(money), 'currencyID' => options[:currency] || currency(money)
160:             xml.tag! 'RefundType', 'Partial'
161:             xml.tag! 'Memo', options[:note] unless options[:note].blank?
162:           end
163:         end
164:       
165:         xml.target!        
166:       end
build_request(body) click to toggle source
     # File lib/active_merchant/billing/gateways/paypal/paypal_common_api.rb, line 280
280:       def build_request(body)
281:         xml = Builder::XmlMarkup.new
282:         
283:         xml.instruct!
284:         xml.tag! 'env:Envelope', ENVELOPE_NAMESPACES do
285:           xml.tag! 'env:Header' do
286:             add_credentials(xml) unless @options[:headers] && @options[:headers]['X-PP-AUTHORIZATION']
287:           end
288:           
289:           xml.tag! 'env:Body' do
290:             xml << body
291:           end
292:         end
293:         xml.target!
294:       end
build_void_request(authorization, options) click to toggle source
     # File lib/active_merchant/billing/gateways/paypal/paypal_common_api.rb, line 168
168:       def build_void_request(authorization, options)
169:         xml = Builder::XmlMarkup.new
170:         
171:         xml.tag! 'DoVoidReq', 'xmlns' => PAYPAL_NAMESPACE do
172:           xml.tag! 'DoVoidRequest', 'xmlns:n2' => EBAY_NAMESPACE do
173:             xml.tag! 'n2:Version', API_VERSION
174:             xml.tag! 'AuthorizationID', authorization
175:             xml.tag! 'Note', options[:description]
176:           end
177:         end
178: 
179:         xml.target!        
180:       end
commit(action, request) click to toggle source
     # File lib/active_merchant/billing/gateways/paypal/paypal_common_api.rb, line 325
325:       def commit(action, request)
326:         response = parse(action, ssl_post(endpoint_url, build_request(request), @options[:headers]))
327:        
328:         build_response(successful?(response), message_from(response), response,
329:             :test => test?,
330:             :authorization => authorization_from(response),
331:             :fraud_review => fraud_review?(response),
332:             :avs_result => { :code => response[:avs_code] },
333:             :cvv_result => response[:cvv2_code]
334:         )
335:       end
endpoint_url() click to toggle source
     # File lib/active_merchant/billing/gateways/paypal/paypal_common_api.rb, line 321
321:       def endpoint_url
322:         URLS[test? ? :test : :live][@options[:signature].blank? ? :certificate : :signature]
323:       end
fraud_review?(response) click to toggle source
     # File lib/active_merchant/billing/gateways/paypal/paypal_common_api.rb, line 337
337:       def fraud_review?(response)
338:         response[:error_codes] == FRAUD_REVIEW_CODE
339:       end
legacy_parse(action, xml) click to toggle source
     # File lib/active_merchant/billing/gateways/paypal/paypal_common_api.rb, line 227
227:       def legacy_parse(action, xml)
228:         response = {}
229:         
230:         error_messages = []
231:         error_codes = []
232:         
233:         xml = REXML::Document.new(xml)
234:         if root = REXML::XPath.first(xml, "//#{action}Response")
235:           root.elements.each do |node|            
236:             case node.name
237:             when 'Errors'
238:               short_message = nil
239:               long_message = nil
240:               
241:               node.elements.each do |child|
242:                 case child.name
243:                 when "LongMessage"
244:                   long_message = child.text unless child.text.blank?
245:                 when "ShortMessage"
246:                   short_message = child.text unless child.text.blank?
247:                 when "ErrorCode"
248:                   error_codes << child.text unless child.text.blank?
249:                 end
250:               end
251: 
252:               if message = long_message || short_message
253:                 error_messages << message
254:               end
255:             else
256:               legacy_parse_element(response, node)
257:             end
258:           end
259:           response[:message] = error_messages.uniq.join(". ") unless error_messages.empty?
260:           response[:error_codes] = error_codes.uniq.join(",") unless error_codes.empty?
261:         elsif root = REXML::XPath.first(xml, "//SOAP-ENV:Fault")
262:           legacy_parse_element(response, root)
263:           response[:message] = "#{response[:faultcode]}: #{response[:faultstring]} - #{response[:detail]}"
264:         end
265: 
266:         response
267:       end
legacy_parse_element(response, node) click to toggle source
     # File lib/active_merchant/billing/gateways/paypal/paypal_common_api.rb, line 269
269:       def legacy_parse_element(response, node)
270:         if node.has_elements?
271:           node.elements.each{|e| legacy_parse_element(response, e) }
272:         else
273:           response[node.name.underscore.to_sym] = node.text
274:           node.attributes.each do |k, v|
275:             response["#{node.name.underscore}_#{k.underscore}".to_sym] = v if k == 'currencyID'
276:           end
277:         end
278:       end
message_from(response) click to toggle source
     # File lib/active_merchant/billing/gateways/paypal/paypal_common_api.rb, line 349
349:       def message_from(response)
350:         response[:message] || response[:ack]
351:       end
parse(action, xml) click to toggle source
     # File lib/active_merchant/billing/gateways/paypal/paypal_common_api.rb, line 207
207:       def parse(action, xml)
208:         legacy_hash = legacy_parse(action, xml)
209:         xml = strip_attributes(xml)
210:         hash = Hash.from_xml(xml)
211:         hash = hash.fetch('Envelope').fetch('Body').fetch("#{action}Response")
212:         hash = hash["#{action}ResponseDetails"] if hash["#{action}ResponseDetails"]
213: 
214:         legacy_hash.merge(hash)
215:       rescue IndexError
216:         legacy_hash.merge(hash['Envelope']['Body'])
217:       end
strip_attributes(xml) click to toggle source
     # File lib/active_merchant/billing/gateways/paypal/paypal_common_api.rb, line 219
219:       def strip_attributes(xml)
220:         xml = REXML::Document.new(xml)
221:         REXML::XPath.each(xml, '//SOAP-ENV:Envelope//*[@*]') do |el|
222:           el.attributes.each_attribute { |a| a.remove }
223:         end
224:         xml.to_s
225:       end
successful?(response) click to toggle source
     # File lib/active_merchant/billing/gateways/paypal/paypal_common_api.rb, line 345
345:       def successful?(response)
346:         SUCCESS_CODES.include?(response[:ack])
347:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.