amazon-iap 0.0.1 → 0.1.0
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/README.md +1 -1
- data/lib/amazon/iap.rb +1 -0
- data/lib/amazon/iap/client.rb +35 -0
- data/lib/amazon/iap/exceptions.rb +13 -0
- data/lib/amazon/iap/result.rb +24 -0
- data/lib/amazon/iap/version.rb +1 -1
- metadata +4 -1
data/README.md
CHANGED
data/lib/amazon/iap.rb
CHANGED
@@ -0,0 +1,35 @@
|
|
1
|
+
class Amazon::Iap::Client
|
2
|
+
require 'net/http'
|
3
|
+
require 'uri'
|
4
|
+
|
5
|
+
PRODUCTION_HOST = 'https://appstore-sdk.amazon.com'
|
6
|
+
|
7
|
+
def initialize(developer_secret, host=nil)
|
8
|
+
host ||= PRODUCTION_HOST
|
9
|
+
@developer_secret = developer_secret
|
10
|
+
@host = host
|
11
|
+
end
|
12
|
+
|
13
|
+
def verify(user_id, purchase_token, renew_on_failure=true)
|
14
|
+
begin
|
15
|
+
process :verify, user_id, purchase_token
|
16
|
+
rescue Amazon::Iap::Exceptions::ExpiredCredentials => e
|
17
|
+
raise e unless renew_on_failure
|
18
|
+
|
19
|
+
renewal = renew(user_id, purchase_token)
|
20
|
+
verify(user_id, renewal.purchase_token, false)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def renew(user_id, purchase_token)
|
25
|
+
process :renew, user_id, purchase_token
|
26
|
+
end
|
27
|
+
|
28
|
+
protected
|
29
|
+
|
30
|
+
def process(path, user_id, purchase_token)
|
31
|
+
path = "/version/2.0/#{path}/developer/#{@developer_secret}/user/#{user_id}/purchaseToken/#{purchase_token}"
|
32
|
+
uri = URI.parse "#{@host}#{path}"
|
33
|
+
Amazon::Iap::Result.new Net::HTTP.get_response(uri)
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module Amazon
|
2
|
+
module Iap
|
3
|
+
module Exceptions
|
4
|
+
class InvalidTransaction < Exception; end
|
5
|
+
class InvalidSharedSecret < Exception; end
|
6
|
+
class InvalidUserId < Exception; end
|
7
|
+
class InvalidPurchaseToken < Exception; end
|
8
|
+
class ExpiredCredentials < Exception; end
|
9
|
+
class InternalError < Exception; end
|
10
|
+
class General < Exception; end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
class Amazon::Iap::Result
|
2
|
+
attr_accessor :item_type, :sku, :start_date, :start_time, :end_date, :end_time, :purchase_token
|
3
|
+
|
4
|
+
def initialize(response)
|
5
|
+
case response.code.to_i
|
6
|
+
when 200
|
7
|
+
parsed = JSON.parse(response.body)
|
8
|
+
parsed['startTime'] = parsed['startDate'].nil? ? nil : Time.at(parsed['startDate'] / 1000)
|
9
|
+
parsed['endTime'] = parsed['endDate'].nil? ? nil : Time.at(parsed['endDate'] / 1000)
|
10
|
+
|
11
|
+
parsed.each do |key, value|
|
12
|
+
underscore = key.gsub(/::/, '/').gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').gsub(/([a-z\d])([A-Z])/, '\1_\2').tr('-', '_').downcase
|
13
|
+
send "#{underscore}=", value
|
14
|
+
end
|
15
|
+
when 400 then raise Amazon::Iap::Exceptions::InvalidTransaction
|
16
|
+
when 496 then raise Amazon::Iap::Exceptions::InvalidSharedSecret
|
17
|
+
when 497 then raise Amazon::Iap::Exceptions::InvalidUserId
|
18
|
+
when 498 then raise Amazon::Iap::Exceptions::InvalidPurchaseToken
|
19
|
+
when 499 then raise Amazon::Iap::Exceptions::ExpiredCredentials
|
20
|
+
when 500 then raise Amazon::Iap::Exceptions::InternalError
|
21
|
+
else raise Amazon::Iap::Exceptions::General
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
data/lib/amazon/iap/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: amazon-iap
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -57,6 +57,9 @@ files:
|
|
57
57
|
- Rakefile
|
58
58
|
- amazon-iap.gemspec
|
59
59
|
- lib/amazon/iap.rb
|
60
|
+
- lib/amazon/iap/client.rb
|
61
|
+
- lib/amazon/iap/exceptions.rb
|
62
|
+
- lib/amazon/iap/result.rb
|
60
63
|
- lib/amazon/iap/version.rb
|
61
64
|
homepage: https://github.com/DailyBurn/amazon-iap
|
62
65
|
licenses:
|