paypal-report 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ .DS_Store
6
+ .rvmrc
data/CHANGES.md ADDED
@@ -0,0 +1,4 @@
1
+ # Changes
2
+
3
+ ## 0.1.0
4
+ - initial release
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in paypal-report.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,10 @@
1
+ Copyright (c) 2011, SoundCloud Ltd., Tobias Bielohlawek
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
5
+
6
+ - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
7
+ - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8
+ - Neither the name of the SoundCloud Ltd. nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
9
+
10
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
data/README.md ADDED
@@ -0,0 +1,40 @@
1
+ # PayPal Report
2
+
3
+ A little lightweight wrapper for [Paypal's Report API](https://cms.paypal.com/cms_content/US/en_US/files/developer/PP_Reporting_Guide.pdf).
4
+ All three major API methods `run_report_request`, `get_meta_data_request` and `get_data_request` are exposed. On top of that, higher methods provide extra functionality. For now, this only _daily_ reports.
5
+
6
+
7
+ ## Usage
8
+ Example usage, receiving all report from today:
9
+
10
+ require 'paypal/report'
11
+ api = Paypal::Report.new(user, password, vendor, partner)
12
+ puts api.daily.inspect
13
+
14
+ As an example, the gem provides a small command line tool to easily get today's earnings. Usage:
15
+
16
+ paypal-report-daily -a <partner> -v <vendor> -u <user> -p <password>
17
+
18
+
19
+ ## Todo
20
+ Gem is in an early stage, so lots of stuff to do:
21
+
22
+ - introduce more higher level functions (e.g. period)
23
+ - add tests
24
+ - add examples
25
+
26
+
27
+ ## Contributing
28
+
29
+ We'll check out your contribution if you:
30
+
31
+ - Provide a comprehensive suite of tests for your fork.
32
+ - Have a clear and documented rationale for your changes.
33
+ - Package these up in a pull request.
34
+
35
+ We'll do our best to help you out with any contribution issues you may have.
36
+
37
+
38
+ ## License
39
+
40
+ The license is included as LICENSE in this directory.
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,30 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'optparse'
4
+ require 'paypal/report'
5
+
6
+ partner = vendor = user = password = nil
7
+ time = (Time.now - 60*60*24).strftime("%Y-%m-%d")
8
+
9
+ opts = ARGV.clone.options do |opts|
10
+ opts.on("-p", "--password=PASSWORD", "Password - required!") { |p| password = p}
11
+ opts.on("-d", "--date=DATE", "Date - default: #{time}") { |t| time = t}
12
+ opts.on("-u", "--user=USER", "User - default: #{user}") { |u| user = u}
13
+ opts.on("-v", "--vendor=VENDOR", "Vendor - default: #{vendor}") { |v| vendor = v}
14
+ opts.on("-a", "--partner=PARTNER", "Partner - default: #{partner}") { |a| partner = a}
15
+
16
+ opts.on_tail("-h", "--help", "Show this help message.") { $stderr.puts opts; exit }
17
+ end
18
+ opts.parse!
19
+
20
+ abort "No password given: \n #{opts}" unless password
21
+
22
+ api = Paypal::Report.new(user, password, vendor, partner)
23
+
24
+ amount = 0
25
+ report = api.daily(time, 5000)
26
+ report.each do |row|
27
+ amount += row[6].to_i
28
+ end
29
+
30
+ puts "Report for #{time}: #{report.size} Entries - #{amount / 100.0} EUR"
data/lib/paypal.rb ADDED
@@ -0,0 +1 @@
1
+ require 'paypal/report'
@@ -0,0 +1,115 @@
1
+ require 'net/https'
2
+ require 'uri'
3
+ require 'builder'
4
+ require 'rexml/document'
5
+
6
+ module Paypal
7
+ class Report
8
+ API_URL = 'https://payments-reports.paypal.com/reportingengine'
9
+
10
+ def initialize(user, password, vendor, partner = 'PayPalUK')
11
+ @user, @password, @vendor, @partner = user, password, vendor, partner
12
+ end
13
+
14
+ #high level functions
15
+ def daily(time = Time.now, page_size = 50)
16
+ time = time.strftime("%Y-%m-%d") unless time.is_a?(String)
17
+ report_id = run_report_request('DailyActivityReport', {'report_date' => time}, page_size)
18
+
19
+ meta_data = get_meta_data_request(report_id)
20
+
21
+ data = []
22
+ meta_data["numberOfPages"].to_i.times do |page_num|
23
+ data += get_data_request(report_id, page_num + 1) #it's zero indexed
24
+ end
25
+ data
26
+ end
27
+
28
+ #low level functions
29
+ def run_report_request(report_name, report_params = {}, page_size = 50)
30
+ response = request 'runReportRequest' do |xml|
31
+ xml.reportName report_name
32
+ report_params.each do |name, value|
33
+ xml.reportParam do
34
+ xml.paramName name
35
+ xml.paramValue value
36
+ end
37
+ end
38
+ xml.pageSize page_size
39
+ end
40
+
41
+ response.elements["runReportResponse/reportId"].get_text.value
42
+ end
43
+
44
+ def get_meta_data_request(report_id)
45
+ response = request 'getMetaDataRequest' do |xml|
46
+ xml.reportId report_id
47
+ end
48
+
49
+ response.elements["getMetaDataResponse"].inject({}) do |h,e|
50
+ if text = e.get_text
51
+ h[e.name] = text.value
52
+ else
53
+ h[e.name] ||= []
54
+ h[e.name] << e.elements["dataName"].get_text.value
55
+ end
56
+ h
57
+ end
58
+ end
59
+
60
+ def get_data_request(report_id, page_num)
61
+ response = request 'getDataRequest' do |xml|
62
+ xml.reportId report_id
63
+ xml.pageNum page_num
64
+ end
65
+
66
+ [].tap do |result|
67
+ response.elements.each("getDataResponse/reportDataRow") do |data_row|
68
+ result << [].tap do |row|
69
+ data_row.elements.each("columnData/data") do |data|
70
+ row << if text = data.get_text
71
+ text.value
72
+ else
73
+ nil
74
+ end
75
+ end
76
+ end
77
+ end
78
+ end
79
+ end
80
+
81
+ private
82
+ def uri
83
+ @uri ||= URI.parse(API_URL)
84
+ end
85
+
86
+ def http
87
+ @http ||= Net::HTTP.new(uri.host, uri.port).tap do |http|
88
+ http.use_ssl = true
89
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
90
+ end
91
+ end
92
+
93
+ def request(type)
94
+ xml = Builder::XmlMarkup.new
95
+ xml.tag! 'reportingEngineRequest' do
96
+ xml.tag! 'authRequest' do
97
+ xml.user @user
98
+ xml.vendor @vendor
99
+ xml.partner @partner
100
+ xml.password @password
101
+ end
102
+ xml.tag! type do
103
+ yield xml
104
+ end
105
+ end
106
+
107
+ body = http.post(uri.request_uri, xml.target!).body
108
+ REXML::Document.new(body).elements['reportingEngineResponse'].tap do |response|
109
+ code = response.elements['baseResponse/responseCode'].get_text.value.to_i
110
+ raise response.elements['baseResponse/responseMsg'].get_text.value unless code == 100
111
+ end
112
+ end
113
+
114
+ end
115
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "paypal-report"
6
+ s.version = File.read("VERSION").to_s
7
+ s.platform = Gem::Platform::RUBY
8
+ s.authors = ["SoundCloud", "Tobias Bielohlawek"]
9
+ s.email = %q{tobi@soundcloud.com}
10
+ s.homepage = "http://github.com/rngtng/paypal-report"
11
+ s.summary = %q{A little lightweight wrapper for Paypal's Report API.}
12
+
13
+ s.files = `git ls-files`.split("\n")
14
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
15
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
16
+ s.require_paths = ["lib"]
17
+
18
+ ['builder 3.0.0'].each do |gem|
19
+ s.add_dependency *gem.split(' ')
20
+ end
21
+ end
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: paypal-report
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - SoundCloud
9
+ - Tobias Bielohlawek
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+
14
+ date: 2011-05-25 00:00:00 +02:00
15
+ default_executable:
16
+ dependencies:
17
+ - !ruby/object:Gem::Dependency
18
+ name: builder
19
+ prerelease: false
20
+ requirement: &id001 !ruby/object:Gem::Requirement
21
+ none: false
22
+ requirements:
23
+ - - "="
24
+ - !ruby/object:Gem::Version
25
+ version: 3.0.0
26
+ type: :runtime
27
+ version_requirements: *id001
28
+ description:
29
+ email: tobi@soundcloud.com
30
+ executables:
31
+ - paypal-report-daily
32
+ extensions: []
33
+
34
+ extra_rdoc_files: []
35
+
36
+ files:
37
+ - .gitignore
38
+ - CHANGES.md
39
+ - Gemfile
40
+ - LICENSE
41
+ - README.md
42
+ - Rakefile
43
+ - VERSION
44
+ - bin/paypal-report-daily
45
+ - lib/paypal.rb
46
+ - lib/paypal/report.rb
47
+ - paypal-report.gemspec
48
+ has_rdoc: true
49
+ homepage: http://github.com/rngtng/paypal-report
50
+ licenses: []
51
+
52
+ post_install_message:
53
+ rdoc_options: []
54
+
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: "0"
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: "0"
69
+ requirements: []
70
+
71
+ rubyforge_project:
72
+ rubygems_version: 1.6.2
73
+ signing_key:
74
+ specification_version: 3
75
+ summary: A little lightweight wrapper for Paypal's Report API.
76
+ test_files: []
77
+