|
| 1 | +// |
| 2 | +// InAppPurchaseManager.m |
| 3 | +// beetight |
| 4 | +// |
| 5 | +// Created by Matt Kane on 20/02/2011. |
| 6 | +// Copyright 2011 Matt Kane. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +#import "InAppPurchaseManager.h" |
| 10 | + |
| 11 | + |
| 12 | +@implementation InAppPurchaseManager |
| 13 | + |
| 14 | +-(void) setup:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options { |
| 15 | + [[SKPaymentQueue defaultQueue] addTransactionObserver:self]; |
| 16 | +} |
| 17 | + |
| 18 | +- (void) requestProductData:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options |
| 19 | +{ |
| 20 | + if([arguments count] < 3) { |
| 21 | + return; |
| 22 | + } |
| 23 | + NSLog(@"Getting product data"); |
| 24 | + NSSet *productIdentifiers = [NSSet setWithObject:[arguments objectAtIndex:0]]; |
| 25 | + SKProductsRequest *productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers]; |
| 26 | + |
| 27 | + ProductsRequestDelegate* delegate = [[[ProductsRequestDelegate alloc] init] retain]; |
| 28 | + delegate.command = self; |
| 29 | + delegate.successCallback = [arguments objectAtIndex:1]; |
| 30 | + delegate.failCallback = [arguments objectAtIndex:2]; |
| 31 | + |
| 32 | + productsRequest.delegate = delegate; |
| 33 | + [productsRequest start]; |
| 34 | + |
| 35 | +} |
| 36 | + |
| 37 | +- (void) makePurchase:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options |
| 38 | +{ |
| 39 | + NSLog(@"About to do IAP"); |
| 40 | + if([arguments count] < 1) { |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + SKMutablePayment *payment = [SKMutablePayment paymentWithProductIdentifier:[arguments objectAtIndex:0]]; |
| 45 | + |
| 46 | + if([arguments count] > 1) { |
| 47 | + id quantity = [arguments objectAtIndex:1]; |
| 48 | + if ([quantity respondsToSelector:@selector(integerValue)]) { |
| 49 | + payment.quantity = [quantity integerValue]; |
| 50 | + } |
| 51 | + } |
| 52 | + [[SKPaymentQueue defaultQueue] addPayment:payment]; |
| 53 | +} |
| 54 | + |
| 55 | +- (void) restoreCompletedTransactions:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options |
| 56 | +{ |
| 57 | + [[SKPaymentQueue defaultQueue] restoreCompletedTransactions]; |
| 58 | +} |
| 59 | + |
| 60 | +// SKPaymentTransactionObserver methods |
| 61 | +// called when the transaction status is updated |
| 62 | +// |
| 63 | +- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions |
| 64 | +{ |
| 65 | + NSString *state, *error, *transactionIdentifier, *transactionReceipt, *productId; |
| 66 | + NSInteger errorCode; |
| 67 | + |
| 68 | + for (SKPaymentTransaction *transaction in transactions) |
| 69 | + { |
| 70 | + error = state = transactionIdentifier = transactionReceipt = productId = @""; |
| 71 | + errorCode = 0; |
| 72 | + |
| 73 | + switch (transaction.transactionState) |
| 74 | + { |
| 75 | + case SKPaymentTransactionStatePurchased: |
| 76 | + state = @"PaymentTransactionStatePurchased"; |
| 77 | + transactionIdentifier = transaction.transactionIdentifier; |
| 78 | + transactionReceipt = [[transaction transactionReceipt] base64EncodedString]; |
| 79 | + productId = transaction.payment.productIdentifier; |
| 80 | + break; |
| 81 | + |
| 82 | + case SKPaymentTransactionStateFailed: |
| 83 | + state = @"PaymentTransactionStateFailed"; |
| 84 | + error = transaction.error.localizedDescription; |
| 85 | + errorCode = transaction.error.code; |
| 86 | + NSLog(@"error %d %@", errorCode, error); |
| 87 | + |
| 88 | + break; |
| 89 | + |
| 90 | + case SKPaymentTransactionStateRestored: |
| 91 | + state = @"PaymentTransactionStateRestored"; |
| 92 | + transactionIdentifier = transaction.originalTransaction.transactionIdentifier; |
| 93 | + transactionReceipt = [[[transaction originalTransaction] transactionReceipt] base64EncodedString]; |
| 94 | + productId = transaction.originalTransaction.payment.productIdentifier; |
| 95 | + break; |
| 96 | + |
| 97 | + default: |
| 98 | + NSLog(@"Invalid state"); |
| 99 | + continue; |
| 100 | + } |
| 101 | + NSLog(@"state: %@", state); |
| 102 | + NSString *js = [NSString stringWithFormat:@"InAppPurchaseManager.manager.updatedTransactionCallback('%@',%d, '%@','%@','%@','%@')", state, errorCode, error, transactionIdentifier, productId, transactionReceipt ]; |
| 103 | + NSLog(@"js: %@", js); |
| 104 | + [self writeJavascript: js]; |
| 105 | + [[SKPaymentQueue defaultQueue] finishTransaction:transaction]; |
| 106 | + |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | + |
| 111 | +@end |
| 112 | + |
| 113 | +@implementation ProductsRequestDelegate |
| 114 | + |
| 115 | +@synthesize successCallback, failCallback, command; |
| 116 | + |
| 117 | + |
| 118 | +- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response |
| 119 | +{ |
| 120 | + NSLog(@"got iap product response"); |
| 121 | + for (SKProduct *product in response.products) { |
| 122 | + NSLog(@"sending js for %@", product.productIdentifier); |
| 123 | + NSString *js = [NSString stringWithFormat:@"%@('%@','%@','%@','%@')", successCallback, product.productIdentifier, product.localizedTitle, product.localizedDescription, product.localizedPrice]; |
| 124 | + NSLog(@"js: %@", js); |
| 125 | + [command writeJavascript: js]; |
| 126 | + } |
| 127 | + |
| 128 | + for (NSString *invalidProductId in response.invalidProductIdentifiers) { |
| 129 | + NSLog(@"sending fail (%@) js for %@", failCallback, invalidProductId); |
| 130 | + |
| 131 | + [command writeJavascript: [NSString stringWithFormat:@"%@('%@')", failCallback, invalidProductId]]; |
| 132 | + } |
| 133 | + NSLog(@"done iap"); |
| 134 | + |
| 135 | + [command writeJavascript: [NSString stringWithFormat:@"%@('__DONE')", successCallback]]; |
| 136 | + |
| 137 | + [request release]; |
| 138 | + [self release]; |
| 139 | +} |
| 140 | + |
| 141 | +- (void) dealloc |
| 142 | +{ |
| 143 | + [successCallback release]; |
| 144 | + [failCallback release]; |
| 145 | + [command release]; |
| 146 | + [super dealloc]; |
| 147 | +} |
| 148 | + |
| 149 | + |
| 150 | +@end |
0 commit comments