paypal-business 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,195 @@
1
+ require File.join(File.dirname(__FILE__),'default.rb')
2
+
3
+ require 'soap/rpc/driver'
4
+
5
+ class PayPalAPIInterface < ::SOAP::RPC::Driver
6
+ DefaultEndpointUrl = "https://api.sandbox.paypal.com/2.0/"
7
+ MappingRegistry = ::SOAP::Mapping::Registry.new
8
+
9
+ Methods = [
10
+ [ nil,
11
+ "refundTransaction",
12
+ [ ["in", "RefundTransactionRequest", ["::SOAP::SOAPElement", "urn:ebay:api:PayPalAPI", "RefundTransactionReq"], true],
13
+ ["out", "RefundTransactionResponse", ["::SOAP::SOAPElement", "urn:ebay:api:PayPalAPI", "RefundTransactionResponse"], true] ],
14
+ { :request_style => :document, :request_use => :literal,
15
+ :response_style => :document, :response_use => :literal }
16
+ ],
17
+ [ nil,
18
+ "getTransactionDetails",
19
+ [ ["in", "GetTransactionDetailsRequest", ["::SOAP::SOAPElement", "urn:ebay:api:PayPalAPI", "GetTransactionDetailsReq"], true],
20
+ ["out", "GetTransactionDetailsResponse", ["::SOAP::SOAPElement", "urn:ebay:api:PayPalAPI", "GetTransactionDetailsResponse"], true] ],
21
+ { :request_style => :document, :request_use => :literal,
22
+ :response_style => :document, :response_use => :literal }
23
+ ],
24
+ [ nil,
25
+ "billUser",
26
+ [ ["in", "BillUserRequest", ["::SOAP::SOAPElement", "urn:ebay:api:PayPalAPI", "BillUserReq"], true],
27
+ ["out", "BillUserResponse", ["::SOAP::SOAPElement", "urn:ebay:api:PayPalAPI", "BillUserResponse"], true] ],
28
+ { :request_style => :document, :request_use => :literal,
29
+ :response_style => :document, :response_use => :literal }
30
+ ],
31
+ [ nil,
32
+ "transactionSearch",
33
+ [ ["in", "TransactionSearchRequest", ["::SOAP::SOAPElement", "urn:ebay:api:PayPalAPI", "TransactionSearchReq"], true],
34
+ ["out", "TransactionSearchResponse", ["::SOAP::SOAPElement", "urn:ebay:api:PayPalAPI", "TransactionSearchResponse"], true] ],
35
+ { :request_style => :document, :request_use => :literal,
36
+ :response_style => :document, :response_use => :literal }
37
+ ],
38
+ [ nil,
39
+ "massPay",
40
+ [ ["in", "MassPayRequest", ["::SOAP::SOAPElement", "urn:ebay:api:PayPalAPI", "MassPayReq"], true],
41
+ ["out", "MassPayResponse", ["::SOAP::SOAPElement", "urn:ebay:api:PayPalAPI", "MassPayResponse"], true] ],
42
+ { :request_style => :document, :request_use => :literal,
43
+ :response_style => :document, :response_use => :literal }
44
+ ],
45
+ [ nil,
46
+ "billAgreementUpdate",
47
+ [ ["in", "BillAgreementUpdateRequest", ["::SOAP::SOAPElement", "urn:ebay:api:PayPalAPI", "BillAgreementUpdateReq"], true],
48
+ ["out", "body", ["::SOAP::SOAPElement", "urn:ebay:api:PayPalAPI", "BAUpdateResponse"], true] ],
49
+ { :request_style => :document, :request_use => :literal,
50
+ :response_style => :document, :response_use => :literal }
51
+ ],
52
+ [ nil,
53
+ "addressVerify",
54
+ [ ["in", "AddressVerifyRequest", ["::SOAP::SOAPElement", "urn:ebay:api:PayPalAPI", "AddressVerifyReq"], true],
55
+ ["out", "AddressVerifyResponse", ["::SOAP::SOAPElement", "urn:ebay:api:PayPalAPI", "AddressVerifyResponse"], true] ],
56
+ { :request_style => :document, :request_use => :literal,
57
+ :response_style => :document, :response_use => :literal }
58
+ ],
59
+ [ nil,
60
+ "enterBoarding",
61
+ [ ["in", "EnterBoardingRequest", ["::SOAP::SOAPElement", "urn:ebay:api:PayPalAPI", "EnterBoardingReq"], true],
62
+ ["out", "EnterBoardingResponse", ["::SOAP::SOAPElement", "urn:ebay:api:PayPalAPI", "EnterBoardingResponse"], true] ],
63
+ { :request_style => :document, :request_use => :literal,
64
+ :response_style => :document, :response_use => :literal }
65
+ ],
66
+ [ nil,
67
+ "getBoardingDetails",
68
+ [ ["in", "GetBoardingDetailsRequest", ["::SOAP::SOAPElement", "urn:ebay:api:PayPalAPI", "GetBoardingDetailsReq"], true],
69
+ ["out", "GetBoardingDetailsResponse", ["::SOAP::SOAPElement", "urn:ebay:api:PayPalAPI", "GetBoardingDetailsResponse"], true] ],
70
+ { :request_style => :document, :request_use => :literal,
71
+ :response_style => :document, :response_use => :literal }
72
+ ]
73
+ ]
74
+
75
+ def initialize(endpoint_url = nil)
76
+ endpoint_url ||= DefaultEndpointUrl
77
+ super(endpoint_url, nil)
78
+ self.mapping_registry = MappingRegistry
79
+ init_methods
80
+ end
81
+
82
+ private
83
+
84
+ def init_methods
85
+ Methods.each do |definitions|
86
+ opt = definitions.last
87
+ if opt[:request_style] == :document
88
+ add_document_operation(*definitions)
89
+ else
90
+ add_rpc_operation(*definitions)
91
+ qname = definitions[0]
92
+ name = definitions[2]
93
+ if qname.name != name and qname.name.capitalize == name.capitalize
94
+ ::SOAP::Mapping.define_singleton_method(self, qname.name) do |*arg|
95
+ __send__(name, *arg)
96
+ end
97
+ end
98
+ end
99
+ end
100
+ end
101
+ end
102
+
103
+ require 'soap/rpc/driver'
104
+
105
+ class PayPalAPIAAInterface < ::SOAP::RPC::Driver
106
+ DefaultEndpointUrl = "https://api-aa.sandbox.paypal.com/2.0/"
107
+ MappingRegistry = ::SOAP::Mapping::Registry.new
108
+
109
+ Methods = [
110
+ [ nil,
111
+ "doExpressCheckoutPayment",
112
+ [ ["in", "DoExpressCheckoutPaymentRequest", ["::SOAP::SOAPElement", "urn:ebay:api:PayPalAPI", "DoExpressCheckoutPaymentReq"], true],
113
+ ["out", "DoExpressCheckoutPaymentResponse", ["::SOAP::SOAPElement", "urn:ebay:api:PayPalAPI", "DoExpressCheckoutPaymentResponse"], true] ],
114
+ { :request_style => :document, :request_use => :literal,
115
+ :response_style => :document, :response_use => :literal }
116
+ ],
117
+ [ nil,
118
+ "setExpressCheckout",
119
+ [ ["in", "SetExpressCheckoutRequest", ["::SOAP::SOAPElement", "urn:ebay:api:PayPalAPI", "SetExpressCheckoutReq"], true],
120
+ ["out", "SetExpressCheckoutResponse", ["::SOAP::SOAPElement", "urn:ebay:api:PayPalAPI", "SetExpressCheckoutResponse"], true] ],
121
+ { :request_style => :document, :request_use => :literal,
122
+ :response_style => :document, :response_use => :literal }
123
+ ],
124
+ [ nil,
125
+ "getExpressCheckoutDetails",
126
+ [ ["in", "GetExpressCheckoutDetailsRequest", ["::SOAP::SOAPElement", "urn:ebay:api:PayPalAPI", "GetExpressCheckoutDetailsReq"], true],
127
+ ["out", "GetExpressCheckoutDetailsResponse", ["::SOAP::SOAPElement", "urn:ebay:api:PayPalAPI", "GetExpressCheckoutDetailsResponse"], true] ],
128
+ { :request_style => :document, :request_use => :literal,
129
+ :response_style => :document, :response_use => :literal }
130
+ ],
131
+ [ nil,
132
+ "doDirectPayment",
133
+ [ ["in", "DoDirectPaymentRequest", ["::SOAP::SOAPElement", "urn:ebay:api:PayPalAPI", "DoDirectPaymentReq"], true],
134
+ ["out", "DoDirectPaymentResponse", ["::SOAP::SOAPElement", "urn:ebay:api:PayPalAPI", "DoDirectPaymentResponse"], true] ],
135
+ { :request_style => :document, :request_use => :literal,
136
+ :response_style => :document, :response_use => :literal }
137
+ ],
138
+ [ nil,
139
+ "doCapture",
140
+ [ ["in", "DoCaptureRequest", ["::SOAP::SOAPElement", "urn:ebay:api:PayPalAPI", "DoCaptureReq"], true],
141
+ ["out", "DoCaptureResponse", ["::SOAP::SOAPElement", "urn:ebay:api:PayPalAPI", "DoCaptureResponse"], true] ],
142
+ { :request_style => :document, :request_use => :literal,
143
+ :response_style => :document, :response_use => :literal }
144
+ ],
145
+ [ nil,
146
+ "doReauthorization",
147
+ [ ["in", "DoReauthorizationRequest", ["::SOAP::SOAPElement", "urn:ebay:api:PayPalAPI", "DoReauthorizationReq"], true],
148
+ ["out", "DoReauthorizationResponse", ["::SOAP::SOAPElement", "urn:ebay:api:PayPalAPI", "DoReauthorizationResponse"], true] ],
149
+ { :request_style => :document, :request_use => :literal,
150
+ :response_style => :document, :response_use => :literal }
151
+ ],
152
+ [ nil,
153
+ "doVoid",
154
+ [ ["in", "DoVoidRequest", ["::SOAP::SOAPElement", "urn:ebay:api:PayPalAPI", "DoVoidReq"], true],
155
+ ["out", "DoVoidResponse", ["::SOAP::SOAPElement", "urn:ebay:api:PayPalAPI", "DoVoidResponse"], true] ],
156
+ { :request_style => :document, :request_use => :literal,
157
+ :response_style => :document, :response_use => :literal }
158
+ ],
159
+ [ nil,
160
+ "doAuthorization",
161
+ [ ["in", "DoAuthorizationRequest", ["::SOAP::SOAPElement", "urn:ebay:api:PayPalAPI", "DoAuthorizationReq"], true],
162
+ ["out", "DoAuthorizationResponse", ["::SOAP::SOAPElement", "urn:ebay:api:PayPalAPI", "DoAuthorizationResponse"], true] ],
163
+ { :request_style => :document, :request_use => :literal,
164
+ :response_style => :document, :response_use => :literal }
165
+ ]
166
+ ]
167
+
168
+ def initialize(endpoint_url = nil)
169
+ endpoint_url ||= DefaultEndpointUrl
170
+ super(endpoint_url, nil)
171
+ self.mapping_registry = MappingRegistry
172
+ init_methods
173
+ end
174
+
175
+ private
176
+
177
+ def init_methods
178
+ Methods.each do |definitions|
179
+ opt = definitions.last
180
+ if opt[:request_style] == :document
181
+ add_document_operation(*definitions)
182
+ else
183
+ add_rpc_operation(*definitions)
184
+ qname = definitions[0]
185
+ name = definitions[2]
186
+ if qname.name != name and qname.name.capitalize == name.capitalize
187
+ ::SOAP::Mapping.define_singleton_method(self, qname.name) do |*arg|
188
+ __send__(name, *arg)
189
+ end
190
+ end
191
+ end
192
+ end
193
+ end
194
+ end
195
+
@@ -0,0 +1,425 @@
1
+ #Ruby on Rails PayPal Plugin v1.0
2
+ #Copyright (C) 2006 Andre Price, ELC Technologies
3
+
4
+ #This library is free software; you can redistribute it and/or
5
+ #modify it under the terms of the GNU Lesser General Public
6
+ #License as published by the Free Software Foundation; either
7
+ #version 2.1 of the License, or (at your option) any later version.
8
+ #
9
+ #This library is distributed in the hope that it will be useful,
10
+ #but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
+ #Lesser General Public License for more details.
13
+ #
14
+ #You should have received a copy of the GNU Lesser General Public
15
+ #License along with this library; if not, write to the Free Software
16
+ #Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17
+
18
+ # PayPal Plugin-
19
+ # ----------------------
20
+ # This plugin implements a directcharge method accessed with Paypal.directcharge(params).
21
+ # It currently has 3 functions
22
+ # * directcharge
23
+ # * directauth
24
+ # * capture
25
+
26
+ require File.join(File.dirname(__FILE__),'defaultDriver.rb')
27
+ require 'soap/header/simplehandler'
28
+ require 'pp'
29
+ # Requester CredentialsHander == internal class used to communicate with paypal api
30
+ class RequesterCredentialsHandler < SOAP::Header::SimpleHandler
31
+ HeaderName = XSD::QName.new('urn:ebay:api:PayPalAPI', 'RequesterCredentials')
32
+ CredentialsName = XSD::QName.new('urn:ebay:apis:eBLBaseComponents', 'Credentials')
33
+ UsernameName = XSD::QName.new(nil, 'Username')
34
+ PasswordName = XSD::QName.new(nil, 'Password')
35
+ SubjectName = XSD::QName.new(nil, 'Subject')
36
+
37
+ def initialize(username, password, subject)
38
+ super(HeaderName)
39
+ @username, @password, @subject = username, password, subject
40
+ end
41
+
42
+ def on_simple_outbound
43
+ {CredentialsName => {UsernameName => @username, PasswordName => @password, SubjectName => @subject}}
44
+ end
45
+ end
46
+
47
+ module PayPalBusiness
48
+ class API
49
+ attr_accessor :cert_file
50
+ attr_accessor :key_file
51
+ attr_accessor :username
52
+ attr_accessor :password
53
+
54
+ def initialize(credentials, live=false)
55
+ credentials = YAML::load(ERB.new(File.read(credentials)).result).to_options if credentials.is_a?(String)
56
+
57
+ @cert_file = credentials[:cert_file]
58
+ @key_file = credentials[:key_file]
59
+ @username = credentials[:username]
60
+ @password = credentials[:password]
61
+ @live = live
62
+ end
63
+
64
+ def live?
65
+ @live
66
+ end
67
+
68
+ =begin rdocdd
69
+ Create the paypal client to communicate with the paypal web services api.
70
+ Internaly used function
71
+ =end
72
+ def create_aa_client
73
+ if(self.live?)
74
+ endpoint_url='https://api.paypal.com/2.0/'
75
+ end
76
+ client = PayPalAPIAAInterface.new(endpoint_url)
77
+ client.options['protocol.http.ssl_config.verify_mode'] = OpenSSL::SSL::VERIFY_NONE # used to be VERIFY_PEER
78
+ client.options['protocol.http.ssl_config.ca_file'] = File.dirname(__FILE__) + '/api_cert_chain.crt'
79
+
80
+ client.options['protocol.http.ssl_config.client_cert'] = @cert_file
81
+ client.options['protocol.http.ssl_config.client_key'] = @key_file
82
+ client.headerhandler << RequesterCredentialsHandler.new(@username, @password, '')
83
+
84
+ # run ruby with -d to see SOAP wiredumps.
85
+ client.wiredump_dev = STDERR if $DEBUG
86
+ client
87
+ end
88
+
89
+ def create_client
90
+ if(self.live?)
91
+ endpoint_url='https://api.paypal.com/2.0/'
92
+ end
93
+ client = PayPalAPIInterface.new(endpoint_url)
94
+ client.options['protocol.http.ssl_config.verify_mode'] = OpenSSL::SSL::VERIFY_NONE # used to be VERIFY_PEER
95
+ client.options['protocol.http.ssl_config.ca_file'] = File.dirname(__FILE__) + '/api_cert_chain.crt'
96
+
97
+ client.options['protocol.http.ssl_config.client_cert'] = @cert_file
98
+ client.options['protocol.http.ssl_config.client_key'] = @key_file
99
+ client.headerhandler << RequesterCredentialsHandler.new(@username, @password, '')
100
+
101
+ # run ruby with -d to see SOAP wiredumps.
102
+ client.wiredump_dev = STDERR if $DEBUG
103
+ client
104
+ end
105
+
106
+ =begin rdoc
107
+ Function: directcharge
108
+ This function performs a doDirectCharge action. Transfering funds from the parameter's account to your paypal merchant account.
109
+
110
+ The return value of this function is either "Success" or "Failure" depending on validity of input.
111
+
112
+ The directcharge method takes a hash argument (params).
113
+ The method expects the hash to be in the form of:
114
+ :firstName => "xxxx",
115
+ :lastName => "xxxxxx",
116
+ :ip => "127.0.0.1",
117
+ :amount => "10,000",
118
+ :itemName => "Item",
119
+ :addressName => "Home",
120
+ :street1 => "<STREET 1 of BILLING ADDRESS",
121
+ :street2 => "<STREET 2 OF Billing address (optional)",
122
+ :cityName => "<City name of billing address>",
123
+ :postalCode => "<Postal code for billing address>",
124
+ :stateOrProvince => "<State of billing address>",
125
+ :country => "<Country Code for billing address (ex US)",
126
+ :creditCardType => "<Credit Card type (Visa, MasterCard, Amex, Discover)>",
127
+ :creditCardNumber => "<Buyers credit card number>",
128
+ :cVV2 => "<Authorization number (last 3 digits on back of ccard>"
129
+ :expMonth => 7,//example
130
+ :expYear => 2999 //example
131
+ =end
132
+ def directcharge(params)
133
+ client = create_aa_client
134
+
135
+ directreq = DoDirectPaymentReq.new
136
+ drtype = DoDirectPaymentRequestType.new
137
+ directreq.doDirectPaymentRequest = drtype
138
+ drtype.version = "2.0"
139
+ dtype = DoDirectPaymentRequestDetailsType.new
140
+ dtype.iPAddress = params[:ip]
141
+
142
+ # JS HACK:
143
+ # currencyID attribute does NOT get set correctly by the webservices stub
144
+ # solution: insert attribute into the soap message
145
+ ns = "urn:ebay:apis:eBLBaseComponents"
146
+ otqname = XSD::QName.new(ns, "OrderTotal")
147
+ otele = SOAP::SOAPElement.new(otqname, "#{params[:amount]}")
148
+ otele.extraattr["currencyID"] = "USD"
149
+ bamount = otele
150
+
151
+
152
+ #logger.debug( "Amount in Paypal: #{params[:amount]}")
153
+ pdetails = PaymentDetailsType.new
154
+ #bamount = BasicAmountType.new("#{params[:amount]}")
155
+ #bamount.xmlattr_currencyID='USD'#currentlyonlysupports USD
156
+
157
+ pdetails.orderTotal = bamount
158
+ dtype.paymentDetails = pdetails
159
+
160
+ drtype.doDirectPaymentRequestDetails = dtype
161
+ dcard = CreditCardDetailsType.new
162
+ dtype.paymentAction = "Sale"
163
+ payerinfo = PayerInfoType.new
164
+ addr = AddressType.new
165
+ addr.name = params[:addressName]
166
+ addr.street1 = params[:street1]
167
+ addr.street2 = params[:street2]
168
+ addr.cityName = params[:cityName]
169
+ addr.postalCode = params[:postalCode]
170
+ addr.stateOrProvince = params[:stateOrProvince]
171
+ addr.country = params[:country]
172
+ dcard.creditCardType = case params[:creditCardType].downcase
173
+ when 'mastercard'
174
+ 'MasterCard'
175
+ when 'discover'
176
+ 'Discover'
177
+ when 'amex', 'americanexpress', 'american express'
178
+ 'Amex'
179
+ else
180
+ 'Visa'
181
+ end
182
+ dcard.creditCardNumber = params[:creditCardNumber]
183
+ if (params[:cVV2]==nil)
184
+ dcard.cVV2 = "000"
185
+ else
186
+ dcard.cVV2 = params[:cVV2]
187
+ end
188
+ dcard.expMonth = params[:expMonth]
189
+ dcard.expYear = params[:expYear]
190
+ pname = PersonNameType.new
191
+ pname.firstName = params[:firstName]
192
+ pname.lastName = params[:lastName]
193
+ payerinfo.payerName = pname
194
+ paydetailssitem = PaymentDetailsItemType.new
195
+ paydetailssitem.name = params[:itemName]
196
+ dtype.creditCard = dcard
197
+ dcard.cardOwner = payerinfo
198
+ payerinfo.address = addr
199
+ pdetails.shipToAddress = addr
200
+ res = client.doDirectPayment(directreq)
201
+ puts res.ack if $DEBUG
202
+ #res.ack
203
+ return res
204
+ end
205
+
206
+ =begin rdoc
207
+ Function: directauth
208
+ This function performs a directcharge in authorization mode. This means no actual funds are
209
+ transfered upon comletetion of this action.
210
+
211
+ The return value of this function is either "Failure" or the transactionID for the authorization.
212
+ The transactionID is needed if you intend to capture the funds authroized by this function.
213
+ The method expects the param hash to be in the form of :
214
+ :firstName => "<FIRST_NAME_VALUE>",
215
+ :lastName => "<LAST_NAME_VALUE>",
216
+ :ip => "<BUYERS_IP_ADDRESS",
217
+ :amount => "<AMOUNT_TO_CHARGE>", //This value has a maximum of 10,000.00
218
+ :itemName => "<ITEM_THAT_IS_BEING_SOLD",
219
+ :addressName => "Billing",
220
+ :street1 => "<FIRST LINE OF STREET ADDRESS>",
221
+ :street2 => "<SECOND LINE OF STREET ADDRESS>",
222
+ :cityName => "<CITY OF BILLING ADDRESS OF CARD>",
223
+ :postalCode => "<ZIPCODE OF BILLING ADDRESS OF CARD>",
224
+ :stateOrProvince => "<STATECODE OF BILLING ADDRESS>",
225
+ :country => "<COUNTRY CODE OF BILLING ADDRESS>",
226
+ :creditCardType => "Visa or MasterCard or Amex or Discover",
227
+ :creditCardNumber => "<VALID CREDIT CARD NUMBER",
228
+ :cVV2 => "<CVV2 AUTHORIZATION NUMBER FROM CARD"
229
+ :expMonth => 1 , //Expiration month in numerical form not string
230
+ :expYear => 2006 ///Expiration year in numerical form
231
+
232
+ =end
233
+ def directauth(params)
234
+ client = create_aa_client
235
+
236
+ directreq = DoDirectPaymentReq.new
237
+ drtype = DoDirectPaymentRequestType.new
238
+ directreq.doDirectPaymentRequest = drtype
239
+ drtype.version = "2.0"
240
+ dtype = DoDirectPaymentRequestDetailsType.new
241
+ dtype.iPAddress = params[:ip]
242
+
243
+ # JS HACK:
244
+ ns = "urn:ebay:apis:eBLBaseComponents"
245
+ otqname = XSD::QName.new(ns, "OrderTotal")
246
+ otele = SOAP::SOAPElement.new(otqname, "#{params[:amount]}")
247
+ otele.extraattr["currencyID"] = "USD"
248
+ bamount = otele
249
+
250
+
251
+ #logger.debug( "Amount in Paypal: #{params[:amount]}")
252
+ pdetails = PaymentDetailsType.new
253
+ #bamount = BasicAmountType.new("#{params[:amount]}")
254
+ #bamount.xmlattr_currencyID='USD'#currentlyonlysupports USD
255
+
256
+ pdetails.orderTotal = bamount
257
+ dtype.paymentDetails = pdetails
258
+
259
+ drtype.doDirectPaymentRequestDetails = dtype
260
+ dcard = CreditCardDetailsType.new
261
+ dtype.paymentAction = "Authorization"
262
+ payerinfo = PayerInfoType.new
263
+ addr = AddressType.new
264
+ addr.name = params[:addressName]
265
+ addr.street1 = params[:street1]
266
+ addr.street2 = params[:street2]
267
+ addr.cityName = params[:cityName]
268
+ addr.postalCode = params[:postalCode]
269
+ addr.stateOrProvince = params[:stateOrProvince]
270
+ addr.country = params[:country]
271
+ dcard.creditCardType = case params[:creditCardType].downcase
272
+ when 'mastercard'
273
+ 'MasterCard'
274
+ when 'discover'
275
+ 'Discover'
276
+ when 'amex', 'americanexpress', 'american express'
277
+ 'Amex'
278
+ else
279
+ 'Visa'
280
+ end
281
+ dcard.creditCardNumber = params[:creditCardNumber]
282
+ if (params[:cVV2]==nil)
283
+ dcard.cVV2 = "000"
284
+ else
285
+ dcard.cVV2 = params[:cVV2]
286
+ end
287
+ dcard.expMonth = params[:expMonth]
288
+ dcard.expYear = params[:expYear]
289
+ pname = PersonNameType.new
290
+ pname.firstName = params[:firstName]
291
+ pname.lastName = params[:lastName]
292
+ payerinfo.payerName = pname
293
+ paydetailssitem = PaymentDetailsItemType.new
294
+ paydetailssitem.name = params[:itemName]
295
+ dtype.creditCard = dcard
296
+ dcard.cardOwner = payerinfo
297
+ payerinfo.address = addr
298
+ pdetails.shipToAddress = addr
299
+ res = client.doDirectPayment(directreq)
300
+ puts res.ack if $DEBUG
301
+ #res.ack
302
+ return res
303
+ end
304
+
305
+ =begin rdoc
306
+ Function: capture
307
+
308
+ This funcion performs a doCapture. It captures or collects the charge from a previously authorized
309
+ transaction. This transaction is referenced by the transactionID returned from directauth.
310
+
311
+ The return value of this function is either "Success" or "Failure"
312
+
313
+ The parameter hash for this function is to be in the following form:
314
+ :transactionID => "<RETURN FROM PREVIOUS DIRECT AUTH>"
315
+ :amount = > "<AMOUNT TO CHARGE>" //should be less than or equal to
316
+ //auth's amount
317
+ **WARNING*** This function is currently not functioning correctly. It throws NoMethodErrors even when successfuly making a capture.
318
+ Even when running in debug mode with wiredumps the responses from paypal are have Ack values of Success. Indicating an error in the parsing of the response outside this plugin.
319
+ =end
320
+ def capture(params)
321
+ client = create_aa_client
322
+ capturetype = DoCaptureRequestType.new
323
+ capturetype.version = "2.0"
324
+ capturereq = DoCaptureReq.new
325
+ capturereq.doCaptureRequest = capturetype
326
+ capturetype.authorizationID = params[:transactionID]
327
+
328
+ #bamount = AmountType.new(params[:amount])
329
+ #bamount.xmlattr_currencyID="USD"
330
+ # bamount = "<Amount currencyID=\"USD\">#{params[:amount]}</Amount>"
331
+
332
+ # JS HACK:
333
+ ns = "urn:ebay:apis:eBLBaseComponents"
334
+ otqname = XSD::QName.new(nil, "Amount")
335
+ otele = SOAP::SOAPElement.new(otqname, "#{params[:amount]}")
336
+ otele.extraattr["currencyID"] = "USD"
337
+ bamount = otele
338
+
339
+ capturetype.amount = bamount
340
+ capturetype.completeType = "Complete"
341
+ #begin
342
+ res = client.doCapture(capturereq)
343
+ #puts res.paymentStatus
344
+ #return res.ack
345
+ return res
346
+ #rescue NoMethodError
347
+ # puts "Error Caught, Status lost\n"
348
+ # return "Failure"
349
+ #end
350
+ end
351
+
352
+ def authorize(params)
353
+ client = create_aa_client
354
+ authreqtype = DoAuthorizationRequestType.new
355
+ authreqtype.version = "2.0"
356
+ authreq = DoAuthorizationReq.new
357
+ authreq.doAuthorizationRequest = authreqtype
358
+ authreqtype.transactionID = params[:transactionID]
359
+ bamount = BasicAmountType.new(params[:amount])
360
+ bamount.xmlattr_currencyID='USD'
361
+ authreqtype.amount = bamount
362
+ res = client.doAuthorization(authreq)
363
+ return res
364
+ end
365
+
366
+ def reauthorize(params)
367
+ client = create_aa_client
368
+ reauthreqtype = DoReauthorizationRequestType.new
369
+ reauthreqtype.version = "2.0"
370
+ reauthreq = DoReauthorizationReq.new
371
+ reauthreq.doReauthorizationRequest = reauthreqtype
372
+ reauthreqtype.authorizationID = params[:transactionID]
373
+ bamount = BasicAmountType.new(params[:amount])
374
+ bamount.xmlattr_currencyID='USD'
375
+ reauthreqtype.amount = bamount
376
+ res = client.doReauthorization(reauthreq)
377
+ return res
378
+ end
379
+
380
+ def void(params)
381
+ client = create_aa_client
382
+ voidreqtype = DoVoidRequestType.new
383
+ voidreqtype.version = "2.0"
384
+ voidreq = DoVoidReq.new
385
+ voidreq.doVoidRequest = voidreqtype
386
+ voidreqtype.authorizationID = params[:transactionID]
387
+ res = client.doVoid(voidreq)
388
+ return res
389
+ end
390
+
391
+ def mass_pay(params)
392
+ raise "Expected parameter 'items' to Paypal.mass_pay as an array of hashes with keys :email, :amount, :note (optional)" if params[:items].nil?
393
+ raise "Expected more than 0 and less than 251 items to Paypal.mass_pay" if params[:items].size < 1 || params[:items].size > 250
394
+ client = create_client
395
+ massreqtype = MassPayRequestType.new
396
+ massreqtype.version = "2.0"
397
+ massreqtype.emailSubject = params[:email_subject] if params[:email_subject]
398
+ massreqtype.receiverType = "EmailAddress"
399
+ massreq = MassPayReq.new
400
+ massreq.massPayRequest = massreqtype
401
+ massitems = []
402
+ for i in params[:items]
403
+ raise "Failed to process item #{i.inspect}, because it did not contain an email and/or amount" if i[:amount].nil? || i[:email].nil?
404
+ it = MassPayRequestItemType.new
405
+ it.receiverEmail = i[:email]
406
+ #amt = BasicAmountType.new(i[:amount])
407
+
408
+ # JS HACK:
409
+ # currencyID attribute does NOT get set correctly by the webservices stub
410
+ # solution: insert attribute into the soap message
411
+ ns = "urn:ebay:api:PayPalAPI"
412
+ otqname = XSD::QName.new(ns, "Amount")
413
+ otele = SOAP::SOAPElement.new(otqname, "#{i[:amount]}")
414
+ otele.extraattr["currencyID"] = "USD"
415
+
416
+ it.amount = otele
417
+ it.note = i[:note] if i[:note]
418
+ massitems << it
419
+ end
420
+ massreqtype.massPayItem = massitems
421
+ res = client.massPay(massreq)
422
+ return res
423
+ end
424
+ end
425
+ end