diff --git a/src/client.coffee b/src/client.coffee index 4242f8b..869d5cd 100644 --- a/src/client.coffee +++ b/src/client.coffee @@ -19,7 +19,7 @@ module.exports = class Client constructor: (provider_key, default_host = "su1.3scale.net") -> unless provider_key? - throw "missing provider_key" + throw new Error("missing provider_key") @provider_key = provider_key @host = default_host @@ -47,7 +47,8 @@ module.exports = class Client authorize: (options, callback) -> _self = this result = null - if (typeof options isnt 'object') && (options.app_id == null) + + if (typeof options isnt 'object') || (options.app_id is undefined) throw "missing app_id" url = "/transactions/authorize.xml?" @@ -74,8 +75,176 @@ module.exports = class Client throw "[Client::authorize] Server Error Code: #{response.statusCode}" request.end() + ### + OAuthorize an Application + Parameters: + options is a Hash object with the following fields + app_id Required + service_id Optional (In case of mmultiple services) + callback {Function} Is the callback function that receives the Response object which includes `is_success` + method to determine the status of the response + + Example: + client.oauth_authorize {app_id: '75165984', (response) -> + if response.is_success + # All Ok + else + sys.puts "#{response.error_message} with code: #{response.error_code}" + + ### + oauth_authorize: (options, callback) -> + _self = this + if (typeof options isnt 'object')|| (options.app_id is undefined) + throw "missing app_id" + + url = "/transactions/oauth_authorize.xml?" + query = querystring.stringify options + query += '&' + querystring.stringify {provider_key: @provider_key} + + req_opts = + host: @host + port: 443 + path: url + query + method: 'GET' + request = https.request req_opts, (response) -> + response.setEncoding 'utf8' + xml = "" + response.on 'data', (chunk) -> + xml += chunk + + response.on 'end', -> + if response.statusCode == 200 || response.statusCode == 409 + callback _self._build_success_authorize_response xml + else if response.statusCode in [400...409] + callback _self._build_error_response xml + else + throw "[Client::oauth_authorize] Server Error Code: #{response.statusCode}" + request.end() + + ### + Authorize with user_key + Parameters: + options is a Hash object with the following fields + user_key Required + service_id Optional (In case of mmultiple services) + callback {Function} Is the callback function that receives the Response object which includes `is_success` + method to determine the status of the response + + Example: + client.authorize_with_user_key {user_key: '123456', (response) -> + if response.is_success + # All Ok + else + sys.puts "#{response.error_message} with code: #{response.error_code}" + + ### + authorize_with_user_key: (options, callback) -> + _self = this + + if (typeof options isnt 'object') || (options.user_key is undefined) + throw "missing user_key" + + url = "/transactions/authorize.xml?" + query = querystring.stringify options + query += '&' + querystring.stringify {provider_key: @provider_key} + + req_opts = + host: @host + port: 443 + path: url + query + method: 'GET' + request = https.request req_opts, (response) -> + response.setEncoding 'utf8' + xml = "" + response.on 'data', (chunk) -> + xml += chunk + + response.on 'end', -> + if response.statusCode == 200 || response.statusCode == 409 + callback _self._build_success_authorize_response xml + else if response.statusCode in [400...409] + callback _self._build_error_response xml + else + throw "[Client::authorize_with_user_key] Server Error Code: #{response.statusCode}" + request.end() + + ### + Authorize and Report in single call + options is a Hash object with the following fields + app_id Required + app_key, user_id, object, usage, no-body, service_id Optional + callback {Function} Is the callback function that receives the Response object which includes `is_success` + method to determine the status of the response + + Example: + client.authrep {app_id: '75165984', (response) -> + if response.is_success + # All Ok + else + sys.puts "#{response.error_message} with code: #{response.error_code}" + + ### + authrep: (options, callback) -> + _self = this + if (typeof options isnt 'object') || (options.app_id is undefined) + throw "missing app_id" + + url = "/transactions/authrep.xml?" + query = querystring.stringify options + query += '&' + querystring.stringify {provider_key: @provider_key} + + req_opts = + host: @host + port: 443 + path: url + query + method: 'GET' + request = https.request req_opts, (response) -> + response.setEncoding 'utf8' + xml = "" + response.on 'data', (chunk) -> + xml += chunk + + response.on 'end', -> + if response.statusCode == 200 || response.statusCode == 409 + callback _self._build_success_authorize_response xml + else if response.statusCode in [400...409] + callback _self._build_error_response xml + else + throw "[Client::authrep] Server Error Code: #{response.statusCode}" + request.end() + + ### + Authorize and Report with :user_key + ### + authrep_with_user_key: (options, callback) -> + _self = this + if (typeof options isnt 'object') || (options.user_key is undefined) + throw "missing user_key" + + url = "/transactions/authrep.xml?" + query = querystring.stringify options + query += '&' + querystring.stringify {provider_key: @provider_key} + req_opts = + host: @host + port: 443 + path: url + query + method: 'GET' + request = https.request req_opts, (response) -> + response.setEncoding 'utf8' + xml = "" + response.on 'data', (chunk) -> + xml += chunk + + response.on 'end', -> + if response.statusCode == 200 || response.statusCode == 409 + callback _self._build_success_authorize_response xml + else if response.statusCode in [400...409] + callback _self._build_error_response xml + else + throw "[Client::authrep_with_user_key] Server Error Code: #{response.statusCode}" + request.end() ### Report transaction(s). diff --git a/test/client_test.coffee b/test/client_test.coffee index c6f7103..680fd38 100644 --- a/test/client_test.coffee +++ b/test/client_test.coffee @@ -1,14 +1,37 @@ ### - Temporal vars, to make the request, that have been remove form de final relase - TODO: REMOVE the vars of the information about the service + Temporal vars, to make the request, that have been remove form de final relase + TODO: REMOVE the vars of the information about the service +### + +### + Default 3scale Keys + Supports basic authentication ### provider_key = '05273bcb282d1d4faafffeb01e224db0' application_key = '3e05c797ef193fee452b1ddf19defa74' -application_id = '75165984' +application_id = '75165984' + +### + Senico Keys + Use these keys for User_Key Login +### +senico_provider_key = '9ac46ea91a02dc8c35bb84d62837addd' +senico_application_key = '8dc7bdce6573eb9cc4b57a8924b02d99' +senico_application_id = '9f45d21e' +senico_user_key = '3d63ef791d0c439878f0a85b45f3a4aa' +### + Use these for oAuth +### +oauth_provider_key = '9bf4a46c63f6bad158e5056ac9a37036' +oauth_application_key = '721ca43c021a4fb3995b4d9bbd3243ae' +oauth_application_id = '23a06147' +### + End Senico Keys +### trans = [ - { "app_id": application_id, "usage": {"hits": 1}}, - { "app_id": application_id, "usage": {"hits": 1000}} + { "app_id": application_id, "usage": {"hits": 1}}, + { "app_id": application_id, "usage": {"hits": 1000}} ] report_test = {transactions: trans, provider_key: provider_key} @@ -22,33 +45,71 @@ Client = require('../src/client') vows .describe("Basic test for the 3Scale::Client") .addBatch - 'A client Should': + 'A client ': topic: -> Client - 'to be throw a exception if init without provider_key': (Client) -> + 'should throw an exception if init without provider_key': (Client) -> assert.throws(`function(){ new Client()}`, "missing provider_key") return - 'have a default host': (Client) -> + 'should have an default host': (Client) -> client = new Client(123) assert.equal client.host, "su1.3scale.net" return - 'to be can change the default host': (Client) -> + 'can change the default host': (Client) -> client = new Client(123, 'example.com') assert.equal client.host, "example.com" return - 'to be have a authorize method': (Client) -> + 'should have an authorize method': (Client) -> client = new Client(provider_key) assert.isFunction client.authorize return - 'to be throw a exception if authorize method is called without :app_id': (Client) -> + 'should throw an exception if authorize method is called without :app_id': (Client) -> + client = new Client(provider_key) + assert.throws (() -> client.authorize({}, () ->)), "missing app_id" + return + + 'should have an oauth_authorize method': (Client) -> + client = new Client(provider_key) + assert.isFunction client.oauth_authorize + return + + 'should throw an exception if oauth_authorize method is called without :app_id': (Client) -> + client = new Client(provider_key) + assert.throws (() -> client.oauth_authorize({}, () ->)), "missing app_id" + return + + 'should have an authorize_with_user_key method': (Client) -> + client = new Client(provider_key) + assert.isFunction client.authorize_with_user_key + return + + 'should throw an exception if authorize_with_user_key is called without :user_key': (Client) -> + client = new Client(provider_key) + assert.throws (() -> client.authorize_with_user_key({}, () ->)), "missing user_key" + return + + 'should have an authrep method': (Client) -> + client = new Client(provider_key) + assert.isFunction client.authrep + return + + 'should throw an exception if authrep called without :app_id': (Client) -> client = new Client(provider_key) - assert.throws client.authorize({}, ()->), "missing app_id" + assert.throws (() -> client.authrep({}, () ->)), "missing app_id" return - 'In the authorize method should': + 'should have an authrep_with_user_key method': (Client) -> + client = new Client(provider_key) + assert.isFunction client.authrep_with_user_key + + 'ahould throw an exception if authrep_with_user_key is called without :user_key': (Client) -> + client = new Client(provider_key) + assert.throws (() -> client.authrep_with_user_key({}, ()->)), 'missing user_key' + + 'The authorize method should': topic: -> promise = new events.EventEmitter client = new Client provider_key @@ -63,7 +124,37 @@ vows 'call the callback with the AuthorizeResponse': (response) -> assert.isTrue response.is_success() - 'The Event Emitter': + 'The oauth_authorize method should': + topic: -> + promise = new events.EventEmitter + client = new Client oauth_provider_key + client.oauth_authorize {app_id: oauth_application_id}, (response) -> + if response.is_success + promise.emit 'success', response + else + promise.else 'error', response + + promise + + 'call the callback with the AuthorizeResponse': (response) -> + assert.isTrue response.is_success() + + 'The authorize_with_user_key method should': + topic: -> + promise = new events.EventEmitter + client = new Client senico_provider_key + client.authorize_with_user_key {user_key: senico_user_key}, (response) -> + if response.is_success + promise.emit 'success', response + else + promise.else 'error', response + + promise + + 'call the callback with the AuthorizeResponse': (response) -> + assert.isTrue response.is_success() + + 'The Event Emitter': topic: -> promise = new events.EventEmitter client = new Client provider_key @@ -85,5 +176,5 @@ vows promise 'give a success response with the correct params': (response) -> - assert.isTrue response.is_success() + assert.isTrue response.is_success() .export module