Skip to content
This repository has been archived by the owner on Apr 28, 2022. It is now read-only.

Commit

Permalink
Merge pull request #239 from GrimReio/unity-ios-query-inventory-#238
Browse files Browse the repository at this point in the history
  • Loading branch information
GrimReio committed Jun 18, 2014
2 parents 9194572 + e405261 commit 29ce739
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,7 @@ public void OnRestoreTransactionSucceeded(string message)
#endif

#if UNITY_IOS
private void OnBillingSupported(string inventory) {
OpenIAB_iOS.CreateInventory(inventory);

private void OnBillingSupported(string empty) {
if (billingSupportedEvent != null) {
billingSupportedEvent();
}
Expand All @@ -144,12 +142,6 @@ private void OnQueryInventorySucceeded(string json) {
}
}

public static void OnQueryInventorySucceeded(Inventory inventory) {
if (queryInventorySucceededEvent != null) {
queryInventorySucceededEvent(inventory);
}
}

private void OnQueryInventoryFailed(string error) {
if (queryInventoryFailedEvent != null)
queryInventoryFailedEvent(error);
Expand Down
14 changes: 4 additions & 10 deletions unity_plugin/unity_src/Assets/Plugins/OpenIAB/iOS/OpenIAB_iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@ public class OpenIAB_iOS
[DllImport ("__Internal")]
private static extern bool Inventory_hasPurchase(string sku);

[DllImport ("__Internal")]
private static extern void Inventory_query();

[DllImport ("__Internal")]
private static extern void Inventory_removePurchase(string sku);
#endregion

static Dictionary<string, string> _sku2storeSkuMappings = new Dictionary<string, string>();
static Dictionary<string, string> _storeSku2skuMappings = new Dictionary<string, string>();
static Inventory _inventory;

private bool IsDevice() {
if (Application.platform != RuntimePlatform.IPhonePlayer) {
Expand All @@ -44,10 +46,6 @@ private bool IsDevice() {
return true;
}

public static void CreateInventory(string json) {
_inventory = new Inventory(json);
}

public void init(Options options) {
if (!IsDevice()) return;
init(options.storeKeys);
Expand Down Expand Up @@ -88,11 +86,7 @@ public void queryInventory() {
if (!IsDevice()) {
return;
}
if (_inventory == null) {
OpenIAB.EventManager.SendMessage("OnQueryInventoryFailed", "Inventory is null");
} else {
OpenIABEventManager.OnQueryInventorySucceeded(_inventory);
}
Inventory_query();
}

public void queryInventory(string[] skus) {
Expand Down
5 changes: 5 additions & 0 deletions unity_plugin/unity_src/Assets/Plugins/iOS/AppStoreBridge.mm
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ void AppStore_restorePurchases()
[[AppStoreDelegate instance] restorePurchases];
}

void Inventory_query()
{
[[AppStoreDelegate instance] queryInventory];
}

bool Inventory_hasPurchase(const char* sku)
{
NSUserDefaults *standardUserDefaults = [NSUserDefaults standardUserDefaults];
Expand Down
2 changes: 2 additions & 0 deletions unity_plugin/unity_src/Assets/Plugins/iOS/AppStoreDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

- (void)startPurchase:(NSString*)sku;

- (void)queryInventory;

- (void)restorePurchases;

@end
87 changes: 47 additions & 40 deletions unity_plugin/unity_src/Assets/Plugins/iOS/AppStoreDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ @implementation AppStoreDelegate
#pragma mark Internal

NSSet* m_skus;
NSMutableArray* m_skuMap;

- (void)storePurchase:(NSString *)sku
{
Expand Down Expand Up @@ -44,10 +45,12 @@ - (id)init
return self;
}

- (void) dealloc
- (void)dealloc
{
[m_skuMap release];
[m_skus release];
m_skus = nil;
m_skuMap = nil;
[super dealloc];
}

Expand All @@ -72,45 +75,10 @@ - (void)startPurchase:(NSString*)sku
[[SKPaymentQueue defaultQueue] addPayment:payment];
}

- (void)restorePurchases
{
[[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
}


#pragma mark SKProductsRequestDelegate Protocol

- (void)productsRequest:(SKProductsRequest*)request didReceiveResponse:(SKProductsResponse*)response
- (void)queryInventory
{
NSMutableDictionary *inventory = [[NSMutableDictionary alloc] init];
NSMutableArray *skuMap = [[NSMutableArray alloc] init];
NSMutableDictionary* inventory = [[NSMutableDictionary alloc] init];
NSMutableArray *purchaseMap = [[NSMutableArray alloc] init];

NSArray * skProducts = response.products;
for (SKProduct * skProduct in skProducts)
{
// Format the price
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
[numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[numberFormatter setLocale:skProduct.priceLocale];
NSString *formattedPrice = [numberFormatter stringFromNumber:skProduct.price];

// Setup sku details
NSDictionary* skuDetails = [NSDictionary dictionaryWithObjectsAndKeys:
@"product", @"itemType",
skProduct.productIdentifier, @"sku",
@"product", @"type",
formattedPrice, @"price",
([skProduct.localizedTitle length] == 0) ? @"" : skProduct.localizedTitle, @"title",
([skProduct.localizedDescription length] == 0) ? @"" : skProduct.localizedDescription, @"description",
@"", @"json",
nil];

NSArray* entry = [NSArray arrayWithObjects:skProduct.productIdentifier, skuDetails, nil];
[skuMap addObject:entry];
}

NSUserDefaults* standardUserDefaults = [NSUserDefaults standardUserDefaults];
if (!standardUserDefaults)
NSLog(@"Couldn't access purchase storage. Purchase map won't be available.");
Expand Down Expand Up @@ -140,12 +108,51 @@ - (void)productsRequest:(SKProductsRequest*)request didReceiveResponse:(SKProduc
}

[inventory setObject:purchaseMap forKey:@"purchaseMap"];
[inventory setObject:skuMap forKey:@"skuMap"];
[inventory setObject:m_skuMap forKey:@"skuMap"];

NSError* error = nil;
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:inventory options:kNilOptions error:&error];
NSString* message = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
UnitySendMessage(EventHandler, "OnBillingSupported", strdup([message UTF8String]));
UnitySendMessage(EventHandler, "OnQueryInventorySucceeded", strdup([message UTF8String]));
}

- (void)restorePurchases
{
[[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
}

#pragma mark SKProductsRequestDelegate Protocol

- (void)productsRequest:(SKProductsRequest*)request didReceiveResponse:(SKProductsResponse*)response
{
m_skuMap = [[NSMutableArray alloc] init];

NSArray* skProducts = response.products;
for (SKProduct * skProduct in skProducts)
{
// Format the price
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
[numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[numberFormatter setLocale:skProduct.priceLocale];
NSString *formattedPrice = [numberFormatter stringFromNumber:skProduct.price];

// Setup sku details
NSDictionary* skuDetails = [NSDictionary dictionaryWithObjectsAndKeys:
@"product", @"itemType",
skProduct.productIdentifier, @"sku",
@"product", @"type",
formattedPrice, @"price",
([skProduct.localizedTitle length] == 0) ? @"" : skProduct.localizedTitle, @"title",
([skProduct.localizedDescription length] == 0) ? @"" : skProduct.localizedDescription, @"description",
@"", @"json",
nil];

NSArray* entry = [NSArray arrayWithObjects:skProduct.productIdentifier, skuDetails, nil];
[m_skuMap addObject:entry];
}

UnitySendMessage(EventHandler, "OnBillingSupported", strdup(""));
}

- (void)request:(SKRequest*)request didFailWithError:(NSError*)error
Expand Down

0 comments on commit 29ce739

Please sign in to comment.