我知道有很多帖子,但似乎都没有解决我们遇到的问题。到目前为止,我认为我已按照iOS Reference Library Verifying Store Receipts documentation中的规定正确设置了所有内容。但是,在我们对AppStore验证URL的POST请求期间,我们收到了21002“java.lang.NullPointerException”,所以我一定做错了。
我注意到的一些问题可能导致了这个问题:
1)收到礼物
文档说明我们的iPhone应该只将收据传递给我们的服务器进行验证,但没有说明收据的确切内容。起初我以为它可能是下面的整个JSON对象,但现在我想知道收据是否只是“购买信息”字段。
Encoded Receipt
ewoJInNpZ25hdHVyZSIgPSAiQWx1SFVsb0dxejZjOVNvSlFVejF0OThhemI2WjJCb2N3WVBhK2ZVYllmdUI5ZE5RQkViV2dTNXVIUUluakdSQ2RScVkxUHhQY2cvMk1kSVlONEN2anc0RkNuc0JqT0d2NGFqNjVBczQ2SERseGd4WjdlTElGUXcrcG9QVlpTZHlEWWF6NHBPaGxxNVBmVXNqdmlZNENYd3VaQzkrSTBHdk55bVZWNkhnR2FCRUFBQURWekNDQTFNd2dnSTdvQU1DQVFJQ0NHVVVrVTNaV0FTMU1BMEdDU3FHU0liM0RRRUJCUVVBTUg4eEN6QUpCZ05WQkFZVEFsVlRNUk13RVFZRFZRUUtEQXBCY0hCc1pTQkpibU11TVNZd0pBWURWUVFMREIxQmNIQnNaU0JEWlhKMGFXWnBZMkYwYVc5dUlFRjFkR2h2Y21sMGVURXpNREVHQTFVRUF3d3FRWEJ3YkdVZ2FWUjFibVZ6SUZOMGIzSmxJRU5sY25ScFptbGpZWFJwYjI0Z1FYVjBhRzl5YVhSNU1CNFhEVEE1TURZeE5USXlNRFUxTmxvWERURTBNRFl4TkRJeU1EVTFObG93WkRFak1DRUdBMVVFQXd3YVVIVnlZMmhoYzJWU1pXTmxhWEIwUTJWeWRHbG1hV05oZEdVeEd6QVpCZ05WQkFzTUVrRndjR3hsSUdsVWRXNWxjeUJUZEc5eVpURVRNQkVHQTFVRUNnd0tRWEJ3YkdVZ1NXNWpMakVMTUFrR0ExVUVCaE1DVlZNd2daOH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0dNVCI7CgkicHJvZHVjdC1pZCIgPSAiY29tLmNvbXBhbnkuQXBwTmFtZS4xMDAwMDAiOwoJInRyYW5zYWN0aW9uLWlkIiA9ICIxMDAwMDAwMDAxNjI0ODEwIjsKCSJxdWFudGl0eSIgPSAiMSI7Cgkib3JpZ2luYWwtcHVyY2hhc2UtZGF0ZSIgPSAiMjAxMS0wMy0wOCAwMjo0NDozNyBFdGMvR01UIjsKCSJiaWQiID0gImNvbS5jb21wYW55LkFwcE5hbWUiOwoJImJ2cnMiID0gIjEuMC4xIjsKfQ==";
"pod" = "100";
"signing-status" = "0";
}
2)INVALID JSON
数据在base64中编码,一旦解码就应该在有效的JSON对象中提供信息,但是从我看到的对象肯定是无效的JSON。 Apple似乎使用“=”,其中“:”应该是和“;”其中“,”应该是:
{
"item-id" = "424742510";
"original-transaction-id" = "1000000001624810";
"purchase-date" = "2011-03-08 02:44:37 Etc/GMT";
"product-id" = "com.company.AppName.100000";
"transaction-id" = "1000000001624810";
"quantity" = "1";
"original-purchase-date" = "2011-03-08 02:44:37 Etc/GMT";
"bid" = "com.company.AppName";
"bvrs" = "1.0.1";
}
我想知道我们收到错误的原因之一可能是因为他们提供的收据不是有效的JSON所以当我们发布数据进行验证时服务器拒绝它。我们应该解码所有内容,将对象修复为有效的JSON,重新编码,然后将其提交给Apple进行验证吗?
我希望那些有这项工作的人可以指出我在上述两个问题上的正确方向,或者更好地提供正确的CURL调用,它会收到一个有效的响应,可以完全解决我的问题。< / p>
提前感谢您的帮助!
答案 0 :(得分:10)
如果我正确阅读了文档,那么你的问题就出错了。您发送以进行验证的收据是transactionReceipt
属性返回的数据块。你不应该关心它解码的内容,或者它是否解码为任何东西。你只需对其进行base64编码,将其作为密钥“receipt-data”的值放入json对象中,然后将该json对象发布到Apple。
当您从Apple收到回复时, 应该包含json格式的收据数据。
答案 1 :(得分:4)
对于将来遇到21002错误的其他人。花了几个小时才弄清楚出了什么问题。它通常意味着您发送到Apple服务器的JSON中的base 64编码字符串不正确。这可能意味着您不是Base-64编码或您的编码例程有问题。这些是您应该使用的步骤:
1)Base-64将从SKPaymentTransaction对象的transactionReceipt属性接收的所有NSData编码为NSString对象。您可以使用以下方法。
2)将其插入JSON字符串。在Objective-C代码中它将是:
NSString* json = [NSString stringWithFormat:@"{ 'receipt-data' : '%@' }", base64String];
3)HTTP POST
4)您将获得一个JSON字符串 - 提取“status”的值。如果它是0,则您的收据已经确认 - 否则是未经验证的购买。
下面是我用来将NSData转换为Base-64编码字符串的代码:
@interface NSData (Base64Encoding)
- (NSString*)base64Encode;
@end
@implementation NSData (Base64Encoding)
- (NSString*)base64Encode
{
static char table [] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
NSInteger length = [self length];
NSMutableData* data = [NSMutableData dataWithLength:((length + 2) / 3) * 4];
uint8_t* input = (uint8_t*)[self bytes];
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] autorelease];
}
@end
要使用它,请执行以下操作:
NSString* base64string = [transaction.transactionReceipt base64Encode];
答案 2 :(得分:2)
看起来像ASCII Property List - 您应该能够使用NSPropertyListSerialization类对其进行解码。
修改强>
解码对象的类是什么?这也类似于使用NSLog()
转储NSDictionary时获得的内容