Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to handle checks for cancelled subscriptions #44

Open
PasqualePuzio opened this issue Apr 16, 2017 · 2 comments
Open

How to handle checks for cancelled subscriptions #44

PasqualePuzio opened this issue Apr 16, 2017 · 2 comments

Comments

@PasqualePuzio
Copy link

Hi,

First of all, let me thank you for the great work. I'm a very happy adopter of your library since almost 2 years now ;)

I would like to implement a simple mechanism to properly handle cancelled subscriptions.
In other words, if a user disables the auto-renewal of the subscription, I would like to be aware of it and thus deactivate the corresponding in-app purchase(s) when the subscription expires.

What's the best way do it?
Will this be enough? Will this automatically clear subscriptions that are no longer active?

[[IAPShare sharedHelper].iap restoreProductsWithCompletion:^(SKPaymentQueue *payment, NSError *error) { if (error) { // error: do your stuff } else { // do your stuff } }];

Or should I clear in-app purchases before doing that?

[[IAPShare sharedHelper].iap clearSavedPurchasedProducts];

Thanks

@tuananhit91
Copy link

tuananhit91 commented Jun 9, 2017

Hi @PasqualePuzio

You can use try function and need to edit this function in the library.

typedef void (^checkReceiptCompleteResponseBlock)(NSDictionary* response,NSError* error);

`- (void)checkReceipt:(NSData*)receiptData AndSharedSecret:(NSString*)secretKey onCompletion:(checkReceiptCompleteResponseBlock)completion
{
self.checkReceiptCompleteBlock = completion;

NSError *jsonError = nil;
//    NSString *receiptBase64 = [NSString base64StringFromData:receiptData length:[receiptData length]];
NSString *receiptBase64 = [receiptData base64EncodedStringWithOptions:0];

NSData *jsonData = nil;

if(secretKey !=nil && ![secretKey isEqualToString:@""]) {
    
    jsonData = [NSJSONSerialization dataWithJSONObject:[NSDictionary dictionaryWithObjectsAndKeys:receiptBase64,@"receipt-data",
                                                        secretKey,@"password",
                                                        nil]
                                               options:NSJSONWritingPrettyPrinted
                                                 error:&jsonError];
    
}
else {
    jsonData = [NSJSONSerialization dataWithJSONObject:[NSDictionary dictionaryWithObjectsAndKeys:
                                                        receiptBase64,@"receipt-data",
                                                        nil]
                                               options:NSJSONWritingPrettyPrinted
                                                 error:&jsonError
                ];
}


//    NSString* jsonStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

NSURL *requestURL = nil;
if(_production)
{
    requestURL = [NSURL URLWithString:@"https://buy.itunes.apple.com/verifyReceipt"];
}
else {
    requestURL = [NSURL URLWithString:@"https://sandbox.itunes.apple.com/verifyReceipt"];
}

NSMutableURLRequest *req = [[NSMutableURLRequest alloc] initWithURL:requestURL];
[req setHTTPMethod:@"POST"];
[req setHTTPBody:jsonData];
[req setTimeoutInterval:30];

NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
//    self.receiptRequestData = [[NSMutableData alloc] init];

[[session
  dataTaskWithRequest:req
  completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
      
      if (error) {
          if(_checkReceiptCompleteBlock) {
              _checkReceiptCompleteBlock(nil,error);
          }
      }else{
          if (data) {
              NSDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
              if(_checkReceiptCompleteBlock) {
                  _checkReceiptCompleteBlock(jsonResponse,nil);
              }
          }else{
              if(_checkReceiptCompleteBlock) {
                  _checkReceiptCompleteBlock(nil, nil);
              }
          }
      }
      
  }]resume];

}
`

@tuananhit91
Copy link

I used this way to check subscription is expired or not.

https://stackoverflow.com/questions/5120177/how-to-check-in-app-purchase-auto-renewable-subscription-is-valid

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants