Class: Worldpay
- Inherits:
-
Object
- Object
- Worldpay
- Defined in:
- lib/worldpay.rb
Instance Method Summary collapse
- #authorise3DSOrder(orderCode, responseCode, threeDsInfo) ⇒ Object
- #authorize3DSOrder(orderCode, responseCode, threeDsInfo) ⇒ Object
-
#cancelAuthorisedOrder(orderCode = false) ⇒ Object
Cancel Authorized Worldpay Order.
- #cancelAuthorizedOrder(orderCode = false) ⇒ Object
-
#captureAuthorisedOrder(orderCode = false, amount = false) ⇒ Object
Capture Authorized Worldpay Order.
- #captureAuthorizedOrder(orderCode = false, amount = false) ⇒ Object
-
#checkOrderInput(order) ⇒ Object
ORDERS.
- #createAPMOrder(order = {}) ⇒ Object
- #createOrder(order = {}) ⇒ Object
-
#disableSSLCheck(disable = false) ⇒ Object
Disable SSL Check ~ Use only for testing!.
- #getOrder(orderCode = false) ⇒ Object
-
#getStoredCardDetails(token = false) ⇒ Object
TOKENS Get card details from Worldpay token.
-
#handleResponse(response) ⇒ Object
RESPONSES.
-
#initialize(service_key = '', timeout = 3, raise_errors = false) ⇒ Worldpay
constructor
A new instance of Worldpay.
-
#onError(error, message = false, code = nil, httpStatusCode = nil, description = nil, customCode = nil) ⇒ Object
ERRORS.
-
#refundOrder(orderCode = false, amount = false) ⇒ Object
Refund Worldpay order.
-
#sendRequest(action = '', json = false, expectResponse = false, method = 'POST') ⇒ Object
REQUESTS.
- #setEndpoint(endpoint) ⇒ Object
-
#setTimeout(timeout = 3) ⇒ Object
Set timeout.
Constructor Details
#initialize(service_key = '', timeout = 3, raise_errors = false) ⇒ Worldpay
Returns a new instance of Worldpay.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/worldpay.rb', line 25 def initialize(service_key='', timeout=3, raise_errors=false) if service_key.empty? raise "Please set a Service Key" else @service_key = service_key @timeout = timeout @disable_ssl = false @endpoint = 'https://api.worldpay.com' @errors = { 'ip' => 'Invalid parameters', 'cine' => 'php_curl was not found', 'to' => 'Request timed out', 'nf' => 'Not found', 'apierror' => 'API Error', 'uanv' => 'Worldpay is currently unavailable, please try again later', 'contact' => 'Error contacting Worldpay, please try again later', 'ssl' => 'You must enable SSL check in production mode', 'verify' => 'Worldpay not verifying SSL connection', 'orderInput' => { 'token' => 'No token found', 'orderCode' => 'No order_code entered', 'orderDescription' => 'No order_description found', 'amount' => 'No amount found, or it is not a whole number', 'currencyCode' => 'No currency_code found', 'name' => 'No name found', 'billingAddress' => 'No billing_address found' }, 'notificationPost' => 'Notification Error: Not a post', 'notificationUnknown' => 'Notification Error: Cannot be processed', 'capture' => { 'ordercode' => 'No order code entered' }, 'refund' => { 'ordercode' => 'No order code entered' }, 'json' => 'JSON could not be decoded', 'key' => 'Please enter your service key', 'sslerror' => 'Worldpay SSL certificate could not be validated' } @raise_errors = raise_errors end end |
Instance Method Details
#authorise3DSOrder(orderCode, responseCode, threeDsInfo) ⇒ Object
248 249 250 |
# File 'lib/worldpay.rb', line 248 def (orderCode, responseCode, threeDsInfo) (orderCode, responseCode, threeDsInfo) end |
#authorize3DSOrder(orderCode, responseCode, threeDsInfo) ⇒ Object
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 |
# File 'lib/worldpay.rb', line 251 def (orderCode, responseCode, threeDsInfo) request = { 'threeDSResponseCode' => responseCode, 'shopperSessionId' => threeDsInfo['shopperSessionId'], 'shopperAcceptHeader' => threeDsInfo['shopperAcceptHeader'], 'shopperUserAgent' => threeDsInfo['shopperUserAgent'], 'shopperIpAddress' => threeDsInfo['shopperIpAddress'] } response = sendRequest('orders/'+orderCode, request.to_json, true, 'PUT') if (response['body']['orderCode']) #success return response else return onError('apierror', response.to_s) end end |
#cancelAuthorisedOrder(orderCode = false) ⇒ Object
Cancel Authorized Worldpay Order
289 290 291 |
# File 'lib/worldpay.rb', line 289 def cancelAuthorisedOrder(orderCode=false) cancelAuthorizedOrder(orderCode) end |
#cancelAuthorizedOrder(orderCode = false) ⇒ Object
292 293 294 295 296 297 298 299 300 |
# File 'lib/worldpay.rb', line 292 def cancelAuthorizedOrder(orderCode=false) if (orderCode || orderCode.is_a?(String)) # else return onError('ip', @errors['capture']['ordercode']) end sendRequest('orders/'+orderCode, false, false, 'DELETE') end |
#captureAuthorisedOrder(orderCode = false, amount = false) ⇒ Object
Capture Authorized Worldpay Order
271 272 273 |
# File 'lib/worldpay.rb', line 271 def captureAuthorisedOrder(orderCode=false, amount=false) captureAuthorizedOrder(orderCode, amount) end |
#captureAuthorizedOrder(orderCode = false, amount = false) ⇒ Object
274 275 276 277 278 279 280 281 282 283 284 285 286 |
# File 'lib/worldpay.rb', line 274 def captureAuthorizedOrder(orderCode=false, amount=false) if (orderCode || orderCode.is_a?(String)) # else return onError('ip', @errors['capture']['ordercode']) end if (amount && amount.is_a?(Integer)) json = {'captureAmount'=>amount}.to_json end sendRequest('orders/'+orderCode+'/capture', (json||false)) end |
#checkOrderInput(order) ⇒ Object
ORDERS
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/worldpay.rb', line 105 def checkOrderInput(order) errors2 = [] if (!order.any?) return onError('ip') end if (order['token']==nil) errors2 << @errors['orderInput']['token'] end if (order['orderDescription']==nil) errors2 << @errors['orderInput']['orderDescription'] end if (order['amount']==nil) errors2 << @errors['orderInput']['amount'] end if (order['currencyCode']==nil) errors2 << @errors['orderInput']['currencyCode'] end if (order['name']==nil) errors2 << @errors['orderInput']['name'] end if (order['billingAddress']==nil) errors2 << @errors['orderInput']['billingAddress'] end if (errors2.length > 0) return onError('ip', errors2.join(', ')) end end |
#createAPMOrder(order = {}) ⇒ Object
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/worldpay.rb', line 200 def createAPMOrder(order={}) self.checkOrderInput(order) defaults = { 'deliveryAddress' => nil, 'billingAddress' => nil, 'successUrl' => nil, 'pendingUrl' => nil, 'failureUrl' => nil, 'cancelUrl' => nil } order = defaults.merge(order) fmt = "%05.2f" % order['amount'] cost_cents = fmt.split('.').join('') request = { 'token' => order['token'], 'orderDescription' => order['orderDescription'], 'amount' => cost_cents, 'currencyCode' => order['currencyCode'], 'name' => order['name'], 'shopperEmailAddress' => order['shopperEmailAddress'], 'billingAddress' => order['billingAddress'], 'deliveryAddress' => order['deliveryAddress'], 'customerOrderCode' => order['customerOrderCode'], 'successUrl' => order['successUrl'], 'pendingUrl' => order['pendingUrl'], 'failureUrl' => order['failureUrl'], 'cancelUrl' => order['cancelUrl'], 'statementNarrative' => order['statementNarrative'], 'settlementCurrency' => order['settlementCurrency'], 'customerIdentifiers' => order['customerIdentifiers'].present? ? JSON.parse(order['customerIdentifiers']) : nil, } response = sendRequest('orders', request.to_json, true) if (response['body']['orderCode']) #success return response else return onError('apierror', response.to_s) end end |
#createOrder(order = {}) ⇒ Object
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/worldpay.rb', line 137 def createOrder(order={}) self.checkOrderInput(order) defaults = { 'orderType' => 'ECOM', #Order Type: ECOM/MOTO/RECURRING/APM 'customerIdentifiers' => nil, 'billingAddress' => nil, 'deliveryAddress' => nil } order = defaults.merge(order) if (order['amount']!="") order['amount'] = order['amount'].to_i >0 ? order['amount'] : -1 fmt = "%05.2f" % order['amount'] cost_cents = fmt.split('.').join('') else cost_cents = order['amount'] end request = { 'token' => order['token'], 'orderDescription' => order['orderDescription'], 'amount' => cost_cents, 'is3DSOrder' => order['3DS'], 'currencyCode' => order['currencyCode'], 'siteCode' => order['siteCode'], 'settlementCurrency' => order['settlementCurrency'], 'name' => order['name'], 'shopperEmailAddress' => order['shopperEmailAddress'] ? order['shopperEmailAddress'] : "", 'orderType' => ['ECOM','MOTO','RECURRING','APM'].include?(order['orderType']) ? order['orderType'] : 'ECOM', 'authorizeOnly' => (!!order['authorizeOnly'] || !!order['authoriseOnly']) || false, 'billingAddress' => order['billingAddress'], 'deliveryAddress' => order['deliveryAddress'], 'customerOrderCode' => order['customerOrderCode'], 'customerIdentifiers' => order['customerIdentifiers'].present? ? JSON.parse(order['customerIdentifiers']) : nil, 'statementNarrative' => order['statementNarrative'], 'orderCodePrefix' => order['orderCodePrefix'], 'orderCodeSuffix' => order['orderCodeSuffix'] } if(request['is3DSOrder'] == true) threeDsInfo = { 'shopperIpAddress' => order['shopperIpAddress'], 'shopperSessionId' => order['shopperSessionId'], 'shopperUserAgent' => order['shopperUserAgent'], 'shopperAcceptHeader' => order['shopperAcceptHeader'] } request.merge!(threeDsInfo) end response = sendRequest('orders', request.to_json, true) if (response['body'] && response['body']['orderCode']) #success return response else return {'response'=> response, 'error'=> onError('apierror', response.to_s)} end end |
#disableSSLCheck(disable = false) ⇒ Object
Disable SSL Check ~ Use only for testing!
75 76 77 78 |
# File 'lib/worldpay.rb', line 75 def disableSSLCheck(disable = false) @disable_ssl = disable end |
#getOrder(orderCode = false) ⇒ Object
416 417 418 419 420 421 422 423 424 425 426 427 428 |
# File 'lib/worldpay.rb', line 416 def getOrder(orderCode = false) if (orderCode==false || !orderCode.present?) return onError('ip', @errors['orderInput']['orderCode']); end response = sendRequest('orders/' + orderCode, false, true, 'GET'); if (!response['body']['orderCode'].present?) return onError('apierror') end return response end |
#getStoredCardDetails(token = false) ⇒ Object
TOKENS Get card details from Worldpay token
401 402 403 404 405 406 407 408 409 410 411 412 413 |
# File 'lib/worldpay.rb', line 401 def getStoredCardDetails(token=false) if (token==false || token!=token.to_s) return onError('ip', @errors['orderInput']['token']) end response = sendRequest('tokens/'+token, false, true, 'GET') if (response['body']['paymentMethod']) return response['body']['paymentMethod'] else return false end end |
#handleResponse(response) ⇒ Object
RESPONSES
395 396 397 |
# File 'lib/worldpay.rb', line 395 def handleResponse(response) response = response.to_json end |
#onError(error, message = false, code = nil, httpStatusCode = nil, description = nil, customCode = nil) ⇒ Object
ERRORS
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/worldpay.rb', line 86 def onError(error, =false, code=nil, httpStatusCode=nil, description=nil, customCode=nil) if (@raise_errors) raise .to_s else if (code!=nil && httpStatusCode!=nil && description!=nil && customCode!=nil) return { 'message'=>, 'code'=>code, 'httpStatusCode'=>httpStatusCode, 'description'=>description, 'customCode'=>customCode } end return .to_s end end |
#refundOrder(orderCode = false, amount = false) ⇒ Object
Refund Worldpay order
303 304 305 306 307 308 309 310 311 312 313 |
# File 'lib/worldpay.rb', line 303 def refundOrder(orderCode=false, amount=false) if (orderCode==false || orderCode!=orderCode.to_s) return onError('ip', @errors['refund']['ordercode']) end if (amount!=false) json = {'refundAmount'=>amount}.to_json else json = false end sendRequest('orders/'+orderCode+'/refund', json) end |
#sendRequest(action = '', json = false, expectResponse = false, method = 'POST') ⇒ Object
REQUESTS
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 |
# File 'lib/worldpay.rb', line 316 def sendRequest(action='', json=false, expectResponse=false, method='POST') uri = URI.parse(@endpoint) net = Net::HTTP.new(uri.host, uri.port) if (method=="POST") request = Net::HTTP::Post.new('/v1/'+action) elsif (method=="GET") request = Net::HTTP::Get.new('/v1/'+action) elsif (method=="PUT") request = Net::HTTP::Put.new('/v1/'+action) elsif (method=="DELETE") request = Net::HTTP::Delete.new('/v1/'+action) end net.use_ssl = true net.verify_mode = @disable_ssl ? OpenSSL::SSL::VERIFY_NONE : OpenSSL::SSL::VERIFY_PEER if (json != false) request.body = json end request.add_field("Authorization", @service_key) request.add_field("Content-Type", "application/json") request.add_field("X-wp-client-user-agent", "os.name="+RUBY_PLATFORM.to_s+";os.version="+RUBY_PLATFORM.to_s+";os.arch="+RUBY_PLATFORM.to_s+";lang.version="+RUBY_VERSION.to_s+";lib.version="+@version.to_s+";api.version=v1;lang=am;owner=worldpay") if (json != false) request.add_field("Content-Length", json.length) end net.set_debug_output $stdout #useful to see the raw messages going over the wire net.read_timeout = @timeout net.open_timeout = @timeout result = net.start do |http| http.request(request) end # raise request.body.to_s if (result.read_body && result.read_body.length >= 2) response = JSON.parse(result.read_body) else response = false end if (expectResponse && !response.present?) return onError('uanv', @errors['json'], 503); end if (response && response["httpStatusCode"].present?) if (response["httpStatusCode"] != '200') return onError( false, response["message"], result.code, response['httpStatusCode'], response['description'], response['customCode'] ) end elsif (expectResponse && result.code != '200') # If we expect a result and we have an error return onError('uanv', @errors['json'], 503) elsif (!expectResponse) if (result.code != '200') return onError('apierror', result.read_body, result.code) else response = true end end return { "code"=>result.code, "body"=>response } end |
#setEndpoint(endpoint) ⇒ Object
70 71 72 |
# File 'lib/worldpay.rb', line 70 def setEndpoint(endpoint) @endpoint = endpoint end |
#setTimeout(timeout = 3) ⇒ Object
Set timeout
81 82 83 |
# File 'lib/worldpay.rb', line 81 def setTimeout(timeout = 3) @timeout = timeout end |