已在 StoreKit 中购买订阅

发布于 2024-11-10 15:27:08 字数 412 浏览 2 评论 0原文

我在 iOS 应用程序的应用程序购买中使用可更新订阅。 当用户尝试购买已付款的订阅时,iTunes 会显示一条消息“您当前已订阅此内容”。

我如何检测此事件何时发生,以便我可以处理交易并授予对我的应用程序的访问权限。

在观察者的 paymentQueue:updatedTransactions: 方法中,它以 SKPaymentTransactionStateFailed 的形式通过。如何区分此类故障和其他故障(例如用户按下取消按钮)?

我是否提交返回的交易或者是否需要调用restorePreviousTransactions。

在苹果文件中,它指出“如果用户尝试购买他们已经购买的非消耗性产品或可更新订阅,您的应用程序会收到该商品的常规交易,而不是恢复交易。但是,用户不会再次付费对于该产品,您的应用程序应将这些交易视为与原始交易相同。”

I am using renewable subscriptions in app purchases in an iOS app.
When a user attempts to purchase a subscription that they already have paid for a message is displayed from iTunes "You're currently subscribed to this".

How I can detect when this event has occurred so that I can process the transaction and grant access to my app.

In the paymentQueue:updatedTransactions: method of the observer it is coming through as a SKPaymentTransactionStateFailed. How do I distinguish between this type of failure and other failures such as the user pressing the cancel buttons?

Do I submit the transaction that is returned or do I need to call restorePreviousTransactions.

In the Apple documents it states "If the user attempts to purchase a non-consumable product or a renewable subscription they have already purchased, your application receives a regular transaction for that item, not a restore transaction. However, the user is not charged again for that product. Your application should treat these transactions identically to those of the original transaction."

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心清如水 2024-11-17 15:27:08
Q: How I can detect when this event (currently subscribed) has occurred so that I can process the transaction and grant access to my app.

您通过 Apple 验证来检测订阅何时存在(我使用 php 网站代码来执行此操作),您会收到“状态代码”响应,并且可以验证它是否是代码 21006(订阅已过期)或其他代码(我将 0 和 21006 以外的任何值视为实际错误)。

我的做法是将交易详细信息存储在 PLIST 文件中,该文件存储在文档目录中。

您可以向 PLIST 添加额外的字段,例如到期日期、布尔标志等。

这样您就拥有了收据的副本,尽管您应该始终验证它,因为它可能已过期。

问:在观察者的 paymentQueue:updatedTransactions: 方法中
正在作为 SKPaymentTransactionStateFailed 出现。我该怎么办
区分此类故障和其他故障,例如
用户按下取消按钮?

您可以在updatedTransactions 方法中使用switch 语句来确定不同类型的响应。

示例

-(void)paymentQueue:(SKPaymentQueue *)queue restoreCompletedTransactionsFailedWithError:(NSError *)error
{    
    NSString *message = [NSString stringWithFormat:@"Transaction failed with error - %@", error.localizedDescription];
    NSLog(@"Error - %@", message);

    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error" 
                                                        message:message 
                                                       delegate:nil
                                              cancelButtonTitle:@"OK" 
                                              otherButtonTitles:nil];
    [alertView show];
    [alertView release];
}

-(void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{
    NSLog(@"updatedTransactions");
    for (SKPaymentTransaction *transaction in transactions)
    {
        switch (transaction.transactionState)
        {
            case SKPaymentTransactionStatePurchasing:
                // take action whilst processing payment
                break;

            case SKPaymentTransactionStatePurchased:
                // take action when feature is purchased
                break;

            case SKPaymentTransactionStateRestored:
                // take action to restore the app as if it was purchased            
                break;


            case SKPaymentTransactionStateFailed:
                if (transaction.error.code != SKErrorPaymentCancelled)
                {
                // Do something with the error
                } // end if
                break;

            default:
                break;
        } // end switch

    } // next

TransactionStateFailed 处理失败,尽管我没有编写取消代码,因为我没有理由在我的应用程序中这样做。

问:退回的交易我是提交还是需要打电话
恢复之前的交易。

我相信 StoreKit 使用 finishTransaction 方法和 RestorePreviousTransaction 方法在内部处理此问题

,即

[[SKPaymentQueue defaultQueue] finishTransaction: transaction];

来完成交易

我希望这有帮助

Q: How I can detect when this event (currently subscribed) has occurred so that I can process the transaction and grant access to my app.

You detect when the subscription exists via validation with Apple (I use php website code to do this), you get a "status code" response back, and can verify whether it is a code 21006 (subscription is expired), or others (I consider anything other than 0 and 21006 to be an actual error).

The way I do things is I store the transaction details inside a PLIST file which I store in the documents directory.

You could add extra fields to the PLIST such as expiryDates, boolean flags, etc.

This way you have a copy of the receipt, although you should always validate it because it might have expired.

Q: In the paymentQueue:updatedTransactions: method of the observer it
is coming through as a SKPaymentTransactionStateFailed. How do I
distinguish between this type of failure and other failures such as
the user pressing the cancel buttons?

You use a switch statement in the updatedTransactions method to determine the different types of responses.

Example

-(void)paymentQueue:(SKPaymentQueue *)queue restoreCompletedTransactionsFailedWithError:(NSError *)error
{    
    NSString *message = [NSString stringWithFormat:@"Transaction failed with error - %@", error.localizedDescription];
    NSLog(@"Error - %@", message);

    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error" 
                                                        message:message 
                                                       delegate:nil
                                              cancelButtonTitle:@"OK" 
                                              otherButtonTitles:nil];
    [alertView show];
    [alertView release];
}

-(void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{
    NSLog(@"updatedTransactions");
    for (SKPaymentTransaction *transaction in transactions)
    {
        switch (transaction.transactionState)
        {
            case SKPaymentTransactionStatePurchasing:
                // take action whilst processing payment
                break;

            case SKPaymentTransactionStatePurchased:
                // take action when feature is purchased
                break;

            case SKPaymentTransactionStateRestored:
                // take action to restore the app as if it was purchased            
                break;


            case SKPaymentTransactionStateFailed:
                if (transaction.error.code != SKErrorPaymentCancelled)
                {
                // Do something with the error
                } // end if
                break;

            default:
                break;
        } // end switch

    } // next

The TransactionStateFailed handles the failures, although I do not code for cancellation as there is no reason for me to do so in my app.

Q: Do I submit the transaction that is returned or do I need to call
restorePreviousTransactions.

I believe StoreKit handles this internally with finishTransaction method and restorePreviousTransaction methods

ie,

[[SKPaymentQueue defaultQueue] finishTransaction: transaction];

to finish off transactions

I hope this helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文