This repository has been archived by the owner on Apr 5, 2022. It is now read-only.
forked from saturngod/IAPHelper
-
Notifications
You must be signed in to change notification settings - Fork 2
/
IAPHelper.m
executable file
·163 lines (134 loc) · 5.86 KB
/
IAPHelper.m
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
//
// IAPHelper.m
//
// Original Created by Ray Wenderlich on 2/28/11.
// Created by saturngod on 7/9/12.
// Copyright 2011 Ray Wenderlich. All rights reserved.
//
#import "IAPHelper.h"
@interface IAPHelper()
@property (nonatomic,strong) NSSet *productIdentifiers;
@property (nonatomic,strong) SKProductsRequest *request;
@property (nonatomic,strong) NSArray *products;
@property (nonatomic,strong) NSMutableSet *purchasedProducts;
@property (nonatomic,copy) IAPRequestProductsResponseBlock requestProductsBlock;
@property (nonatomic,copy) IAPBuyProductCompletionBlock buyProductCompleteBlock;
@property (nonatomic,copy) IAPRestoreProductsCompletionBlock restoreCompletedBlock;
@end
@implementation IAPHelper
- (id)initWithProductIdentifiers:(NSSet *)productIdentifiers {
if ((self = [super init])) {
_productIdentifiers = productIdentifiers;
// Check for previously purchased products
NSMutableSet *purchasedProducts = [NSMutableSet set];
for (NSString *productIdentifier in _productIdentifiers) {
BOOL productPurchased = [[NSUserDefaults standardUserDefaults] boolForKey:productIdentifier];
if (productPurchased) {
[purchasedProducts addObject:productIdentifier];
NSLog(@"Previously purchased: %@", productIdentifier);
}
NSLog(@"Not purchased: %@", productIdentifier);
}
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
self.purchasedProducts = purchasedProducts;
}
return self;
}
- (BOOL)isPurchasedProductsIdentifier:(NSString *)productID {
return [[NSUserDefaults standardUserDefaults] boolForKey:productID];
}
- (void)requestProductsWithCompletion:(IAPRequestProductsResponseBlock)completion {
self.request = [[SKProductsRequest alloc] initWithProductIdentifiers:_productIdentifiers];
_request.delegate = self;
self.requestProductsBlock = completion;
[_request start];
}
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response {
NSLog(@"Received products results...");
self.products = response.products;
self.request = nil;
if (_requestProductsBlock) {
_requestProductsBlock(request,response);
}
}
- (void)recordTransaction:(SKPaymentTransaction *)transaction {
NSString *dataString = [[NSString alloc] initWithData:transaction.transactionReceipt
encoding:NSUTF8StringEncoding];
[[BPHandsetLog sharedInstance] recordEvent:@"transaction_receipt"
withData:(@{
@"product_identifier" : transaction.payment.productIdentifier,
@"data": dataString
})];
}
- (void)provideContent:(NSString *)productIdentifier {
NSLog(@"Toggling flag for: %@", productIdentifier);
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:productIdentifier];
[[NSUserDefaults standardUserDefaults] synchronize];
[_purchasedProducts addObject:productIdentifier];
}
- (void)completeTransaction:(SKPaymentTransaction *)transaction {
NSLog(@"completeTransaction...");
[self recordTransaction:transaction];
[self provideContent:transaction.payment.productIdentifier];
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
if(_buyProductCompleteBlock) {
_buyProductCompleteBlock(transaction, nil);
}
}
- (void)restoreTransaction:(SKPaymentTransaction *)transaction {
NSLog(@"restoreTransaction...");
[self recordTransaction:transaction];
[self provideContent:transaction.originalTransaction.payment.productIdentifier];
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
if (_buyProductCompleteBlock) {
_buyProductCompleteBlock(transaction, nil);
}
}
- (void)failedTransaction:(SKPaymentTransaction *)transaction {
if (transaction.error.code != SKErrorPaymentCancelled) {
NSLog(@"Transaction error: %@ %d", transaction.error.localizedDescription, transaction.error.code);
}
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
if(_buyProductCompleteBlock) {
_buyProductCompleteBlock(transaction, transaction.error);
}
}
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {
for (SKPaymentTransaction *transaction in transactions) {
switch (transaction.transactionState) {
case SKPaymentTransactionStatePurchased:
[self completeTransaction:transaction];
break;
case SKPaymentTransactionStateFailed:
[self failedTransaction:transaction];
break;
case SKPaymentTransactionStateRestored:
[self restoreTransaction:transaction];
default:
break;
}
}
}
- (void)buyProduct:(SKProduct *)productIdentifier completion:(IAPBuyProductCompletionBlock)completion {
_buyProductCompleteBlock = completion;
self.restoreCompletedBlock = nil;
SKPayment *payment = [SKPayment paymentWithProduct:productIdentifier];
[[SKPaymentQueue defaultQueue] addPayment:payment];
}
- (void)restoreProductsWithCompletion:(IAPRestoreProductsCompletionBlock)completion {
self.buyProductCompleteBlock = nil;
self.restoreCompletedBlock = completion;
[[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
}
- (void)paymentQueue:(SKPaymentQueue *)queue restoreCompletedTransactionsFailedWithError:(NSError *)error {
NSLog(@"Transaction error: %@ %d", error.localizedDescription,error.code);
if (_restoreCompletedBlock) {
_restoreCompletedBlock(queue, error);
}
}
- (void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue {
if (_restoreCompletedBlock) {
_restoreCompletedBlock(queue,nil);
}
}
@end