ProMotion-iap 0.1.0 → 0.2.0

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: b7e41cfc55cf06853a59e0f826b7161765189039
4
- data.tar.gz: a2eba57b0b4f0d42ddd1fde6dd0f38c27c4f1bb6
3
+ metadata.gz: a58cbbb9a1bfea9bed76bf23bcf61cb8c538bc17
4
+ data.tar.gz: 2dd27741089d27722aa8a4e7e4fa2fb1153cf75c
5
5
  SHA512:
6
- metadata.gz: ea6ebbe840ca8d1718d22a7f48706084917b62f1d8473d5e21550bdebdc53073bb76049def4d3f3fff77a613e0e0d410a21d66902c704f92abd61d5437bac9f1
7
- data.tar.gz: 391efff17e8eb4ee1d5d9cd92f34469d59c6de8d8b9eb24fd8899437e2de2579cddf29e710a002f1fdb9a7d72436ad0f1192dd2fcb94dc3c04caed2c66bc8ce2
6
+ metadata.gz: a15d8be62ab306ff6fb4fa1ac41736a62f92db1c66e27a1c7a330ec510293c8549c020e459575372c6cd1deaded3cc7709e57e0193c51756524afb727e49140c
7
+ data.tar.gz: c681697056432c9dee095f61392f4dd7b60bb10a4d9da50f13c081364cda6b26ac89444bcbf375757c3b609d98dc14cd06f78914367ec9be51e44716750a0a46
data/README.md CHANGED
@@ -1,7 +1,10 @@
1
1
  # ProMotion-iap
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/ProMotion-iap.svg)](http://badge.fury.io/rb/ProMotion-iap)
4
+ [![Build Status](https://travis-ci.org/clearsightstudio/ProMotion-iap.svg)](https://travis-ci.org/clearsightstudio/ProMotion-iap)
5
+
3
6
  ProMotion-iap is in-app purchase notification support for the
4
- popular RubyMotion gem [ProMotion](https://github.com/clearsightstudio/ProMotion).
7
+ popular RubyMotion gem [ProMotion](https://github.com/clearsightstudio/ProMotion).
5
8
 
6
9
  ## Installation
7
10
 
@@ -11,7 +14,79 @@ gem 'ProMotion-iap'
11
14
 
12
15
  ## Usage
13
16
 
14
- ### AppDelegate
17
+ ### PM::IAP::Product Class
18
+
19
+ The `Product` class is an abstraction layer that provides a simpler interface when working with a single IAP product.
20
+ If you are dealing with multiple products you will want to use the IAP Module directly (documented below).
21
+
22
+ ```ruby
23
+ class PurchaseScreen < PM::Screen
24
+
25
+ def on_load
26
+
27
+ product = PM::IAP::Product.new("productid")
28
+
29
+ product.retrieve do |product, error|
30
+ # product looks something like the following
31
+ {
32
+ product_id: "productid1",
33
+ title: "title",
34
+ description: "description",
35
+ price: <BigDecimal 0.99>,
36
+ formatted_price: "$0.99",
37
+ price_locale: <NSLocale>,
38
+ downloadable: false,
39
+ download_content_lengths: <?>, # TODO: ?
40
+ download_content_version: <?>, # TODO: ?
41
+ product: <SKProduct>
42
+ }
43
+ end
44
+
45
+ product.purchase do |status, transaction|
46
+ case status
47
+ when :in_progress
48
+ # Usually do nothing, maybe a spinner
49
+ when :deferred
50
+ # Waiting on a prompt to the user
51
+ when :purchased
52
+ # Notify the user, update any affected UI elements
53
+ when :canceled
54
+ # They just canceled, no big deal.
55
+ when :error
56
+ # Failed to purchase
57
+ transaction.error.localizedDescription # => error message
58
+ end
59
+ end
60
+
61
+ product.restore do |status, product|
62
+ if status == :restored
63
+ # Update your UI, notify the user
64
+ end
65
+ end
66
+
67
+ end
68
+ end
69
+
70
+ ```
71
+
72
+ #### Product.new(product_id)
73
+
74
+ Stores the product_id for use in the instance methods.
75
+
76
+ #### retrieve(&callback)
77
+
78
+ Retrieves the product.
79
+
80
+ #### purchase(&callback)
81
+
82
+ Begins a purchase of the product.
83
+
84
+ #### restore(&callback)
85
+
86
+ Begins a restoration of the previously purchased product.
87
+
88
+
89
+ ### IAP Module
15
90
 
16
91
  Include `PM::IAP` to add some in-app purchase methods to a screen, app delegate, or other class.
17
92
 
@@ -22,7 +97,7 @@ class PurchaseScreen < PM::Screen
22
97
 
23
98
  def on_load
24
99
 
25
- retrieve_iaps([ "productid1", "productid2" ]) do |products, error|
100
+ retrieve_iaps [ "productid1", "productid2" ] do |products, error|
26
101
  # products looks something like the following
27
102
  [{
28
103
  product_id: "productid1",
@@ -54,7 +129,7 @@ class PurchaseScreen < PM::Screen
54
129
  end
55
130
  end
56
131
 
57
- restore_iaps do |status, products|
132
+ restore_iaps "productid" do |status, products|
58
133
  if status == :restored
59
134
  # Update your UI, notify the user
60
135
  end
@@ -65,19 +140,42 @@ class PurchaseScreen < PM::Screen
65
140
  end
66
141
  ```
67
142
 
68
- #### purchase_iap(*product_ids, &callback)
143
+ #### retrieve_iaps(`*`product_ids, &callback)
144
+
145
+ Retrieves in-app purchase products in an array of mapped hashes. The callback method should accept `products` and `error`.
146
+
147
+
148
+ #### purchase_iaps(`*`product_ids, &callback)
149
+
150
+ Prompts the user to login to their Apple ID and complete the purchase. The callback method should accept `status` and `transaction`.
151
+ The callback method will be called several times with the various statuses in the process. If more than one `product_id` is provided
152
+ the callback method will be called several times per product with the applicable transaction.
153
+
154
+
155
+ #### restore_iaps(`*`product_ids, &callback)
156
+
157
+ Restores a previously purchased IAP to the user (for example if they have upgraded their device). This relies on the Apple ID the user
158
+ enters at the prompt. Unfortunately, if there is no purchase to restore for the signed-in account, no error message is generated and
159
+ will fail silently.
160
+
69
161
 
70
- TODO
71
162
 
72
- ```ruby
73
163
 
74
- ```
75
164
 
76
165
  Find the Product ID here:
77
166
 
78
167
  ![product id](http://clrsight.co/jh/2015-02-11-d8xw6.png?+)
79
168
 
80
169
 
170
+ ## Authors
171
+ | Contributor | Twitter |
172
+ | Jamon Holmgren | [@jamonholmgren](http://twitter.com/jamonholmgren) |
173
+ | Kevin VanGelder | [@kevinvangelder](http://twitter.com/kevin_vangelder) |
174
+
175
+ ## Inspired By
176
+ - [Helu](https://github.com/ivanacostarubio/helu)
177
+ - [Mark Rickert's Code Example](https://github.com/OTGApps/TheShowCloser/blob/master/app/helpers/iap_helper.rb)
178
+
81
179
 
82
180
  ## Contributing
83
181
 
@@ -7,5 +7,6 @@ end
7
7
  Motion::Project::App.setup do |app|
8
8
  lib_dir_path = File.dirname(File.expand_path(__FILE__))
9
9
  app.files << File.join(lib_dir_path, "ProMotion/iap.rb")
10
+ app.files << File.join(lib_dir_path, "ProMotion/product.rb")
10
11
  app.frameworks << "StoreKit"
11
12
  end
@@ -2,9 +2,9 @@ module ProMotion
2
2
  module IAP
3
3
  attr_accessor :completion_handlers
4
4
 
5
- def purchase_iap(product_id, &callback)
5
+ def purchase_iaps(*product_ids, &callback)
6
6
  iap_setup
7
- retrieve_iaps product_id do |products|
7
+ retrieve_iaps product_ids do |products|
8
8
  products.each do |product|
9
9
  self.completion_handlers["purchase-#{product[:product_id]}"] = callback
10
10
  payment = SKPayment.paymentWithProduct(product[:product])
@@ -12,16 +12,18 @@ module ProMotion
12
12
  end
13
13
  end
14
14
  end
15
+ alias purchase_iap purchase_iaps
15
16
 
16
- def restore_iaps(product_id, &callback)
17
+ def restore_iaps(*product_ids, &callback)
17
18
  iap_setup
18
- retrieve_iaps product_id do |products|
19
+ retrieve_iaps product_ids do |products|
19
20
  products.each do |product|
20
21
  self.completion_handlers["restore-#{product[:product_id]}"] = callback
21
22
  SKPaymentQueue.defaultQueue.restoreCompletedTransactions
22
23
  end
23
24
  end
24
25
  end
26
+ alias restore_iap restore_iaps
25
27
 
26
28
  def retrieve_iaps(*product_ids, &callback)
27
29
  iap_setup
@@ -76,13 +78,28 @@ module ProMotion
76
78
  num_formatter.stringFromNumber price
77
79
  end
78
80
 
81
+ def iap_callback(status, transaction, finish=false)
82
+ product_id = transaction.payment.productIdentifier
83
+ if self.completion_handlers["purchase-#{product_id}"]
84
+ self.completion_handlers["purchase-#{product_id}"].call status, transaction
85
+ self.completion_handlers["purchase-#{product_id}"] = nil if finish
86
+ end
87
+ if self.completion_handlers["restore-#{product_id}"]
88
+ self.completion_handlers["restore-#{product_id}"].call status, transaction
89
+ self.completion_handlers["restore-#{product_id}"] = nil if finish
90
+ end
91
+ SKPaymentQueue.defaultQueue.finishTransaction(transaction) if finish
92
+ end
93
+
79
94
  public
80
95
 
81
96
  # SKProductsRequestDelegate methods
82
97
 
83
98
  def productsRequest(_, didReceiveResponse:response)
84
99
  unless response.invalidProductIdentifiers.empty?
85
- PM.logger.error "PM::IAP Error - invalid product identifier(s) '#{response.invalidProductIdentifiers.join("', '")}' for application identifier #{NSBundle.mainBundle.infoDictionary['CFBundleIdentifier'].inspect}"
100
+ red = "\e[0;31m"
101
+ color_off = "\e[0m"
102
+ puts "#{red}PM::IAP Error - invalid product identifier(s) '#{response.invalidProductIdentifiers.join("', '")}' for application identifier #{NSBundle.mainBundle.infoDictionary['CFBundleIdentifier'].inspect}#{color_off}"
86
103
  end
87
104
  retrieved_iaps_handler(response.products, &self.completion_handlers["retrieve_iaps"]) if self.completion_handlers["retrieve_iaps"]
88
105
  @products_request = nil
@@ -115,18 +132,6 @@ module ProMotion
115
132
  end
116
133
  end
117
134
 
118
- def iap_callback(status, transaction, finish=false)
119
- product_id = transaction.payment.productIdentifier
120
- if self.completion_handlers["purchase-#{product_id}"]
121
- self.completion_handlers["purchase-#{product_id}"].call status, transaction
122
- self.completion_handlers["purchase-#{product_id}"] = nil if finish
123
- end
124
- if self.completion_handlers["restore-#{product_id}"]
125
- self.completion_handlers["restore-#{product_id}"].call status, transaction
126
- self.completion_handlers["restore-#{product_id}"] = nil if finish
127
- end
128
- SKPaymentQueue.defaultQueue.finishTransaction(transaction) if finish
129
- end
130
-
131
135
  end
132
136
  end
137
+ ::PM = ProMotion unless defined?(::PM)
@@ -0,0 +1,27 @@
1
+ module ProMotion
2
+ class IAP::Product
3
+ include PM::IAP
4
+
5
+ attr_reader :product_id
6
+
7
+ def initialize(product_id)
8
+ @product_id = product_id
9
+ end
10
+
11
+ def retrieve(&callback)
12
+ retrieve_iaps(product_id) do |products, error|
13
+ callback.call products.first, error
14
+ end
15
+ end
16
+
17
+ def purchase(&callback)
18
+ purchase_iaps(product_id, &callback)
19
+ end
20
+
21
+ def restore(&callback)
22
+ restore_iaps(product_id) do |status, products|
23
+ callback.call status, products.find{|p| p[:product_id] == product_id }
24
+ end
25
+ end
26
+ end
27
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ProMotion-iap
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jamon Holmgren
@@ -11,20 +11,6 @@ bindir: bin
11
11
  cert_chain: []
12
12
  date: 2015-02-12 00:00:00.000000000 Z
13
13
  dependencies:
14
- - !ruby/object:Gem::Dependency
15
- name: ProMotion
16
- requirement: !ruby/object:Gem::Requirement
17
- requirements:
18
- - - "~>"
19
- - !ruby/object:Gem::Version
20
- version: '2.0'
21
- type: :runtime
22
- prerelease: false
23
- version_requirements: !ruby/object:Gem::Requirement
24
- requirements:
25
- - - "~>"
26
- - !ruby/object:Gem::Version
27
- version: '2.0'
28
14
  - !ruby/object:Gem::Dependency
29
15
  name: motion-stump
30
16
  requirement: !ruby/object:Gem::Requirement
@@ -78,6 +64,7 @@ files:
78
64
  - README.md
79
65
  - lib/ProMotion-iap.rb
80
66
  - lib/ProMotion/iap.rb
67
+ - lib/ProMotion/product.rb
81
68
  homepage: https://github.com/clearsightstudio/ProMotion-iap
82
69
  licenses:
83
70
  - MIT