dotpay 0.0.1

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,44 @@
1
+ = dotpay.pl gem
2
+
3
+ {<img src="https://travis-ci.org/friendlyfashion/dotpay.png" />}[https://travis-ci.org/friendlyfashion/dotpay]
4
+
5
+ == Description
6
+
7
+ dotpay.pl gem provides tools for working with http://dotpay.pl online payments system
8
+
9
+ == How to use
10
+
11
+ Start by configuring Dotpay:
12
+
13
+ Dotpay.configure do |config|
14
+ config.account_id = 1234
15
+ config.pin = 'your URLC pin here'
16
+ end
17
+
18
+ == Authorizing URLC callback
19
+
20
+ Sample URLC callback handling action.
21
+
22
+ def callback
23
+ response = Dotpay::Response.new(params)
24
+
25
+ if response.authorized?
26
+ if response.status_done?
27
+ # process your order
28
+ end
29
+
30
+ render :text => 'OK', :layout => false
31
+ else
32
+ render :text => 'ERROR', :layout => false
33
+ end
34
+ end
35
+
36
+ == Cancelling transaction using cancel API
37
+
38
+ You can cancel existing transaction:
39
+
40
+ Dotpay.cancel_transaction(t_id, amount, control)
41
+
42
+ Also you can do partial refund:
43
+
44
+ Dotpay.cancel_transaction(t_id, amount, control, type: 2)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env rake
2
+ require 'bundler'
3
+ require 'rspec/core/rake_task'
4
+
5
+ Bundler::GemHelper.install_tasks
6
+ RSpec::Core::RakeTask.new
7
+
8
+ task :default => :spec
@@ -0,0 +1,50 @@
1
+ require "dotpay/version"
2
+ require "dotpay/configuration"
3
+ require "dotpay/error"
4
+ require "dotpay/response"
5
+ require "dotpay/client"
6
+ require "dotpay/cancel_request"
7
+
8
+ module Dotpay
9
+ class << self
10
+ attr_writer :configuration
11
+
12
+ ##
13
+ # Used to configure Dotpay.
14
+ #
15
+ # = Example
16
+ #
17
+ # Dotpay.configure do |config|
18
+ # config.account_id = 0000
19
+ # config.pin = 'aaaabbbbccccdddd'
20
+ # config.cancel_login = 'cancelapilogin'
21
+ # config.cancel_password = 'cancelapipassword'
22
+ # end
23
+ def configure
24
+ yield(configuration)
25
+ end
26
+
27
+ ##
28
+ # The configuration object.
29
+ # Recommended to use Dotpay.configure
30
+ def configuration
31
+ @configuration ||= Configuration.new
32
+ end
33
+
34
+ # Cancel transaction
35
+ def cancel_transaction(t_id, amount, control, options = {})
36
+ cancel_request = CancelRequest.new(t_id, amount, control, options)
37
+
38
+ client.cancel_transaction(cancel_request)
39
+ end
40
+
41
+ private
42
+
43
+ ##
44
+ # Dotpay Client.
45
+ def client
46
+ @client ||= Client.new(configuration)
47
+ end
48
+
49
+ end
50
+ end
@@ -0,0 +1,42 @@
1
+ require 'digest/md5'
2
+
3
+ module Dotpay
4
+ class CancelRequest
5
+ attr_reader :t_id, :amount, :control, :type
6
+
7
+ TYPE_FULL = 1
8
+ TYPE_PARTIAL = 2
9
+
10
+ def initialize(t_id, amount, control, options = {})
11
+ options = {
12
+ type: TYPE_FULL
13
+ }.merge(options)
14
+
15
+ @t_id = t_id
16
+ @amount = "%0.2f" % amount
17
+ @control = control
18
+ @type = options[:type]
19
+ end
20
+
21
+ def checksum
22
+ calculate_checksum
23
+ end
24
+
25
+ private
26
+
27
+ def calculate_checksum
28
+ data = [ Dotpay.configuration.cancel_login,
29
+ Dotpay.configuration.pin,
30
+ type,
31
+ Dotpay.configuration.account_id,
32
+ t_id,
33
+ amount,
34
+ control ]
35
+
36
+ data_string = data.join(':')
37
+
38
+ Digest::MD5.hexdigest(data_string)
39
+ end
40
+
41
+ end
42
+ end
@@ -0,0 +1,44 @@
1
+ require 'uri'
2
+ require 'net/http'
3
+ require 'net/https'
4
+
5
+ module Dotpay
6
+ class Client
7
+ def initialize(configuration)
8
+ @configuration = configuration
9
+ end
10
+
11
+ def cancel_transaction(cancel_request)
12
+ params = {
13
+ id: @configuration.account_id,
14
+ t_id: cancel_request.t_id,
15
+ control: cancel_request.control,
16
+ amount: cancel_request.amount,
17
+ type: cancel_request.type,
18
+ login: @configuration.cancel_login,
19
+ passwd: @configuration.cancel_password,
20
+ md5: cancel_request.checksum
21
+ }
22
+
23
+ response = post(@configuration.cancel_endpoint, params)
24
+
25
+ raise Error.new(response.body) unless response.body.strip == "OK"
26
+
27
+ return true
28
+ end
29
+
30
+ private
31
+
32
+ def post(url, params)
33
+ uri = URI.parse(url)
34
+
35
+ https = Net::HTTP.new(uri.host,uri.port)
36
+ https.use_ssl = true
37
+
38
+ request = Net::HTTP::Post.new(uri.request_uri)
39
+ request.set_form_data(params)
40
+
41
+ https.request(request)
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,54 @@
1
+ module Dotpay
2
+ class Configuration
3
+ # Account ID in Dotpay system, for which payment is made (Seller's account ID)
4
+ attr_accessor :account_id
5
+
6
+ # URLC PIN
7
+ # 16-character alphanumeric string defined in section: Settings → URLC parameters
8
+ attr_accessor :pin
9
+
10
+ # Language
11
+ #
12
+ # Defaults to :pl
13
+ #
14
+ # :pl → Polish
15
+ # :en → English
16
+ # :de → German
17
+ # :it → Italian
18
+ # :fr → French
19
+ # :es → Spanish
20
+ # :cz → Czech
21
+ # :ru → Russian
22
+ # :bg → Bulgarian
23
+ attr_accessor :language
24
+
25
+ # service hostname
26
+ attr_accessor :endpoint
27
+
28
+ # provider's email
29
+ attr_accessor :email
30
+
31
+ # Cancel API endpoint
32
+ attr_accessor :cancel_endpoint
33
+
34
+ # Cancel API login
35
+ attr_accessor :cancel_login
36
+
37
+ # Cancel API password
38
+ attr_accessor :cancel_password
39
+
40
+ def initialize
41
+ @language = :pl
42
+ @endpoint = 'https://ssl.dotpay.pl/'
43
+ @cancel_endpoint = 'https://ssl.dotpay.pl/api/cancel/'
44
+ end
45
+
46
+ ##
47
+ # Allows config options to be read like a hash
48
+ #
49
+ # option: Key for a given attribute
50
+ def [](option)
51
+ send(option)
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,4 @@
1
+ module Dotpay
2
+ class Error < ::StandardError
3
+ end
4
+ end
@@ -0,0 +1,72 @@
1
+ require 'digest/md5'
2
+
3
+ module Dotpay
4
+ class Response
5
+ CHECKSUM_KEYS = %w(id control t_id amount email service code username password t_status)
6
+
7
+ STATUS_NEW = 1
8
+ STATUS_DONE = 2
9
+ STATUS_REJECTED = 3
10
+ STATUS_REFUND = 4
11
+ STATUS_COMPLAINT = 5
12
+
13
+ attr_reader :params
14
+
15
+ def initialize(params)
16
+ @params = params
17
+ end
18
+
19
+ def t_id
20
+ params['t_id']
21
+ end
22
+
23
+ def control
24
+ params['control']
25
+ end
26
+
27
+ def amount
28
+ params['amount']
29
+ end
30
+
31
+ def authorized?
32
+ params['md5'] == calculate_checksum
33
+ end
34
+
35
+ def status
36
+ params['t_status'].to_i
37
+ end
38
+
39
+ def status_new?
40
+ status == STATUS_NEW
41
+ end
42
+
43
+ def status_done?
44
+ status == STATUS_DONE
45
+ end
46
+
47
+ def status_rejected?
48
+ status == STATUS_REJECTED
49
+ end
50
+
51
+ def status_refund?
52
+ status == STATUS_REFUND
53
+ end
54
+
55
+ def status_complaint?
56
+ status == STATUS_COMPLAINT
57
+ end
58
+
59
+ private
60
+
61
+ def calculate_checksum
62
+ data = [ Dotpay.configuration.pin ]
63
+
64
+ CHECKSUM_KEYS.each { |key| data << params[key] }
65
+
66
+ data_string = data.join(':')
67
+
68
+ Digest::MD5.hexdigest(data_string)
69
+ end
70
+
71
+ end
72
+ end
@@ -0,0 +1,3 @@
1
+ module Dotpay
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+
3
+ describe Dotpay::CancelRequest do
4
+ before :each do
5
+ Dotpay.configure do |config|
6
+ config.account_id = 1234
7
+ config.pin = 'testtesttesttest'
8
+ config.cancel_login = 'testlogin'
9
+ config.cancel_password = 'testpass'
10
+ end
11
+ end
12
+
13
+ describe ".new" do
14
+ let(:t_id) { "TST01" }
15
+ let(:amount) { "1.00" }
16
+ let(:control) { "ABCD" }
17
+
18
+ subject { Dotpay::CancelRequest.new(t_id, amount, control) }
19
+
20
+ its(:t_id) { should == t_id }
21
+ its(:amount) { should == amount }
22
+ its(:control) { should == control }
23
+ its(:type) { should == Dotpay::CancelRequest::TYPE_FULL }
24
+ its(:checksum) { should == "5f6b85d31fa19ee60db2e2bcc9c595f6" }
25
+ end
26
+ end
@@ -0,0 +1,69 @@
1
+ require 'spec_helper'
2
+
3
+ describe Dotpay do
4
+ before :each do
5
+ Dotpay.configure do |config|
6
+ config.account_id = 1234
7
+ config.pin = 'testtesttesttest'
8
+ config.cancel_login = 'testlogin'
9
+ config.cancel_password = 'testpass'
10
+ end
11
+ end
12
+
13
+ it "should have defaults" do
14
+ Dotpay.configuration.endpoint.should == 'https://ssl.dotpay.pl/'
15
+ Dotpay.configuration.cancel_endpoint.should == 'https://ssl.dotpay.pl/api/cancel/'
16
+ Dotpay.configuration.language.should == :pl
17
+ end
18
+
19
+ describe "#configure" do
20
+ it "should allow configuring" do
21
+ Dotpay.configuration.account_id.should == 1234
22
+ Dotpay.configuration.pin.should == 'testtesttesttest'
23
+ Dotpay.configuration.cancel_login.should == 'testlogin'
24
+ Dotpay.configuration.cancel_password.should == 'testpass'
25
+ end
26
+ end
27
+
28
+ describe "#cancel_transaction" do
29
+ subject { Dotpay.cancel_transaction("TX01", 1.00, 'ABC') }
30
+
31
+ context "successful" do
32
+ before do
33
+ stub_request(:post, "https://ssl.dotpay.pl/api/cancel/").
34
+ with(:body => {
35
+ "amount"=>"1.00",
36
+ "control"=>"ABC",
37
+ "id"=>"1234",
38
+ "login"=>"testlogin",
39
+ "md5"=>"e077b995f4e592ce5b9a428df2d009bf",
40
+ "passwd"=>"testpass",
41
+ "t_id"=>"TX01",
42
+ "type"=>"1"}
43
+ ).to_return(:status => 200, :body => "OK", :headers => {})
44
+ end
45
+
46
+ it { should be_true }
47
+ end
48
+
49
+ context "error" do
50
+ before do
51
+ stub_request(:post, "https://ssl.dotpay.pl/api/cancel/").
52
+ with(:body => {
53
+ "amount"=>"1.00",
54
+ "control"=>"ABC",
55
+ "id"=>"1234",
56
+ "login"=>"testlogin",
57
+ "md5"=>"e077b995f4e592ce5b9a428df2d009bf",
58
+ "passwd"=>"testpass",
59
+ "t_id"=>"TX01",
60
+ "type"=>"1"}
61
+ ).to_return(:status => 200, :body => "err01", :headers => {})
62
+ end
63
+
64
+ specify do
65
+ expect { subject }.to raise_error(Dotpay::Error)
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,137 @@
1
+ require 'spec_helper'
2
+
3
+ describe Dotpay::Response do
4
+ before :each do
5
+ Dotpay.configure do |config|
6
+ config.account_id = 1234
7
+ config.pin = 'testtesttesttest'
8
+ end
9
+ end
10
+
11
+ describe "#authorized?" do
12
+ let(:params) do
13
+ { "status"=>"FAIL",
14
+ "control"=>"5DA5L8F5",
15
+ "amount"=>"1.00",
16
+ "id"=>"66548",
17
+ "transaction_id"=>"66548-TST55",
18
+ "t_id"=>"66548-TST55",
19
+ "t_date"=>"2013-02-13 16:23:43",
20
+ "o_id"=>"66548-ZTST55",
21
+ "email"=>"test@email.com",
22
+ "t_status"=>"3",
23
+ "description"=>"Uzsakymo 5DA5L8F5 apmokejimas",
24
+ "version"=>"1.4",
25
+ "orginal_amount"=>"1.00 PLN",
26
+ "channel"=>"20",
27
+ "md5"=>"a4d146472f99f123d0960732dcfd4be8"}
28
+ end
29
+
30
+ let(:response) { Dotpay::Response.new(params) }
31
+
32
+ subject { response.authorized? }
33
+
34
+ it { should be_true }
35
+
36
+ context "invalid pin" do
37
+ before { Dotpay.configuration.stub(:pin).and_return('badpin') }
38
+ it { should be_false }
39
+ end
40
+ end
41
+
42
+ describe "#status_done?" do
43
+ let(:params) do
44
+ { "status"=>"OK",
45
+ "control"=>"5DA5L8F5",
46
+ "amount"=>"1.00",
47
+ "id"=>"66548",
48
+ "transaction_id"=>"66548-TST55",
49
+ "t_id"=>"66548-TST55",
50
+ "t_date"=>"2013-02-13 16:23:43",
51
+ "o_id"=>"66548-ZTST55",
52
+ "email"=>"test@email.com",
53
+ "t_status"=>"2",
54
+ "description"=>"Uzsakymo 5DA5L8F5 apmokejimas",
55
+ "version"=>"1.4",
56
+ "orginal_amount"=>"1.00 PLN",
57
+ "channel"=>"20",
58
+ "md5"=>"a4d146472f99f123d0960732dcfd4be8"}
59
+ end
60
+
61
+ let(:response) { Dotpay::Response.new(params) }
62
+
63
+ subject { response.status_done? }
64
+
65
+ it { should be_true }
66
+
67
+ context "not done" do
68
+ let(:params) do
69
+ { "status"=>"FAIL",
70
+ "control"=>"5DA5L8F5",
71
+ "amount"=>"1.00",
72
+ "id"=>"66548",
73
+ "transaction_id"=>"66548-TST55",
74
+ "t_id"=>"66548-TST55",
75
+ "t_date"=>"2013-02-13 16:23:43",
76
+ "o_id"=>"66548-ZTST55",
77
+ "email"=>"test@email.com",
78
+ "t_status"=>"3",
79
+ "description"=>"Uzsakymo 5DA5L8F5 apmokejimas",
80
+ "version"=>"1.4",
81
+ "orginal_amount"=>"1.00 PLN",
82
+ "channel"=>"20",
83
+ "md5"=>"a4d146472f99f123d0960732dcfd4be8"}
84
+ end
85
+
86
+ it { should be_false }
87
+ end
88
+
89
+ describe "#status_rejected?" do
90
+ let(:params) do
91
+ { "status"=>"FAIL",
92
+ "control"=>"5DA5L8F5",
93
+ "amount"=>"1.00",
94
+ "id"=>"66548",
95
+ "transaction_id"=>"66548-TST55",
96
+ "t_id"=>"66548-TST55",
97
+ "t_date"=>"2013-02-13 16:23:43",
98
+ "o_id"=>"66548-ZTST55",
99
+ "email"=>"test@email.com",
100
+ "t_status"=>"3",
101
+ "description"=>"Uzsakymo 5DA5L8F5 apmokejimas",
102
+ "version"=>"1.4",
103
+ "orginal_amount"=>"1.00 PLN",
104
+ "channel"=>"20",
105
+ "md5"=>"a4d146472f99f123d0960732dcfd4be8"}
106
+ end
107
+
108
+ let(:response) { Dotpay::Response.new(params) }
109
+
110
+ subject { response.status_rejected? }
111
+
112
+ it { should be_true }
113
+
114
+ context "not rejected" do
115
+ let(:params) do
116
+ { "status"=>"OK",
117
+ "control"=>"5DA5L8F5",
118
+ "amount"=>"1.00",
119
+ "id"=>"66548",
120
+ "transaction_id"=>"66548-TST55",
121
+ "t_id"=>"66548-TST55",
122
+ "t_date"=>"2013-02-13 16:23:43",
123
+ "o_id"=>"66548-ZTST55",
124
+ "email"=>"test@email.com",
125
+ "t_status"=>"2",
126
+ "description"=>"Uzsakymo 5DA5L8F5 apmokejimas",
127
+ "version"=>"1.4",
128
+ "orginal_amount"=>"1.00 PLN",
129
+ "channel"=>"20",
130
+ "md5"=>"a4d146472f99f123d0960732dcfd4be8"}
131
+ end
132
+
133
+ it { should be_false }
134
+ end
135
+ end
136
+ end
137
+ end
@@ -0,0 +1,15 @@
1
+ if ENV['COVERAGE']
2
+ require 'simplecov'
3
+ require 'simplecov-rcov'
4
+ SimpleCov.formatter = SimpleCov::Formatter::RcovFormatter
5
+ SimpleCov.start
6
+ end
7
+
8
+ require 'rubygems'
9
+ require 'bundler/setup'
10
+ require 'dotpay'
11
+ require 'webmock/rspec'
12
+
13
+ RSpec.configure do |config|
14
+ config.order = "random"
15
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dotpay
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Laurynas Butkus
9
+ - Mindaugas Mozuras
10
+ - Tomas Varaneckas
11
+ - Andrius Janauskas
12
+ autorequire:
13
+ bindir: bin
14
+ cert_chain: []
15
+ date: 2013-02-21 00:00:00.000000000 Z
16
+ dependencies: []
17
+ description: Online payments using dotpay.pl
18
+ email:
19
+ - laurynas.butkus@gmail.com
20
+ - mindaugas.mozuras@gmail.com
21
+ - tomas.varaneckas@gmail.com
22
+ - andrius.janauskas@gmail.com
23
+ executables: []
24
+ extensions: []
25
+ extra_rdoc_files: []
26
+ files:
27
+ - lib/dotpay/cancel_request.rb
28
+ - lib/dotpay/client.rb
29
+ - lib/dotpay/configuration.rb
30
+ - lib/dotpay/error.rb
31
+ - lib/dotpay/response.rb
32
+ - lib/dotpay/version.rb
33
+ - lib/dotpay.rb
34
+ - Rakefile
35
+ - README.rdoc
36
+ - spec/cancel_request_spec.rb
37
+ - spec/dotpay_pl_spec.rb
38
+ - spec/response_spec.rb
39
+ - spec/spec_helper.rb
40
+ homepage: https://github.com/friendlyfashion/dotpay
41
+ licenses: []
42
+ post_install_message:
43
+ rdoc_options: []
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ! '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ segments:
53
+ - 0
54
+ hash: -1359324167960202671
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ! '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ segments:
62
+ - 0
63
+ hash: -1359324167960202671
64
+ requirements: []
65
+ rubyforge_project:
66
+ rubygems_version: 1.8.25
67
+ signing_key:
68
+ specification_version: 3
69
+ summary: dotpay gem
70
+ test_files:
71
+ - spec/cancel_request_spec.rb
72
+ - spec/dotpay_pl_spec.rb
73
+ - spec/response_spec.rb
74
+ - spec/spec_helper.rb