amazon-iap 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,7 @@
1
1
  ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- NGY4MmIwN2MwMzg5YzcyZWFhOTk1NjE3Nzg4MjhlNzllZWZmMjc2Mg==
5
- data.tar.gz: !binary |-
6
- ODVlMGFhZWFmNzkxYzQ3MmQ3Y2M1NjhjOWFkMTRhY2VhYWM0MjZiZg==
2
+ SHA1:
3
+ metadata.gz: 7e91f2416ee5969443b5ac801d4249c4f653b52b
4
+ data.tar.gz: 0e2d7858bb235eaa21e97a238a26c414a35ead36
7
5
  SHA512:
8
- metadata.gz: !binary |-
9
- YTdmYjUxMDdmMWZjMWZjMDc4ZTk0ZTZkNzI3NTc2ZWJmYmFlNTg4NGNhMzAw
10
- MzhiOWRjMGE1OWE4MjAyOTg2ZGE1NmNiNDc1NjlkNzdjNTE3YzBmNWNmYWI4
11
- YzMzYzZiODQ2NTEwZWI2Y2FlYWFmMzQ5MGYwMTYwODlkM2JhMjk=
12
- data.tar.gz: !binary |-
13
- YjljZDgxNmMyYzRhOTFhMWQ0YTUyNmUxNzY2MmM1MjAxNzMxYTVjNmZkZjEw
14
- MDcwMmRjMWQzNDM5NTNjNDQyNWIzNDhiOWZhNGYwM2FlOGRjNjIxZjg2Yzg5
15
- OTBmZmMzMDc3YzcyMTUwMTI3Zjc0NGExYzVjZjhkZmFhOWE1N2U=
6
+ metadata.gz: 38b757f0e2f13a0ea6cc34c65ce2e175f3b235eab71d90d6ebf23b9bb504e56fafa09efe2acecc147c9385dbc14cd78119e9567fbe8563d0e52eb37d2993f26e
7
+ data.tar.gz: 161514496dbfc228b10bed920b393fdd856571401817cfae5412e106be79933c42a6dc28c291a26040c4271ec2bd2bf30b6292b15771e084947b26ef6d771b8c
data/README.md CHANGED
@@ -1,8 +1,10 @@
1
1
  # Amazon::Iap
2
2
 
3
- This gem is a simple implementation of the Amazon receipt verfication service outlined
4
- [here](https://developer.amazon.com/sdk/in-app-purchasing/documentation/rvs.html).
3
+ This gem is a simple implementation of the Amazon Receipt Verfication Service. It supports both V1 and V2 APIs.
5
4
 
5
+ [V1 spec](https://developer.amazon.com/sdk/in-app-purchasing/documentation/rvs.html)
6
+
7
+ [V2 spec](https://developer.amazon.com/public/apis/earn/in-app-purchasing/docs-v2/verifying-receipts-in-iap-2.0)
6
8
 
7
9
  ## Installation
8
10
 
@@ -33,19 +35,28 @@ client = Amazon::Iap::Client.new 'my_developer_secret', 'http://iap-staging.doma
33
35
  client = Amazon::Iap::Client.new 'my_developer_secret' # production server
34
36
  ```
35
37
 
36
- From there, you can call either `verify` or `renew` on the client and pass in the user id and purchase token:
38
+ From there, you can call either `verify_v1` or `verify_v2` on the client and pass in the user id and purchase token:
37
39
 
38
40
  ```ruby
39
- result = client.verify 'some-user-id', 'some-purchase-token'
40
- result = client.renew 'some-user-id', 'some-purchase-token'
41
+ result = client.verify_v1 'some-user-id', 'some-purchase-token'
42
+ result = client.verify_v2 'some-user-id', 'some-purchase-token'
41
43
  ```
44
+ Both methods will return the same fields in the same place, while the v2 one will supply some additional data. Mapping is done internally to provide a uniform interface between versions.
42
45
 
43
- By default, the `verify` method will automatically try to renew expired tokens, and will recall `verify`
46
+ For compatibility purposes, the `verify` method is an alias for `verify_v1`.
47
+
48
+ By default, the `verify_v1` method will automatically try to renew expired tokens, and will recall `verify_v1`
44
49
  against the new token returned. If you do not want this behavior, simply pass in `false` as the third
45
50
  attribute to the `verify` method:
46
51
 
47
52
  ```ruby
48
- result = client.verify 'some-user-id', 'some-purchase-token', false
53
+ result = client.verify_v1 'some-user-id', 'some-purchase-token', false
54
+ ```
55
+
56
+ You can also manually renew a token with
57
+
58
+ ```ruby
59
+ result = client.renew 'some-user-id', 'some-purchase-token'
49
60
  ```
50
61
 
51
62
  ## Returned Values
@@ -55,7 +66,7 @@ of the hash keys returned in the JSON object. For convenience, we also add `sta
55
66
  which are `Time` representations of the milliseconds returned in `start_date` and `end_date` respectively. E.g.,
56
67
 
57
68
  ```ruby
58
- result = client.verify 'some-user-id', 'some-purchase-token'
69
+ result = client.verify_v2 'some-user-id', 'some-purchase-token'
59
70
 
60
71
  result.class.name # "Amazon::Iap::Result"
61
72
  result.item_type # "SUBSCRIPTION"
@@ -1,36 +1,45 @@
1
1
  class Amazon::Iap::Client
2
2
  require 'net/http'
3
3
  require 'uri'
4
-
4
+
5
5
  PRODUCTION_HOST = 'https://appstore-sdk.amazon.com'
6
-
6
+
7
7
  def initialize(developer_secret, host=nil)
8
8
  @developer_secret = developer_secret
9
9
  @host = host || PRODUCTION_HOST
10
10
  end
11
-
12
- def verify(user_id, purchase_token, renew_on_failure=true)
11
+
12
+ def verify_v1(user_id, purchase_token, renew_on_failure=true)
13
13
  begin
14
- process :verify, user_id, purchase_token
14
+ process_v1 :verify, user_id, purchase_token
15
15
  rescue Amazon::Iap::Exceptions::ExpiredCredentials => e
16
16
  raise e unless renew_on_failure
17
-
17
+
18
18
  renewal = renew(user_id, purchase_token)
19
19
  verify(user_id, renewal.purchase_token, false)
20
20
  end
21
21
  end
22
-
23
- def renew(user_id, purchase_token)
24
- process :renew, user_id, purchase_token
22
+ alias_method :verify, :verify_v1
23
+
24
+ def renew_v1(user_id, purchase_token)
25
+ process_v1 :renew, user_id, purchase_token
25
26
  end
26
-
27
+ alias_method :renew, :renew_v1
28
+
29
+ def verify_v2(user_id, receipt_id)
30
+ uri = URI.parse "#{@host}/version/1.0/verifyReceiptId/developer/#{@developer_secret}/user/#{user_id}/receiptId/#{receipt_id}"
31
+ req = Net::HTTP::Get.new uri.request_uri
32
+ res = Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') { |http| http.request req }
33
+ Amazon::Iap::Result.new res
34
+ end
35
+
27
36
  protected
28
-
29
- def process(path, user_id, purchase_token)
37
+
38
+ def process_v1(path, user_id, purchase_token)
30
39
  path = "/version/2.0/#{path}/developer/#{@developer_secret}/user/#{user_id}/purchaseToken/#{purchase_token}"
31
40
  uri = URI.parse "#{@host}#{path}"
32
41
  req = Net::HTTP::Get.new uri.request_uri
33
42
  res = Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') { |http| http.request req }
34
43
  Amazon::Iap::Result.new res
35
- end
36
- end
44
+ end
45
+ end
@@ -1,21 +1,27 @@
1
1
  class Amazon::Iap::Result
2
- attr_accessor :item_type, :sku, :start_date, :start_time, :end_date, :end_time, :purchase_token
3
-
2
+ attr_accessor :item_type, :sku, :start_date, :start_time, :end_date, :end_time, :purchase_token, :test_transaction, :beta_product, :parent_product_id, :term, :term_sku, :quantity
3
+ V2_V1_MAPPING = {
4
+ productType: :itemType,
5
+ productId: :sku,
6
+ receiptId: :purchaseToken,
7
+ purchaseDate: :startDate,
8
+ cancelDate: :endDate,
9
+ }
4
10
  def initialize(response)
5
11
  case response.code.to_i
6
12
  when 200
7
13
  parsed = JSON.parse(response.body)
8
-
14
+ V2_V1_MAPPING.each { |v2,v1| parsed[v1.to_s] = parsed.delete(v2.to_s) if parsed.has_key?(v2.to_s) }
9
15
  if parsed.has_key? 'startDate'
10
16
  parsed['startTime'] = parsed['startDate'].nil? ? nil : Time.at(parsed['startDate'] / 1000)
11
17
  end
12
18
  if parsed.has_key? 'endDate'
13
19
  parsed['endTime'] = parsed['endDate'].nil? ? nil : Time.at(parsed['endDate'] / 1000)
14
20
  end
15
-
21
+
16
22
  parsed.each do |key, value|
17
23
  underscore = key.gsub(/::/, '/').gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2').gsub(/([a-z\d])([A-Z])/, '\1_\2').tr('-', '_').downcase
18
- send "#{underscore}=", value
24
+ send "#{underscore}=", value if respond_to?("#{underscore}=")
19
25
  end
20
26
  when 400 then raise Amazon::Iap::Exceptions::InvalidTransaction
21
27
  when 496 then raise Amazon::Iap::Exceptions::InvalidSharedSecret
@@ -26,4 +32,4 @@ class Amazon::Iap::Result
26
32
  else raise Amazon::Iap::Exceptions::General
27
33
  end
28
34
  end
29
- end
35
+ end
@@ -1,5 +1,5 @@
1
1
  module Amazon
2
2
  module Iap
3
- VERSION = "0.2.2"
3
+ VERSION = "0.3.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: amazon-iap
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - DailyBurn
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-04 00:00:00.000000000 Z
11
+ date: 2016-08-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -28,14 +28,14 @@ dependencies:
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ! '>='
31
+ - - '>='
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ! '>='
38
+ - - '>='
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  description: Verify Amazon in app purchases
@@ -66,17 +66,17 @@ require_paths:
66
66
  - lib
67
67
  required_ruby_version: !ruby/object:Gem::Requirement
68
68
  requirements:
69
- - - ! '>='
69
+ - - '>='
70
70
  - !ruby/object:Gem::Version
71
71
  version: '0'
72
72
  required_rubygems_version: !ruby/object:Gem::Requirement
73
73
  requirements:
74
- - - ! '>='
74
+ - - '>='
75
75
  - !ruby/object:Gem::Version
76
76
  version: '0'
77
77
  requirements: []
78
78
  rubyforge_project:
79
- rubygems_version: 2.1.10
79
+ rubygems_version: 2.0.14
80
80
  signing_key:
81
81
  specification_version: 4
82
82
  summary: Verify Amazon in app purchases