//
// MyInAppPurchase.mm
// Vegas Baby
//
// Created by Gururaj T on 13/09/13.
// Copyright (c) 2013 Guru. All rights reserved.
//
#import "MyInAppPurchase.h"
#import "MyInAppPurchase.h"
#import "AppDelegate.h"
#import "Reachability.h"
static MyInAppPurchase *gInApp = nil;
@interface MyInAppPurchase()
-(void)StopIndicator;
-(void)ShowActivityIndicator;
-(void)BuyGameCoins;
@end
@implementation MyInAppPurchase
@synthesize currentID = mCurrentId;
@synthesize currentProductId = mCurrentleaderProductId;
+ (MyInAppPurchase *)sharedInAppPurchase
{
if (!gInApp) {
gInApp = [[MyInAppPurchase alloc] init];
}
return gInApp;
}
-(id)init
{
if(self = [super init])
{
mLoadingView = nil;
}
return self;
}
-(void)buyCoins_1
{
self.currentProductId = IN_APP_KEY_BUY_COIN_1;
self.currentID = k_Buy_Coins_Pack_1;
[self BuyGameCoins];
}
-(void)buyCoins_2
{
self.currentProductId = IN_APP_KEY_BUY_COIN_2;
self.currentID = k_Buy_Coins_Pack_2;
[self BuyGameCoins];
}
-(void)buyCoins_3
{
self.currentProductId = IN_APP_KEY_BUY_COIN_3;
self.currentID = k_Buy_Coins_Pack_3;
[self BuyGameCoins];
}
- (BOOL)connected
{
Reachability *reachability = [Reachability reachabilityForInternetConnection];
NetworkStatus networkStatus = [reachability currentReachabilityStatus];
return !(networkStatus == NotReachable);
}
-(void)BuyGameCoins
{
if([self connected])
{
if ([SKPaymentQueue canMakePayments]) {
[self ShowActivityIndicator];
SKProductsRequest *request = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObject:self.currentProductId]];
request.delegate = self;
[request start];
} else {
UIAlertView *tmp = [[UIAlertView alloc]
initWithTitle:@"Prohibited"
message:@"Parental Control is enabled, cannot make a purchase!"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:@"Ok", nil];
tmp.tag = kInApPurchaseFailedAlertTag;
[tmp show];
[tmp release];
}
}
else
{
UIAlertView* alert= [[[UIAlertView alloc] initWithTitle:@"No Internet Connection!" message: @"Checkout your internet connectivity!"
delegate: NULL cancelButtonTitle: @"OK" otherButtonTitles: NULL] autorelease];
[alert show];
}
}
-(void)RestoreTransaction
{
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
[[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
}
//Then this delegate Funtion Will be fired
- (void) paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue
{
NSMutableArray *purchasedItemIDs = [[NSMutableArray alloc] init];
//NSLog(@"received restored transactions: %i", queue.transactions.count);
for (SKPaymentTransaction *transaction in queue.transactions)
{
NSString *productID = transaction.payment.productIdentifier;
[purchasedItemIDs addObject:productID];
if([productID isEqualToString:IN_APP_KEY_BUY_COIN_1])
{
//NSError *error = nil;
//[[NSNotificationCenter defaultCenter] postNotificationName:@"inApSuccessful" object:nil];
}
}
}
#pragma mark StoreKit Delegate
-(void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {
for (SKPaymentTransaction *transaction in transactions) {
switch (transaction.transactionState) {
case SKPaymentTransactionStatePurchasing:
// show wait view here
[self ShowActivityIndicator];
break;
case SKPaymentTransactionStatePurchased:
{
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
// remove wait view and unlock feature 2
[self StopIndicator];
UIAlertView *tmp = [[UIAlertView alloc]
initWithTitle:@"Complete"
message:@"Thanks for purchasing Credits!"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:@"Ok", nil];
tmp.tag = kInApPurchaseSucessAlertTag;
[tmp show];
[tmp release];
// do other thing to enable the features
}
break;
case SKPaymentTransactionStateRestored:
{
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
// remove wait view here
[self StopIndicator];
}
break;
case SKPaymentTransactionStateFailed:
{
if (transaction.error.code != SKErrorPaymentCancelled) {
//NSLog(@"Error payment cancelled");
}
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
// remove wait view here
//statusLabel.text = @"Purchase Error!";
[self StopIndicator];
}
break;
default:
break;
}
}
}
-(void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{
// remove wait view here
SKProduct *validProduct = nil;
int count = [response.products count];
if (count>0) {
validProduct = [response.products objectAtIndex:0];
// //#define kStoredData @"com.emirbytes.IAPNoobService"
// SKPayment *payment = [SKPayment paymentWithProductIdentifier:@"com.emirbytes.IAPNoob.01"];
SKPayment *payment = [SKPayment paymentWithProductIdentifier:self.currentProductId];
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
[[SKPaymentQueue defaultQueue] addPayment:payment];
} else {
[self StopIndicator];
UIAlertView *tmp = [[UIAlertView alloc]
initWithTitle:@"Not Available"
message:@"No products to purchase"
delegate:self
cancelButtonTitle:nil
otherButtonTitles:@"Ok", nil];
tmp.tag = kInApPurchaseFailedAlertTag;
[tmp show];
[tmp release];
}
}
-(void)requestDidFinish:(SKRequest *)request
{
[request release];
}
-(void)request:(SKRequest *)request didFailWithError:(NSError *)error
{
//NSLog(@"Failed to connect with error: %@", [error localizedDescription]);
[[NSNotificationCenter defaultCenter] postNotificationName:@"inApFailed" object:nil];
}
#pragma mark AlertView Delegate
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if( alertView.tag == kInApPurchaseSucessAlertTag)
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"inApSuccessful" object:nil];
}
else if( alertView.tag == kInApPurchaseFailedAlertTag)
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"inApFailed" object:nil];
}
}
-(bool)IS_IOS_7
{
NSString *currOsVersion = [[UIDevice currentDevice] systemVersion];
float sysver = [currOsVersion floatValue] ;
if(sysver >=7.0f)
return true;
return false;
}
-(void)ShowActivityIndicator
{
if(!mLoadingView) // UIAlertView *mLoadingView;
{
if([self IS_IOS_7])
{
mLoadingView = [[UIAlertView alloc] initWithTitle:@"" message:@"Please wait..." delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
UIActivityIndicatorView *actInd = [[[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(125, 50, 30, 30)] autorelease];
actInd.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
[mLoadingView addSubview:actInd];
[actInd startAnimating];
[mLoadingView show];
}
else
{
mLoadingView = [[UIAlertView alloc] initWithTitle:@"" message:@"" delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
UIActivityIndicatorView *actInd = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
actInd.frame = CGRectMake(128.0f, 45.0f, 25.0f, 25.0f);
[mLoadingView addSubview:actInd];
[actInd startAnimating];
[actInd release];
UILabel *l = [[UILabel alloc]init];
l.frame = CGRectMake(100, -25, 210, 100);
l.text = @"Please wait...";
l.font = [UIFont fontWithName:@"Helvetica" size:16];
l.textColor = [UIColor whiteColor];
l.shadowColor = [UIColor blackColor];
l.shadowOffset = CGSizeMake(1.0, 1.0);
l.backgroundColor = [UIColor clearColor];
[mLoadingView addSubview:l];
[l release];
}
}
[mLoadingView show];
}
-(void)StopIndicator
{
[mLoadingView dismissWithClickedButtonIndex:0 animated:NO];
}
@end