Skip to content
This repository has been archived by the owner on Mar 4, 2020. It is now read-only.

Added notification for "Invalid Product IDs" #254

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions MKStoreKit/MKStoreKit.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@
*/
extern NSString *const kMKStoreKitProductsAvailableNotification;


/*!
* @abstract This notification is posted when if MKStoreKit finds invalid product ids after initialization sequence
*/
extern NSString *const kMKStoreKitProductsInvalidIdsNotification;

/*!
* @abstract This notification is posted when MKStoreKit completes purchase of a product
*/
Expand Down
26 changes: 14 additions & 12 deletions MKStoreKit/MKStoreKit.m
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

@import StoreKit;
NSString *const kMKStoreKitProductsAvailableNotification = @"com.mugunthkumar.mkstorekit.productsavailable";
NSString *const kMKStoreKitProductsInvalidIdsNotification = @"com.mugunthkumar.mkstorekit.productsinvalidids";
NSString *const kMKStoreKitProductPurchasedNotification = @"com.mugunthkumar.mkstorekit.productspurchased";
NSString *const kMKStoreKitProductPurchaseFailedNotification = @"com.mugunthkumar.mkstorekit.productspurchasefailed";
NSString *const kMKStoreKitProductPurchaseDeferredNotification = @"com.mugunthkumar.mkstorekit.productspurchasedeferred";
Expand Down Expand Up @@ -135,7 +136,7 @@ - (void)restorePurchaseRecord {
if (self.purchaseRecord == nil) {
self.purchaseRecord = [NSMutableDictionary dictionary];
}
NSLog(@"%@", self.purchaseRecord);
// NSLog(@"[MKStoreKit] self.purchaseRecord: %@", self.purchaseRecord);
}

- (void)savePurchaseRecord {
Expand All @@ -148,10 +149,10 @@ - (void)savePurchaseRecord {
#endif

if (!success) {
NSLog(@"Failed to remember data record");
NSLog(@"[MKStoreKit] Failed to remember data record");
}

NSLog(@"%@", self.purchaseRecord);
// NSLog(@"[MKStoreKit] self.purchaseRecord: %@", self.purchaseRecord);
}

#pragma mark -
Expand Down Expand Up @@ -214,7 +215,8 @@ - (void)startProductRequest {

- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response {
if (response.invalidProductIdentifiers.count > 0) {
NSLog(@"Invalid Product IDs: %@", response.invalidProductIdentifiers);
[[NSNotificationCenter defaultCenter] postNotificationName:kMKStoreKitProductsInvalidIdsNotification
object:response.invalidProductIdentifiers];
}

self.availableProducts = response.products;
Expand All @@ -223,7 +225,7 @@ - (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProdu
}

- (void)request:(SKRequest *)request didFailWithError:(NSError *)error {
NSLog(@"Product request failed with error: %@", error);
NSLog(@"[MKStoreKit] Product request failed with error: %@", error);
}

#pragma mark -
Expand All @@ -248,7 +250,7 @@ - (void)initiatePaymentRequestForProductWithIdentifier:(NSString *)productId {
if (!self.availableProducts) {
// TODO: FIX ME
// Initializer might be running or internet might not be available
NSLog(@"No products are available. Did you initialize MKStoreKit by calling [[MKStoreKit sharedKit] startProductRequest]?");
NSLog(@"[MKStoreKit] No products are available. Did you initialize MKStoreKit by calling [[MKStoreKit sharedKit] startProductRequest]?");
}

if (![SKPaymentQueue canMakePayments]) {
Expand Down Expand Up @@ -290,10 +292,10 @@ - (void)requestDidFinish:(SKRequest *)request {
if([request isKindOfClass:[SKReceiptRefreshRequest class]]) {
NSURL *receiptUrl = [[NSBundle mainBundle] appStoreReceiptURL];
if ([[NSFileManager defaultManager] fileExistsAtPath:[receiptUrl path]]) {
NSLog(@"App receipt exists. Preparing to validate and update local stores.");
NSLog(@"[MKStoreKit] App receipt exists. Preparing to validate and update local stores.");
[self startValidatingReceiptsAndUpdateLocalStore];
} else {
NSLog(@"Receipt request completed but there is no receipt. The user may have refused to login, or the reciept is missing.");
NSLog(@"[MKStoreKit] Receipt request completed but there is no receipt. The user may have refused to login, or the reciept is missing.");
// Disable features of your app, but do not terminate the app
}
}
Expand All @@ -312,7 +314,7 @@ - (void)startValidatingAppStoreReceiptWithCompletionHandler:(void (^)(NSArray *r
NSData *receiptData = [NSData dataWithContentsOfURL:receiptURL];
if (!receiptData) {
// Validation fails
NSLog(@"Receipt exists but there is no data available. Try refreshing the reciept payload and then checking again.");
NSLog(@"[MKStoreKit] Receipt exists but there is no data available. Try refreshing the reciept payload and then checking again.");
completionHandler(nil, nil);
return;
}
Expand Down Expand Up @@ -387,7 +389,7 @@ - (BOOL)purchasedAppBeforeVersion:(NSString *)requiredVersion {
- (void)startValidatingReceiptsAndUpdateLocalStore {
[self startValidatingAppStoreReceiptWithCompletionHandler:^(NSArray *receipts, NSError *error) {
if (error) {
NSLog(@"Receipt validation failed with error: %@", error);
NSLog(@"[MKStoreKit] Receipt validation failed with error: %@", error);
[[NSNotificationCenter defaultCenter] postNotificationName:kMKStoreKitReceiptValidationFailedNotification object:error];
} else {
__block BOOL purchaseRecordDirty = NO;
Expand Down Expand Up @@ -517,14 +519,14 @@ - (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)tran
}

- (void)failedTransaction:(SKPaymentTransaction *)transaction inQueue:(SKPaymentQueue *)queue {
NSLog(@"Transaction Failed with error: %@", transaction.error);
NSLog(@"[MKStoreKit] Transaction Failed with error: %@", transaction.error);
[queue finishTransaction:transaction];
[[NSNotificationCenter defaultCenter] postNotificationName:kMKStoreKitProductPurchaseFailedNotification
object:transaction.payment.productIdentifier];
}

- (void)deferredTransaction:(SKPaymentTransaction *)transaction inQueue:(SKPaymentQueue *)queue {
NSLog(@"Transaction Deferred: %@", transaction);
NSLog(@"[MKStoreKit] Transaction Deferred: %@", transaction);
[[NSNotificationCenter defaultCenter] postNotificationName:kMKStoreKitProductPurchaseDeferredNotification
object:transaction.payment.productIdentifier];
}
Expand Down