2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* in_app_store.mm */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:16:55 +00:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
2020-01-01 10:16:22 +00:00
|
|
|
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
|
2014-02-10 01:10:30 +00:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2018-01-04 23:50:27 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
#ifdef STOREKIT_ENABLED
|
|
|
|
|
2017-04-09 12:18:49 +00:00
|
|
|
#include "in_app_store.h"
|
|
|
|
|
2014-09-03 02:13:40 +00:00
|
|
|
#import <Foundation/Foundation.h>
|
2017-04-09 12:18:49 +00:00
|
|
|
#import <StoreKit/StoreKit.h>
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-10-16 12:26:31 +00:00
|
|
|
InAppStore *InAppStore::instance = NULL;
|
2014-09-03 02:13:40 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
@interface SKProduct (LocalizedPrice)
|
2020-10-16 12:26:31 +00:00
|
|
|
|
2017-04-09 12:18:49 +00:00
|
|
|
@property(nonatomic, readonly) NSString *localizedPrice;
|
2020-10-16 12:26:31 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
//----------------------------------//
|
|
|
|
// SKProduct extension
|
|
|
|
//----------------------------------//
|
|
|
|
@implementation SKProduct (LocalizedPrice)
|
2020-10-16 12:26:31 +00:00
|
|
|
|
2017-04-09 11:22:40 +00:00
|
|
|
- (NSString *)localizedPrice {
|
2014-02-10 01:10:30 +00:00
|
|
|
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
|
|
|
|
[numberFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
|
|
|
|
[numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
|
|
|
|
[numberFormatter setLocale:self.priceLocale];
|
|
|
|
NSString *formattedString = [numberFormatter stringFromNumber:self.price];
|
|
|
|
return formattedString;
|
|
|
|
}
|
2020-10-16 12:26:31 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
@end
|
|
|
|
|
2020-10-16 12:26:31 +00:00
|
|
|
@interface GodotProductsDelegate : NSObject <SKProductsRequestDelegate>
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-10-16 12:26:31 +00:00
|
|
|
@property(nonatomic, strong) NSMutableArray *loadedProducts;
|
|
|
|
@property(nonatomic, strong) NSMutableArray *pendingRequests;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-10-16 12:26:31 +00:00
|
|
|
- (void)performRequestWithProductIDs:(NSSet *)productIDs;
|
|
|
|
- (Error)purchaseProductWithProductID:(NSString *)productID;
|
|
|
|
- (void)reset;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
2020-10-16 12:26:31 +00:00
|
|
|
@implementation GodotProductsDelegate
|
|
|
|
|
|
|
|
- (instancetype)init {
|
|
|
|
self = [super init];
|
|
|
|
|
|
|
|
if (self) {
|
|
|
|
[self godot_commonInit];
|
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)godot_commonInit {
|
|
|
|
self.loadedProducts = [NSMutableArray new];
|
|
|
|
self.pendingRequests = [NSMutableArray new];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)performRequestWithProductIDs:(NSSet *)productIDs {
|
|
|
|
SKProductsRequest *request = [[SKProductsRequest alloc] initWithProductIdentifiers:productIDs];
|
|
|
|
|
|
|
|
request.delegate = self;
|
|
|
|
[self.pendingRequests addObject:request];
|
|
|
|
[request start];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (Error)purchaseProductWithProductID:(NSString *)productID {
|
|
|
|
SKProduct *product = nil;
|
|
|
|
|
|
|
|
NSLog(@"searching for product!");
|
|
|
|
|
|
|
|
if (self.loadedProducts) {
|
|
|
|
for (SKProduct *storedProduct in self.loadedProducts) {
|
|
|
|
if ([storedProduct.productIdentifier isEqualToString:productID]) {
|
|
|
|
product = storedProduct;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!product) {
|
|
|
|
return ERR_INVALID_PARAMETER;
|
|
|
|
}
|
|
|
|
|
|
|
|
NSLog(@"product found!");
|
|
|
|
|
|
|
|
SKPayment *payment = [SKPayment paymentWithProduct:product];
|
|
|
|
[[SKPaymentQueue defaultQueue] addPayment:payment];
|
|
|
|
|
|
|
|
NSLog(@"purchase sent!");
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)reset {
|
|
|
|
[self.loadedProducts removeAllObjects];
|
|
|
|
[self.pendingRequests removeAllObjects];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)request:(SKRequest *)request didFailWithError:(NSError *)error {
|
|
|
|
[self.pendingRequests removeObject:request];
|
|
|
|
|
|
|
|
Dictionary ret;
|
|
|
|
ret["type"] = "product_info";
|
|
|
|
ret["result"] = "error";
|
|
|
|
ret["error"] = String::utf8([error.localizedDescription UTF8String]);
|
|
|
|
|
|
|
|
InAppStore::get_singleton()->_post_event(ret);
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-04-09 12:18:49 +00:00
|
|
|
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response {
|
2020-10-16 12:26:31 +00:00
|
|
|
[self.pendingRequests removeObject:request];
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-04-09 12:18:49 +00:00
|
|
|
NSArray *products = response.products;
|
2020-10-16 12:26:31 +00:00
|
|
|
[self.loadedProducts addObjectsFromArray:products];
|
2020-09-30 19:08:01 +00:00
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
Dictionary ret;
|
|
|
|
ret["type"] = "product_info";
|
|
|
|
ret["result"] = "ok";
|
2017-01-07 08:33:11 +00:00
|
|
|
PoolStringArray titles;
|
|
|
|
PoolStringArray descriptions;
|
|
|
|
PoolRealArray prices;
|
|
|
|
PoolStringArray ids;
|
|
|
|
PoolStringArray localized_prices;
|
2017-11-17 11:35:47 +00:00
|
|
|
PoolStringArray currency_codes;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2019-10-24 14:34:18 +00:00
|
|
|
for (NSUInteger i = 0; i < [products count]; i++) {
|
2017-04-09 12:18:49 +00:00
|
|
|
SKProduct *product = [products objectAtIndex:i];
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-04-09 12:18:49 +00:00
|
|
|
const char *str = [product.localizedTitle UTF8String];
|
2014-02-10 01:10:30 +00:00
|
|
|
titles.push_back(String::utf8(str != NULL ? str : ""));
|
|
|
|
|
|
|
|
str = [product.localizedDescription UTF8String];
|
|
|
|
descriptions.push_back(String::utf8(str != NULL ? str : ""));
|
|
|
|
prices.push_back([product.price doubleValue]);
|
|
|
|
ids.push_back(String::utf8([product.productIdentifier UTF8String]));
|
|
|
|
localized_prices.push_back(String::utf8([product.localizedPrice UTF8String]));
|
2017-11-17 11:35:47 +00:00
|
|
|
currency_codes.push_back(String::utf8([[[product priceLocale] objectForKey:NSLocaleCurrencyCode] UTF8String]));
|
2020-10-16 12:26:31 +00:00
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
ret["titles"] = titles;
|
|
|
|
ret["descriptions"] = descriptions;
|
|
|
|
ret["prices"] = prices;
|
|
|
|
ret["ids"] = ids;
|
|
|
|
ret["localized_prices"] = localized_prices;
|
2017-11-17 11:35:47 +00:00
|
|
|
ret["currency_codes"] = currency_codes;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-01-07 08:33:11 +00:00
|
|
|
PoolStringArray invalid_ids;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-04-09 12:18:49 +00:00
|
|
|
for (NSString *ipid in response.invalidProductIdentifiers) {
|
2014-02-10 01:10:30 +00:00
|
|
|
invalid_ids.push_back(String::utf8([ipid UTF8String]));
|
2020-10-16 12:26:31 +00:00
|
|
|
}
|
|
|
|
|
2014-02-10 01:10:30 +00:00
|
|
|
ret["invalid_ids"] = invalid_ids;
|
|
|
|
|
|
|
|
InAppStore::get_singleton()->_post_event(ret);
|
2020-10-16 12:26:31 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
2020-10-16 12:26:31 +00:00
|
|
|
@interface GodotTransactionsObserver : NSObject <SKPaymentTransactionObserver>
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-10-16 12:26:31 +00:00
|
|
|
@property(nonatomic, assign) BOOL shouldAutoFinishTransactions;
|
|
|
|
@property(nonatomic, strong) NSMutableDictionary *pendingTransactions;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-10-16 12:26:31 +00:00
|
|
|
- (void)finishTransactionWithProductID:(NSString *)productID;
|
|
|
|
- (void)reset;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-10-16 12:26:31 +00:00
|
|
|
@end
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-10-16 12:26:31 +00:00
|
|
|
@implementation GodotTransactionsObserver
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-10-16 12:26:31 +00:00
|
|
|
- (instancetype)init {
|
|
|
|
self = [super init];
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-10-16 12:26:31 +00:00
|
|
|
if (self) {
|
|
|
|
[self godot_commonInit];
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-10-16 12:26:31 +00:00
|
|
|
return self;
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-10-16 12:26:31 +00:00
|
|
|
- (void)godot_commonInit {
|
|
|
|
self.pendingTransactions = [NSMutableDictionary new];
|
|
|
|
}
|
2018-02-22 19:28:37 +00:00
|
|
|
|
2020-10-16 12:26:31 +00:00
|
|
|
- (void)finishTransactionWithProductID:(NSString *)productID {
|
|
|
|
SKPaymentTransaction *transaction = self.pendingTransactions[productID];
|
2018-02-22 19:28:37 +00:00
|
|
|
|
2020-10-16 12:26:31 +00:00
|
|
|
if (transaction) {
|
|
|
|
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
|
|
|
|
}
|
2018-02-22 19:28:37 +00:00
|
|
|
|
2020-10-16 12:26:31 +00:00
|
|
|
self.pendingTransactions[productID] = nil;
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-10-16 12:26:31 +00:00
|
|
|
- (void)reset {
|
|
|
|
[self.pendingTransactions removeAllObjects];
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-04-09 12:18:49 +00:00
|
|
|
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-04-09 11:22:40 +00:00
|
|
|
printf("transactions updated!\n");
|
2017-04-09 12:18:49 +00:00
|
|
|
for (SKPaymentTransaction *transaction in transactions) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
switch (transaction.transactionState) {
|
2017-04-09 11:22:40 +00:00
|
|
|
case SKPaymentTransactionStatePurchased: {
|
|
|
|
printf("status purchased!\n");
|
|
|
|
String pid = String::utf8([transaction.payment.productIdentifier UTF8String]);
|
|
|
|
String transactionId = String::utf8([transaction.transactionIdentifier UTF8String]);
|
|
|
|
InAppStore::get_singleton()->_record_purchase(pid);
|
|
|
|
Dictionary ret;
|
|
|
|
ret["type"] = "purchase";
|
|
|
|
ret["result"] = "ok";
|
|
|
|
ret["product_id"] = pid;
|
|
|
|
ret["transaction_id"] = transactionId;
|
|
|
|
|
2017-04-09 12:18:49 +00:00
|
|
|
NSData *receipt = nil;
|
2020-10-05 19:19:38 +00:00
|
|
|
int sdk_version = [[[UIDevice currentDevice] systemVersion] intValue];
|
2017-04-09 11:22:40 +00:00
|
|
|
|
2020-10-05 19:19:38 +00:00
|
|
|
NSBundle *bundle = [NSBundle mainBundle];
|
|
|
|
// Get the transaction receipt file path location in the app bundle.
|
|
|
|
NSURL *receiptFileURL = [bundle appStoreReceiptURL];
|
2017-04-09 11:22:40 +00:00
|
|
|
|
2020-10-05 19:19:38 +00:00
|
|
|
// Read in the contents of the transaction file.
|
|
|
|
receipt = [NSData dataWithContentsOfURL:receiptFileURL];
|
2017-04-09 11:22:40 +00:00
|
|
|
|
2017-04-09 12:18:49 +00:00
|
|
|
NSString *receipt_to_send = nil;
|
2020-10-05 19:19:38 +00:00
|
|
|
|
2017-04-09 11:22:40 +00:00
|
|
|
if (receipt != nil) {
|
2020-06-24 07:34:20 +00:00
|
|
|
receipt_to_send = [receipt base64EncodedStringWithOptions:0];
|
2017-04-09 11:22:40 +00:00
|
|
|
}
|
|
|
|
Dictionary receipt_ret;
|
|
|
|
receipt_ret["receipt"] = String::utf8(receipt_to_send != nil ? [receipt_to_send UTF8String] : "");
|
|
|
|
receipt_ret["sdk"] = sdk_version;
|
|
|
|
ret["receipt"] = receipt_ret;
|
|
|
|
|
|
|
|
InAppStore::get_singleton()->_post_event(ret);
|
|
|
|
|
2020-10-16 12:26:31 +00:00
|
|
|
if (self.shouldAutoFinishTransactions) {
|
2017-04-09 12:18:49 +00:00
|
|
|
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
|
|
|
|
} else {
|
2020-10-16 12:26:31 +00:00
|
|
|
self.pendingTransactions[transaction.payment.productIdentifier] = transaction;
|
2017-04-09 11:22:40 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-10-16 12:26:31 +00:00
|
|
|
} break;
|
2017-04-09 11:22:40 +00:00
|
|
|
case SKPaymentTransactionStateFailed: {
|
|
|
|
printf("status transaction failed!\n");
|
|
|
|
String pid = String::utf8([transaction.payment.productIdentifier UTF8String]);
|
|
|
|
Dictionary ret;
|
|
|
|
ret["type"] = "purchase";
|
|
|
|
ret["result"] = "error";
|
|
|
|
ret["product_id"] = pid;
|
2018-04-19 10:35:21 +00:00
|
|
|
ret["error"] = String::utf8([transaction.error.localizedDescription UTF8String]);
|
2017-04-09 11:22:40 +00:00
|
|
|
InAppStore::get_singleton()->_post_event(ret);
|
|
|
|
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
|
|
|
|
} break;
|
|
|
|
case SKPaymentTransactionStateRestored: {
|
|
|
|
printf("status transaction restored!\n");
|
|
|
|
String pid = String::utf8([transaction.originalTransaction.payment.productIdentifier UTF8String]);
|
|
|
|
InAppStore::get_singleton()->_record_purchase(pid);
|
2018-02-22 19:28:37 +00:00
|
|
|
Dictionary ret;
|
|
|
|
ret["type"] = "restore";
|
|
|
|
ret["result"] = "ok";
|
|
|
|
ret["product_id"] = pid;
|
|
|
|
InAppStore::get_singleton()->_post_event(ret);
|
2017-04-09 11:22:40 +00:00
|
|
|
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
|
|
|
|
} break;
|
|
|
|
default: {
|
|
|
|
printf("status default %i!\n", (int)transaction.transactionState);
|
2020-10-16 12:26:31 +00:00
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
@end
|
|
|
|
|
2020-10-16 12:26:31 +00:00
|
|
|
void InAppStore::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("request_product_info"), &InAppStore::request_product_info);
|
|
|
|
ClassDB::bind_method(D_METHOD("restore_purchases"), &InAppStore::restore_purchases);
|
|
|
|
ClassDB::bind_method(D_METHOD("purchase"), &InAppStore::purchase);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-10-16 12:26:31 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("get_pending_event_count"), &InAppStore::get_pending_event_count);
|
|
|
|
ClassDB::bind_method(D_METHOD("pop_pending_event"), &InAppStore::pop_pending_event);
|
|
|
|
ClassDB::bind_method(D_METHOD("finish_transaction"), &InAppStore::finish_transaction);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_auto_finish_transaction"), &InAppStore::set_auto_finish_transaction);
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2020-10-16 12:26:31 +00:00
|
|
|
Error InAppStore::request_product_info(Variant p_params) {
|
2014-02-10 01:10:30 +00:00
|
|
|
Dictionary params = p_params;
|
2020-10-16 12:26:31 +00:00
|
|
|
ERR_FAIL_COND_V(!params.has("product_ids"), ERR_INVALID_PARAMETER);
|
2020-09-30 19:08:01 +00:00
|
|
|
|
2020-10-16 12:26:31 +00:00
|
|
|
PoolStringArray pids = params["product_ids"];
|
|
|
|
printf("************ request product info! %i\n", pids.size());
|
2020-09-30 19:08:01 +00:00
|
|
|
|
2020-10-16 12:26:31 +00:00
|
|
|
NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:pids.size()];
|
|
|
|
for (int i = 0; i < pids.size(); i++) {
|
|
|
|
printf("******** adding %ls to product list\n", pids[i].c_str());
|
|
|
|
NSString *pid = [[NSString alloc] initWithUTF8String:pids[i].utf8().get_data()];
|
|
|
|
[array addObject:pid];
|
2020-09-30 19:08:01 +00:00
|
|
|
}
|
|
|
|
|
2020-10-16 12:26:31 +00:00
|
|
|
NSSet *products = [[NSSet alloc] initWithArray:array];
|
2020-09-30 19:08:01 +00:00
|
|
|
|
2020-10-16 12:26:31 +00:00
|
|
|
[products_request_delegate performRequestWithProductIDs:products];
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
Error InAppStore::restore_purchases() {
|
|
|
|
printf("restoring purchases!\n");
|
|
|
|
[[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
return OK;
|
2020-10-16 12:26:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Error InAppStore::purchase(Variant p_params) {
|
|
|
|
ERR_FAIL_COND_V(![SKPaymentQueue canMakePayments], ERR_UNAVAILABLE);
|
|
|
|
if (![SKPaymentQueue canMakePayments]) {
|
|
|
|
return ERR_UNAVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("purchasing!\n");
|
|
|
|
Dictionary params = p_params;
|
|
|
|
ERR_FAIL_COND_V(!params.has("product_id"), ERR_INVALID_PARAMETER);
|
|
|
|
|
|
|
|
NSString *pid = [[NSString alloc] initWithUTF8String:String(params["product_id"]).utf8().get_data()];
|
|
|
|
|
|
|
|
return [products_request_delegate purchaseProductWithProductID:pid];
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
int InAppStore::get_pending_event_count() {
|
|
|
|
return pending_events.size();
|
2020-10-16 12:26:31 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
Variant InAppStore::pop_pending_event() {
|
|
|
|
Variant front = pending_events.front()->get();
|
|
|
|
pending_events.pop_front();
|
|
|
|
|
|
|
|
return front;
|
2020-10-16 12:26:31 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
void InAppStore::_post_event(Variant p_event) {
|
|
|
|
pending_events.push_back(p_event);
|
2020-10-16 12:26:31 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
void InAppStore::_record_purchase(String product_id) {
|
2017-04-09 12:18:49 +00:00
|
|
|
String skey = "purchased/" + product_id;
|
2020-10-05 20:26:43 +00:00
|
|
|
NSString *key = [[NSString alloc] initWithUTF8String:skey.utf8().get_data()];
|
2014-02-10 01:10:30 +00:00
|
|
|
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:key];
|
|
|
|
[[NSUserDefaults standardUserDefaults] synchronize];
|
2020-10-16 12:26:31 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-04-09 12:18:49 +00:00
|
|
|
InAppStore *InAppStore::get_singleton() {
|
2014-02-10 01:10:30 +00:00
|
|
|
return instance;
|
2020-10-16 12:26:31 +00:00
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
InAppStore::InAppStore() {
|
|
|
|
ERR_FAIL_COND(instance != NULL);
|
|
|
|
instance = this;
|
|
|
|
|
2020-10-16 12:26:31 +00:00
|
|
|
products_request_delegate = [[GodotProductsDelegate alloc] init];
|
|
|
|
transactions_observer = [[GodotTransactionsObserver alloc] init];
|
|
|
|
|
|
|
|
[[SKPaymentQueue defaultQueue] addTransactionObserver:transactions_observer];
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-04-09 12:18:49 +00:00
|
|
|
void InAppStore::finish_transaction(String product_id) {
|
|
|
|
NSString *prod_id = [NSString stringWithCString:product_id.utf8().get_data() encoding:NSUTF8StringEncoding];
|
2016-04-02 18:26:12 +00:00
|
|
|
|
2020-10-16 12:26:31 +00:00
|
|
|
[transactions_observer finishTransactionWithProductID:prod_id];
|
|
|
|
}
|
2014-09-03 02:13:40 +00:00
|
|
|
|
2017-04-09 12:18:49 +00:00
|
|
|
void InAppStore::set_auto_finish_transaction(bool b) {
|
2020-10-16 12:26:31 +00:00
|
|
|
transactions_observer.shouldAutoFinishTransactions = b;
|
2014-09-03 02:13:40 +00:00
|
|
|
}
|
|
|
|
|
2020-10-16 12:26:31 +00:00
|
|
|
InAppStore::~InAppStore() {
|
|
|
|
[products_request_delegate reset];
|
|
|
|
[transactions_observer reset];
|
|
|
|
|
|
|
|
products_request_delegate = nil;
|
|
|
|
[[SKPaymentQueue defaultQueue] removeTransactionObserver:transactions_observer];
|
|
|
|
transactions_observer = nil;
|
|
|
|
}
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
#endif
|