itunes-receipt 0.0.5 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.5
1
+ 0.1.0
@@ -10,27 +10,38 @@ module Itunes
10
10
  end
11
11
  end
12
12
 
13
- attr_reader :quantity, :product_id, :transaction_id, :purchase_date, :app_item_id, :version_external_identifier, :bid, :bvrs, :original
13
+ # expires_date, receipt_data, and latest (receipt) will only appear for autorenew subscription products
14
+ attr_reader :quantity, :product_id, :transaction_id, :purchase_date, :app_item_id, :version_external_identifier, :bid, :bvrs, :original, :expires_date, :receipt_data, :latest
14
15
 
15
16
  def initialize(attributes = {})
16
- if attributes[:quantity]
17
- @quantity = attributes[:quantity].to_i
17
+ receipt_attributes = attributes.with_indifferent_access[:receipt]
18
+ if receipt_attributes[:quantity]
19
+ @quantity = receipt_attributes[:quantity].to_i
18
20
  end
19
- @product_id = attributes[:product_id]
20
- @transaction_id = attributes[:transaction_id]
21
- @purchase_date = if attributes[:purchase_date]
22
- Time.parse attributes[:purchase_date].sub('Etc/', '')
21
+ @product_id = receipt_attributes[:product_id]
22
+ @transaction_id = receipt_attributes[:transaction_id]
23
+ @purchase_date = if receipt_attributes[:purchase_date]
24
+ Time.parse receipt_attributes[:purchase_date].sub('Etc/', '')
23
25
  end
24
- @app_item_id = attributes[:app_item_id]
25
- @version_external_identifier = attributes[:version_external_identifier]
26
- @bid = attributes[:bid]
27
- @bvrs = attributes[:bvrs]
28
- if attributes[:original_transaction_id] || attributes[:original_purchase_date]
29
- @original = self.class.new(
30
- :transaction_id => attributes[:original_transaction_id],
31
- :purchase_date => attributes[:original_purchase_date]
32
- )
26
+ @app_item_id = receipt_attributes[:app_item_id]
27
+ @version_external_identifier = receipt_attributes[:version_external_identifier]
28
+ @bid = receipt_attributes[:bid]
29
+ @bvrs = receipt_attributes[:bvrs]
30
+ if receipt_attributes[:original_transaction_id] || receipt_attributes[:original_purchase_date]
31
+ @original = self.class.new(:receipt => {
32
+ :transaction_id => receipt_attributes[:original_transaction_id],
33
+ :purchase_date => receipt_attributes[:original_purchase_date]
34
+ })
33
35
  end
36
+
37
+ # autorenew subscription handling
38
+ # attributes[:latest_receipt_info] and attributes[:latest_receipt] will be nil if you already have the receipt for the most recent renewal.
39
+ if attributes[:latest_receipt_info]
40
+ full_receipt_data = attributes[:latest_receipt] # should also be in the top-level hash if attributes[:latest_receipt_info] is there, but this won't break if it isn't
41
+ @latest = self.class.new(:receipt => attributes[:latest_receipt_info], :latest_receipt => full_receipt_data, :receipt_type => :latest)
42
+ end
43
+ @expires_date = Time.at(receipt_attributes[:expires_date].to_i / 1000) if receipt_attributes[:expires_date]
44
+ @receipt_data = attributes[:latest_receipt] if attributes[:receipt_type] == :latest # it feels wrong to include the receipt_data for the latest receipt on anything other than the latest receipt
34
45
  end
35
46
 
36
47
  def self.verify!(receipt_data)
@@ -41,7 +52,7 @@ module Itunes
41
52
  response = JSON.parse(response).with_indifferent_access
42
53
  case response[:status]
43
54
  when 0
44
- new response[:receipt]
55
+ new response
45
56
  else
46
57
  raise VerificationFailed.new(response)
47
58
  end
@@ -0,0 +1,2 @@
1
+ {"receipt":{"original_purchase_date_pst":"2012-10-11 07:45:40 America/Los_Angeles", "unique_identifier":"4d69cc362c5c58ce62da68ee43721ea670907bad", "original_transaction_id":"1000000057005439", "expires_date":"1350157508197", "transaction_id":"1000000055076747", "quantity":"1", "product_id":"com.notkeepingitreal.fizzbuzz.subscription.autorenew1m", "original_purchase_date_ms":"1349966740000", "bid":"com.notkeepingitreal.fizzbuzz", "web_order_line_item_id":"1000000026553289", "bvrs":"1.0", "expires_date_formatted":"2012-10-13 19:45:08 Etc/GMT", "purchase_date":"2012-10-13 19:40:08 Etc/GMT", "purchase_date_ms":"1350157208197", "expires_date_formatted_pst":"2012-10-13 12:45:08 America/Los_Angeles", "purchase_date_pst":"2012-10-13 12:40:08 America/Los_Angeles", "original_purchase_date":"2012-10-11 14:45:40 Etc/GMT", "item_id":"570504929"}, "latest_receipt_info":{"original_purchase_date_pst":"2012-10-11 07:45:40 America/Los_Angeles", "unique_identifier":"4d79cc562c5c58ce62da68ee43721ea670907bad", "original_transaction_id":"1000000057005439", "expires_date":"1350157808000", "transaction_id":"1000000052076747", "quantity":"1", "product_id":"com.notkeepingitreal.fizzbuzz.subscription.autorenew1m", "original_purchase_date_ms":"1349966740000", "bid":"com.notkeepingitreal.fizzbuzz", "web_order_line_item_id":"1000000027293289", "bvrs":"1.0", "expires_date_formatted":"2012-10-13 19:50:08 Etc/GMT", "purchase_date":"2012-10-13 19:40:08 Etc/GMT", "purchase_date_ms":"1350157208000", "expires_date_formatted_pst":"2012-10-13 12:50:08 America/Los_Angeles", "purchase_date_pst":"2012-10-13 12:40:08 America/Los_Angeles", "original_purchase_date":"2012-10-11 14:45:40 Etc/GMT", "item_id":"570504929"}, "status":0, "latest_receipt":"junk="}
2
+
@@ -7,7 +7,7 @@ describe Itunes::Receipt do
7
7
  sandbox_mode do
8
8
  expect do
9
9
  Itunes::Receipt.verify! 'receipt-data'
10
- end.should post_to Itunes::ENDPOINT[:sandbox]
10
+ end.to post_to Itunes::ENDPOINT[:sandbox]
11
11
  end
12
12
  end
13
13
 
@@ -19,7 +19,7 @@ describe Itunes::Receipt do
19
19
  it 'should raise VerificationFailed' do
20
20
  expect do
21
21
  Itunes::Receipt.verify! 'invalid'
22
- end.should raise_error Itunes::Receipt::VerificationFailed
22
+ end.to raise_error Itunes::Receipt::VerificationFailed
23
23
  end
24
24
  end
25
25
 
@@ -40,11 +40,59 @@ describe Itunes::Receipt do
40
40
  receipt.original.quantity.should be_nil
41
41
  receipt.original.transaction_id.should == '1000000001479608'
42
42
  receipt.original.purchase_date.should == Time.utc(2011, 2, 17, 6, 20, 57)
43
+ receipt.expires_date.should be_nil
44
+ receipt.receipt_data.should be_nil
43
45
 
44
46
  # Those attributes are not returned from iTunes Connect Sandbox
45
47
  receipt.app_item_id.should be_nil
46
48
  receipt.version_external_identifier.should be_nil
47
49
  end
48
50
  end
