paytrail 0.0.0

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,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 998d4096f959c262c564f34055dbbc104985bce8
4
+ data.tar.gz: 3879f7fc13dad3fb54626cfb715b8f0d8237cae4
5
+ SHA512:
6
+ metadata.gz: c2aef54fd2d3d3e65cc6c25ea3e3faedfd68f7215857e24c88bd8104ac93ac2a16438034de6a35d359bfed18ea2223b80670f496f2db0b0043b39a6a68ce9137
7
+ data.tar.gz: 77cc32ab2f250c2211744c5dcb3276f9e380f4a3b140c7bafafa7b40fba279ec62201c1267dd1f340754274731d61574740a6dcc6f7a28be5071c870ce93de81
@@ -0,0 +1,54 @@
1
+ require 'time'
2
+ require 'openSSL'
3
+ require 'base64'
4
+ require 'digest'
5
+ require 'json'
6
+ require 'net/http'
7
+ require 'uri'
8
+
9
+ class Paytrail
10
+ def makeTimestamp()
11
+ Time.now.iso8601
12
+ end
13
+
14
+ def makeAuthorization(apiName, apiKey, secret, method, url, timestamp, contentMD5)
15
+ signatureData = "%s\n%s\n%s %s\n%s\n%s" % [method, url, apiName, apiKey, timestamp, contentMD5]
16
+ digest = OpenSSL::Digest::SHA256.new
17
+ hmac = OpenSSL::HMAC.digest(digest, secret, signatureData)
18
+
19
+ Base64.encode64(hmac)
20
+ end
21
+
22
+ def makeContentMD5(content)
23
+ Digest::MD5.base64digest content
24
+ end
25
+
26
+ def makeHeaders(apiName, apiKey, secret, method, uri, timestamp, body)
27
+ {
28
+ 'Timestamp' => timestamp,
29
+ 'Content-MD5' => makeContentMD5(body),
30
+ 'Authorization' => "%s %s:%s" % [apiName, apiKey, makeAuthorization(apiName, apiKey, secret, method, uri, timestamp, makeContentMD5(body))]
31
+ }
32
+ end
33
+
34
+ def post(uri, headers, body)
35
+ uri = URI(uri)
36
+ request = Net::HTTP::Post.new(uri, headers)
37
+ request.body = body
38
+
39
+ makeRequest(uri, request)
40
+ end
41
+
42
+ def delete(uri, headers)
43
+ uri = URI(uri)
44
+ request = Net::HTTP::Delete.new(uri, headers)
45
+
46
+ makeRequest(uri, request)
47
+ end
48
+
49
+ def makeRequest(uri, request)
50
+ http = Net::HTTP.start(uri.hostname, uri.port, :use_ssl => true)
51
+
52
+ http.request(request)
53
+ end
54
+ end
@@ -0,0 +1,76 @@
1
+ require 'paytrail'
2
+
3
+ class Paytrail::MerchantApi
4
+ # Example usage
5
+ #
6
+ # merchantApi = Paytrail::MerchantApi.new(13466, '6pKF4jkv97zmqBJ3ZL8gUw5DfT2NMQ', 'https://api.paytrail.com')
7
+ # response = merchantApi.createRefund(
8
+ # 'ORD-59867',
9
+ # 'test@paytrail.com',
10
+ # [{
11
+ # :amount => 100,
12
+ # :vatPercent => 1000,
13
+ # :description => 'Refund for item 123'
14
+ # }],
15
+ # 'http://paytrail.com'
16
+ # )
17
+ #
18
+ # refundToken = nil
19
+ #
20
+ # if response.code == '202'
21
+ # puts 'Refund created!'
22
+ # location = response.get_fields('location')
23
+ # refundToken = location[0].split('/')[-1]
24
+ # else
25
+ # puts 'ERROR'
26
+ # print 'HTTP code: '
27
+ # puts response.code
28
+ # puts response.body
29
+ # end
30
+ #
31
+ # if refundToken
32
+ # response = merchantApi.deleteRefund(refundToken)
33
+ #
34
+ # if response.code == '204'
35
+ # puts 'Refund deleted!'
36
+ # else
37
+ # puts 'ERROR'
38
+ # print 'HTTP code: '
39
+ # puts response.code
40
+ # puts response.body
41
+ # end
42
+ # end
43
+
44
+ @@apiName = 'PaytrailMerchantAPI'
45
+
46
+ def initialize(apiKey, secret, uri)
47
+ @apiKey = apiKey
48
+ @secret = secret
49
+ @uri = uri
50
+ @paytrail = Paytrail.new
51
+ end
52
+
53
+ def createRefund(orderNumber, email, refundRows, notifyUrl)
54
+ data = {
55
+ :email => email,
56
+ :rows => refundRows
57
+ }
58
+
59
+ if notifyUrl
60
+ data[:notifyUrl] = notifyUrl
61
+ end
62
+
63
+ data = data.to_json
64
+ location = '/merchant/v1/payments/%s/refunds' % [orderNumber]
65
+ headers = @paytrail.makeHeaders(@@apiName, @apiKey, @secret, 'POST', location, @paytrail.makeTimestamp, data)
66
+
67
+ @paytrail.post(@uri + location, headers, data)
68
+ end
69
+
70
+ def deleteRefund(refundToken)
71
+ location = '/merchant/v1/refunds/%s' % [refundToken]
72
+ headers = @paytrail.makeHeaders(@@apiName, @apiKey, @secret, 'DELETE', location, @paytrail.makeTimestamp, '')
73
+
74
+ @paytrail.delete(@uri + location, headers)
75
+ end
76
+ end
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: paytrail
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Johanna Turkia
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-09-04 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Paytrail Merchant API allows merchant to create and delete refunds for
14
+ payments made via Paytrail
15
+ email: johanna.turkia@paytrail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/paytrail.rb
21
+ - lib/paytrail/merchantapi.rb
22
+ homepage: http://paytrail.com
23
+ licenses:
24
+ - MIT
25
+ metadata: {}
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubyforge_project:
42
+ rubygems_version: 2.0.14
43
+ signing_key:
44
+ specification_version: 4
45
+ summary: Paytrail Merchant API allows merchant to create and delete refunds for payments
46
+ made via Paytrail
47
+ test_files: []