This repository was archived by the owner on Apr 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOTHERStoreManagerValidationEngine.m
More file actions
195 lines (130 loc) · 5.57 KB
/
OTHERStoreManagerValidationEngine.m
File metadata and controls
195 lines (130 loc) · 5.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
//
// OTHERStoreManagerValidationEngine.m
//
// Created by Edwin Bosire on 17/12/2012.
// Copyright (c) 2012 othermedia.com. All rights reserved.
//
#import "OTHERStoreManagerValidationEngine.h"
#import "NSData+Base64.h"
typedef void (^OTHERStoreManagerNSURLConnectionSuccessBlock) (void);
typedef void (^OTHERStoreManagerNSURLConnectionFailureBlock) (void);
@interface OTHERStoreManagerValidationEngine () <NSURLConnectionDelegate>{
NSMutableDictionary *transactionsReceiptStorageDictionary;
NSMutableData *receivedData;
}
@property (nonatomic, strong) OTHERStoreManagerNSURLConnectionSuccessBlock successBlock;
@property (nonatomic, strong) OTHERStoreManagerNSURLConnectionFailureBlock failureBlock;
@end
@implementation OTHERStoreManagerValidationEngine
@synthesize successBlock;
@synthesize failureBlock;
@synthesize verificationURL;
+ (id)sharedEngine{
static dispatch_once_t pred = 0;
__strong static id _sharedObject = nil;
dispatch_once(&pred, ^{
_sharedObject = [self new];
});
return _sharedObject;
}
- (void)setVerificationURL:(NSURL *)_verificationURL{
if (_verificationURL == verificationURL) return;
verificationURL = _verificationURL;
}
- (void)verifyPurchaseWithTransaction:(SKPaymentTransaction *)transaction
onCompletion:(OTHERStoreManagerVerifyBlock)completionBlock{
transactionsReceiptStorageDictionary = [[NSMutableDictionary alloc] init];
//Encode the transactionReceipt to base64
NSString *jsonObjectString = [transaction.transactionReceipt base64EncodedString];
// Create the POST request payload.
NSString *payload = [NSString stringWithFormat:@"receipt=%@", jsonObjectString];
NSData *payloadData = [payload dataUsingEncoding:NSUTF8StringEncoding];
NSAssert(self.verificationURL, @"Verification URL MUST be supplied in order for verification to work, alternatively you can disable verification by calling [[OTHERStoreManager sharedManager] setShouldVerify:No]");
// Create the POST request to the server.
[self startConnectionWithURL:self.verificationURL
HTTPMethod:kHTTPMETHOD_POST
payloadData:payloadData
onSuccess:^{
completionBlock(YES);
}onFailure:^{
completionBlock(NO);
}];
}
#pragma -mark NSURLConnection Block Method
- (void)startConnectionWithURL:(NSURL *)url
HTTPMethod:httpMethod
payloadData:(NSData *)payload
onSuccess:(OTHERStoreManagerNSURLConnectionSuccessBlock)success
onFailure:(OTHERStoreManagerNSURLConnectionFailureBlock)failure{
NSAssert(url, @"The URL must be provided when calling startConnectionWithURL:HTTPMethod:payloadData:onSuccess:onFailure");
[self initConnectionWithURL:url
HTTPMethod:httpMethod
payloadData:payload];
self.successBlock = success;
self.failureBlock = failure;
}
- (void)initConnectionWithURL:(NSURL *)url
HTTPMethod:(NSString *)httpMethod
payloadData:(NSData *)payload {
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:httpMethod];
[request setHTTPBody:payload];
if ([httpMethod isEqualToString:@"POST"]) {
[request setValue:[NSString stringWithFormat:@"%ud", payload.length] forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
}
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
if (conn) {
// Create the NSMutableData to hold the received data.
receivedData = [NSMutableData data];
} else {
NSLog(@"Failed to create an NSURLRequest in OTHERStoreManagerValidationEngine");
}
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
// This method is called when the server has determined that it
// has enough information to create the NSURLResponse.
// It can be called multiple times, for example in the case of a
// redirect, so each time we reset the data.
// receivedData is an instance variable declared elsewhere.
[receivedData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
// Append the new data to receivedData.
// receivedData is an instance variable declared elsewhere.
[receivedData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
NSString *responseString = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];
NSDictionary *verifiedReceiptDictionary = [self dictionaryFromJSONData:[responseString dataUsingEncoding:NSUTF8StringEncoding]];
// Check the status of the verifyReceipt call
id status = [verifiedReceiptDictionary objectForKey:@"status"];
if ([status integerValue] == 0) {
self.successBlock();
}else{
self.failureBlock();
}
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
self.failureBlock();
}
#pragma mark - Parsing JSON Data
- (NSDictionary *)dictionaryFromJSONData:(NSData *)data
{
NSError *error;
NSDictionary *dictionaryParsed = [NSJSONSerialization JSONObjectWithData:data
options:0
error:&error];
if (!dictionaryParsed)
{
NSLog(@"Failed parsing JSON response from receipt validation web service.");
if (error)
{
NSLog(@"Error parsing JSON response: %@", error);
}
return nil;
}
return dictionaryParsed;
}
#pragma mark - Experimental Stuff Goes Here
@end