iOS in-app purchase never completes
Asked Answered
B

1

5

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");
        }
    }
}
Brachium answered 7/5, 2015 at 0:26 Comment(6)
apple in-app purchase servers are super flaky and most likely they maybe down. I have seen that happen before with many apps of mine. There is nothing wrong with your code that I can seeCorrinnecorrival
@Sam B Thanks, but this problem has been occurring for multiple daysBrachium
Hope you have signed out from App Store application, because in which you have have added your original apple account credential & missed to remove it.Rebhun
@NileshPatel what do you mean by that?Brachium
Before testing the IAP in sandbox environment you need to sign out from your App Store application, because it contains credential which you are using to download application from App Store.. Have you done that?Rebhun
Just did, that was the problemBrachium
C
1

Ok, I had the exact same problem yesterday with another app of mine. Do this.

  • Launch App Store on your actual device
  • Go to featured tab
  • All the way at the bottom change your Apple ID to your test ID
  • When you do that then maybe another window that may appear. This one appeared for me and told me that I needed to add my actual credit card security code, update expiration date for my test account.
  • I also needed to accept terms and conditions for my test account.
  • Once I did that and hit ok. I launched my app on the actual device and retried in-app purchase again and it worked.

For some bizarre reason if there is a problem like that with your test account instead of failing my in-app purchases just hung.

Corrinnecorrival answered 7/5, 2015 at 11:45 Comment(5)
Thanks I'll try this out when I have a chanceBrachium
Great this worked. Ahh it's been bugging me for daysBrachium
Gald I could help you with Apple's madness :-)Corrinnecorrival
This is isn't a good solution. You're turning a test sandbox account into a real account and doing real in app purchases instead of going through the sandbox enviromentFrown
That answer should be removed. the op has no idea that he turns sandbox acc into the real oneRagouzis

© 2022 - 2024 — McMap. All rights reserved.