// IAPUtilOC.m
#import "IAPUtilOC.h"
//应用内收费
#define ProductID_TEST @"cn.com.firedragonpzy.Test"
int
temp;
@implementation IAPUtilOC
-(id)init{
if
((self = [super init])) {
currentLevelId = 0;
_buyState = FALSE;
temp = 1;
}
return
self;
}
-(
void
)finishBuyWithHand{
if
(_buyState) {
_buyState = FALSE;
}
}
-(
BOOL
)getBuyState{
return
_buyState;
}
-(
void
)setCurrentLevelId:(
int
)current_levelId{
//----------监听购买结果
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
currentLevelId = current_levelId;
}
-(
BOOL
)checkIsHadPay{
NSString* productId = nil;
switch
(currentLevelId) {
case
1:
productId = [NSString stringWithString:ProductID_TEST];
break
;
// case 2:
// productId = [NSString stringWithString:ProductID_IAP_SHUI];
// break;
// case 3:
// productId = [NSString stringWithString:ProductID_IAP_HUO];
// break;
// case 4:
// productId = [NSString stringWithString:ProductID_IAP_TU];
// break;
// case 5:
// productId = [NSString stringWithString:ProductID_IAP_LUNHUI];
// break;
default
:
break
;
}
BOOL
productPaurchased = [[NSUserDefaults standardUserDefaults] boolForKey:productId];
return
productPaurchased;
}
-(
void
)buy{
NSLog(@
"buy========="
);
if
([SKPaymentQueue canMakePayments]) {
_buyState = TRUE;
[self RequestProductData];
NSLog(@
"允许程序内付费"
);
}
else
{
NSLog(@
"不允许程序内付费"
);
}
}
-(
void
)restorePay{
_buyState = TRUE;
//[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
[[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
}
-(
bool
)CanMakePay{
return
[SKPaymentQueue canMakePayments];
}
-(
void
)RequestProductData{
NSLog(@
"-----------请求对应的产品信息-----------------"
);
NSArray* product = nil;
switch
(currentLevelId) {
case
1:
product = [[NSArray alloc] initWithObjects:ProductID_TEST, nil];
break
;
// case 2:
// product = [[NSArray alloc] initWithObjects:ProductID_IAP_SHUI, nil];
// break;
// case 3:
// product = [[NSArray alloc] initWithObjects:ProductID_IAP_HUO, nil];
// break;
// case 4:
// product = [[NSArray alloc] initWithObjects:ProductID_IAP_TU, nil];
// break;
// case 5:
// product = [[NSArray alloc] initWithObjects:ProductID_IAP_LUNHUI, nil];
// break;
default
:
break
;
}
NSSet *nsset = [NSSet setWithArray:product];
SKProductsRequest* request = [[SKProductsRequest alloc] initWithProductIdentifiers:nsset];
request.delegate = self;
[request start];
[product release];
}
// 请求协议
//收到产品信息
-(
void
)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response{
NSLog(@
"---------收到产品反馈信息----------"
);
NSArray* myProduct = response.products;
NSLog(@
"Product ID:%@"
, response.invalidProductIdentifiers);
NSLog(@
"priduct numbers:%d"
, [myProduct count]);
//populate UI
for
(SKProduct* product in myProduct) {
NSLog(@
"product info"
);
NSLog(@
"SKProduct 描述信息:%@"
, [product description]);
NSLog(@
"product title:%@"
, product.localizedTitle);
NSLog(@
"product info:%@"
, product.localizedDescription);
NSLog(@
"price:%@"
, product.price);
NSLog(@
"Product id:%@"
, product.productIdentifier);
}
SKPayment* payment = nil;
switch
(currentLevelId) {
case
1:
payment = [SKPayment paymentWithProductIdentifier:ProductID_TEST];
break
;
// case 2:
// payment = [SKPayment paymentWithProductIdentifier:ProductID_IAP_SHUI];
// break;
// case 3:
// payment = [SKPayment paymentWithProductIdentifier:ProductID_IAP_HUO];
// break;
// case 4:
// payment = [SKPayment paymentWithProductIdentifier:ProductID_IAP_TU];
// break;
// case 5:
// payment = [SKPayment paymentWithProductIdentifier:ProductID_IAP_LUNHUI];
// break;
default
:
break
;
}
NSLog(@
"------------------发送购买请求--------------------"
);
[[SKPaymentQueue defaultQueue] addPayment:payment];
[request autorelease];
}
-(
void
)requestProUpgradeProductData{
NSLog(@
"-------请求升级数据------------"
);
}
//弹出错误信息
-(
void
)request:(SKRequest *)request didFailWithError:(NSError *)error{
NSLog(@
"---------弹出错误信息-------------"
);
_buyState = FALSE;
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@
"Alert"
, NULL) message:[error localizedDescription] delegate:nil cancelButtonTitle:NSLocalizedString(@
"Close"
, nil) otherButtonTitles:nil, nil];
[alertView show];
[alertView release];
}
-(
void
)requestDidFinish:(SKRequest *)request{
NSLog(@
"---------------反馈信息结束-------------"
);
}
-(
void
)PurchasedTransaction:(SKPaymentTransaction *)transaction{
NSLog(@
"------------PurchasedTransaction-----------------"
);
NSArray* transactions = [[NSArray alloc] initWithObjects:transaction, nil];
[self paymentQueue:[SKPaymentQueue defaultQueue] updatedTransactions:transactions];
[transactions release];
}
// 千万不要忘记绑定,代码如下:
//----监听购买结果
//[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
-(
void
)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions{
NSLog(@
"---------------------paymentQueue aaa--------------------"
);
for
(SKPaymentTransaction* transaction in transactions) {
NSLog(@
"start -------state:%d"
, transaction.transactionState);
switch
(transaction.transactionState) {
case
SKPaymentTransactionStatePurchased:
//交易完成
NSLog(@
"-----------------------交易完成-------------------"
);
[self completeTransaction:transaction];
NSLog(@
"不允许程序内付费购买"
);
// UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"Successful purchase!(购买成功!)" delegate:nil cancelButtonTitle:NSLocalizedString(@"Close(关闭)", nil) otherButtonTitles:nil, nil];
// [alertView show];
// [alertView release];
break
;
case
SKPaymentTransactionStateFailed:
//交易失败
NSLog(@
"-----------交易失败------------"
);
[self failedTransaction:transaction];
// UIAlertView* alertView2 = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"Purchase failed, please try again to buy.(购买失败,请重新尝试购买。)" delegate:nil cancelButtonTitle:NSLocalizedString(@"Close(关闭)", nil) otherButtonTitles:nil, nil];
// [alertView2 show];
// [alertView2 release];
break
;
case
SKPaymentTransactionStateRestored:
//已经购买过该商品
NSLog(@
"--------had pay for the product---------------"
);
[self restoreTransaction:transaction];
//[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
break
;
case
SKPaymentTransactionStatePurchasing:
//商品添加进列表
NSLog(@
"---------商品添加进列表----------------"
);
break
;
default
:
NSLog(@
"other "
);
break
;
}
}
}
-(
void
)completeTransaction:(SKPaymentTransaction *)transaction{
NSLog(@
"-----------completeTransaction---------------"
);
NSString* product = transaction.payment.productIdentifier;
NSLog(@
"-----------%@---------------"
, product);
_buyState = FALSE;
if
([product length] > 0) {
[self recordTransaction:product];
}
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
}
//记录交易
-(
void
)recordTransaction:(NSString *)product{
NSLog(@
"----------记录交易-------------"
);
[[NSUserDefaults standardUserDefaults] setBool:TRUE forKey:product];
}
//处理下载内容
-(
void
)provideContent:(NSString *)product{
NSLog(@
"------------下载-----------------"
);
}
-(
void
)failedTransaction:(SKPaymentTransaction *)transaction{
NSLog(@
"----------------交易失败------------------"
);
_buyState = FALSE;
if
(transaction.error.code != SKErrorPaymentCancelled) {
}
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
}
-(
void
)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue{
NSLog(@
"restore finished"
);
_buyState = FALSE;
if
(temp == 1) {
NSArray *transactionsArray = [[NSArray alloc] initWithArray:[queue transactions]];
int
transactionsNumber = [transactionsArray count];
NSString *messageStr = [NSString stringWithFormat:@
"Has been successfully restored to purchase goods(已成功恢复购买的商品)"
, transactionsNumber, transactionsNumber];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@
"恢复"
message:messageStr delegate:nil cancelButtonTitle:@
"OK"
otherButtonTitles:nil, nil];
[alertView show];
[alertView release];
temp = 2;
}
}
-(
void
)restoreTransaction:(SKPaymentTransaction *)transaction{
NSLog(@
"------------------restore ing------------------"
);
SKPayment *payment = [transaction payment];
[self recordTransaction:[payment productIdentifier]];
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
}
-(
void
)paymentQueue:(SKPaymentQueue *)queue restoreCompletedTransactionsFailedWithError:(NSError *)error{
//取消
_buyState = FALSE;
NSLog(@
"==============paymentQueue restore========================="
);
NSArray *transactionsArray = [[NSArray alloc] initWithArray:[queue transactions]];
NSLog(@
"%d"
, [transactionsArray count]);
}
#pragma mark connection delegate
- (
void
)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSLog(@
"%@"
, [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease]);
}
- (
void
)connectionDidFinishLoading:(NSURLConnection *)connection{
}
- (
void
)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
switch
([(NSHTTPURLResponse *)response statusCode]) {
case
200:
case
206:
break
;
case
304:
break
;
case
400:
break
;
case
404:
break
;
case
416:
break
;
case
403:
break
;
case
401:
case
500:
break
;
default
:
break
;
}
}
- (
void
)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@
"test"
);
}
-(
void
)dealloc
{
NSLog(@
"-----------------IAP release-------------------"
);
[[SKPaymentQueue defaultQueue] removeTransactionObserver:self];
//解除监听
[super dealloc];
}
@end
联系客服