IOS receipt validation error 21002

前端 未结 5 524
旧巷少年郎
旧巷少年郎 2021-01-01 03:01

I\'m trying to use receipt validation with my server side. Everything is ok, but sometimes I see strange: 10 times validation is OK, but on 11 i get 21002 error. I dont know

5条回答
  •  感动是毒
    2021-01-01 03:29

    Code 21002 means that the JSON you are sending to apple which has your shared secret and your receipt data is "misformed" or not in the format apple wants it.

    Here is a screenshot with the subsequent error codes and their meaning

    This is how i did it (Objective C and Local Validation)

      #define kAppReceipt @"LATEST_RECEIPT"
      #define kStoreKitSecret @"YOUR SHARED SECRET"
      #define kSandboxServer @"https://sandbox.itunes.apple.com/verifyReceipt"
    
    -(void)loadProducts{
     NSError *error;
    
    if(![[NSUserDefaults standardUserDefaults]objectForKey:kAppReceipt]){
        NSURL *recieptURL  = [[NSBundle mainBundle]appStoreReceiptURL];
        NSError *recieptError ;
        BOOL isPresent = [recieptURL checkResourceIsReachableAndReturnError:&recieptError];
        if(!isPresent){
            return;
        }
    
        NSData *recieptData = [NSData dataWithContentsOfURL:recieptURL];
        if(!recieptData){
            return;
        }
    
        payLoad = [NSMutableDictionary dictionaryWithObject:[recieptData base64EncodedStringWithOptions:0] forKey:@"receipt-data"];
    }
    else {
        [payLoad setObject:[[NSUserDefaults standardUserDefaults]objectForKey:kAppReceipt] forKey:@"receipt-data"];
    }
    
    
    [payLoad setObject:kStoreKitSecret forKey:@"password"];
    
    NSData *requestData = [NSJSONSerialization dataWithJSONObject:payLoad options:0 error:&error];
    
    NSMutableURLRequest *sandBoxReq = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:kSandboxServer]];
    [sandBoxReq setHTTPMethod:@"POST"];
    [sandBoxReq setHTTPBody:requestData];
    
    
    NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    [[session dataTaskWithRequest:sandBoxReq completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
    
        if(!error){
            NSDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
            NSString * latestReceipt = [jsonResponse objectForKey:@"latest_receipt"];
    
           // this is the latest receipt that you should store in NSUSER DEFAULT to then later sent this same receipt when you make this same call
            [[NSUserDefaults standardUserDefaults] setObject:latestReceipt forKey:kAppReceipt];
        }
    
      }] resume];
    }
    

提交回复
热议问题