我正在尝试验证Apple IAP自动续订收据(https://developer.apple.com/library/ios/releasenotes/General/ValidateAppStoreReceipt/Chapters/ValidateRemotely.html),但响应有效负载似乎(令人惊讶地)为空。如果你能指出我正确的方向,我将不胜感激。
以下是通过Parse CloudCode实现的javascript:
Parse.Cloud.define('validateReceipt', function (request, response) {
// params:
// debugMode
// userObjectId
// receiptData
var storeURL = null;
if (request.params.debugMode) {
storeURL = 'https://sandbox.itunes.apple.com/verifyReceipt';
} else {
storeURL = 'http://buy.itunes.apple.com/verifyReceipt';
}
var receiptAsBase64EncodedString = request.params.receiptData;
var postData = {
method: 'POST',
url: storeURL,
headers: { 'Content-Type': 'application/json' },
body: { 'receipt-data': receiptAsBase64EncodedString,
'password': 'SHARED_SECRET' }
}
Parse.Cloud.httpRequest(postData).then(function (httpResponse) {
var expirationDate = httpResponse.data.latest_receipt.expiration_date;
var userQuery = new Parse.Query('_User');
userQuery.get(request.params.userObjectId, {
success: function(user) {
user.set('subscriptionExpirationDate', expirationDate);
user.save(null, {
success: function(thread) {
return response.success('Subscription Active');
},
error: function(user, error) {
console.error('Error saving subscriptionExpirationDate for User: ' + error.code + ' - ' + error.message);
return response.error('Error saving subscriptionExpirationDate for User: ' + error.code + ' - ' + error.message);
}
});
},
error: function(object, error) {
console.error('Error fetching User: ' + error.code + ' - ' + error.message);
return response.error('Error saving subscriptionExpirationDate for User: ' + error.code + ' - ' + error.message);
}
});
});
});
如果我将httpResponse.data打印到控制台,则输出为:
No Message provided
但是,如果我在obj-C中从我的客户端实现相同的逻辑,我会得到预期的结果:
NSError *error;
NSDictionary *requestContents = @{
@"receipt-data": [receiptData base64EncodedStringWithOptions:0],
@"password": @"SHARED_SECRET"
};
NSData *requestData = [NSJSONSerialization dataWithJSONObject:requestContents
options:0
error:&error];
NSURL *storeURL = [NSURL URLWithString:@"https://sandbox.itunes.apple.com/verifyReceipt"];
NSMutableURLRequest *storeRequest = [NSMutableURLRequest requestWithURL:storeURL];
[storeRequest setHTTPMethod:@"POST"];
[storeRequest setHTTPBody:requestData];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:storeRequest queue:queue
completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if (!connectionError) {
NSError *error;
NSDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
}
}];
打印jsonReponse:
{
environment = Sandbox;
"latest_receipt" = "...";
"latest_receipt_info" = (
{
"expires_date" = "2015-10-09 02:11:19 Etc/GMT";
[...]
},
status = 0;
}