macos - 交易收据属性 : can't find it anymore?

标签 macos cocoa app-store in-app-purchase

注意:我正在为 Mac 开发,而不是 iOS!

- (void)recordTransaction:(SKPaymentTransaction *)transaction
{
    if ([transaction.payment.productIdentifier isEqualToString:kInAppProIdentifier])
    {
        [[NSUserDefaults standardUserDefaults] setValue:transaction.transactionReceipt     forKey:@"proUpgradeTransactionReceipt" ];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
}

我从编译器中收到此错误:

error: property 'transactionReceipt' not found on object of type 'SKPaymentTransaction *'; did you mean 'transactionDate'? [3]

此外,我在 reference for the SKPaymentTransaction class 中找不到 transactionReceipt 属性! (尽管此页面包含一些对“收据”的引用,但没有 transactionReceipt 属性)。

但是the documentation说它应该存在!

A successful transaction includes a transactionIdentifier property and a transactionReceipt property that record the details of the processed payment. Your application is not required to do anything with this information. You may wish to record this information to establish an audit trail for the transaction. If your application uses a server to deliver content, the receipt can be sent to your server and validated by the App Store.

这有什么问题吗?

最佳答案

该属性是私有(private)的,并且在 OS X 上返回空字符串。

正如 Apple 文档中所述: https://developer.apple.com/library/mac/documentation/NetworkingInternet/Conceptual/StoreKitGuide/VerifyingStoreReceipts/VerifyingStoreReceipts.html#//apple_ref/doc/uid/TP40008267-CH104-SW1

"On iOS, this is the value of the transaction's transactionReceipt property. On OS X, this is the entire contents of the receipt file inside the application bundle. Encode the receipt data using base64 encoding."

要获取收据,请使用:

[NSData dataWithContentsOfURL:[[NSBundle mainBundle] appStoreReceiptURL]]

适用于 iOS 和 OS X 的示例,用于获取收据并将其发送到服务器进行验证(使用针对 OS X 版本设置的宏):

    NSData *tr  ;
#ifdef OSX
    tr = [NSData dataWithContentsOfURL:[[NSBundle mainBundle] appStoreReceiptURL]] ;
#else
    tr = [transaction transactionReceipt];
#endif

    NSString *jsonObjectString = [[self encode:(uint8_t *)[tr bytes] length:[tr length]] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    NSString *completeString = [NSString stringWithFormat:@"https://ssl.myserver.com/verify.php?%@", jsonObjectString];
    NSURL *urlForValidation = [NSURL URLWithString:completeString];
    NSMutableURLRequest *validationRequest = [[NSMutableURLRequest alloc] initWithURL:urlForValidation];
    [validationRequest setHTTPMethod:@"GET"];
    NSData *responseData = [NSURLConnection sendSynchronousRequest:validationRequest returningResponse:nil error:nil];

验证时要小心,OS X 和 iOS 收据的 json 对象不同,因此您可能需要单独的服务器端代码进行验证。

更新:添加对发布收据进行编码的功能:

+ (NSString *)encode:(const uint8_t *)input length:(NSInteger)length {
    static char table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

    NSMutableData *data = [NSMutableData dataWithLength:((length + 2) / 3) * 4];
    uint8_t *output = (uint8_t *)data.mutableBytes;

    for (NSInteger i = 0; i < length; i += 3) {
        NSInteger value = 0;
        for (NSInteger j = i; j < (i + 3); j++) {
            value <<= 8;

            if (j < length) {
                value |= (0xFF & input[j]);
            }
        }

        NSInteger index = (i / 3) * 4;
        output[index + 0] =                    table[(value >> 18) & 0x3F];
        output[index + 1] =                    table[(value >> 12) & 0x3F];
        output[index + 2] = (i + 1) < length ? table[(value >> 6)  & 0x3F] : '=';
        output[index + 3] = (i + 2) < length ? table[(value >> 0)  & 0x3F] : '=';
    }

    return [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
}

关于macos - 交易收据属性 : can't find it anymore?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7980935/

相关文章:

macos - 如何在 osx 上分发 .net core 2.0 控制台应用程序

c++ - Xcode 和 C++ [初学者] - 为什么 Hello World 程序打印所有这些额外的东西?

swift - 来自带有 @objc 的未识别选择器的实例错误

ios - 如何在 App Store 中更改 iOS 应用程序的类别

ios - 缩小整体应用程序大小

ruby - 在 XCode 中编码/运行 Ruby

swift - 通过 macOS 辅助功能 API 在 Webkit 应用程序中选择文本

objective-c - NSView 或 NSWindow 中的漏洞

objective-c - 简单的 NSMutable 数组问题

ios - 应用程序经过广泛测试,也得到了苹果的认可,但一旦进入 App Store,所有用户都会崩溃