mercredi 13 juin 2018

Apple month autorenew sandbox subscription not expire in 5 minutes and not receive data in apple receipt

I have implemented auto-renew monthly subscription in-app purchase in my application. I get apple receipt by calling sandbox URL of iTunes. My problem is my auto-renew subscription not expired after 5 minutes.

I didn't make it in my iPhone to renew it automatically. Please suggest how can I test in-app purchase in sandbox enviorenment.

Here is my code:

Here we are calling for apple receipt.

    -(BOOL) getSubscriptionStatusFromAppleWithReceipt:(NSData *) receiptData
{
    NSError *error;
    NSMutableDictionary *requestContents = [NSMutableDictionary dictionaryWithObject:
                                            [receiptData base64EncodedStringWithOptions:0] forKey:@"receipt-data"];
    NSString *sharedSecret = @"efd594143c2840bdac89fc22eccd3a3d";
    if (sharedSecret) requestContents[@"password"] = sharedSecret;
    NSData *requestData = [NSJSONSerialization dataWithJSONObject:requestContents options:0 error:&error];

    NSString *strUrl = @"";

    // set 1 in AppConstant.h for Live enviorment
    if (kPARSE_DEVELOPMENT_SERVER) {
        // Production
        strUrl = @"https://buy.itunes.apple.com/verifyReceipt";
    }
    else{
        // Sandbox
        strUrl = @"https://sandbox.itunes.apple.com/verifyReceipt";
    }

    //Chandni commented live server url
    // Live server
   // strUrl = @"https://buy.itunes.apple.com/verifyReceipt";

    // Devlopment server
    strUrl = @"https://sandbox.itunes.apple.com/verifyReceipt";


    NSMutableURLRequest *storeRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:strUrl]];
    [storeRequest setHTTPMethod:@"POST"];
    [storeRequest setHTTPBody:requestData];

    queue = [NSOperationQueue mainQueue];

    //    NSError *error = nil; 
    NSHTTPURLResponse *response = nil;

    NSData *data = [NSURLConnection sendSynchronousRequest:storeRequest returningResponse:&response error:&error];

    if (!error)
    {
        NSString* newStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
//        NSLog(@"%@", newStr);
        NSDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        //        NSLog(@"%@", jsonResponse[@"latest_receipt_info"]);

        if ([jsonResponse[@"latest_receipt_info"] isKindOfClass:[NSArray class]])
        {

            NSArray *receiptInfo = (NSArray *) jsonResponse[@"latest_receipt_info"];
            return [self parseJsonFromAppleServer:receiptInfo];
        }
    }
    else
    {
        NSString *strTemp = @" \n Database getSuscriptionStatus error in api calling of apple   ";


        NSLog(@"%@", error.localizedDescription);
    }

    return false;   
}

-(BOOL) parseJsonFromAppleServer:(NSArray *) receiptInfo
{
    if (receiptInfo.count > 0)
    {
        id lastReceipt = [receiptInfo lastObject];
        if ([lastReceipt isKindOfClass:[NSDictionary class]])
        {
            NSDictionary *dictLastReceipt = (NSDictionary *) lastReceipt;
            NSString *lastPurchase = [dictLastReceipt valueForKey:@"product_id"];

            NSString *secondLastPurchase = @""; // In case if users last inapp is Removal of Advertisement
            if (receiptInfo.count > 1) {
                id secondLastReceipt = receiptInfo[receiptInfo.count - 2];
                if ([secondLastReceipt isKindOfClass:[NSDictionary class]])
                {
                    NSDictionary *dictSecondLastReceipt = (NSDictionary *) secondLastReceipt;
                    secondLastPurchase = [dictSecondLastReceipt valueForKey:@"product_id"];
                }
            }

            if ([lastPurchase isEqualToString:@"com.divorcecommunity.monthly"] ||  [secondLastPurchase isEqualToString:@"com.divorcecommunity.monthly"])
            {
                NSString *strExpirationDate = [dictLastReceipt valueForKey:@"expires_date_pst"];
                NSDate *expirationDate = [CommonUtilities getExpirationDateFromDateString:strExpirationDate];

                [[NSUserDefaults standardUserDefaults] setObject:expirationDate forKey:@"ExpirationDate"];

                NSDate *serverDate = [[AppDelegate sharedAppDelegate] serverDate];

                NSString *strExpirateDate = [CommonUtilities getStringDateFromDate:expirationDate];
                NSString *strServerDate = [CommonUtilities getStringDateFromDate:serverDate];

                NSDate *dateExpiration = [CommonUtilities getDateFromDateString:strExpirateDate havingDateFormat:@"yyyy-MM-dd"];
                NSDate *dateServer = [CommonUtilities getDateFromDateString:strServerDate havingDateFormat:@"yyyy-MM-dd"];

                //  CS  Save data in default for reciept
                [[NSUserDefaults standardUserDefaults] setObject:receiptInfo forKey:@"AppleReciept"];
                [[NSUserDefaults standardUserDefaults] setObject:serverDate forKey:@"ServerDate"];
                [[NSUserDefaults standardUserDefaults] setObject:expirationDate forKey:@"ExpirationDate"];

                [[NSUserDefaults standardUserDefaults] synchronize];



                NSInteger daysDifference = [CommonUtilities getDifferenceBetweenStartDate:dateServer andEndDate:dateExpiration];

                NSInteger daysRemainingInExpiration = [[CommonUtilities sharedManager] getMinDifferenceBetweenStartDate:serverDate andEndDate:expirationDate];


                NSLog(@"Subscription expires +ve or -ve %ld",(long)daysRemainingInExpiration);
                if (daysRemainingInExpiration >= 0)
                {

              [self saveNewInAppOfType:lastPurchase withExpirationDate:expirationDate];

                    return true;

                }
                else
                {
                    return false;
                }
            }
            else
            {
            return false;
            }
        }
    }

    return false;
}

Is any one has a solution of it then please share.

Thanks

Aucun commentaire:

Enregistrer un commentaire