activemerchant-kiddy 1.15.0.kiddy

Sign up to get free protection for your applications and to get access to all the features.
Files changed (184) hide show
  1. data/CHANGELOG +688 -0
  2. data/CONTRIBUTORS +233 -0
  3. data/MIT-LICENSE +20 -0
  4. data/README.rdoc +164 -0
  5. data/gem-public_cert.pem +20 -0
  6. data/lib/active_merchant/billing/avs_result.rb +98 -0
  7. data/lib/active_merchant/billing/base.rb +57 -0
  8. data/lib/active_merchant/billing/check.rb +68 -0
  9. data/lib/active_merchant/billing/credit_card.rb +172 -0
  10. data/lib/active_merchant/billing/credit_card_formatting.rb +21 -0
  11. data/lib/active_merchant/billing/credit_card_methods.rb +125 -0
  12. data/lib/active_merchant/billing/cvv_result.rb +38 -0
  13. data/lib/active_merchant/billing/expiry_date.rb +34 -0
  14. data/lib/active_merchant/billing/gateway.rb +170 -0
  15. data/lib/active_merchant/billing/gateways/authorize_net.rb +671 -0
  16. data/lib/active_merchant/billing/gateways/authorize_net_cim.rb +858 -0
  17. data/lib/active_merchant/billing/gateways/barclays_epdq.rb +309 -0
  18. data/lib/active_merchant/billing/gateways/beanstream/beanstream_core.rb +282 -0
  19. data/lib/active_merchant/billing/gateways/beanstream.rb +139 -0
  20. data/lib/active_merchant/billing/gateways/beanstream_interac.rb +54 -0
  21. data/lib/active_merchant/billing/gateways/blue_pay.rb +11 -0
  22. data/lib/active_merchant/billing/gateways/bogus.rb +132 -0
  23. data/lib/active_merchant/billing/gateways/braintree/braintree_common.rb +9 -0
  24. data/lib/active_merchant/billing/gateways/braintree.rb +17 -0
  25. data/lib/active_merchant/billing/gateways/braintree_blue.rb +293 -0
  26. data/lib/active_merchant/billing/gateways/braintree_orange.rb +17 -0
  27. data/lib/active_merchant/billing/gateways/card_stream.rb +230 -0
  28. data/lib/active_merchant/billing/gateways/cyber_source.rb +410 -0
  29. data/lib/active_merchant/billing/gateways/data_cash.rb +597 -0
  30. data/lib/active_merchant/billing/gateways/efsnet.rb +235 -0
  31. data/lib/active_merchant/billing/gateways/elavon.rb +134 -0
  32. data/lib/active_merchant/billing/gateways/epay.rb +268 -0
  33. data/lib/active_merchant/billing/gateways/eway.rb +277 -0
  34. data/lib/active_merchant/billing/gateways/eway_managed.rb +231 -0
  35. data/lib/active_merchant/billing/gateways/exact.rb +222 -0
  36. data/lib/active_merchant/billing/gateways/federated_canada.rb +168 -0
  37. data/lib/active_merchant/billing/gateways/first_pay.rb +177 -0
  38. data/lib/active_merchant/billing/gateways/garanti.rb +262 -0
  39. data/lib/active_merchant/billing/gateways/ideal/ideal_base.rb +250 -0
  40. data/lib/active_merchant/billing/gateways/ideal/ideal_rabobank.pem +13 -0
  41. data/lib/active_merchant/billing/gateways/ideal/ideal_response.rb +29 -0
  42. data/lib/active_merchant/billing/gateways/ideal_rabobank.rb +55 -0
  43. data/lib/active_merchant/billing/gateways/inspire.rb +221 -0
  44. data/lib/active_merchant/billing/gateways/instapay.rb +164 -0
  45. data/lib/active_merchant/billing/gateways/iridium.rb +258 -0
  46. data/lib/active_merchant/billing/gateways/jetpay.rb +276 -0
  47. data/lib/active_merchant/billing/gateways/linkpoint.rb +454 -0
  48. data/lib/active_merchant/billing/gateways/merchant_e_solutions.rb +156 -0
  49. data/lib/active_merchant/billing/gateways/merchant_ware.rb +289 -0
  50. data/lib/active_merchant/billing/gateways/modern_payments.rb +36 -0
  51. data/lib/active_merchant/billing/gateways/modern_payments_cim.rb +220 -0
  52. data/lib/active_merchant/billing/gateways/moneris.rb +209 -0
  53. data/lib/active_merchant/billing/gateways/net_registry.rb +189 -0
  54. data/lib/active_merchant/billing/gateways/netaxept.rb +239 -0
  55. data/lib/active_merchant/billing/gateways/netbilling.rb +168 -0
  56. data/lib/active_merchant/billing/gateways/nmi.rb +13 -0
  57. data/lib/active_merchant/billing/gateways/ogone.rb +292 -0
  58. data/lib/active_merchant/billing/gateways/orbital/orbital_soft_descriptors.rb +46 -0
  59. data/lib/active_merchant/billing/gateways/orbital.rb +317 -0
  60. data/lib/active_merchant/billing/gateways/pay_junction.rb +392 -0
  61. data/lib/active_merchant/billing/gateways/pay_secure.rb +120 -0
  62. data/lib/active_merchant/billing/gateways/paybox_direct.rb +207 -0
  63. data/lib/active_merchant/billing/gateways/payflow/payflow_common_api.rb +207 -0
  64. data/lib/active_merchant/billing/gateways/payflow/payflow_express_response.rb +39 -0
  65. data/lib/active_merchant/billing/gateways/payflow/payflow_response.rb +13 -0
  66. data/lib/active_merchant/billing/gateways/payflow.rb +253 -0
  67. data/lib/active_merchant/billing/gateways/payflow_express.rb +222 -0
  68. data/lib/active_merchant/billing/gateways/payflow_express_uk.rb +15 -0
  69. data/lib/active_merchant/billing/gateways/payflow_uk.rb +21 -0
  70. data/lib/active_merchant/billing/gateways/payment_express.rb +235 -0
  71. data/lib/active_merchant/billing/gateways/paypal/paypal_common_api.rb +351 -0
  72. data/lib/active_merchant/billing/gateways/paypal/paypal_express_response.rb +49 -0
  73. data/lib/active_merchant/billing/gateways/paypal.rb +121 -0
  74. data/lib/active_merchant/billing/gateways/paypal_ca.rb +13 -0
  75. data/lib/active_merchant/billing/gateways/paypal_express.rb +177 -0
  76. data/lib/active_merchant/billing/gateways/paypal_express_common.rb +25 -0
  77. data/lib/active_merchant/billing/gateways/plugnpay.rb +298 -0
  78. data/lib/active_merchant/billing/gateways/psigate.rb +219 -0
  79. data/lib/active_merchant/billing/gateways/psl_card.rb +304 -0
  80. data/lib/active_merchant/billing/gateways/qbms.rb +295 -0
  81. data/lib/active_merchant/billing/gateways/quantum.rb +282 -0
  82. data/lib/active_merchant/billing/gateways/quickpay.rb +218 -0
  83. data/lib/active_merchant/billing/gateways/realex.rb +311 -0
  84. data/lib/active_merchant/billing/gateways/sage/sage_bankcard.rb +88 -0
  85. data/lib/active_merchant/billing/gateways/sage/sage_core.rb +116 -0
  86. data/lib/active_merchant/billing/gateways/sage/sage_virtual_check.rb +97 -0
  87. data/lib/active_merchant/billing/gateways/sage.rb +146 -0
  88. data/lib/active_merchant/billing/gateways/sage_pay.rb +320 -0
  89. data/lib/active_merchant/billing/gateways/sallie_mae.rb +144 -0
  90. data/lib/active_merchant/billing/gateways/secure_net.rb +330 -0
  91. data/lib/active_merchant/billing/gateways/secure_pay.rb +31 -0
  92. data/lib/active_merchant/billing/gateways/secure_pay_au.rb +193 -0
  93. data/lib/active_merchant/billing/gateways/secure_pay_tech.rb +113 -0
  94. data/lib/active_merchant/billing/gateways/skip_jack.rb +453 -0
  95. data/lib/active_merchant/billing/gateways/smart_ps.rb +271 -0
  96. data/lib/active_merchant/billing/gateways/trans_first.rb +127 -0
  97. data/lib/active_merchant/billing/gateways/transax.rb +25 -0
  98. data/lib/active_merchant/billing/gateways/trust_commerce.rb +423 -0
  99. data/lib/active_merchant/billing/gateways/usa_epay.rb +194 -0
  100. data/lib/active_merchant/billing/gateways/verifi.rb +233 -0
  101. data/lib/active_merchant/billing/gateways/viaklix.rb +189 -0
  102. data/lib/active_merchant/billing/gateways/wirecard.rb +318 -0
  103. data/lib/active_merchant/billing/gateways/worldpay.rb +280 -0
  104. data/lib/active_merchant/billing/gateways.rb +18 -0
  105. data/lib/active_merchant/billing/integrations/action_view_helper.rb +68 -0
  106. data/lib/active_merchant/billing/integrations/bogus/helper.rb +17 -0
  107. data/lib/active_merchant/billing/integrations/bogus/notification.rb +11 -0
  108. data/lib/active_merchant/billing/integrations/bogus/return.rb +10 -0
  109. data/lib/active_merchant/billing/integrations/bogus.rb +23 -0
  110. data/lib/active_merchant/billing/integrations/chronopay/helper.rb +120 -0
  111. data/lib/active_merchant/billing/integrations/chronopay/notification.rb +158 -0
  112. data/lib/active_merchant/billing/integrations/chronopay/return.rb +10 -0
  113. data/lib/active_merchant/billing/integrations/chronopay.rb +23 -0
  114. data/lib/active_merchant/billing/integrations/direc_pay/helper.rb +200 -0
  115. data/lib/active_merchant/billing/integrations/direc_pay/notification.rb +76 -0
  116. data/lib/active_merchant/billing/integrations/direc_pay/return.rb +32 -0
  117. data/lib/active_merchant/billing/integrations/direc_pay/status.rb +37 -0
  118. data/lib/active_merchant/billing/integrations/direc_pay.rb +41 -0
  119. data/lib/active_merchant/billing/integrations/directebanking/helper.rb +90 -0
  120. data/lib/active_merchant/billing/integrations/directebanking/notification.rb +120 -0
  121. data/lib/active_merchant/billing/integrations/directebanking/return.rb +11 -0
  122. data/lib/active_merchant/billing/integrations/directebanking.rb +47 -0
  123. data/lib/active_merchant/billing/integrations/gestpay/common.rb +42 -0
  124. data/lib/active_merchant/billing/integrations/gestpay/helper.rb +70 -0
  125. data/lib/active_merchant/billing/integrations/gestpay/notification.rb +85 -0
  126. data/lib/active_merchant/billing/integrations/gestpay/return.rb +10 -0
  127. data/lib/active_merchant/billing/integrations/gestpay.rb +25 -0
  128. data/lib/active_merchant/billing/integrations/helper.rb +96 -0
  129. data/lib/active_merchant/billing/integrations/hi_trust/helper.rb +58 -0
  130. data/lib/active_merchant/billing/integrations/hi_trust/notification.rb +59 -0
  131. data/lib/active_merchant/billing/integrations/hi_trust/return.rb +67 -0
  132. data/lib/active_merchant/billing/integrations/hi_trust.rb +27 -0
  133. data/lib/active_merchant/billing/integrations/moneybookers/helper.rb +48 -0
  134. data/lib/active_merchant/billing/integrations/moneybookers/notification.rb +129 -0
  135. data/lib/active_merchant/billing/integrations/moneybookers.rb +26 -0
  136. data/lib/active_merchant/billing/integrations/nochex/helper.rb +68 -0
  137. data/lib/active_merchant/billing/integrations/nochex/notification.rb +94 -0
  138. data/lib/active_merchant/billing/integrations/nochex/return.rb +10 -0
  139. data/lib/active_merchant/billing/integrations/nochex.rb +88 -0
  140. data/lib/active_merchant/billing/integrations/notification.rb +62 -0
  141. data/lib/active_merchant/billing/integrations/paypal/helper.rb +119 -0
  142. data/lib/active_merchant/billing/integrations/paypal/notification.rb +154 -0
  143. data/lib/active_merchant/billing/integrations/paypal/return.rb +10 -0
  144. data/lib/active_merchant/billing/integrations/paypal.rb +39 -0
  145. data/lib/active_merchant/billing/integrations/quickpay/helper.rb +72 -0
  146. data/lib/active_merchant/billing/integrations/quickpay/notification.rb +74 -0
  147. data/lib/active_merchant/billing/integrations/quickpay.rb +21 -0
  148. data/lib/active_merchant/billing/integrations/return.rb +42 -0
  149. data/lib/active_merchant/billing/integrations/sage_pay_form/encryption.rb +33 -0
  150. data/lib/active_merchant/billing/integrations/sage_pay_form/helper.rb +111 -0
  151. data/lib/active_merchant/billing/integrations/sage_pay_form/notification.rb +210 -0
  152. data/lib/active_merchant/billing/integrations/sage_pay_form/return.rb +31 -0
  153. data/lib/active_merchant/billing/integrations/sage_pay_form.rb +37 -0
  154. data/lib/active_merchant/billing/integrations/two_checkout/helper.rb +59 -0
  155. data/lib/active_merchant/billing/integrations/two_checkout/notification.rb +114 -0
  156. data/lib/active_merchant/billing/integrations/two_checkout/return.rb +17 -0
  157. data/lib/active_merchant/billing/integrations/two_checkout.rb +23 -0
  158. data/lib/active_merchant/billing/integrations/valitor/helper.rb +86 -0
  159. data/lib/active_merchant/billing/integrations/valitor/notification.rb +13 -0
  160. data/lib/active_merchant/billing/integrations/valitor/response_fields.rb +88 -0
  161. data/lib/active_merchant/billing/integrations/valitor/return.rb +13 -0
  162. data/lib/active_merchant/billing/integrations/valitor.rb +33 -0
  163. data/lib/active_merchant/billing/integrations/world_pay/helper.rb +100 -0
  164. data/lib/active_merchant/billing/integrations/world_pay/notification.rb +160 -0
  165. data/lib/active_merchant/billing/integrations/world_pay.rb +38 -0
  166. data/lib/active_merchant/billing/integrations.rb +17 -0
  167. data/lib/active_merchant/billing/response.rb +32 -0
  168. data/lib/active_merchant/billing.rb +9 -0
  169. data/lib/active_merchant/common/connection.rb +177 -0
  170. data/lib/active_merchant/common/country.rb +328 -0
  171. data/lib/active_merchant/common/error.rb +26 -0
  172. data/lib/active_merchant/common/post_data.rb +24 -0
  173. data/lib/active_merchant/common/posts_data.rb +63 -0
  174. data/lib/active_merchant/common/requires_parameters.rb +16 -0
  175. data/lib/active_merchant/common/utils.rb +22 -0
  176. data/lib/active_merchant/common/validateable.rb +81 -0
  177. data/lib/active_merchant/common.rb +14 -0
  178. data/lib/active_merchant/version.rb +3 -0
  179. data/lib/active_merchant.rb +50 -0
  180. data/lib/activemerchant.rb +1 -0
  181. data/lib/certs/cacert.pem +7815 -0
  182. data/lib/support/gateway_support.rb +58 -0
  183. data/lib/support/outbound_hosts.rb +25 -0
  184. metadata +273 -0
