I am trying to create an in-app purchase for my app, but I am running into issues. The payment process starts, and the user has to enter in their password; however, the payment never actually completes. I know I have the right identifier set up in iTunesconnect, and I also created a test account to buy the in-app purchase with.
When I run the code below, I get the following messages outputted:
"User can make payments"
"Products are available"
"Transaction state -> Purchasing"
After entering in my password, I am prompted to confirm my In-App Purchase in the Sandbox Environment. I click buy, and the prompt disappears; however I never get the actual purchased message. It's just that nothing happens. No enabling of the add-on, nothing. This happens on both the simulator and actual device. However, if I press cancel instead of buy, I get the "Transaction state -> Cancelled" message.
Any idea what I am doing wrong?
- (void)buyTapped {
if ([SKPaymentQueue canMakePayments]) {
NSLog(@"User can make payments");
SKProductsRequest *productRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObject:kLineColorProductIdentifier]];
[productRequest setDelegate:self];
[productRequest start];
}
else {
NSLog(@"User cannot make payments");
}
}
#pragma mark - SKProductsRequestDelegate
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response {
validProduct = nil;
int count = (int)[response.products count];
if (count > 0) {
validProduct = [response.products objectAtIndex:0];
NSLog(@"Products are available");
SKPayment *payment = [SKPayment paymentWithProduct:validProduct];
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
[[SKPaymentQueue defaultQueue] addPayment:payment];
}
else if (!validProduct)
{
NSLog(@"Product not available");
}
}
- (void)restore {
[[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
}
- (void) paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue
{
NSLog(@"received restored transactions: %lu", (unsigned long)queue.transactions.count);
for(SKPaymentTransaction *transaction in queue.transactions){
if(transaction.transactionState == SKPaymentTransactionStateRestored){
//called when the user successfully restores a purchase
NSLog(@"Transaction state -> Restored");
[self enableLineColors];
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
break;
}
}
}
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions{
for(SKPaymentTransaction *transaction in transactions){
//[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
switch(transaction.transactionState){
case SKPaymentTransactionStatePurchasing:
NSLog(@"Transaction state -> Purchasing");
break;
case SKPaymentTransactionStatePurchased:
[self enableLineColors];
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
NSLog(@"Transaction state -> Purchased");
break;
case SKPaymentTransactionStateRestored:
NSLog(@"Transaction state -> Restored");
[self enableLineColors];
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
break;
case SKPaymentTransactionStateFailed:
if(transaction.error.code == SKErrorPaymentCancelled){
NSLog(@"Transaction state -> Cancelled");
//the user cancelled the payment ;(
}
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
break;
case SKPaymentTransactionStateDeferred:
NSLog(@"Transaction state -> Deferred");
}
}
}