payline_sdk 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,50 @@
1
+ require 'active_support/all'
2
+ require "payline_sdk/version"
3
+ require "nokogiri"
4
+ require "yaml"
5
+
6
+ require "payline_sdk/configuration"
7
+ require "payline_sdk/object/base"
8
+ require "payline_sdk/client"
9
+ require "payline_sdk/request"
10
+
11
+ module PaylineSDK
12
+
13
+ class << self
14
+ attr_accessor :configuration
15
+ end
16
+
17
+ def self.configure
18
+ self.configuration ||= Configuration.new
19
+ self.define_objects
20
+ yield(configuration)
21
+ end
22
+
23
+ def self.root
24
+ File.expand_path '../..', __FILE__
25
+ end
26
+
27
+ private
28
+
29
+ def self.define_objects
30
+ PaylineSDK::Configuration::API[:objects].keys.each do |key|
31
+ attributes = PaylineSDK::Configuration::API[:objects][key][:attributes].map {|attr| attr[:name]}
32
+ klass = Class.new(PaylineSDK::Object::Base) do
33
+ attributes.each do |attribute|
34
+ define_method attribute.to_sym do
35
+ instance_variable_get("@#{attribute}")
36
+ end
37
+ define_method "#{attribute}=".to_sym do |arg|
38
+ instance_variable_set("@#{attribute}", arg)
39
+ end
40
+ end
41
+
42
+ define_method :to_h do
43
+ Hash[attributes.zip(attributes.map {|attr| eval("@#{attr}")})]
44
+ end
45
+ end
46
+ PaylineSDK::Object.const_set(key.to_s.camelize, klass)
47
+ end
48
+ end
49
+
50
+ end
@@ -0,0 +1,86 @@
1
+ module PaylineSDK
2
+ class Client
3
+
4
+ PAYLINE_NAMESPACE = 'http://obj.ws.payline.experian.com'.freeze
5
+ PROD_ENDPOINT = 'https://services.payline.com/V4/services/'.freeze
6
+ HOMO_ENDPOINT = 'https://homologation.payline.com/V4/services/'.freeze
7
+
8
+ DIRECT_API = 'DirectPaymentAPI'.freeze
9
+ EXTENDED_API = 'ExtendedAPI'.freeze
10
+ WEB_API = 'WebPaymentAPI'.freeze
11
+
12
+ attr_accessor :web_client, :direct_client, :extended_client, :attributes
13
+
14
+ def initialize
15
+ @env = PaylineSDK.configuration.environment
16
+ init_clients(PaylineSDK.configuration.file, PaylineSDK.configuration.merchant_id, PaylineSDK.configuration.access_key)
17
+ end
18
+
19
+ PaylineSDK::Configuration::API[:apis].keys.each do |api|
20
+ PaylineSDK::Configuration::API[:apis][api][:methods].each do |method|
21
+ define_method(method[:name]) do |request|
22
+ attrs = method[:attributes].map {|attr| attr[:name]}
23
+ # ap __method__
24
+ # ap attrs
25
+ response = get_client_for_method(method[:name]).call(__method__) do
26
+ message(request.to_requestable_hash(attrs))
27
+ end
28
+ clean_response(response.body[("#{__method__.to_s}_response").to_sym])
29
+ end
30
+ end
31
+ end
32
+
33
+ protected
34
+
35
+ def clean_response(hash)
36
+ hash.each do |k,v|
37
+ if v.is_a?(Hash)
38
+ v = clean_response(v)
39
+ next
40
+ end
41
+ if k == :@xmlns
42
+ hash.delete(k)
43
+ end
44
+ end
45
+ end
46
+
47
+ def init_clients(file, merchant_id, access_key, opts = {})
48
+ env = @env
49
+ endpoint = if @env == :test
50
+ HOMO_ENDPOINT
51
+ elsif @env == :production
52
+ PROD_ENDPOINT
53
+ end
54
+
55
+ ['web', 'direct', 'extended'].each do |type|
56
+ client = Savon.client do
57
+ basic_auth merchant_id, access_key
58
+ wsdl(file)
59
+ pretty_print_xml(true) if env == :test
60
+ namespace(PAYLINE_NAMESPACE)
61
+ endpoint(endpoint + eval("#{type.upcase}_API"))
62
+ end
63
+ instance_variable_set("@#{type}_client", client)
64
+ end
65
+ end
66
+
67
+ def get_client_for_method(method_name)
68
+ PaylineSDK::Configuration::API[:apis].keys.each do |api|
69
+ PaylineSDK::Configuration::API[:apis][api][:methods].each do |method|
70
+ if method[:name] == method_name
71
+ case api
72
+ when :web_payment_api
73
+ return @web_client
74
+ when :direct_payment_api
75
+ return @direct_client
76
+ when :extended_api
77
+ return @extended_client
78
+ end
79
+ end
80
+ end
81
+ end
82
+ return nil
83
+ end
84
+
85
+ end
86
+ end
@@ -0,0 +1,20 @@
1
+ module PaylineSDK
2
+ class Configuration
3
+
4
+ API = YAML.load(File.read([(File.expand_path '../../..', __FILE__), 'files', 'settings.yml'].join('/'))).freeze
5
+
6
+ attr_accessor :file, :merchant_id, :access_key, :environment
7
+
8
+ def initialize
9
+ @file = [PaylineSDK.root, 'files', 'payline.wsdl'].join('/')
10
+ end
11
+
12
+ def self.api_methods
13
+ API[:apis].keys.each do |api|
14
+ return API[:apis][api][:methods].map {|method| method[:name]}
15
+ end
16
+ return []
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,21 @@
1
+ module PaylineSDK
2
+ module Object
3
+ class Base
4
+
5
+ def initialize(opts = {})
6
+ set_attributes(opts)
7
+ end
8
+
9
+ def set_attributes(attributes)
10
+ attributes.each do |k,v|
11
+ instance_variable_set("@#{k}", v)
12
+ end
13
+ end
14
+
15
+ def to_h
16
+ {}
17
+ end
18
+
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,56 @@
1
+ module PaylineSDK
2
+ class Request
3
+
4
+ def initialize(opts = {})
5
+ define_accessors
6
+ set_attributes(opts)
7
+ self.version = 3
8
+ end
9
+
10
+ def set_attributes(attributes)
11
+ attributes.each do |k,v|
12
+ instance_variable_set("@#{k}", v)
13
+ end
14
+ end
15
+
16
+ ## Recursive method to transform the following objects to Hash (Search in depth):
17
+ ## - PaylineSDK::Object::Base
18
+ ## - Un objet definissant la methode :to_payline_sdk_obj
19
+ def to_requestable_attribute(obj)
20
+ if obj.is_a?(Hash)
21
+ obj.each do |k,v|
22
+ obj[k] = to_requestable_attribute(v)
23
+ end
24
+ elsif obj.is_a?(PaylineSDK::Object::Base)
25
+ obj = to_requestable_attribute(obj.to_h)
26
+ elsif obj.class.method_defined?(:to_payline_sdk_obj)
27
+ obj = to_requestable_attribute(obj.to_payline_sdk_obj.to_h)
28
+ end
29
+ return obj
30
+ end
31
+
32
+ ## Transform current Request object to hash
33
+ def to_requestable_hash(allowed_keys)
34
+ Hash[allowed_keys.zip(allowed_keys.map {|key| to_requestable_attribute(eval("@#{key.to_s}"))})]
35
+ end
36
+
37
+ private
38
+
39
+ def define_accessors
40
+ get_attributes_from_api.each do |method|
41
+ self.class.send(:attr_accessor, method)
42
+ end
43
+ end
44
+
45
+ def get_attributes_from_api
46
+ attributes = []
47
+ PaylineSDK::Configuration::API[:apis].keys.each do |api|
48
+ PaylineSDK::Configuration::API[:apis][api][:methods].each do |method|
49
+ method[:attributes].each {|attribute| attributes << attribute[:name]}
50
+ end
51
+ end
52
+ return attributes.uniq!
53
+ end
54
+
55
+ end
56
+ end
@@ -0,0 +1,3 @@
1
+ module PaylineSDK
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'payline_sdk/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "payline_sdk"
8
+ spec.version = PaylineSDK::VERSION
9
+ spec.authors = ["Sébastien Loyer"]
10
+ spec.email = ["loyer.sebastien@gmail.com"]
11
+ spec.summary = %q{SDK for Payline API}
12
+ spec.description = %q{}
13
+ spec.homepage = "https://github.com/Sbastien/PaylineSDK"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency 'savon', '~> 2.3.0'
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.7"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: payline_sdk
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Sébastien Loyer
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-09-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: savon
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 2.3.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 2.3.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ description: ''
56
+ email:
57
+ - loyer.sebastien@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - files/payline.wsdl
68
+ - files/settings.yml
69
+ - lib/payline_sdk.rb
70
+ - lib/payline_sdk/client.rb
71
+ - lib/payline_sdk/configuration.rb
72
+ - lib/payline_sdk/object/base.rb
73
+ - lib/payline_sdk/request.rb
74
+ - lib/payline_sdk/version.rb
75
+ - payline_sdk.gemspec
76
+ homepage: https://github.com/Sbastien/PaylineSDK
77
+ licenses:
78
+ - MIT
79
+ metadata: {}
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.2.2
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: SDK for Payline API
100
+ test_files: []
101
+ has_rdoc: