itunes-receipt 0.1.1 → 0.1.2

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.
data/.travis.yml CHANGED
@@ -1,4 +1,3 @@
1
1
  rvm:
2
2
  - 1.9.2
3
3
  - 1.9.3
4
- - jruby
data/Gemfile CHANGED
@@ -1,7 +1,2 @@
1
1
  source "http://rubygems.org"
2
-
3
- platform :jruby do
4
- gem 'jruby-openssl', '>= 0.7'
5
- end
6
-
7
- gemspec
2
+ gemspec
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.1.2
@@ -10,8 +10,11 @@ module Itunes
10
10
  end
11
11
  end
12
12
 
13
+ class SandboxReceiptReceived < VerificationFailed; end;
14
+
15
+
13
16
  # 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
17
+ attr_reader :quantity, :product_id, :transaction_id, :purchase_date, :app_item_id, :version_external_identifier, :bid, :bvrs, :original, :expires_date, :receipt_data, :latest, :itunes_env
15
18
 
16
19
  def initialize(attributes = {})
17
20
  receipt_attributes = attributes.with_indifferent_access[:receipt]
@@ -42,22 +45,51 @@ module Itunes
42
45
  end
43
46
  @expires_date = Time.at(receipt_attributes[:expires_date].to_i / 1000) if receipt_attributes[:expires_date]
44
47
  @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
48
+
49
+ @itunes_env = attributes[:itunes_env] || Itunes.itunes_env
45
50
  end
46
51
 
47
- def self.verify!(receipt_data)
52
+ def self.verify!(receipt_data, allow_sandbox_receipt = false)
48
53
  request_data = {:'receipt-data' => receipt_data}
49
54
  request_data.merge!(:password => Itunes.shared_secret) if Itunes.shared_secret
55
+ response = post_to_endpoint(request_data)
56
+ begin
57
+ successful_response(response)
58
+ rescue SandboxReceiptReceived => e
59
+ # Retry with sandbox, as per:
60
+ # http://developer.apple.com/library/ios/#technotes/tn2259/_index.html
61
+ # FAQ#16
62
+ if allow_sandbox_receipt
63
+ sandbox_response = post_to_endpoint(request_data, Itunes::ENDPOINT[:sandbox])
64
+ successful_response(
65
+ sandbox_response.merge(:itunes_env => :sandbox)
66
+ )
67
+ else
68
+ raise e
69
+ end
70
+ end
71
+ end
72
+
73
+ private
74
+
75
+ def self.post_to_endpoint(request_data, endpoint = Itunes.endpoint)
50
76
  response = RestClient.post(
51
- Itunes.endpoint,
77
+ endpoint,
52
78
  request_data.to_json
53
79
  )
54
80
  response = JSON.parse(response).with_indifferent_access
81
+ end
82
+
83
+ def self.successful_response(response)
55
84
  case response[:status]
56
85
  when 0
57
86
  new response
87
+ when 21007
88
+ raise SandboxReceiptReceived.new(response)
58
89
  else
59
90
  raise VerificationFailed.new(response)
60
91
  end
61
92
  end
93
+
62
94
  end
63
95
  end
data/lib/itunes.rb CHANGED
@@ -10,11 +10,11 @@ module Itunes
10
10
  }
11
11
 
12
12
  def self.endpoint
13
- if sandbox?
14
- ENDPOINT[:sandbox]
15
- else
16
- ENDPOINT[:production]
17
- end
13
+ ENDPOINT[itunes_env]
14
+ end
15
+
16
+ def self.itunes_env
17
+ sandbox? ? :sandbox : :production
18
18
  end
19
19
 
20
20
  def self.sandbox?
@@ -0,0 +1 @@
1
+ {"receipt":{"item_id":"420862234", "original_transaction_id":"1000000001479608", "bvrs":"1.0", "product_id":"com.cerego.iknow.30d", "purchase_date":"2011-02-17 06:20:57 Etc/GMT", "quantity":"1", "bid":"com.cerego.iknow", "original_purchase_date":"2011-02-17 06:20:57 Etc/GMT", "transaction_id":"1000000001500000"}, "status":21007}
@@ -39,6 +39,32 @@ describe Itunes::Receipt do
39
39
  Itunes::Receipt.verify! 'invalid'
