Permalink
Browse files

This introduce 2 singleton (sandboxReceiptVerificationClient and prod…

…uctionReceiptVerificationClient) and remove the swapping of _receiptVerificationClient in status 21007 and 21008.

This create a better multithread environment but at the expense of a dumb API that always try to hit production when it knew it is in sandbox environment.
  • Loading branch information...
1 parent 41d3d9f commit 01ac0314981b42e3714fd1fef5379a9c066537a2 @lxcid committed Dec 1, 2012
Showing with 33 additions and 11 deletions.
  1. +33 −11 CargoBay/CargoBay.m
View
@@ -485,21 +485,34 @@ + (CargoBay *)sharedManager {
return _sharedManager;
}
-- (AFHTTPClient *)sandboxReceiptVerificationClient {
- AFHTTPClient *theHTTPClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:kCargoBaySandboxReceiptVerificationBaseURLString]];
+- (AFHTTPClient *)receiptVerificationClientWithBaseURL:(NSURL *)theBaseURL {
+ AFHTTPClient *theHTTPClient = [[AFHTTPClient alloc] initWithBaseURL:theBaseURL];
[theHTTPClient setDefaultHeader:@"Accept" value:@"application/json"];
[theHTTPClient registerHTTPOperationClass:[AFJSONRequestOperation class]];
[theHTTPClient setParameterEncoding:AFJSONParameterEncoding];
[AFJSONRequestOperation addAcceptableContentTypes:[NSSet setWithObject:@"text/plain"]];
return theHTTPClient;
}
+- (AFHTTPClient *)sandboxReceiptVerificationClient {
+ static AFHTTPClient *theHTTPClient = nil;
+
+ static dispatch_once_t theOnceToken;
+ dispatch_once(&theOnceToken, ^{
+ theHTTPClient = [self receiptVerificationClientWithBaseURL:[NSURL URLWithString:kCargoBaySandboxReceiptVerificationBaseURLString]];
+ });
+
+ return theHTTPClient;
+}
+
- (AFHTTPClient *)productionReceiptVerificationClient {
- AFHTTPClient *theHTTPClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:kCargoBayProductionReceiptVerificationBaseURLString]];
- [theHTTPClient setDefaultHeader:@"Accept" value:@"application/json"];
- [theHTTPClient registerHTTPOperationClass:[AFJSONRequestOperation class]];
- [theHTTPClient setParameterEncoding:AFJSONParameterEncoding];
- [AFJSONRequestOperation addAcceptableContentTypes:[NSSet setWithObject:@"text/plain"]];
+ static AFHTTPClient *theHTTPClient = nil;
+
+ static dispatch_once_t theOnceToken;
+ dispatch_once(&theOnceToken, ^{
+ theHTTPClient = [self receiptVerificationClientWithBaseURL:[NSURL URLWithString:kCargoBayProductionReceiptVerificationBaseURLString]];
+ });
+
return theHTTPClient;
}
@@ -549,6 +562,7 @@ - (void)productsWithRequest:(NSURLRequest *)request
}
- (void)verifyTransaction:(SKPaymentTransaction *)transaction
+ client:(AFHTTPClient *)client
password:(NSString *)password
success:(void (^)(NSDictionary *receipt))success
failure:(void (^)(NSError *error))failure
@@ -572,8 +586,8 @@ - (void)verifyTransaction:(SKPaymentTransaction *)transaction
[parameters setObject:password forKey:@"password"];
}
- NSURLRequest *request = [_receiptVerificationClient requestWithMethod:@"POST" path:@"verifyReceipt" parameters:parameters];
- AFHTTPRequestOperation *operation = [_receiptVerificationClient HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
+ NSURLRequest *request = [client requestWithMethod:@"POST" path:@"verifyReceipt" parameters:parameters];
+ AFHTTPRequestOperation *operation = [client HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSInteger status = [responseObject valueForKey:@"status"] ? [[responseObject valueForKey:@"status"] integerValue] : NSNotFound;
switch (status) {
@@ -594,15 +608,15 @@ - (void)verifyTransaction:(SKPaymentTransaction *)transaction
}
} break;
case 21007: { // Status 21007: This receipt is a sandbox receipt, but it was sent to the production service for verification.
- _receiptVerificationClient = [self sandboxReceiptVerificationClient];
[self verifyTransaction:transaction
+ client:[self sandboxReceiptVerificationClient]
password:password
success:success
failure:failure];
} break;
case 21008: { // Status 21008: This receipt is a production receipt, but it was sent to the sandbox service for verification.
- _receiptVerificationClient = [self productionReceiptVerificationClient];
[self verifyTransaction:transaction
+ client:[self productionReceiptVerificationClient]
password:password
success:success
failure:failure];
@@ -653,6 +667,14 @@ - (void)verifyTransaction:(SKPaymentTransaction *)transaction
[_receiptVerificationClient enqueueHTTPRequestOperation:operation];
}
+- (void)verifyTransaction:(SKPaymentTransaction *)transaction
+ password:(NSString *)password
+ success:(void (^)(NSDictionary *receipt))success
+ failure:(void (^)(NSError *error))failure {
+ AFHTTPClient *client = _receiptVerificationClient;
+ [self verifyTransaction:transaction client:client password:password success:success failure:failure];
+}
+
- (void)verifyTransaction:(SKPaymentTransaction *)transaction
success:(void (^)(NSDictionary *receipt))success
failure:(void (^)(NSError *error))failure

0 comments on commit 01ac031

Please sign in to comment.