-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathInAppService.h
353 lines (309 loc) · 10.8 KB
/
InAppService.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#pragma once
#include <string>
#include <vector>
#include <map>
#include <functional>
namespace ludei { namespace inapps {
using std::string;
/**
* Defines an InApp product.
*/
struct InAppProduct {
/**
* The id of the product.
*/
string productId;
/**
* The title of the product.
*/
string title;
/**
* The description of the product.
*/
string description;
/**
* The price of the product in local currency.
*/
string localizedPrice;
/**
* The price os the product.
*/
double price;
};
/**
* Defines an InApp purchase.
*/
struct InAppPurchase {
/**
* The transaction id.
*/
string transactionId;
/**
* The id of the product.
*/
string productId;
/**
* The date when the purchase takes place.
*/
uint64_t purchaseDate;
/**
* The quantity of the product.
*/
int32_t quantity;
};
/**
* Defines an InApp purchase observer.
*/
class InAppPurchaseObserver;
/**
* Defines the different InApp provider.
*/
enum class InAppProvider {
/**
* Automatic. The provider is automatically selected depending on linked classes.
*/
AUTO,
/**
* App Store.
*/
APP_STORE,
/**
* Google Play.
*/
GOOGLE_PLAY,
/**
* Amazon App Store.
*/
AMAZON_APPSTORE,
};
/**
* The InApp service.
*/
class InAppService {
public:
/**
* Defines an error.
*/
struct Error {
Error() : code(0) {};
/**
* The code of the error
*/
int32_t code;
/**
* The message that describes the error.
*/
string message;
/**
* Returns true if there is no error.
*
* @return True if the error is empty.
*/
inline bool empty() const { return code == 0 && message.empty();}
};
/**
* The fetch callback.
*
* @param products The fetched products.
* @param error The error description if the process fails.
*/
typedef std::function<void(const std::vector<InAppProduct> & products, const InAppService::Error & error)> FetchCallback;
/**
* The restore callback.
*
* @param error The error description if the process fails.
*/
typedef std::function<void(const InAppService::Error & error)> RestoreCallback;
/**
* The purchase callback.
*
* @param purchase The purchase information.
* @param error The error description if the process fails.
*/
typedef std::function<void(const InAppPurchase & purchase, const InAppService::Error & error)> PurchaseCallback;
/**
* The consume callback.
*
* @param consumed The quantity of product consumed.
* @param error The error description if the process fails.
*/
typedef std::function<void(int32_t consumed, const InAppService::Error & error)> ConsumeCallback;
/**
* Defines the validation completion.
*
* @param receipt The receipt.
* @param productId The id of the product.
* @param completion Completion.
*/
typedef std::function<void(const InAppService::Error & error)> ValidationCompletion;
/**
* Defines the validation handler.
*
* @param receipt The receipt.
* @param productId The id of the product.
* @param completion Completion.
*/
typedef std::function<void(const string & receipt, const string & productId, const ValidationCompletion & completion)>ValidationHandler;
/**
* Creates a new InAppService
*
* @param provider The InApp Provider that will be used or AUTO to automatically select the one linked within the binary
* @result The InAppService with the selected provider or NULL if the provider is not available
*/
static InAppService * create(InAppProvider provider = InAppProvider::AUTO);
/**
* Creates a new InAppService
*
* @param className The className of the provider
* @result The InAppService with the selected provider or NULL if the provider is not available
*/
static InAppService * create(const char * className);
virtual ~InAppService() {};
/**
* Adds an Inapp observer to the process.
*
* @param observer An InApp observer.
*/
virtual void addPurchaseObserver(InAppPurchaseObserver * observer) = 0;
/**
* Removes an Inapp observer from the process.
*
* @param observer An InApp observer.
*/
virtual void removePurchaseObserver(InAppPurchaseObserver * observer) = 0;
/**
* Starts the service.
* You should call this method when your InAppPurchaseObservers are already registered.
*/
virtual void start() = 0;
/**
* Requests information about products from the remote Store.
* Products are cached in a local DB (@see getProducts)
*
* @param productIds The ids of all the fetched products.
* @param callback The FetchCallback.
*/
virtual void fetchProducts(const std::vector<string> & productIds, const FetchCallback & callback) = 0;
/**
* Returns the cached products.
*
* @return The products.
*/
virtual std::vector<InAppProduct> getProducts() const = 0;
/**
* Asociates an alias to each productId.
*
* @param productsMap The map containing the aliases as keys and the real productId as values.
*/
virtual void mapProductIds(const std::map<string, string> & productsMap) = 0;
/**
* Gets a product given its id.
*
* @param productId The id of the product.
* @param product The product returned by reference
*
* @return True if the products exits.
*/
virtual bool productForId(const string & productId, InAppProduct * product) const = 0;
/**
* Check if a product has been previously purchased.
*
* @param productId the id of the product.
*
* @return True if the product has been purchased and false otherswise.
*/
virtual bool isPurchased(const string & productId) const = 0;
/**
* Returns the stock of a product given its id.
*
* @param productId The id of the product to check.
*
* @return The stock of the given product.
*/
virtual int32_t stockOfProduct(const string & productId) = 0;
/**
* Restores already completed transactions and purchases.
*
* @param callback The RestoreCallback.
*/
virtual void restorePurchases(const RestoreCallback & callback) = 0;
/**
* Resturs YES if the service is available
*
* @return True if the service is available and False otherwise.
*/
virtual bool canPurchase() const = 0;
/**
* Purchases a quantity of a specific product.
*
* @param productId The id of the product.
* @param quantity The quantity.
* @param callback The PurchaseCallback
*/
virtual void purchase(const string & productId, int32_t quantity, const PurchaseCallback & callback) = 0;
/**
* Purchases a product.
*
* @param productId The id of the product.
* @param callback The PurchaseCallback.
*/
inline void purchase(const string & productId, const PurchaseCallback & callback) {
purchase(productId, 1, callback);
}
/**
* Consumes a consumable item.
*
* @param productId The id of the product.
* @param quantity The quantity.
* @param callback The ConsumeCallback.
*/
virtual void consume(const string & productId, int32_t quantity, const ConsumeCallback & callback) = 0;
/**
* Removes a finished purchase transaction from the queue.
*
* @param transactionId Transaction id.
*/
virtual void finishPurchase(const string & transactionId) = 0;
/**
* Sets a custom purchase validation handler.
* Purchases are always validated to TRUE by default.
* Set a custom validation handler to use you own custom server to validate purchases.
*
* @param handler The validation handler.
*/
virtual void setValidationHandler(const ValidationHandler & handler) = 0;
/**
* Use Ludei's server to validate purchases.
* To enable validation using Ludei's server you first need to create an account in Ludei's Cloud server and create a project with you bundleId.
*/
virtual void setLudeiServerValidationHandler() = 0;
};
/**
* Allows to listen to different events regarding the purchasing process.
*/
class InAppPurchaseObserver {
public:
/**
* Triggered when the purchasing process has started.
*
* @param service The service.
* @param productId The id of the product.
*/
virtual void purchaseStarted(InAppService * service, const string & productId) = 0;
/**
* Triggered when the purchasing process has faild.
*
* @param service The service.
* @param productId The id of the product.
* @param error The reported error description.
*/
virtual void purchaseFailed(InAppService * service, const string & productId, const InAppService::Error & error) = 0;
/**
* Triggered when the purchasing proccess is completed successfully.
*
* @param service The service.
* @param purchase The purchase information.
*/
virtual void purchaseCompleted(InAppService * service, const InAppPurchase & purchase) = 0;
};
} }