@@ -0,0 +1,235 @@
1
+ require 'rexml/document'
2
+
3
+ module ActiveMerchant #:nodoc:
4
+ module Billing #:nodoc:
5
+
6
+ # In NZ DPS supports ANZ, Westpac, National Bank, ASB and BNZ.
7
+ # In Australia DPS supports ANZ, NAB, Westpac, CBA, St George and Bank of South Australia.
8
+ # The Maybank in Malaysia is supported and the Citibank for Singapore.
9
+ class PaymentExpressGateway < Gateway
10
+ self.default_currency = 'NZD'
11
+ # PS supports all major credit cards; Visa, Mastercard, Amex, Diners, BankCard & JCB.
12
+ # Various white label cards can be accepted as well; Farmers, AirNZCard and Elders etc.
13
+ # Please note that not all acquirers and Eftpos networks can support some of these card types.
14
+ # VISA, Mastercard, Diners Club and Farmers cards are supported
15
+ #
16
+ # However, regular accounts with DPS only support VISA and Mastercard
17
+ self.supported_cardtypes = [ :visa, :master, :american_express, :diners_club, :jcb ]
18
+
19
+ self.supported_countries = [ 'AU', 'MY', 'NZ', 'SG', 'ZA', 'GB', 'US' ]
20
+
21
+ self.homepage_url = 'http://www.paymentexpress.com/'
22
+ self.display_name = 'PaymentExpress'
23
+
24
+ URL = 'https://www.paymentexpress.com/pxpost.aspx'
25
+
26
+ APPROVED = '1'
27
+
28
+ TRANSACTIONS = {
29
+ :purchase => 'Purchase',
30
+ :credit => 'Refund',
31
+ :authorization => 'Auth',
32
+ :capture => 'Complete',
33
+ :validate => 'Validate'
34
+ }
35
+
36
+ # We require the DPS gateway username and password when the object is created.
37
+ def initialize(options = {})
38
+ # A DPS username and password must exist
39
+ requires!(options, :login, :password)
40
+ # Make the options an instance variable
41
+ @options = options
42
+ super
43
+ end
44
+
45
+ # Funds are transferred immediately.
46
+ def purchase(money, payment_source, options = {})
47
+ request = build_purchase_or_authorization_request(money, payment_source, options)
48
+ commit(:purchase, request)
49
+ end
50
+
51
+ # NOTE: Perhaps in options we allow a transaction note to be inserted
52
+ # Verifies that funds are available for the requested card and amount and reserves the specified amount.
53
+ # See: http://www.paymentexpress.com/technical_resources/ecommerce_nonhosted/pxpost.html#Authcomplete
54
+ def authorize(money, payment_source, options = {})
55
+ request = build_purchase_or_authorization_request(money, payment_source, options)
56
+ commit(:authorization, request)
57
+ end
58
+
59
+ # Transfer pre-authorized funds immediately
60
+ # See: http://www.paymentexpress.com/technical_resources/ecommerce_nonhosted/pxpost.html#Authcomplete
61
+ def capture(money, identification, options = {})
62
+ request = build_capture_or_credit_request(money, identification, options)
63
+ commit(:capture, request)
64
+ end
65
+
66
+ # Refund funds to the card holder
67
+ def refund(money, identification, options = {})
68
+ requires!(options, :description)
69
+
70
+ request = build_capture_or_credit_request(money, identification, options)
71
+ commit(:credit, request)
72
+ end
73
+
74
+ def credit(money, identification, options = {})
75
+ deprecated CREDIT_DEPRECATION_MESSAGE
76
+ refund(money, identification, options)
77
+ end
78
+
79
+ # token based billing
80
+
81
+ # initiates a "Validate" transcation to store card data on payment express servers
82
+ # returns a "token" that can be used to rebill this card
83
+ # see: http://www.paymentexpress.com/technical_resources/ecommerce_nonhosted/pxpost.html#Tokenbilling
84
+ # PaymentExpress does not support unstoring a stored card.
85
+ def store(credit_card, options = {})
86
+ request = build_token_request(credit_card, options)
87
+ commit(:validate, request)
88
+ end
89
+
90
+ private
91
+
92
+ def build_purchase_or_authorization_request(money, payment_source, options)
93
+ result = new_transaction
94
+
95
+ if payment_source.is_a?(String)
96
+ add_billing_token(result, payment_source)
97
+ else
98
+ add_credit_card(result, payment_source)
99
+ end
100
+
101
+ add_amount(result, money, options)
102
+ add_invoice(result, options)
103
+ add_address_verification_data(result, options)
104
+ result
105
+ end
106
+
107
+ def build_capture_or_credit_request(money, identification, options)
108
+ result = new_transaction
109
+
110
+ add_amount(result, money, options)
111
+ add_invoice(result, options)
112
+ add_reference(result, identification)
113
+ result
114
+ end
115
+
116
+ def build_token_request(credit_card, options)
117
+ result = new_transaction
118
+ add_credit_card(result, credit_card)
119
+ add_amount(result, 100, options) #need to make an auth request for $1
120
+ add_token_request(result, options)
121
+ result
122
+ end
123
+
124
+ def add_credentials(xml)
125
+ xml.add_element("PostUsername").text = @options[:login]
126
+ xml.add_element("PostPassword").text = @options[:password]
127
+ end
128
+
129
+ def add_reference(xml, identification)
130
+ xml.add_element("DpsTxnRef").text = identification
131
+ end
132
+
133
+ def add_credit_card(xml, credit_card)
134
+ xml.add_element("CardHolderName").text = credit_card.name
135
+ xml.add_element("CardNumber").text = credit_card.number
136
+ xml.add_element("DateExpiry").text = format_date(credit_card.month, credit_card.year)
137
+
138
+ if credit_card.verification_value?
139
+ xml.add_element("Cvc2").text = credit_card.verification_value
140
+ end
141
+
142
+ if requires_start_date_or_issue_number?(credit_card)
143
+ xml.add_element("DateStart").text = format_date(credit_card.start_month, credit_card.start_year) unless credit_card.start_month.blank? || credit_card.start_year.blank?
144
+ xml.add_element("IssueNumber").text = credit_card.issue_number unless credit_card.issue_number.blank?
145
+ end
146
+ end
147
+
148
+ def add_billing_token(xml, token)
149
+ xml.add_element("DpsBillingId").text = token
150
+ end
151
+
152
+ def add_token_request(xml, options)
153
+ xml.add_element("BillingId").text = options[:billing_id] if options[:billing_id]
154
+ xml.add_element("EnableAddBillCard").text = 1
155
+ end
156
+
157
+ def add_amount(xml, money, options)
158
+ xml.add_element("Amount").text = amount(money)
159
+ xml.add_element("InputCurrency").text = options[:currency] || currency(money)
160
+ end
161
+
162
+ def add_transaction_type(xml, action)
163
+ xml.add_element("TxnType").text = TRANSACTIONS[action]
164
+ end
165
+
166
+ def add_invoice(xml, options)
167
+ xml.add_element("TxnId").text = options[:order_id].to_s.slice(0, 16) unless options[:order_id].blank?
168
+ xml.add_element("MerchantReference").text = options[:description] unless options[:description].blank?
169
+ end
170
+
171
+ def add_address_verification_data(xml, options)
172
+ address = options[:billing_address] || options[:address]
173
+ return if address.nil?
174
+
175
+ xml.add_element("EnableAvsData").text = 1
176
+ xml.add_element("AvsAction").text = 1
177
+
178
+ xml.add_element("AvsStreetAddress").text = address[:address1]
179
+ xml.add_element("AvsPostCode").text = address[:zip]
180
+ end
181
+
182
+ def new_transaction
183
+ REXML::Document.new.add_element("Txn")
184
+ end
185
+
186
+ # Take in the request and post it to DPS
187
+ def commit(action, request)
188
+ add_credentials(request)
189
+ add_transaction_type(request, action)
190
+
191
+ # Parse the XML response
192
+ response = parse( ssl_post(URL, request.to_s) )
193
+
194
+ # Return a response
195
+ PaymentExpressResponse.new(response[:success] == APPROVED, response[:card_holder_help_text], response,
196
+ :test => response[:test_mode] == '1',
197
+ :authorization => response[:dps_txn_ref]
198
+ )
199
+ end
200
+
201
+ # Response XML documentation: http://www.paymentexpress.com/technical_resources/ecommerce_nonhosted/pxpost.html#XMLTxnOutput
202
+ def parse(xml_string)
203
+ response = {}
204
+
205
+ xml = REXML::Document.new(xml_string)
206
+
207
+ # Gather all root elements such as HelpText
208
+ xml.elements.each('Txn/*') do |element|
209
+ response[element.name.underscore.to_sym] = element.text unless element.name == 'Transaction'
210
+ end
211
+
212
+ # Gather all transaction elements and prefix with "account_"
213
+ # So we could access the MerchantResponseText by going
214
+ # response[account_merchant_response_text]
215
+ xml.elements.each('Txn/Transaction/*') do |element|
216
+ response[element.name.underscore.to_sym] = element.text
217
+ end
218
+
219
+ response
220
+ end
221
+
222
+ def format_date(month, year)
223
+ "#{format(month, :two_digits)}#{format(year, :two_digits)}"
224
+ end
225
+ end
226
+
227
+ class PaymentExpressResponse < Response
228
+ # add a method to response so we can easily get the token
229
+ # for Validate transactions
230
+ def token
231
+ @params["billing_id"] || @params["dps_billing_id"]
232
+ end
233
+ end
234
+ end
235
+ end
@@ -0,0 +1,351 @@
1
+ module ActiveMerchant #:nodoc:
2
+ module Billing #:nodoc:
3
+ # This module is included in both PaypalGateway and PaypalExpressGateway
4
+ module PaypalCommonAPI
5
+ def self.included(base)
6
+ base.default_currency = 'USD'
7
+ base.cattr_accessor :pem_file
8
+ base.cattr_accessor :signature
9
+ end
10
+
11
+ API_VERSION = '62.0'
12
+
13
+ URLS = {
14
+ :test => { :certificate => 'https://api.sandbox.paypal.com/2.0/',
15
+ :signature => 'https://api-3t.sandbox.paypal.com/2.0/' },
16
+ :live => { :certificate => 'https://api-aa.paypal.com/2.0/',
17
+ :signature => 'https://api-3t.paypal.com/2.0/' }
18
+ }
19
+
20
+ PAYPAL_NAMESPACE = 'urn:ebay:api:PayPalAPI'
21
+ EBAY_NAMESPACE = 'urn:ebay:apis:eBLBaseComponents'
22
+
23
+ ENVELOPE_NAMESPACES = { 'xmlns:xsd' => 'http://www.w3.org/2001/XMLSchema',
24
+ 'xmlns:env' => 'http://schemas.xmlsoap.org/soap/envelope/',
25
+ 'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance'
26
+ }
27
+ CREDENTIALS_NAMESPACES = { 'xmlns' => PAYPAL_NAMESPACE,
28
+ 'xmlns:n1' => EBAY_NAMESPACE,
29
+ 'env:mustUnderstand' => '0'
30
+ }
31
+
32
+ AUSTRALIAN_STATES = {
33
+ 'ACT' => 'Australian Capital Territory',
34
+ 'NSW' => 'New South Wales',
35
+ 'NT' => 'Northern Territory',
36
+ 'QLD' => 'Queensland',
37
+ 'SA' => 'South Australia',
38
+ 'TAS' => 'Tasmania',
39
+ 'VIC' => 'Victoria',
40
+ 'WA' => 'Western Australia'
41
+ }
42
+
43
+ SUCCESS_CODES = [ 'Success', 'SuccessWithWarning' ]
44
+
45
+ FRAUD_REVIEW_CODE = "11610"
46
+
47
+ # The gateway must be configured with either your PayPal PEM file
48
+ # or your PayPal API Signature. Only one is required.
49
+ #
50
+ # <tt>:pem</tt> The text of your PayPal PEM file. Note
51
+ # this is not the path to file, but its
52
+ # contents. If you are only using one PEM
53
+ # file on your site you can declare it
54
+ # globally and then you won't need to
55
+ # include this option
56
+ #
57
+ # <tt>:signature</tt> The text of your PayPal signature.
58
+ # If you are only using one API Signature
59
+ # on your site you can declare it
60
+ # globally and then you won't need to
61
+ # include this option
62
+
63
+ def initialize(options = {})
64
+ requires!(options, :login, :password)
65
+
66
+ @options = {
67
+ :pem => pem_file,
68
+ :signature => signature
69
+ }.update(options)
70
+
71
+ if @options[:pem].blank? && @options[:signature].blank?
72
+ raise ArgumentError, "An API Certificate or API Signature is required to make requests to PayPal"
73
+ end
74
+
75
+ super
76
+ end
77
+
78
+ def test?
79
+ @options[:test] || Base.gateway_mode == :test
80
+ end
81
+
82
+ def reauthorize(money, authorization, options = {})
83
+ commit 'DoReauthorization', build_reauthorize_request(money, authorization, options)
84
+ end
85
+
86
+ def capture(money, authorization, options = {})
87
+ commit 'DoCapture', build_capture_request(money, authorization, options)
88
+ end
89
+
90
+ # Transfer money to one or more recipients.
91
+ #
92
+ # gateway.transfer 1000, 'bob@example.com',
93
+ # :subject => "The money I owe you", :note => "Sorry it's so late"
94
+ #
95
+ # gateway.transfer [1000, 'fred@example.com'],
96
+ # [2450, 'wilma@example.com', :note => 'You will receive another payment on 3/24'],
97
+ # [2000, 'barney@example.com'],
98
+ # :subject => "Your Earnings", :note => "Thanks for your business."
99
+ #
100
+ def transfer(*args)
101
+ commit 'MassPay', build_mass_pay_request(*args)
102
+ end
103
+
104
+ def void(authorization, options = {})
105
+ commit 'DoVoid', build_void_request(authorization, options)
106
+ end
107
+
108
+ def refund(money, identification, options = {})
109
+ commit 'RefundTransaction', build_refund_request(money, identification, options)
110
+ end
111
+
112
+ def credit(money, identification, options = {})
113
+ deprecated Gateway::CREDIT_DEPRECATION_MESSAGE
114
+ refund(money, identification, options)
115
+ end
116
+
117
+ private
118
+ def build_reauthorize_request(money, authorization, options)
119
+ xml = Builder::XmlMarkup.new
120
+
121
+ xml.tag! 'DoReauthorizationReq', 'xmlns' => PAYPAL_NAMESPACE do
122
+ xml.tag! 'DoReauthorizationRequest', 'xmlns:n2' => EBAY_NAMESPACE do
123
+ xml.tag! 'n2:Version', API_VERSION
124
+ xml.tag! 'AuthorizationID', authorization
125
+ xml.tag! 'Amount', amount(money), 'currencyID' => options[:currency] || currency(money)
126
+ end
127
+ end
128
+
129
+ xml.target!
130
+ end
131
+
132
+ def build_capture_request(money, authorization, options)
133
+ xml = Builder::XmlMarkup.new
134
+
135
+ xml.tag! 'DoCaptureReq', 'xmlns' => PAYPAL_NAMESPACE do
136
+ xml.tag! 'DoCaptureRequest', 'xmlns:n2' => EBAY_NAMESPACE do
137
+ xml.tag! 'n2:Version', API_VERSION
138
+ xml.tag! 'AuthorizationID', authorization
139
+ xml.tag! 'Amount', amount(money), 'currencyID' => options[:currency] || currency(money)
140
+ xml.tag! 'CompleteType', 'Complete'
141
+ xml.tag! 'InvoiceID', options[:order_id] unless options[:order_id].blank?
142
+ xml.tag! 'Note', options[:description]
143
+ end
144
+ end
145
+
146
+ xml.target!
147
+ end
148
+
149
+ def build_refund_request(money, identification, options)
150
+ xml = Builder::XmlMarkup.new
151
+
152
+ xml.tag! 'RefundTransactionReq', 'xmlns' => PAYPAL_NAMESPACE do
153
+ xml.tag! 'RefundTransactionRequest', 'xmlns:n2' => EBAY_NAMESPACE do
154
+ xml.tag! 'n2:Version', API_VERSION
155
+ xml.tag! 'TransactionID', identification
156
+ xml.tag! 'Amount', amount(money), 'currencyID' => options[:currency] || currency(money)
157
+ xml.tag! 'RefundType', 'Partial'
158
+ xml.tag! 'Memo', options[:note] unless options[:note].blank?
159
+ end
160
+ end
161
+
162
+ xml.target!
163
+ end
164
+
165
+ def build_void_request(authorization, options)
166
+ xml = Builder::XmlMarkup.new
167
+
168
+ xml.tag! 'DoVoidReq', 'xmlns' => PAYPAL_NAMESPACE do
169
+ xml.tag! 'DoVoidRequest', 'xmlns:n2' => EBAY_NAMESPACE do
170
+ xml.tag! 'n2:Version', API_VERSION
171
+ xml.tag! 'AuthorizationID', authorization
172
+ xml.tag! 'Note', options[:description]
173
+ end
174
+ end
175
+
176
+ xml.target!
177
+ end
178
+
179
+ def build_mass_pay_request(*args)
180
+ default_options = args.last.is_a?(Hash) ? args.pop : {}
181
+ recipients = args.first.is_a?(Array) ? args : [args]
182
+
183
+ xml = Builder::XmlMarkup.new
184
+
185
+ xml.tag! 'MassPayReq', 'xmlns' => PAYPAL_NAMESPACE do
186
+ xml.tag! 'MassPayRequest', 'xmlns:n2' => EBAY_NAMESPACE do
187
+ xml.tag! 'n2:Version', API_VERSION
188
+ xml.tag! 'EmailSubject', default_options[:subject] if default_options[:subject]
189
+ recipients.each do |money, recipient, options|
190
+ options ||= default_options
191
+ xml.tag! 'MassPayItem' do
192
+ xml.tag! 'ReceiverEmail', recipient
193
+ xml.tag! 'Amount', amount(money), 'currencyID' => options[:currency] || currency(money)
194
+ xml.tag! 'Note', options[:note] if options[:note]
195
+ xml.tag! 'UniqueId', options[:unique_id] if options[:unique_id]
196
+ end
197
+ end
198
+ end
199
+ end
200
+
201
+ xml.target!
202
+ end
203
+
204
+ def parse(action, xml)
205
+ legacy_hash = legacy_parse(action, xml)
206
+ xml = strip_attributes(xml)
207
+ hash = Hash.from_xml(xml)
208
+ hash = hash.fetch('Envelope').fetch('Body').fetch("#{action}Response")
209
+ hash = hash["#{action}ResponseDetails"] if hash["#{action}ResponseDetails"]
210
+
211
+ legacy_hash.merge(hash)
212
+ rescue IndexError
213
+ legacy_hash.merge(hash['Envelope']['Body'])
214
+ end
215
+
216
+ def strip_attributes(xml)
217
+ xml = REXML::Document.new(xml)
218
+ REXML::XPath.each(xml, '//SOAP-ENV:Envelope//*[@*]') do |el|
219
+ el.attributes.each_attribute { |a| a.remove }
220
+ end
221
+ xml.to_s
222
+ end
223
+
224
+ def legacy_parse(action, xml)
225
+ response = {}
226
+
227
+ error_messages = []
228
+ error_codes = []
229
+
230
+ xml = REXML::Document.new(xml)
231
+ if root = REXML::XPath.first(xml, "//#{action}Response")
232
+ root.elements.each do |node|
233
+ case node.name
234
+ when 'Errors'
235
+ short_message = nil
236
+ long_message = nil
237
+
238
+ node.elements.each do |child|
239
+ case child.name
240
+ when "LongMessage"
241
+ long_message = child.text unless child.text.blank?
242
+ when "ShortMessage"
243
+ short_message = child.text unless child.text.blank?
244
+ when "ErrorCode"
245
+ error_codes << child.text unless child.text.blank?
246
+ end
247
+ end
248
+
249
+ if message = long_message || short_message
250
+ error_messages << message
251
+ end
252
+ else
253
+ legacy_parse_element(response, node)
254
+ end
255
+ end
256
+ response[:message] = error_messages.uniq.join(". ") unless error_messages.empty?
257
+ response[:error_codes] = error_codes.uniq.join(",") unless error_codes.empty?
258
+ elsif root = REXML::XPath.first(xml, "//SOAP-ENV:Fault")
259
+ legacy_parse_element(response, root)
260
+ response[:message] = "#{response[:faultcode]}: #{response[:faultstring]} - #{response[:detail]}"
261
+ end
262
+
263
+ response
264
+ end
265
+
266
+ def legacy_parse_element(response, node)
267
+ if node.has_elements?
268
+ node.elements.each{|e| legacy_parse_element(response, e) }
269
+ else
270
+ response[node.name.underscore.to_sym] = node.text
271
+ node.attributes.each do |k, v|
272
+ response["#{node.name.underscore}_#{k.underscore}".to_sym] = v if k == 'currencyID'
273
+ end
274
+ end
275
+ end
276
+
277
+ def build_request(body)
278
+ xml = Builder::XmlMarkup.new
279
+
280
+ xml.instruct!
281
+ xml.tag! 'env:Envelope', ENVELOPE_NAMESPACES do
282
+ xml.tag! 'env:Header' do
283
+ add_credentials(xml)
284
+ end
285
+
286
+ xml.tag! 'env:Body' do
287
+ xml << body
288
+ end
289
+ end
290
+ xml.target!
291
+ end
292
+
293
+ def add_credentials(xml)
294
+ xml.tag! 'RequesterCredentials', CREDENTIALS_NAMESPACES do
295
+ xml.tag! 'n1:Credentials' do
296
+ xml.tag! 'Username', @options[:login]
297
+ xml.tag! 'Password', @options[:password]
298
+ xml.tag! 'Subject', @options[:subject]
299
+ xml.tag! 'Signature', @options[:signature] unless @options[:signature].blank?
300
+ end
301
+ end
302
+ end
303
+
304
+ def add_address(xml, element, address)
305
+ return if address.nil?
306
+ xml.tag! element do
307
+ xml.tag! 'n2:Name', address[:name]
308
+ xml.tag! 'n2:Street1', address[:address1]
309
+ xml.tag! 'n2:Street2', address[:address2]
310
+ xml.tag! 'n2:CityName', address[:city]
311
+ xml.tag! 'n2:StateOrProvince', address[:state].blank? ? 'N/A' : address[:state]
312
+ xml.tag! 'n2:Country', address[:country]
313
+ xml.tag! 'n2:PostalCode', address[:zip]
314
+ xml.tag! 'n2:Phone', address[:phone]
315
+ end
316
+ end
317
+
318
+ def endpoint_url
319
+ URLS[test? ? :test : :live][@options[:signature].blank? ? :certificate : :signature]
320
+ end
321
+
322
+ def commit(action, request)
323
+ response = parse(action, ssl_post(endpoint_url, build_request(request)))
324
+
325
+ build_response(successful?(response), message_from(response), response,
326
+ :test => test?,
327
+ :authorization => authorization_from(response),
328
+ :fraud_review => fraud_review?(response),
329
+ :avs_result => { :code => response[:avs_code] },
330
+ :cvv_result => response[:cvv2_code]
331
+ )
332
+ end
333
+
334
+ def fraud_review?(response)
335
+ response[:error_codes] == FRAUD_REVIEW_CODE
336
+ end
337
+
338
+ def authorization_from(response)
339
+ response[:transaction_id] || response[:authorization_id] || response[:refund_transaction_id] # middle one is from reauthorization
340
+ end
341
+
342
+ def successful?(response)
343
+ SUCCESS_CODES.include?(response[:ack])
344
+ end
345
+
346
+ def message_from(response)
347
+ response[:message] || response[:ack]
348
+ end
349
+ end
350
+ end
351
+ end
@@ -0,0 +1,49 @@
1
+ module ActiveMerchant #:nodoc:
2
+ module Billing #:nodoc:
3
+ class PaypalExpressResponse < Response
4
+ def email
5
+ info['Payer']
6
+ end
7
+
8
+ def info
9
+ (@params['PayerInfo']||{})
10
+ end
11
+
12
+ def name
13
+ payer = (info['PayerName']||{})
14
+ [payer['FirstName'], payer['MiddleName'], payer['LastName']].compact.join(' ')
15
+ end
16
+
17
+ def token
18
+ @params['Token']
19
+ end
20
+
21
+ def payer_id
22
+ info['PayerID']
23
+ end
24
+
25
+ def payer_country
26
+ info['PayerCountry']
27
+ end
28
+
29
+ # PayPal returns a contact telephone number only if your Merchant account profile settings require that the buyer enter one.
30
+ def contact_phone
31
+ @params['ContactPhone']
32
+ end
33
+
34
+ def address
35
+ address = (@params['PaymentDetails']||{})['ShipToAddress']
36
+ { 'name' => address['Name'],
37
+ 'company' => info['PayerBusiness'],
38
+ 'address1' => address['Street1'],
39
+ 'address2' => address['Street2'],
40
+ 'city' => address['CityName'],
41
+ 'state' => address['StateOrProvince'],
42
+ 'country' => address['Country'],
43
+ 'zip' => address['PostalCode'],
44
+ 'phone' => (contact_phone || address['Phone'])
45
+ }
46
+ end
47
+ end
48
+ end
49
+ end