itunes_receipt_validator 0.0.1 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 38e317636d5e42242456b34d6210cb329441ca5b
4
- data.tar.gz: 0f98438cbc20775e4e04cc33f1064506e62b6950
3
+ metadata.gz: 8b77b13bb8f2873dbee2a213fd62d354f4fedbde
4
+ data.tar.gz: eb68ad8842b921cbbcb4f02d688db66eb7ba78f5
5
5
  SHA512:
6
- metadata.gz: 0eb6b796ef6302d5cf953da5f4bb9640738ef0dfac5bc0953eb6847c263f34c9d9c10be325314fbd686d906aad7e4c47d4b5231ad327f3a335c2da232baedef9
7
- data.tar.gz: dff34a3e35e7e68fe59eed49f54d46346c5a2f49178e5a8feb44009331a8949f40a1a44e2af685ff46ded97da8353eaa18166471133a1426ad349a2ebeac8f80
6
+ metadata.gz: 96134027c767ab5c262377750b0f1a92e330a1917056dfba6a433b08cfabab0767c7dc39695e64cfafc02a80635555dd4f1d9ce2609ad8da9de776313d06c774
7
+ data.tar.gz: 3bce5284cf4ef617120923c604acd2d2dcddee596cde29bc93ba58a981117f1ef8381195ce6b80dc18107af5c5774df863129d90438f535999e972d6658a687d
@@ -0,0 +1,15 @@
1
+ ##
2
+ # ItunesReceiptValidator
3
+ module ItunesReceiptValidator
4
+ ##
5
+ # ItunesReceiptValidator::Error
6
+ class Error < StandardError; end
7
+
8
+ ##
9
+ # ItunesReceiptValidator::LocalDecodingError
10
+ class LocalDecodingError < Error; end
11
+
12
+ ##
13
+ # ItunesReceiptValidator::RemoteNetworkError
14
+ class RemoteNetworkError < Error; end
15
+ end
@@ -4,25 +4,19 @@ module ItunesReceiptValidator
4
4
  ##
5
5
  # ItunesReceiptValidator::Receipt
6
6
  class Receipt
7
- attr_reader :receipt, :options
7
+ attr_reader :receipt
8
+ attr_accessor :shared_secret, :request_method
8
9
 
9
10
  def initialize(receipt, options = {})
10
11
  @receipt = receipt
11
- @options = options
12
+ @shared_secret = options.fetch(:shared_secret, nil)
13
+ @request_method = options.fetch(:request_method, nil)
12
14
  local
13
- end
14
-
15
- def sandbox?
16
- local.sandbox?
17
- end
18
-
19
- def production?
20
- !sandbox?
15
+ yield self if block_given?
21
16
  end
22
17
 
23
18
  def bundle_id
24
- @bundle_id = local.receipt
25
- .fetch(local.style == :unified ? :bundle_id : :bid)
19
+ @bundle_id = local.receipt.fetch(style == :unified ? :bundle_id : :bid)
26
20
  end
27
21
 
28
22
  def transactions
@@ -38,32 +32,58 @@ module ItunesReceiptValidator
38
32
  end
39
33
 
40
34
  def latest_receipt
41
- @latest_receipt = remote.json.fetch(:latest_receipt)
35
+ @latest_receipt = remote.json.fetch :latest_receipt
42
36
  end
43
37
 
44
38
  def local
45
- @local ||= ItunesReceiptDecoder.new(receipt, expand_timestamps: true)
39
+ @local ||= ItunesReceiptDecoder.new receipt, expand_timestamps: true
40
+ rescue ItunesReceiptDecoder::DecodingError => e
41
+ raise LocalDecodingError, e.message
42
+ end
43
+
44
+ def environment
45
+ @environment ||= local.environment
46
+ end
47
+
48
+ def production?
49
+ environment == :production
50
+ end
51
+
52
+ def sandbox?
53
+ !production?
54
+ end
55
+
56
+ def style
57
+ local.style
46
58
  end
