iOS in-app purchase never completes

2019-06-23 19:03发布

问题:

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");
        }
    }
}

回答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.