PDA

View Full Version : In-app purchase - SKPaymentTransactionStatePurchased called all the time




Danneman101
Apr 11, 2011, 10:22 AM
I'm trying to implement an in-app purchase that unlocks a non-consumable file by setting a setting-variable to "yes".

I've set up a test user, and tried purchasing, and it seems to work.


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (![[[NSUserDefaults standardUserDefaults] stringForKey:@"settings_FULL_VERSION"] isEqualToString:@"yes"])
{
if ([SKPaymentQueue canMakePayments]) {
SKProductsRequest *productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObject:@"com.mysite.myapp.fullversion"]];
productsRequest.delegate = self;
[productsRequest start];

[self purchase];

} else {
NSLog(@"Parental-controls are enabled");
}
}

- (void)purchase
{
SKPayment *payment = [SKPayment paymentWithProductIdentifier:@"com.mysite.myapp.fullversion"];
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
[[SKPaymentQueue defaultQueue] addPayment:payment];
}
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response {
SKProduct *validProduct = nil;
int count = [response.products count];
if (count > 0) {
validProduct = [response.products objectAtIndex:0];
} else if (!validProduct) {
NSLog(@"No products available");
}
}
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {
for (SKPaymentTransaction *transaction in transactions) {
switch (transaction.transactionState) {
case SKPaymentTransactionStatePurchasing:
break;

case SKPaymentTransactionStatePurchased:
// Set: settings_FULL_VERSION = YES
[[NSUserDefaults standardUserDefaults] setObject:@"yes" forKey:@"settings_FULL_VERSION"];
// DEBUG:
NSLog(@"SETTING SET TO YES");
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
break;

case SKPaymentTransactionStateRestored:
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
break;

case SKPaymentTransactionStateFailed:
if (transaction.error.code != SKErrorPaymentCancelled) {
NSLog(@"An error encounterd");
}
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
break;
}
}
}


The problem is that even though I choose to cancel the transaction, the "settings_FULL_VERSION" is set to "yes".

In other words, the SKPaymentTransactionStatePurchased-case is always called, despite having uninstalled and reinstalled the app on my test-device, and even when having created a new product ID for the in-app purchase item.

Can anybody spot where I've gone wrong with this code?