47
59
 
48
60
  def remote
49
- @remote ||= Remote.new(
50
- receipt,
51
- { sandbox: local.sandbox? }.merge(options)
52
- )
61
+ return @remote if @remote
62
+ instance = Remote.new(receipt) do |remote|
63
+ remote.shared_secret = shared_secret
64
+ remote.sandbox = sandbox?
65
+ remote.request_method = request_method if request_method
66
+ end
67
+
68
+ if instance.status == 21_007
69
+ @environment = :sandbox
70
+ remote
71
+ elsif instance.status == 21_008
72
+ @environment = :production
73
+ remote
74
+ else
75
+ @remote = instance
76
+ end
53
77
  end
54
78
 
55
79
  private
56
80
 
57
81
  def local_transactions_source
58
- if local.style == :unified
59
- local.receipt.fetch(:in_app)
60
- else
61
- [local.receipt]
62
- end
82
+ style == :unified ? local.receipt.fetch(:in_app) : [local.receipt]
63
83
  end
64
84
 
65
85
  def remote_transactions_source
66
- if local.style == :unified
86
+ if style == :unified
67
87
  remote.json.fetch(:latest_receipt_info)
68
88
  else
69
89
  [remote.json.fetch(:latest_receipt_info)]
@@ -1,3 +1,7 @@
1
+ require 'json'
2
+ require 'uri'
3
+ require 'net/https'
4
+
1
5
  ##
2
6
  # ItunesReceiptValidator
3
7
  module ItunesReceiptValidator
@@ -7,13 +11,15 @@ module ItunesReceiptValidator
7
11
  PRODUCTION_ENDPOINT = 'https://buy.itunes.apple.com/verifyReceipt'
8
12
  SANDBOX_ENDPOINT = 'https://sandbox.itunes.apple.com/verifyReceipt'
9
13
 
10
- attr_reader :receipt, :sandbox, :shared_secret, :options
14
+ attr_reader :receipt
15
+ attr_accessor :sandbox, :shared_secret, :request_method
11
16
 
12
- def initialize(receipt, options = {})
17
+ def initialize(receipt)
13
18
  @receipt = receipt
14
- @sandbox = options.fetch(:sandbox)
15
- @shared_secret = options.fetch(:shared_secret)
16
- @options = options
19
+ @request_method = lambda do |url, headers, body|
20
+ default_request_method(url, headers, body)
21
+ end
22
+ yield self
17
23
  end
18
24
 
19
25
  def status
@@ -34,24 +40,39 @@ module ItunesReceiptValidator
34
40
 
35
41
  private
36
42
 
37
- def response
38
- @response ||= HTTParty.post(
39
- sandbox ? SANDBOX_ENDPOINT : PRODUCTION_ENDPOINT,
40
- options.merge(payload)
41
- )
43
+ def request_url
44
+ sandbox ? SANDBOX_ENDPOINT : PRODUCTION_ENDPOINT
42
45
  end
43
46
 
44
- def payload
47
+ def request_headers
45
48
  {
46
- headers: {
47
- 'Accept' => 'application/json',
48
- 'Content-Type' => 'application/json'
49
- },
50
- body: {
51
- 'password' => shared_secret,
52
- 'receipt-data' => receipt
53
- }.to_json
49
+ 'Accept' => 'application/json',
50
+ 'Content-Type' => 'application/json'
54
51
  }
55
52
  end
53
+
54
+ def request_body
55
+ {
56
+ 'password' => shared_secret,
57
+ 'receipt-data' => receipt
58
+ }.to_json
59
+ end
60
+
61
+ def response
62
+ @response ||= request_method.call(
63
+ request_url, request_headers, request_body
64
+ )
65
+ end
66
+
67
+ def default_request_method(url, headers, body)
68
+ uri = URI(url)
69
+ http = Net::HTTP.new(uri.host, uri.port)
70
+ http.use_ssl = true
71
+ http.verify_mode = OpenSSL::SSL::VERIFY_PEER
72
+ request = Net::HTTP::Post.new(uri.request_uri)
73
+ headers.each { |key, val| request[key] = val }
74
+ request.body = body
75
+ http.request request
76
+ end
56
77
  end