51
+
52
+ context 'when autorenew subscription' do
53
+ before do
54
+ fake_json :autorenew_subscription
55
+ end
56
+
57
+ it 'should return valid Receipt instance for autorenew subscription' do
58
+ original_transaction_id = '1000000057005439'
59
+ original_purchase_date = Time.utc(2012, 10, 11, 14, 45, 40)
60
+ receipt = Itunes::Receipt.verify! 'autorenew_subscription'
61
+ receipt.should be_instance_of Itunes::Receipt
62
+ receipt.quantity == 1
63
+ receipt.product_id.should == 'com.notkeepingitreal.fizzbuzz.subscription.autorenew1m'
64
+ receipt.transaction_id.should == '1000000055076747'
65
+ receipt.purchase_date.should == Time.utc(2012, 10, 13, 19, 40, 8)
66
+ receipt.bid.should == 'com.notkeepingitreal.fizzbuzz'
67
+ receipt.bvrs.should == '1.0'
68
+ receipt.original.quantity.should be_nil
69
+ receipt.original.transaction_id.should == original_transaction_id
70
+ receipt.original.purchase_date.should == original_purchase_date
71
+ receipt.expires_date.should == Time.utc(2012, 10, 13, 19, 45, 8)
72
+ receipt.receipt_data.should be_nil
73
+
74
+ # Those attributes are not returned from iTunes Connect Sandbox
75
+ receipt.app_item_id.should be_nil
76
+ receipt.version_external_identifier.should be_nil
77
+
78
+ latest = receipt.latest
79
+ latest.should be_instance_of Itunes::Receipt
80
+ latest.quantity == 1
81
+ latest.product_id.should == 'com.notkeepingitreal.fizzbuzz.subscription.autorenew1m'
82
+ latest.transaction_id.should == '1000000052076747'
83
+ latest.purchase_date.should == Time.utc(2012, 10, 13, 19, 40, 8)
84
+ latest.expires_date.should == Time.utc(2012, 10, 13, 19, 50, 8) # five minutes after the "old" receipt
85
+ latest.bid.should == 'com.notkeepingitreal.fizzbuzz'
86
+ latest.bvrs.should == '1.0'
87
+ latest.original.quantity.should be_nil
88
+ latest.original.transaction_id.should == original_transaction_id
89
+ latest.original.purchase_date.should == original_purchase_date
90
+ latest.receipt_data.should == 'junk='
91
+
92
+ # Those attributes are not returned from iTunes Connect Sandbox
93
+ latest.app_item_id.should be_nil
94
+ latest.version_external_identifier.should be_nil
95
+ end
96
+ end
49
97
  end
50
- end
98
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: itunes-receipt
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,12 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-11-16 00:00:00.000000000 +09:00
13
- default_executable:
12
+ date: 2012-10-15 00:00:00.000000000 Z
14
13
  dependencies:
15
14
  - !ruby/object:Gem::Dependency
16
15
  name: json
17
- requirement: &70319856666960 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
18
17
  none: false
19
18
  requirements:
20
19
  - - ! '>='
@@ -22,10 +21,15 @@ dependencies:
22
21
  version: 1.4.3
23
22
  type: :runtime
24
23
  prerelease: false
25
- version_requirements: *70319856666960
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 1.4.3
26
30
  - !ruby/object:Gem::Dependency
27
31
  name: restclient_with_cert
28
- requirement: &70319856666540 !ruby/object:Gem::Requirement
32
+ requirement: !ruby/object:Gem::Requirement
29
33
  none: false
30
34
  requirements:
31
35
  - - ! '>='
@@ -33,10 +37,15 @@ dependencies:
33
37
  version: '0'
34
38
  type: :runtime
35
39
  prerelease: false
36
- version_requirements: *70319856666540
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
37
46
  - !ruby/object:Gem::Dependency
38
47
  name: activesupport
39
- requirement: &70319856666000 !ruby/object:Gem::Requirement
48
+ requirement: !ruby/object:Gem::Requirement
40
49
  none: false
41
50
  requirements:
42
51
  - - ! '>='
@@ -44,10 +53,15 @@ dependencies:
44
53
  version: '2.3'
45
54
  type: :runtime
46
55
  prerelease: false
47
- version_requirements: *70319856666000
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '2.3'
48
62
  - !ruby/object:Gem::Dependency
