gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 16:27:16 27 4
gpt4 key购买 nike

注意:我正在为 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com