40
40
  end.to raise_error Itunes::Receipt::VerificationFailed
41
41
  end
42
+
43
+ context 'due to a sandbox receipt reply' do
44
+ before do
45
+ fake_json :sandboxed
46
+ sandbox_mode do
47
+ fake_json :valid
48
+ end
49
+ end
50
+
51
+ context 'when sandbox receipt accepted explicitly' do
52
+ it 'should try and verify the receipt against the sandbox ' do
53
+ receipt = Itunes::Receipt.verify! 'sandboxed', :allow_sandbox_receipt
54
+ receipt.should be_instance_of Itunes::Receipt
55
+ receipt.transaction_id.should == '1000000001479608'
56
+ receipt.itunes_env.should == :sandbox
57
+ end
58
+ end
59
+
60
+ context 'otherwise' do
61
+ it 'should raise SandboxReceiptReceived exception' do
62
+ expect do
63
+ Itunes::Receipt.verify! 'sandboxed'
64
+ end.to raise_error Itunes::Receipt::SandboxReceiptReceived
65
+ end
66
+ end
67
+ end
42
68
  end
43
69
 
44
70
  context 'when valid' do
@@ -60,6 +86,7 @@ describe Itunes::Receipt do
60
86
  receipt.original.purchase_date.should == Time.utc(2011, 2, 17, 6, 20, 57)
61
87
  receipt.expires_date.should be_nil
62
88
  receipt.receipt_data.should be_nil
89
+ receipt.itunes_env.should == :production
63
90
 
64
91
  # Those attributes are not returned from iTunes Connect Sandbox
65
92
  receipt.app_item_id.should be_nil
@@ -106,7 +133,7 @@ describe Itunes::Receipt do
106
133
  latest.original.transaction_id.should == original_transaction_id
107
134
  latest.original.purchase_date.should == original_purchase_date
108
135
  latest.receipt_data.should == 'junk='
109
-
136
+
110
137
  # Those attributes are not returned from iTunes Connect Sandbox
111
138
  latest.app_item_id.should be_nil
112
139
  latest.version_external_identifier.should be_nil
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.1.1
4
+ version: 0.1.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-15 00:00:00.000000000 Z
12
+ date: 2012-11-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json
@@ -158,6 +158,7 @@ files:
158
158
  - lib/itunes/receipt.rb
159
159
  - spec/fake_json/autorenew_subscription.json
160
160
  - spec/fake_json/invalid.json
161
+ - spec/fake_json/sandboxed.json
161
162
  - spec/fake_json/valid.json
162
163
  - spec/helpers/fake_json_helper.rb
163
164
  - spec/itunes/receipt_spec.rb
@@ -175,12 +176,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
175
176
  - - ! '>='
176
177
  - !ruby/object:Gem::Version
177
178
  version: '0'
179
+ segments:
180
+ - 0
181
+ hash: 2311659879300256820
178
182
  required_rubygems_version: !ruby/object:Gem::Requirement
179
183
  none: false
180
184
  requirements:
181
185
  - - ! '>='
182
186
  - !ruby/object:Gem::Version
183
187
  version: '0'
188
+ segments:
189
+ - 0
190
+ hash: 2311659879300256820
184
191
  requirements: []
185
192
  rubyforge_project:
186
193
  rubygems_version: 1.8.24
@@ -190,6 +197,7 @@ summary: Handle iTunes In App Purchase Receipt Verification
190
197
  test_files:
191
198
  - spec/fake_json/autorenew_subscription.json
192
199
  - spec/fake_json/invalid.json
200
+ - spec/fake_json/sandboxed.json
193
201
  - spec/fake_json/valid.json
194
202
  - spec/helpers/fake_json_helper.rb
195
203
  - spec/itunes/receipt_spec.rb