-
Notifications
You must be signed in to change notification settings - Fork 0
/
InAppPurchaseManager.m
132 lines (114 loc) · 3.67 KB
/
InAppPurchaseManager.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
//
// InAppPurchaseManager.m
//
// Copyright (c) 2012 Symbiotic Software LLC. All rights reserved.
//
#import "InAppPurchaseManager.h"
static id sharedInstance = nil;
@interface InAppPurchaseManager ()
{
BOOL started;
}
- (void)applicationWillTerminate:(NSNotification *)notification;
@end
@implementation InAppPurchaseManager
#pragma mark - Internal methods
- (void)applicationWillTerminate:(NSNotification *)notification
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[self release];
}
#pragma mark - Methods
+ (InAppPurchaseManager *)sharedManager
{
if(sharedInstance == nil)
{
sharedInstance = [[InAppPurchaseManager alloc] init];
[[NSNotificationCenter defaultCenter] addObserver:sharedInstance selector:@selector(applicationWillTerminate:) name:UIApplicationWillTerminateNotification object:nil];
}
return (InAppPurchaseManager *)sharedInstance;
}
- (void)startManager
{
if(!started)
{
[[SKPaymentQueue defaultQueue] addTransactionObserver:sharedInstance];
started = YES;
}
}
- (BOOL)canMakePurchases
{
return [SKPaymentQueue canMakePayments];
}
- (void)restoreCompletedTransactions
{
[self startManager];
[[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
}
- (void)purchase:(NSString *)productId
{
SKPayment *payment;
[self startManager];
payment = [SKPayment paymentWithProductIdentifier:productId];
[[SKPaymentQueue defaultQueue] addPayment:payment];
}
- (void)finishTransaction:(SKPaymentTransaction *)transaction
{
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
}
#pragma mark - SKPaymentTransactionObserver methods
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{
NSDictionary *userInfo;
NSString *notification;
BOOL success;
for(SKPaymentTransaction *transaction in transactions)
{
success = NO;
notification = nil;
switch(transaction.transactionState)
{
case SKPaymentTransactionStatePurchased:
userInfo = [NSDictionary dictionaryWithObjectsAndKeys:transaction, KEY_TRANSACTION, [[transaction payment] productIdentifier], KEY_PRODUCT_ID, nil];
notification = NOTIFICATION_PURCHASE_SUCCEEDED;
success = YES;
break;
case SKPaymentTransactionStateFailed:
if(transaction.error.code != SKErrorPaymentCancelled)
{
userInfo = [NSDictionary dictionaryWithObjectsAndKeys:transaction, KEY_TRANSACTION, [[transaction payment] productIdentifier], KEY_PRODUCT_ID, transaction.error, KEY_TRANSACTION_ERROR, nil];
notification = NOTIFICATION_PURCHASE_FAILED;
}
else
{
userInfo = [NSDictionary dictionaryWithObjectsAndKeys:transaction, KEY_TRANSACTION, [[transaction payment] productIdentifier], KEY_PRODUCT_ID, nil];
notification = NOTIFICATION_PURCHASE_FAILED;
}
break;
case SKPaymentTransactionStateRestored:
userInfo = [NSDictionary dictionaryWithObjectsAndKeys:transaction, KEY_TRANSACTION, [[transaction.originalTransaction payment] productIdentifier], KEY_PRODUCT_ID, nil];
notification = NOTIFICATION_PURCHASE_SUCCEEDED;
success = YES;
break;
default:
break;
}
if(notification)
{
if(success)
{
NSString *productId = [userInfo objectForKey:KEY_PRODUCT_ID];
[[NSUserDefaults standardUserDefaults] setValue:transaction.transactionReceipt forKey:[NSString stringWithFormat:@"%@Receipt", productId]];
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:productId];
[[NSUserDefaults standardUserDefaults] synchronize];
}
else
{
// Failure, go ahead and finish the transaction
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
}
[[NSNotificationCenter defaultCenter] postNotificationName:notification object:self userInfo:userInfo];
}
}
}
@end