57
78
  end
@@ -1,3 +1,5 @@
1
+ require 'time'
2
+
1
3
  ##
2
4
  # ItunesReceiptValidator
3
5
  module ItunesReceiptValidator
@@ -14,7 +16,9 @@ module ItunesReceiptValidator
14
16
  end
15
17
 
16
18
  def expired?
17
- receipt.remote.expired? || expires_at < Time.now.utc
19
+ !auto_renewing? ||
20
+ receipt.remote.expired? ||
21
+ latest.expires_at < Time.now.utc
18
22
  end
19
23
 
20
24
  def cancelled?
@@ -3,5 +3,5 @@
3
3
  module ItunesReceiptValidator
4
4
  ##
5
5
  # Gem version
6
- VERSION = '0.0.1'
6
+ VERSION = '0.0.3'
7
7
  end
@@ -1,7 +1,6 @@
1
- require 'pp'
2
- require 'httparty'
3
1
  require 'itunes_receipt_decoder'
4
2
  require 'itunes_receipt_validator/receipt'
3
+ require 'itunes_receipt_validator/error'
5
4
  require 'itunes_receipt_validator/remote'
6
5
  require 'itunes_receipt_validator/transaction'
7
6
  require 'itunes_receipt_validator/transactions_proxy'
@@ -9,7 +8,7 @@ require 'itunes_receipt_validator/transactions_proxy'
9
8
  ##
10
9
  # ItunesReceiptValidator
11
10
  module ItunesReceiptValidator
12
- def self.new(receipt, options = {})
13
- Receipt.new(receipt, options)
11
+ def self.new(receipt, options = {}, &block)
12
+ Receipt.new(receipt, options, &block)
14
13
  end
15
14
  end
metadata CHANGED
@@ -1,43 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: itunes_receipt_validator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - mbaasy.com
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-11-23 00:00:00.000000000 Z
11
+ date: 2015-12-07 00:00:00.000000000 Z
12
12
  dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: httparty
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - "~>"
18
- - !ruby/object:Gem::Version
19
- version: '0.13'
20
- type: :runtime
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - "~>"
25
- - !ruby/object:Gem::Version
26
- version: '0.13'
27
13
  - !ruby/object:Gem::Dependency
28
14
  name: itunes_receipt_decoder
29
15
  requirement: !ruby/object:Gem::Requirement
30
16
  requirements:
31
- - - "~>"
17
+ - - '='
32
18
  - !ruby/object:Gem::Version
33
- version: '0.2'
19
+ version: 0.2.3
34
20
  type: :runtime
35
21
  prerelease: false
36
22
  version_requirements: !ruby/object:Gem::Requirement
37
23
  requirements:
38
- - - "~>"
24
+ - - '='
39
25
  - !ruby/object:Gem::Version
40
- version: '0.2'
26
+ version: 0.2.3
41
27
  - !ruby/object:Gem::Dependency
42
28
  name: rake
43
29
  requirement: !ruby/object:Gem::Requirement
@@ -137,13 +123,15 @@ dependencies:
137
123
  - !ruby/object:Gem::Version
138
124
  version: '1.22'
139
125
  description: |2
140
- Validate iTunes Transaction and Unified receipts
126
+ Validate iTunes Transaction and Unified style receipts with local decoding
127
+ and remote validation.
141
128
  email: hello@mbaasy.com
142
129
  executables: []
143
130
  extensions: []
144
131
  extra_rdoc_files: []
145
132
  files:
146
133
  - lib/itunes_receipt_validator.rb
134
+ - lib/itunes_receipt_validator/error.rb
147
135
  - lib/itunes_receipt_validator/receipt.rb
148
136
  - lib/itunes_receipt_validator/remote.rb
149
137
  - lib/itunes_receipt_validator/transaction.rb