puntopagos 0.0.2 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -2,3 +2,4 @@
2
2
  .bundle
3
3
  Gemfile.lock
4
4
  pkg/*
5
+ config/puntopagos.yml
@@ -0,0 +1,14 @@
1
+ require 'base64'
2
+ require 'openssl'
3
+
4
+ module PuntoPagos
5
+ class Authorization
6
+ def initialize env = nil
7
+ @@config ||= PuntoPagos::Config.new(env)
8
+ end
9
+
10
+ def sign(string)
11
+ "PP "+@@config.puntopagos_key+":"+ Base64.encode64(OpenSSL::HMAC.digest('sha1',@@config.puntopagos_secret, string)).chomp
12
+ end
13
+ end
14
+ end
@@ -3,13 +3,13 @@ require 'yaml'
3
3
  module PuntoPagos
4
4
  class Config
5
5
  PUNTOPAGOS_BASE_URL = {
6
- :production => "https://www.puntopagos.com",
7
- :sandbox => "https://sandbox.puntopagos.com"
6
+ :production => "https://www.puntopagos.com/",
7
+ :sandbox => "https://sandbox.puntopagos.com/"
8
8
  }
9
9
 
10
10
  attr_accessor :config_filepath, :puntopagos_base_url, :puntopagos_key, :puntopagos_secret
11
11
 
12
- def initialize(env = nil, config_override = nil)
12
+ def initialize env = nil, config_override = nil
13
13
  if env
14
14
  # For non-rails apps
15
15
  @config_filepath = File.join(File.dirname(__FILE__), "..", "..", "config", "puntopagos.yml")
@@ -0,0 +1,32 @@
1
+ require 'json'
2
+ require 'rest_client'
3
+
4
+ module PuntoPagos
5
+ class Executioner
6
+ def initialize env = nil
7
+ @@config ||= PuntoPagos::Config.new(env)
8
+ @@puntopagos_base_url ||= @@config.puntopagos_base_url
9
+ end
10
+
11
+ def call_api data, path, method, signature, timestamp
12
+ #hack fix: JSON.unparse doesn't work in Rails 2.3.5; only {}.to_json does..
13
+ headers = set_headers(signature, timestamp)
14
+ api_request_data = JSON.unparse(data) rescue data.to_json
15
+ resp = RestClient.method(method).call(@@puntopagos_base_url+path, data.to_json, headers)
16
+ JSON.parse(resp)
17
+ end
18
+
19
+ private
20
+
21
+ def set_headers signature, timestamp
22
+ headers = {
23
+ 'User-Agent' => "puntopagos-ruby-#{PuntoPagos::VERSION}",
24
+ 'Accept' => 'application/json',
25
+ 'Accept-Charset' => 'utf-8',
26
+ 'Content-Type' => 'application/json; charset=utf-8',
27
+ 'Fecha' => timestamp,
28
+ 'Autorizacion' => signature
29
+ }
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,22 @@
1
+ module PuntoPagos
2
+ class Notification
3
+ def initialize env = nil
4
+ @env = env
5
+ @@config ||= PuntoPagos::Config.new(@env)
6
+ @@function = "transaccion/notificar"
7
+ end
8
+
9
+ def correct? token, trx_id, amount, timestamp, pp_signature
10
+ message = create_message token, trx_id, amount, timestamp
11
+ authorization = Authorization.new(@env)
12
+ signature = authorization.sign(message)
13
+ signature == pp_signature
14
+ end
15
+
16
+ private
17
+
18
+ def create_message token, trx_id, amount, timestamp
19
+ @@function + "\n" + token + "\n" + trx_id + "\n" + amount + "\n" + timestamp
20
+ end
21
+ end
22
+ end
@@ -5,59 +5,55 @@ require 'rest_client'
5
5
 
6
6
 
7
7
  module PuntoPagos
8
-
8
+
9
9
  class NoDataError < Exception
10
10
  end
11
-
11
+
12
12
  class Request
13
-
14
- def initialize(env = nil)
15
- @env = env
16
- @@config ||= PuntoPagos::Config.new(@env)
17
- @@puntopagos_base_url ||= @@config.puntopagos_base_url
18
- end
19
-
20
- def validate
21
- #TODO validate JSON must have monto and trx_id
22
- end
23
-
24
- def create(data)
25
- raise NoDataError unless data
26
- get_headers("transaccion/crear",data)
27
- response_data = call_api(data, "/TransactionService/crear", :post)
28
- PuntoPagos::Response.new(response_data, @env)
29
- end
30
-
31
-
13
+ def initialize env = nil
14
+ @env = env
15
+ @@config ||= PuntoPagos::Config.new(@env)
16
+ @@puntopagos_base_url ||= @@config.puntopagos_base_url
17
+ @@function = "transaccion/crear"
18
+ end
19
+
20
+ def validate
21
+ #TODO validate JSON must have monto and trx_id
22
+ end
23
+
24
+ def create trx_id, amount, payment_type = nil
25
+ raise NoDataError unless trx_id and amount
26
+ data = create_data trx_id, amount, payment_type
27
+
28
+ timestamp = get_timestamp
29
+
30
+ message = create_message(data['trx_id'], data['monto'], timestamp)
31
+ authorization = PuntoPagos::Authorization.new(@env)
32
+ signature = authorization.sign(message)
33
+ executioner = PuntoPagos::Executioner.new(@env)
34
+
35
+ response_data = executioner.call_api(data, @@function, :post, signature, timestamp)
36
+ PuntoPagos::Response.new(response_data, @env)
37
+ end
38
+
32
39
 
33
40
  private
34
41
 
35
- def get_headers(function, data)
36
- raise NoDataError unless data
37
-
38
- timestamp = Time.now.strftime("%a, %d %b %Y %H:%M:%S GMT")
39
- message = function+"\n"+data['trx_id'].to_s+"\n"+data['monto'].to_s+"\n"+timestamp
40
- signature = "PP "+@@config.puntopagos_key+":"+sign(message).chomp!
41
- @@headers = {
42
- 'User-Agent' => "puntopagos-ruby-#{PuntoPagos::VERSION}",
43
- 'Accept' => 'application/json',
44
- 'Accept-Charset' => 'utf-8',
45
- 'Content-Type' => 'application/json; charset=utf-8',
46
- 'Fecha' => timestamp,
47
- 'Autorizacion' => signature
48
- }
49
- end
50
-
51
- def call_api(data, path, method)
52
- #hack fix: JSON.unparse doesn't work in Rails 2.3.5; only {}.to_json does..
53
- api_request_data = JSON.unparse(data) rescue data.to_json
54
- resp = RestClient.method(method).call(@@puntopagos_base_url+path, data.to_json, @@headers)
55
-
56
- JSON.parse(resp)
57
- end
58
-
59
- def sign(string)
60
- Base64.encode64(OpenSSL::HMAC.digest('sha1',@@config.puntopagos_secret, string))
61
- end
42
+ def create_message trx_id, amount, timestamp
43
+ @@function + "\n" + trx_id + "\n" + amount + "\n" + timestamp
44
+ end
45
+
46
+ def create_data trx_id, amount, payment_type = nil
47
+ data = {}
48
+ data['trx_id'] = trx_id
49
+ data['monto'] = amount
50
+ data['medio_pago'] = payment_type if payment_type
51
+ data
62
52
  end
53
+
54
+ def get_timestamp
55
+ Time.now.strftime("%a, %d %b %Y %H:%M:%S GMT")
56
+ end
57
+
58
+ end
63
59
  end
@@ -4,25 +4,25 @@ module PuntoPagos
4
4
  @@config ||= PuntoPagos::Config.new(env)
5
5
  @@puntopagos_base_url ||= @@config.puntopagos_base_url
6
6
  @@response = response
7
-
7
+
8
8
  end
9
-
9
+
10
10
  # TODO validate JSON
11
11
  def success?
12
12
  @@response["respuesta"] == "00"
13
13
  end
14
-
14
+
15
15
  def get_token
16
16
  @@response["token"]
17
17
  end
18
-
18
+
19
19
  def get_error
20
20
  @@response["error"]
21
21
  end
22
-
22
+
23
23
  def payment_process_url
24
24
  @@puntopagos_base_url + "/transaccion/procesar/"+get_token
25
25
  end
26
-
26
+
27
27
  end
28
28
  end
@@ -1,3 +1,3 @@
1
1
  module PuntoPagos
2
- VERSION = "0.0.2"
2
+ VERSION = "0.1.1"
3
3
  end
data/lib/puntopagos.rb CHANGED
@@ -1,4 +1,6 @@
1
1
  require File.join(File.dirname(__FILE__),"puntopagos/config")
2
+ require File.join(File.dirname(__FILE__),"puntopagos/authorization")
3
+ require File.join(File.dirname(__FILE__),"puntopagos/executioner")
2
4
  require File.join(File.dirname(__FILE__),"puntopagos/request")
3
5
  require File.join(File.dirname(__FILE__),"puntopagos/response")
4
6
  require File.join(File.dirname(__FILE__),"puntopagos/version")
@@ -2,36 +2,37 @@ require 'test_helper'
2
2
 
3
3
 
4
4
  class CreateRequestTest < Test::Unit::TestCase
5
-
6
- def setup
7
- @req = PuntoPagos::Request.new('test')
5
+
6
+ def setup
7
+ @req = PuntoPagos::Request.new('production')
8
8
  end
9
-
9
+
10
10
  def test_valid_create
11
11
  puts "-------"
12
12
  puts "webpay valid"
13
-
14
- data = {
13
+
14
+ data = {
15
15
  "trx_id" => "#{Time.now.to_i}",
16
- "monto" => "100.00"
17
- }
18
- resp = @req.create(data)
19
-
20
- assert resp.success? == true
21
-
16
+ "monto" => "100.00",
17
+ "medio_pago" => "3"
18
+ }
19
+ resp = @req.create("#{Time.now.to_i}", "100.00")
20
+ puts "RESP: #{resp.success?}"
21
+ assert resp.success? == true, "Pass"
22
+
22
23
  # payload = YAML.load_file(File.join(File.dirname(__FILE__),"..", "data","webpay_paylaod.yml"))
23
24
  end
24
-
25
+
25
26
  def test_invalid_webpay_pay
26
27
  puts "webpay invalid"
27
-
28
- data = {
28
+
29
+ data = {
29
30
  'trx_id' => "#{Time.now.to_i}"
30
- }
31
- resp = @req.create(data)
32
-
33
- assert resp.success? == false, "Pass"
34
-
31
+ }
32
+ resp = @req.create("#{Time.now.to_i}","10")
33
+
34
+ assert resp.success? == false
35
+
35
36
  end
36
37
 
37
38
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: puntopagos
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -14,7 +14,7 @@ date: 2012-06-15 00:00:00.000000000Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rest-client
17
- requirement: &2162551120 !ruby/object:Gem::Requirement
17
+ requirement: &2156966000 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ~>
@@ -22,7 +22,7 @@ dependencies:
22
22
  version: 1.6.7
23
23
  type: :runtime
24
24
  prerelease: false
25
- version_requirements: *2162551120
25
+ version_requirements: *2156966000
26
26
  description: Ruby wrapper for PuntoPagos Payment API
27
27
  email:
28
28
  - imella@acid.cl
@@ -35,10 +35,12 @@ files:
35
35
  - LICENSE
36
36
  - README.textile
37
37
  - Rakefile
38
- - config/puntopagos.yml
39
38
  - init.rb
40
39
  - lib/puntopagos.rb
40
+ - lib/puntopagos/authorization.rb
41
41
  - lib/puntopagos/config.rb
42
+ - lib/puntopagos/executioner.rb
43
+ - lib/puntopagos/notification.rb
42
44
  - lib/puntopagos/request.rb
43
45
  - lib/puntopagos/response.rb
44
46
  - lib/puntopagos/version.rb
@@ -1,4 +0,0 @@
1
- test:
2
- environment: "sandbox"
3
- puntopagos_key: "YOUR_API_KEY"
4
- puntopagos_secret: "YOUR_API_SECRET"