monza 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3998800ea642fc16909e970b155bc39445ca7df4
4
+ data.tar.gz: ba775fd1840cd84ed2514d85209b121f1603abc8
5
+ SHA512:
6
+ metadata.gz: 143e1db2f456cd1e63de7b8d2d111b716a9f68402ea1f7be20252bebae8c50827d74189e8ed0a4efe008af96dfd2dcb667f5cab3943704a2043eef4f055b6e24
7
+ data.tar.gz: bb879ec57bb337fe00a592a991af4c4983d1abc991cbf155b251f4b404ab58f50cbb45bcfd5fa8ab4470ac5617cf8344628e48ec27604c4b7a924a0c509d2dbb
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.1
4
+ before_install: gem install bundler -v 1.10.6
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in monza.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Gabriel
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,78 @@
1
+ # Monza
2
+
3
+ Monza is a ruby gem that makes App Store in app purchase receipt validation and auto-renewable subscription validation easy.
4
+
5
+ You should always validate receipts on the server, in [Apple's words] (https://developer.apple.com/library/ios/releasenotes/General/ValidateAppStoreReceipt/Chapters/ValidateRemotely.html#//apple_ref/doc/uid/TP40010573-CH104-SW1):
6
+ > Use a trusted server to communicate with the App Store. Using your own server lets you design your app to recognize and trust only your server, and lets you ensure that your server connects with the App Store server. It is not possible to build a trusted connection between a user’s device and the App Store directly because you don’t control either end of that connection.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'monza'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle install
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install monza
23
+
24
+ ## Usage
25
+
26
+ Basic Usage:
27
+ ```ruby
28
+
29
+ data = "base64 receipt data string"
30
+ options = { shared_secret: "your shared secret" }
31
+ response = Monza::Receipt.verify(data, options)
32
+
33
+ # Check if subscription is active
34
+ # this checks if latest transaction receipt expiry_date is in today or the future
35
+ response.is_subscription_active? # => true or false
36
+
37
+ # Check most recent expiry date
38
+ response.latest_expiry_date # => DateTime
39
+
40
+ # Receipt object
41
+ # See Receipt class
42
+ response.receipt
43
+
44
+ # Receipt In App Transactions
45
+ # Returns array of TransactionReceipt objects, see TransactionReceipt class
46
+ response.receipt.in_app
47
+
48
+ # Receipt Latest Transactions List, use these instead if in_app to ensure you always have the latest
49
+ # Returns array of TransactionReceipt objects, see TransactionReceipt class
50
+ response.latest_receipt_info
51
+
52
+ # Latest receipt base64 string
53
+ response.latest_receipt
54
+
55
+
56
+ ```
57
+
58
+
59
+
60
+
61
+
62
+ ## Development
63
+
64
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
65
+
66
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
67
+
68
+ ## Contributing
69
+
70
+ Bug reports and pull requests are welcome on GitHub at https://github.com/gabrielgarza/monza. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
71
+
72
+ TODO:
73
+ - Need help with testing using rspec
74
+
75
+
76
+ ## License
77
+
78
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "monza"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,134 @@
1
+ require 'json'
2
+ require 'net/https'
3
+ require 'uri'
4
+
5
+ module Monza
6
+ class Client
7
+ attr_accessor :verification_url
8
+ attr_writer :shared_secret
9
+
10
+ PRODUCTION_URL = "https://buy.itunes.apple.com/verifyReceipt"
11
+ DEVELOPMENT_URL = "https://sandbox.itunes.apple.com/verifyReceipt"
12
+
13
+ def self.development
14
+ client = self.new
15
+ client.verification_url = PRODUCTION_URL
16
+ client
17
+ end
18
+
19
+ def self.production
20
+ client = self.new
21
+ client.verification_url = DEVELOPMENT_URL
22
+ client
23
+ end
24
+
25
+ def initialize
26
+ end
27
+
28
+ def verify(data, options = {})
29
+ # Post to apple and receive json_response
30
+ json_response = post_receipt_verification(data, options)
31
+ # Get status code of response
32
+ status = json_response['status'].to_i
33
+
34
+ case status
35
+ when 0
36
+ return VerificationResponse.new(json_response)
37
+ else
38
+ puts status
39
+ raise VerificationResponse::VerificationError.new(status)
40
+ end
41
+
42
+ end
43
+
44
+ private
45
+
46
+ def post_receipt_verification(data, options = {})
47
+ parameters = {
48
+ 'receipt-data' => data
49
+ }
50
+
51
+ parameters['password'] = options[:shared_secret] if options[:shared_secret]
52
+
53
+ uri = URI(@verification_url)
54
+ http = Net::HTTP.new(uri.host, uri.port)
55
+ http.use_ssl = true
56
+ http.verify_mode = OpenSSL::SSL::VERIFY_PEER
57
+
58
+ request = Net::HTTP::Post.new(uri.request_uri)
59
+ request['Accept'] = "application/json"
60
+ request['Content-Type'] = "application/json"
61
+ request.body = parameters.to_json
62
+
63
+ response = http.request(request)
64
+
65
+ JSON.parse(response.body)
66
+ end
67
+ end
68
+ end
69
+
70
+
71
+ # Sample JSON Response
72
+ #
73
+ # {
74
+ # "status": 0,
75
+ # "environment": "Sandbox",
76
+ # "receipt": {
77
+ # "receipt_type": "ProductionSandbox",
78
+ # "adam_id": 0,
79
+ # "app_item_id": 0,
80
+ # "bundle_id": "your_product_id",
81
+ # "application_version": "58",
82
+ # "download_id": 0,
83
+ # "version_external_identifier": 0,
84
+ # "receipt_creation_date": "2016-06-17 01:54:26 Etc/GMT",
85
+ # "receipt_creation_date_ms": "1466128466000",
86
+ # "receipt_creation_date_pst": "2016-06-16 18:54:26 America/Los_Angeles",
87
+ # "request_date": "2016-06-17 17:34:41 Etc/GMT",
88
+ # "request_date_ms": "1466184881174",
89
+ # "request_date_pst": "2016-06-17 10:34:41 America/Los_Angeles",
90
+ # "original_purchase_date": "2013-08-01 07:00:00 Etc/GMT",
91
+ # "original_purchase_date_ms": "1375340400000",
92
+ # "original_purchase_date_pst": "2013-08-01 00:00:00 America/Los_Angeles",
93
+ # "original_application_version": "1.0",
94
+ # "in_app": [
95
+ # {
96
+ # "quantity": "1",
97
+ # "product_id": "com.everlance.everlance.pro.monthly.test",
98
+ # "transaction_id": "1000000218147651",
99
+ # "original_transaction_id": "1000000218147500",
100
+ # "purchase_date": "2016-06-17 01:32:28 Etc/GMT",
101
+ # "purchase_date_ms": "1466127148000",
102
+ # "purchase_date_pst": "2016-06-16 18:32:28 America/Los_Angeles",
103
+ # "original_purchase_date": "2016-06-17 01:30:33 Etc/GMT",
104
+ # "original_purchase_date_ms": "1466127033000",
105
+ # "original_purchase_date_pst": "2016-06-16 18:30:33 America/Los_Angeles",
106
+ # "expires_date": "2016-06-17 01:37:28 Etc/GMT",
107
+ # "expires_date_ms": "1466127448000",
108
+ # "expires_date_pst": "2016-06-16 18:37:28 America/Los_Angeles",
109
+ # "web_order_line_item_id": "1000000032727764",
110
+ # "is_trial_period": "false"
111
+ # }
112
+ # ]
113
+ # },
114
+ # "latest_receipt_info": [
115
+ # {
116
+ # "quantity": "1",
117
+ # "product_id": "com.everlance.everlance.pro.monthly.test",
118
+ # "transaction_id": "1000000218147500",
119
+ # "original_transaction_id": "1000000218147500",
120
+ # "purchase_date": "2016-06-17 01:27:28 Etc/GMT",
121
+ # "purchase_date_ms": "1466126848000",
122
+ # "purchase_date_pst": "2016-06-16 18:27:28 America/Los_Angeles",
123
+ # "original_purchase_date": "2016-06-17 01:27:28 Etc/GMT",
124
+ # "original_purchase_date_ms": "1466126848000",
125
+ # "original_purchase_date_pst": "2016-06-16 18:27:28 America/Los_Angeles",
126
+ # "expires_date": "2016-06-17 01:32:28 Etc/GMT",
127
+ # "expires_date_ms": "1466127148000",
128
+ # "expires_date_pst": "2016-06-16 18:32:28 America/Los_Angeles",
129
+ # "web_order_line_item_id": "1000000032727765",
130
+ # "is_trial_period": "true"
131
+ # }
132
+ # ],
133
+ # "latest_receipt": "base 64"
134
+ # }
@@ -0,0 +1,131 @@
1
+ require 'time'
2
+
3
+ module Monza
4
+ class Receipt
5
+ # Receipt Fields Documentation
6
+ # https://developer.apple.com/library/ios/releasenotes/General/ValidateAppStoreReceipt/Chapters/ReceiptFields.html#//apple_ref/doc/uid/TP40010573-CH106-SW1
7
+
8
+ attr_reader :receipt_type
9
+ attr_reader :adam_id
10
+ attr_reader :bundle_id
11
+ attr_reader :application_version
12
+ attr_reader :download_id
13
+ attr_reader :receipt_creation_date
14
+ attr_reader :receipt_creation_date_ms
15
+ attr_reader :receipt_creation_date_pst
16
+ attr_reader :request_date
17
+ attr_reader :request_date_ms
18
+ attr_reader :request_date_pst
19
+ attr_reader :original_purchase_date
20
+ attr_reader :original_purchase_date_ms
21
+ attr_reader :original_purchase_date_pst
22
+ attr_reader :original_application_version
23
+ attr_reader :in_app
24
+
25
+ # This key is present only for apps purchased through the Volume Purchase Program. If this key is not present, the receipt does not expire.
26
+ attr_reader :expiration_date
27
+
28
+ # This field is not present for Mac apps
29
+ attr_reader :app_item_id
30
+
31
+ # This key is not present for receipts created in the test environment.
32
+ attr_reader :version_external_identifier
33
+
34
+ def initialize(attributes)
35
+ @receipt_type = attributes['receipt_type']
36
+ @adam_id = attributes['adam_id']
37
+ @bundle_id = attributes['bundle_id']
38
+ @application_version = attributes['application_version']
39
+ @download_id = attributes['download_id']
40
+ @receipt_creation_date = DateTime.parse(attributes['receipt_creation_date'])
41
+ @receipt_creation_date_ms = Time.at(attributes['receipt_creation_date_ms'].to_i / 1000).to_datetime
42
+ @receipt_creation_date_pst = DateTime.parse(attributes['receipt_creation_date_pst'])
43
+ @request_date = DateTime.parse(attributes['request_date'])
44
+ @request_date_ms = Time.at(attributes['request_date_ms'].to_i / 1000).to_datetime
45
+ @request_date_pst = DateTime.parse(attributes['request_date_pst'])
46
+ @original_purchase_date = DateTime.parse(attributes['original_purchase_date'])
47
+ @original_purchase_date_ms = Time.at(attributes['original_purchase_date_ms'].to_i / 1000).to_datetime
48
+ @original_purchase_date_pst = DateTime.parse(attributes['original_purchase_date_pst'])
49
+ @original_application_version = attributes['original_application_version']
50
+
51
+ if attributes['version_external_identifier']
52
+ @version_external_identifier = attributes['version_external_identifier']
53
+ end
54
+ if attributes['app_item_id']
55
+ @app_item_id = attributes['app_item_id']
56
+ end
57
+ if attributes['expiration_date']
58
+ @expires_at = Time.at(attributes['expiration_date'].to_i / 1000).to_datetime
59
+ end
60
+
61
+ @in_app = []
62
+ if attributes['in_app']
63
+ attributes['in_app'].each do |transaction_receipt_attributes|
64
+ @in_app << TransactionReceipt.new(transaction_receipt_attributes)
65
+ end
66
+ end
67
+ end # end initialize
68
+
69
+ def self.verify(data, options = {})
70
+ client = Client.production
71
+
72
+ begin
73
+ client.verify(data, options)
74
+ rescue VerificationResponse::VerificationError => error
75
+ case error.code
76
+ when 21007 # This receipt is from the test environment, but it was sent to the production environment for verification. Send it to the test environment instead.
77
+ client = Client.development
78
+ retry
79
+ when 21008 # This receipt is from the production environment, but it was sent to the test environment for verification. Send it to the production environment instead.
80
+ client = Client.production
81
+ retry
82
+ else
83
+ raise error
84
+ end
85
+ end
86
+ end
87
+
88
+ end # end class
89
+ end # end module
90
+
91
+ #
92
+ # Sample JSON Object
93
+ #
94
+ # "receipt": {
95
+ # "receipt_type": "ProductionSandbox",
96
+ # "adam_id": 0,
97
+ # "app_item_id": 0,
98
+ # "bundle_id": "your_product_id",
99
+ # "application_version": "58",
100
+ # "download_id": 0,
101
+ # "version_external_identifier": 0,
102
+ # "receipt_creation_date": "2016-06-17 01:54:26 Etc/GMT",
103
+ # "receipt_creation_date_ms": "1466128466000",
104
+ # "receipt_creation_date_pst": "2016-06-16 18:54:26 America/Los_Angeles",
105
+ # "request_date": "2016-06-17 17:34:41 Etc/GMT",
106
+ # "request_date_ms": "1466184881174",
107
+ # "request_date_pst": "2016-06-17 10:34:41 America/Los_Angeles",
108
+ # "original_purchase_date": "2013-08-01 07:00:00 Etc/GMT",
109
+ # "original_purchase_date_ms": "1375340400000",
110
+ # "original_purchase_date_pst": "2013-08-01 00:00:00 America/Los_Angeles",
111
+ # "original_application_version": "1.0",
112
+ # "in_app": [
113
+ # {
114
+ # "quantity": "1",
115
+ # "product_id": "com.everlance.everlance.pro.monthly.test",
116
+ # "transaction_id": "1000000218147651",
117
+ # "original_transaction_id": "1000000218147500",
118
+ # "purchase_date": "2016-06-17 01:32:28 Etc/GMT",
119
+ # "purchase_date_ms": "1466127148000",
120
+ # "purchase_date_pst": "2016-06-16 18:32:28 America/Los_Angeles",
121
+ # "original_purchase_date": "2016-06-17 01:30:33 Etc/GMT",
122
+ # "original_purchase_date_ms": "1466127033000",
123
+ # "original_purchase_date_pst": "2016-06-16 18:30:33 America/Los_Angeles",
124
+ # "expires_date": "2016-06-17 01:37:28 Etc/GMT",
125
+ # "expires_date_ms": "1466127448000",
126
+ # "expires_date_pst": "2016-06-16 18:37:28 America/Los_Angeles",
127
+ # "web_order_line_item_id": "1000000032727764",
128
+ # "is_trial_period": "false"
129
+ # }
130
+ # ]
131
+ # }
@@ -0,0 +1,83 @@
1
+ require 'time'
2
+
3
+ module Monza
4
+ class TransactionReceipt
5
+ # Receipt Fields Documentation
6
+ # https://developer.apple.com/library/ios/releasenotes/General/ValidateAppStoreReceipt/Chapters/ReceiptFields.html#//apple_ref/doc/uid/TP40010573-CH106-SW1
7
+
8
+ attr_reader :quantity
9
+ attr_reader :product_id
10
+ attr_reader :transaction_id
11
+ attr_reader :original_transaction_id
12
+ attr_reader :purchase_date
13
+ attr_reader :purchase_date_ms
14
+ attr_reader :purchase_date_pst
15
+ attr_reader :original_purchase_date
16
+ attr_reader :original_purchase_date_ms
17
+ attr_reader :original_purchase_date_pst
18
+ attr_reader :web_order_line_item_id
19
+
20
+ attr_reader :expires_date
21
+ attr_reader :expires_date_ms
22
+ attr_reader :expires_date_pst
23
+ attr_reader :is_trial_period
24
+
25
+ def initialize(attributes)
26
+ @quantity = attributes['quantity']
27
+ @product_id = attributes['product_id']
28
+ @transaction_id = attributes['transaction_id']
29
+ @original_transaction_id = attributes['original_transaction_id']
30
+ @purchase_date = DateTime.parse(attributes['purchase_date']) if attributes['purchase_date']
31
+ @purchase_date_ms = Time.at(attributes['purchase_date_ms'].to_i / 1000).to_datetime
32
+ @purchase_date_pst = DateTime.parse(attributes['purchase_date_pst']) if attributes['purchase_date_pst']
33
+ @original_purchase_date = DateTime.parse(attributes['original_purchase_date']) if attributes['original_purchase_date']
34
+ @original_purchase_date_ms = Time.at(attributes['original_purchase_date_ms'].to_i / 1000).to_datetime
35
+ @original_purchase_date_pst = DateTime.parse(attributes['original_purchase_date_pst']) if attributes['original_purchase_date_pst']
36
+ @web_order_line_item_id = attributes['web_order_line_item_id']
37
+
38
+ if attributes['expires_date']
39
+ @expires_date = DateTime.parse(attributes['expires_date'])
40
+ end
41
+ if attributes['expires_date_ms']
42
+ @expires_date_ms = Time.at(attributes['expires_date_ms'].to_i / 1000).to_datetime
43
+ end
44
+ if attributes['expires_date_pst']
45
+ @expires_date_pst = DateTime.parse(attributes['expires_date_pst'])
46
+ end
47
+ if attributes['is_trial_period']
48
+ @is_trial_period = attributes['is_trial_period']
49
+ end
50
+ end # end initialize
51
+
52
+ def is_renewal?
53
+ !is_first_transaction?
54
+ end
55
+
56
+ def is_first_transaction?
57
+ @original_transaction_id == @transaction_id
58
+ end
59
+
60
+
61
+ end # end class
62
+ end # end module
63
+
64
+ #
65
+ # Sample JSON Object
66
+ #
67
+ # {
68
+ # "quantity": "1",
69
+ # "product_id": "com.everlance.everlance.pro.monthly.test",
70
+ # "transaction_id": "1000000218147651",
71
+ # "original_transaction_id": "1000000218147500",
72
+ # "purchase_date": "2016-06-17 01:32:28 Etc/GMT",
73
+ # "purchase_date_ms": "1466127148000",
74
+ # "purchase_date_pst": "2016-06-16 18:32:28 America/Los_Angeles",
75
+ # "original_purchase_date": "2016-06-17 01:30:33 Etc/GMT",
76
+ # "original_purchase_date_ms": "1466127033000",
77
+ # "original_purchase_date_pst": "2016-06-16 18:30:33 America/Los_Angeles",
78
+ # "expires_date": "2016-06-17 01:37:28 Etc/GMT",
79
+ # "expires_date_ms": "1466127448000",
80
+ # "expires_date_pst": "2016-06-16 18:37:28 America/Los_Angeles",
81
+ # "web_order_line_item_id": "1000000032727764",
82
+ # "is_trial_period": "false"
83
+ # }
@@ -0,0 +1,137 @@
1
+ require 'time'
2
+
3
+ module Monza
4
+ class VerificationResponse
5
+ # https://developer.apple.com/library/ios/releasenotes/General/ValidateAppStoreReceipt/Chapters/ValidateRemotely.html
6
+
7
+ attr_reader :status
8
+ attr_reader :environment
9
+ attr_reader :receipt
10
+ attr_reader :latest_receipt_info
11
+ attr_reader :latest_receipt
12
+
13
+
14
+ def initialize(attributes)
15
+ @status = attributes['status']
16
+ @environment = attributes['environment']
17
+ @receipt = Receipt.new(attributes['receipt'])
18
+ @latest_receipt_info = []
19
+ if attributes['latest_receipt_info']
20
+ attributes['latest_receipt_info'].each do |transaction_receipt_attributes|
21
+ @latest_receipt_info << TransactionReceipt.new(transaction_receipt_attributes)
22
+ end
23
+ end
24
+ @latest_receipt = attributes['latest_receipt']
25
+ end
26
+
27
+ def is_subscription_active?
28
+ if @latest_receipt_info.last
29
+ @latest_receipt_info.last.expires_date >= Date.today
30
+ else
31
+ false
32
+ end
33
+ end
34
+
35
+ def latest_expiry_date
36
+ @latest_receipt_info.last.expires_date if @latest_receipt_info.last
37
+ end
38
+
39
+ class VerificationError < StandardError
40
+ attr_accessor :code
41
+
42
+ def initialize(code)
43
+ @code = Integer(code)
44
+ end
45
+
46
+ def message
47
+ case @code
48
+ when 21000
49
+ "The App Store could not read the JSON object you provided."
50
+ when 21002
51
+ "The data in the receipt-data property was malformed."
52
+ when 21003
53
+ "The receipt could not be authenticated."
54
+ when 21004
55
+ "The shared secret you provided does not match the shared secret on file for your account."
56
+ when 21005
57
+ "The receipt server is not currently available."
58
+ when 21006
59
+ "This receipt is valid but the subscription has expired. When this status code is returned to your server, the receipt data is also decoded and returned as part of the response."
60
+ when 21007
61
+ "This receipt is a sandbox receipt, but it was sent to the production service for verification."
62
+ when 21008
63
+ "This receipt is a production receipt, but it was sent to the sandbox service for verification."
64
+ else
65
+ "Unknown Error: #{@code}"
66
+ end
67
+ end
68
+ end # end VerificationError
69
+
70
+ end # class
71
+ end # module
72
+
73
+
74
+ # Sample JSON Response
75
+ #
76
+ # {
77
+ # "status": 0,
78
+ # "environment": "Sandbox",
79
+ # "receipt": {
80
+ # "receipt_type": "ProductionSandbox",
81
+ # "adam_id": 0,
82
+ # "app_item_id": 0,
83
+ # "bundle_id": "your_product_id",
84
+ # "application_version": "58",
85
+ # "download_id": 0,
86
+ # "version_external_identifier": 0,
87
+ # "receipt_creation_date": "2016-06-17 01:54:26 Etc/GMT",
88
+ # "receipt_creation_date_ms": "1466128466000",
89
+ # "receipt_creation_date_pst": "2016-06-16 18:54:26 America/Los_Angeles",
90
+ # "request_date": "2016-06-17 17:34:41 Etc/GMT",
91
+ # "request_date_ms": "1466184881174",
92
+ # "request_date_pst": "2016-06-17 10:34:41 America/Los_Angeles",
93
+ # "original_purchase_date": "2013-08-01 07:00:00 Etc/GMT",
94
+ # "original_purchase_date_ms": "1375340400000",
95
+ # "original_purchase_date_pst": "2013-08-01 00:00:00 America/Los_Angeles",
96
+ # "original_application_version": "1.0",
97
+ # "in_app": [
98
+ # {
99
+ # "quantity": "1",
100
+ # "product_id": "com.everlance.everlance.pro.monthly.test",
101
+ # "transaction_id": "1000000218147651",
102
+ # "original_transaction_id": "1000000218147500",
103
+ # "purchase_date": "2016-06-17 01:32:28 Etc/GMT",
104
+ # "purchase_date_ms": "1466127148000",
105
+ # "purchase_date_pst": "2016-06-16 18:32:28 America/Los_Angeles",
106
+ # "original_purchase_date": "2016-06-17 01:30:33 Etc/GMT",
107
+ # "original_purchase_date_ms": "1466127033000",
108
+ # "original_purchase_date_pst": "2016-06-16 18:30:33 America/Los_Angeles",
109
+ # "expires_date": "2016-06-17 01:37:28 Etc/GMT",
110
+ # "expires_date_ms": "1466127448000",
111
+ # "expires_date_pst": "2016-06-16 18:37:28 America/Los_Angeles",
112
+ # "web_order_line_item_id": "1000000032727764",
113
+ # "is_trial_period": "false"
114
+ # }
115
+ # ]
116
+ # },
117
+ # "latest_receipt_info": [
118
+ # {
119
+ # "quantity": "1",
120
+ # "product_id": "com.everlance.everlance.pro.monthly.test",
121
+ # "transaction_id": "1000000218147500",
122
+ # "original_transaction_id": "1000000218147500",
123
+ # "purchase_date": "2016-06-17 01:27:28 Etc/GMT",
124
+ # "purchase_date_ms": "1466126848000",
125
+ # "purchase_date_pst": "2016-06-16 18:27:28 America/Los_Angeles",
126
+ # "original_purchase_date": "2016-06-17 01:27:28 Etc/GMT",
127
+ # "original_purchase_date_ms": "1466126848000",
128
+ # "original_purchase_date_pst": "2016-06-16 18:27:28 America/Los_Angeles",
129
+ # "expires_date": "2016-06-17 01:32:28 Etc/GMT",
130
+ # "expires_date_ms": "1466127148000",
131
+ # "expires_date_pst": "2016-06-16 18:32:28 America/Los_Angeles",
132
+ # "web_order_line_item_id": "1000000032727765",
133
+ # "is_trial_period": "true"
134
+ # }
135
+ # ],
136
+ # "latest_receipt": "base 64"
137
+ # }
@@ -0,0 +1,3 @@
1
+ module Monza
2
+ VERSION = "0.1.0"
3
+ end
data/lib/monza.rb ADDED
@@ -0,0 +1,5 @@
1
+ require "monza/version"
2
+ require "monza/client"
3
+ require "monza/verification_response"
4
+ require "monza/receipt"
5
+ require "monza/transaction_receipt"
data/monza.gemspec ADDED
@@ -0,0 +1,35 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'monza/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "monza"
8
+ spec.version = Monza::VERSION
9
+ spec.authors = ["Gabriel Garza"]
10
+ spec.email = ["garzagabriel@gmail.com"]
11
+
12
+ spec.summary = %q{Validate In-App purchase receipts, including auto-renewable subscriptions, with the App Store.}
13
+ spec.description = %q{Validate In-App purchase receipts, including auto-renewable subscriptions, with the App Store.}
14
+ spec.homepage = "https://github.com/gabrielgarza/monza"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
+ # delete this section to allow pushing this gem to any host.
19
+ # if spec.respond_to?(:metadata)
20
+ # spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
21
+ # else
22
+ # raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
+ # end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_dependency "json"
31
+
32
+ spec.add_development_dependency "bundler", "~> 1.10"
33
+ spec.add_development_dependency "rake", "~> 10.0"
34
+ spec.add_development_dependency "rspec"
35
+ end
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: monza
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Gabriel Garza
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-06-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.10'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.10'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Validate In-App purchase receipts, including auto-renewable subscriptions,
70
+ with the App Store.
71
+ email:
72
+ - garzagabriel@gmail.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - ".travis.yml"
80
+ - CODE_OF_CONDUCT.md
81
+ - Gemfile
82
+ - LICENSE.txt
83
+ - README.md
84
+ - Rakefile
85
+ - bin/console
86
+ - bin/setup
87
+ - lib/monza.rb
88
+ - lib/monza/client.rb
89
+ - lib/monza/receipt.rb
90
+ - lib/monza/transaction_receipt.rb
91
+ - lib/monza/verification_response.rb
92
+ - lib/monza/version.rb
93
+ - monza.gemspec
94
+ homepage: https://github.com/gabrielgarza/monza
95
+ licenses:
96
+ - MIT
97
+ metadata: {}
98
+ post_install_message:
99
+ rdoc_options: []
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ requirements: []
113
+ rubyforge_project:
114
+ rubygems_version: 2.4.6
115
+ signing_key:
116
+ specification_version: 4
117
+ summary: Validate In-App purchase receipts, including auto-renewable subscriptions,
118
+ with the App Store.
119
+ test_files: []