49
63
  name: i18n
50
- requirement: &70319860928480 !ruby/object:Gem::Requirement
64
+ requirement: !ruby/object:Gem::Requirement
51
65
  none: false
52
66
  requirements:
53
67
  - - ! '>='
@@ -55,10 +69,15 @@ dependencies:
55
69
  version: '0'
56
70
  type: :runtime
57
71
  prerelease: false
58
- version_requirements: *70319860928480
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
59
78
  - !ruby/object:Gem::Dependency
60
79
  name: rake
61
- requirement: &70319860927940 !ruby/object:Gem::Requirement
80
+ requirement: !ruby/object:Gem::Requirement
62
81
  none: false
63
82
  requirements:
64
83
  - - ! '>='
@@ -66,10 +85,15 @@ dependencies:
66
85
  version: '0.8'
67
86
  type: :development
68
87
  prerelease: false
69
- version_requirements: *70319860927940
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0.8'
70
94
  - !ruby/object:Gem::Dependency
71
95
  name: rcov
72
- requirement: &70319860927440 !ruby/object:Gem::Requirement
96
+ requirement: !ruby/object:Gem::Requirement
73
97
  none: false
74
98
  requirements:
75
99
  - - ! '>='
@@ -77,10 +101,15 @@ dependencies:
77
101
  version: '0.9'
78
102
  type: :development
79
103
  prerelease: false
80
- version_requirements: *70319860927440
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0.9'
81
110
  - !ruby/object:Gem::Dependency
82
111
  name: rspec
83
- requirement: &70319860926980 !ruby/object:Gem::Requirement
112
+ requirement: !ruby/object:Gem::Requirement
84
113
  none: false
85
114
  requirements:
86
115
  - - ! '>='
@@ -88,10 +117,15 @@ dependencies:
88
117
  version: '2'
89
118
  type: :development
90
119
  prerelease: false
91
- version_requirements: *70319860926980
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '2'
92
126
  - !ruby/object:Gem::Dependency
93
127
  name: fakeweb
94
- requirement: &70319860926520 !ruby/object:Gem::Requirement
128
+ requirement: !ruby/object:Gem::Requirement
95
129
  none: false
96
130
  requirements:
97
131
  - - ! '>='
@@ -99,7 +133,12 @@ dependencies:
99
133
  version: 1.3.0
100
134
  type: :development
101
135
  prerelease: false
102
- version_requirements: *70319860926520
136
+ version_requirements: !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ! '>='
140
+ - !ruby/object:Gem::Version
141
+ version: 1.3.0
103
142
  description: Handle iTunes In App Purchase Receipt Verification
104
143
  email: nov@matake.jp
105
144
  executables: []
@@ -117,12 +156,12 @@ files:
117
156
  - itunes-receipt.gemspec
118
157
  - lib/itunes.rb
119
158
  - lib/itunes/receipt.rb
159
+ - spec/fake_json/autorenew_subscription.json
120
160
  - spec/fake_json/invalid.json
121
161
  - spec/fake_json/valid.json
122
162
  - spec/helpers/fake_json_helper.rb
123
163
  - spec/itunes/receipt_spec.rb
124
164
  - spec/spec_helper.rb
125
- has_rdoc: true
126
165
  homepage: http://github.com/nov/itunes-receipt
127
166
  licenses: []
128
167
  post_install_message:
@@ -143,11 +182,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
143
182
  version: '0'
144
183
  requirements: []
145
184
  rubyforge_project:
146
- rubygems_version: 1.6.2
185
+ rubygems_version: 1.8.24
147
186
  signing_key:
148
187
  specification_version: 3
149
188
  summary: Handle iTunes In App Purchase Receipt Verification
150
189
  test_files:
190
+ - spec/fake_json/autorenew_subscription.json
151
191
  - spec/fake_json/invalid.json
152
192
  - spec/fake_json/valid.json
153
193
  - spec/helpers/fake_json_helper.rb