应用内购买:自动续订订阅流程

时间:2018-11-28 16:36:20

标签: ios objective-c iphone in-app-purchase

我已将月度订阅集成到我的应用程序中,为此,我在应用程序购买中使用了自动更新功能。 SandBox构建一切正常,但是通过TestFlight测试构建时,我面临两个问题。

  1. 成功购买后,应用程序仍要求我确认我的Apple ID密码,并显示消息“ 输入Apple ID xxxxxx.xxxx.xx的密码,以验证iTunes帐户并允许该设备确认购买。
  2. 删除并重新安装该应用程序后,我试图购买上一个项目,并且该应用程序显示一个弹出窗口,显示 “您当前已订阅..........管理。确定。 “ ,当我点击“确定”时,什么也没有发生。我认为,此时应使用 TransactionStatePurchased 调用“ updatedTransactions” 委托,但没有任何反应。或可能正在发生某些事情,但我不知道如何在TestFlight构建中对其进行调试。

我已将事务观察器添加到AppDelegate

/*
 * ----------------- FOLLOWING ARE OBSERVERS FOR PAYMENT TRANSACTION ----------------------------
 */


-(void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray<SKPaymentTransaction *> *)transactions
{

    NSLog(@"Count =  %lu",(unsigned long)transactions.count);
    for (SKPaymentTransaction *transaction in transactions)
    {
        switch (transaction.transactionState)
        {
            case SKPaymentTransactionStatePurchasing:
            {
                NSLog(@"1. Transaction is in Purchasing state");
                [self handlePurchasingStateFor:transaction withQueue:queue];
                break;
            }

            case SKPaymentTransactionStatePurchased:
            {
                NSLog(@"2. Transaction is in Purchased state");
                [self handlePurchasedStateFor:transaction withQueue:queue];
                break;
            }

            case SKPaymentTransactionStateFailed:
            {

                NSString *failMsg = [[NSString alloc]initWithFormat:@"Purchase failed with error: %@",transaction.error.localizedDescription];
                NSLog(@"3. Error Mesg - %@",failMsg);

                [self handleFailStateFor:transaction withQueue:queue];
                break;
            }

            case SKPaymentTransactionStateRestored:
            {
                NSLog(@"4. Transaction is Restored");
                [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
                [self handleRestoreStateFor:transaction withQueue:queue];
                break;
            }

            case SKPaymentTransactionStateDeferred:
            {
                NSLog(@"5. Transaction is deffered, waiting for parent approval");
                [self handleDefferedStateFor:transaction withQueue:queue];
                break;
            }
        }
    }
}

-(void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue {

    if (queue.transactions.count > 0)
    {
        NSDictionary* userInfo = @{@"error": @"Subscription restored successfully."};
        [[NSNotificationCenter defaultCenter] postNotificationName:InAppServicerestoreSuccessful object:self userInfo:userInfo];
    }
    else
    {
        NSDictionary* userInfo = @{@"error": @"There is no product to restore."};
        [[NSNotificationCenter defaultCenter] postNotificationName:InAppServicerestoreFail object:self userInfo:userInfo];
    }

}

-(void)paymentQueue:(SKPaymentQueue *)queue restoreCompletedTransactionsFailedWithError:(NSError *)error
{
    NSDictionary* userInfo = @{@"error": @"Restore failed, please try again later"};
    [[NSNotificationCenter defaultCenter] postNotificationName:InAppServicerestoreFail object:self userInfo:userInfo];
}



/*
 * ------------------------------ CUSTOM METHODS TO HANDLE IN APP STATUS -----------------------
 */


-(void)handlePurchasingStateFor:(SKPaymentTransaction*)transaction withQueue:(SKPaymentQueue*)queue {
    NSLog(@"User is attempting to purchase product id: %@",transaction.payment.productIdentifier);
}

-(void)handlePurchasedStateFor:(SKPaymentTransaction*)transaction withQueue:(SKPaymentQueue*)queue {
    NSLog(@"User purchased product id: %@",transaction.payment.productIdentifier);
    [queue finishTransaction:transaction];

    NSDictionary* userInfo = @{@"error": @"Purchase is successful, you are all set."};
    [[NSNotificationCenter defaultCenter] postNotificationName:InAppServicepurchaseSuccessful object:self userInfo:userInfo];
}

-(void)handleRestoreStateFor:(SKPaymentTransaction*)transaction withQueue:(SKPaymentQueue*)queue {
    NSLog(@"User restored product id: %@",transaction.payment.productIdentifier);
    [queue finishTransaction:transaction];
}

-(void)handleFailStateFor:(SKPaymentTransaction*)transaction withQueue:(SKPaymentQueue*)queue {
    NSLog(@"User failed to purchase product id: %@",transaction.payment.productIdentifier);
}

-(void)handleDefferedStateFor:(SKPaymentTransaction*)transaction withQueue:(SKPaymentQueue*)queue {
    NSLog(@"App is waiting for parental permission for product id : %@",transaction.payment.productIdentifier);
}

enter image description here enter image description here

0 个答案:

没有答案