How to handle checks for cancelled subscriptions
PasqualePuzio opened this issue · 2 comments
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
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];
}
`
I used this way to check subscription is expired or not.