amazon-iap 0.2.1 → 0.3.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7e91f2416ee5969443b5ac801d4249c4f653b52b
4
+ data.tar.gz: 0e2d7858bb235eaa21e97a238a26c414a35ead36
5
+ SHA512:
6
+ metadata.gz: 38b757f0e2f13a0ea6cc34c65ce2e175f3b235eab71d90d6ebf23b9bb504e56fafa09efe2acecc147c9385dbc14cd78119e9567fbe8563d0e52eb37d2993f26e
7
+ data.tar.gz: 161514496dbfc228b10bed920b393fdd856571401817cfae5412e106be79933c42a6dc28c291a26040c4271ec2bd2bf30b6292b15771e084947b26ef6d771b8c
data/.gitignore CHANGED
@@ -15,3 +15,4 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
+ .DS_Store
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.
45
+
46
+ For compatibility purposes, the `verify` method is an alias for `verify_v1`.
42
47
 
43
- By default, the `verify` method will automatically try to renew expired tokens, and will recall `verify`
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"
@@ -91,6 +102,19 @@ rescue Amazon::Iap::Exceptions::InternalError => e
91
102
  end
92
103
  ```
93
104
 
105
+ For convenience, all exceptions inherit from `Amazon::Iap::Exceptions::Exception` so you can rescue from
106
+ any exception (or as a default):
107
+
108
+ ```ruby
109
+ begin
110
+ result = client.verify 'some-user-id', 'some-purchase-token'
111
+ rescue Amazon::Iap::Exceptions::InternalError => e
112
+ # enqueue to try again later
113
+ rescue Amazon::Iap::Exceptions::Exception => e
114
+ # log the exception
115
+ end
116
+ ```
117
+
94
118
  ## Contributing
95
119
 
96
120
  1. Fork it
@@ -1,35 +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
- host ||= PRODUCTION_HOST
9
8
  @developer_secret = developer_secret
10
- @host = host
9
+ @host = host || PRODUCTION_HOST
11
10
  end
12
-
13
- def verify(user_id, purchase_token, renew_on_failure=true)
11
+
12
+ def verify_v1(user_id, purchase_token, renew_on_failure=true)
14
13
  begin
15
- process :verify, user_id, purchase_token
14
+ process_v1 :verify, user_id, purchase_token
16
15
  rescue Amazon::Iap::Exceptions::ExpiredCredentials => e
17
16
  raise e unless renew_on_failure
18
-
17
+
19
18
  renewal = renew(user_id, purchase_token)
20
19
  verify(user_id, renewal.purchase_token, false)
21
20
  end
22
21
  end
23
-
24
- def renew(user_id, purchase_token)
25
- 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
26
26
  end
27
-
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
+
28
36
  protected
29
-
30
- def process(path, user_id, purchase_token)
37
+
38
+ def process_v1(path, user_id, purchase_token)
31
39
  path = "/version/2.0/#{path}/developer/#{@developer_secret}/user/#{user_id}/purchaseToken/#{purchase_token}"
32
40
  uri = URI.parse "#{@host}#{path}"
33
- Amazon::Iap::Result.new Net::HTTP.get_response(uri)
34
- end
35
- end
41
+ req = Net::HTTP::Get.new uri.request_uri
42
+ res = Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') { |http| http.request req }
43
+ Amazon::Iap::Result.new res
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.1"
3
+ VERSION = "0.3.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,20 +1,18 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: amazon-iap
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
5
- prerelease:
4
+ version: 0.3.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - DailyBurn
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-09-17 00:00:00.000000000 Z
11
+ date: 2016-08-23 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: bundler
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
17
  - - ~>
20
18
  - !ruby/object:Gem::Version
@@ -22,7 +20,6 @@ dependencies:
22
20
  type: :development
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
24
  - - ~>
28
25
  - !ruby/object:Gem::Version
@@ -30,17 +27,15 @@ dependencies:
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: rake
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ! '>='
31
+ - - '>='
36
32
  - !ruby/object:Gem::Version
37
33
  version: '0'
38
34
  type: :development
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ! '>='
38
+ - - '>='
44
39
  - !ruby/object:Gem::Version
45
40
  version: '0'
46
41
  description: Verify Amazon in app purchases
@@ -64,26 +59,25 @@ files:
64
59
  homepage: https://github.com/DailyBurn/amazon-iap
65
60
  licenses:
66
61
  - MIT
62
+ metadata: {}
67
63
  post_install_message:
68
64
  rdoc_options: []
69
65
  require_paths:
70
66
  - lib
71
67
  required_ruby_version: !ruby/object:Gem::Requirement
72
- none: false
73
68
  requirements:
74
- - - ! '>='
69
+ - - '>='
75
70
  - !ruby/object:Gem::Version
76
71
  version: '0'
77
72
  required_rubygems_version: !ruby/object:Gem::Requirement
78
- none: false
79
73
  requirements:
80
- - - ! '>='
74
+ - - '>='
81
75
  - !ruby/object:Gem::Version
82
76
  version: '0'
83
77
  requirements: []
84
78
  rubyforge_project:
85
- rubygems_version: 1.8.25
79
+ rubygems_version: 2.0.14
86
80
  signing_key:
87
- specification_version: 3
81
+ specification_version: 4
88
82
  summary: Verify Amazon in app purchases
89
83
  test_files: []