193 lines
6.7 KiB
Objective-C
193 lines
6.7 KiB
Objective-C
//
|
|
// IAPManager.m
|
|
// Unity-iPhone
|
|
//
|
|
// Created by admin on 2017/8/28.
|
|
//
|
|
//
|
|
|
|
#import "IAPManager.h"
|
|
|
|
|
|
|
|
@implementation IAPManager
|
|
|
|
|
|
-(void) attachObserver{
|
|
NSLog(@"AttachObserver");
|
|
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
|
|
}
|
|
|
|
|
|
-(BOOL) CanMakePayment{
|
|
return [SKPaymentQueue canMakePayments];
|
|
}
|
|
//请求商品信息
|
|
|
|
-(void) requestProductData:(NSString *)productIdentifiers{
|
|
NSLog(@"unity.log----test ~~xcode~~请求商品信息回掉!!!---001---");
|
|
|
|
NSArray *idArray = [productIdentifiers componentsSeparatedByString:@"\t"];
|
|
NSSet *idSet = [NSSet setWithArray:idArray];
|
|
[self sendRequest:idSet];
|
|
NSLog(@"unity.log----test ~~xcode~~请求商品信息回掉!!!---002---");
|
|
|
|
}
|
|
|
|
//请求商品信息
|
|
-(void)sendRequest:(NSSet *)idSet{
|
|
NSLog(@"unity.log----test ~~xcode~~请求商品信息回掉!!!---003---");
|
|
|
|
SKProductsRequest *request = [[SKProductsRequest alloc] initWithProductIdentifiers:idSet];
|
|
request.delegate = self;
|
|
[request start];
|
|
NSLog(@"unity.log----test ~~xcode~~请求商品信息回掉!!!---004--");
|
|
|
|
}
|
|
//请求商品回掉信息
|
|
|
|
-(void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response{
|
|
NSLog(@"unity.log----test ~~xcode~~请求商品信息回掉!!!---001--1000-");
|
|
|
|
NSArray *products = response.products;
|
|
|
|
for (SKProduct *p in products) {
|
|
NSLog(@"unity.log----test ~~xcode~~请求商品信息回掉!!!---001---*****1");
|
|
|
|
UnitySendMessage("ShopPanel", "ShowProductList", [[self productInfo:p] UTF8String]);
|
|
}
|
|
|
|
for(NSString *invalidProductId in response.invalidProductIdentifiers){
|
|
NSLog(@"Invalid product id:%@",invalidProductId);
|
|
}
|
|
|
|
[request autorelease];
|
|
}
|
|
//请求商品购买
|
|
|
|
-(void)buyRequest:(NSString *)productIdentifier{
|
|
// NSLog(@"unity.log----test ~~xcode~~请求商品!!!---001--1000-");
|
|
SKPayment *payment = [SKPayment paymentWithProductIdentifier:productIdentifier];
|
|
// NSLog(@"unity.log----test ~~xcode~~请求商品!!!---001--1000-");
|
|
[[SKPaymentQueue defaultQueue] addPayment:payment];
|
|
// NSLog(@"unity.log----test ~~xcode~~请求商品!!!---001--1000-");
|
|
}
|
|
|
|
|
|
-(NSString *)productInfo:(SKProduct *)product{
|
|
NSLog(@"unity.log----test ~~xcode~~productInfo!!!---001---");
|
|
NSArray *info = [NSArray arrayWithObjects:product.localizedTitle,product.localizedDescription,product.price,product.productIdentifier, nil];
|
|
|
|
return [info componentsJoinedByString:@"\t"];
|
|
}
|
|
|
|
|
|
-(NSString *)transactionInfo:(SKPaymentTransaction *)transaction{
|
|
NSLog(@"unity.log----test ~~xcode~~transactionInfo!!!---001---");
|
|
|
|
return [self encode:(uint8_t *)transaction.transactionReceipt.bytes length:transaction.transactionReceipt.length];
|
|
|
|
//return [[NSString alloc] initWithData:transaction.transactionReceipt encoding:NSASCIIStringEncoding];
|
|
}
|
|
|
|
|
|
-(NSString *)encode:(const uint8_t *)input length:(NSInteger) length{
|
|
NSLog(@"unity.log----test ~~xcode~~encode!!!---001---");
|
|
static char table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
|
|
|
|
NSMutableData *data = [NSMutableData dataWithLength:((length+2)/3)*4];
|
|
uint8_t *output = (uint8_t *)data.mutableBytes;
|
|
|
|
for(NSInteger i=0; i<length; i+=3){
|
|
NSInteger value = 0;
|
|
for (NSInteger j= i; j<(i+3); j++) {
|
|
value<<=8;
|
|
|
|
if(j<length){
|
|
value |=(0xff & input[j]);
|
|
}
|
|
}
|
|
|
|
NSInteger index = (i/3)*4;
|
|
output[index + 0] = table[(value>>18) & 0x3f];
|
|
output[index + 1] = table[(value>>12) & 0x3f];
|
|
output[index + 2] = (i+1)<length ? table[(value>>6) & 0x3f] : '=';
|
|
output[index + 3] = (i+2)<length ? table[(value>>0) & 0x3f] : '=';
|
|
}
|
|
|
|
return [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
|
|
}
|
|
|
|
|
|
-(void) provideContent:(SKPaymentTransaction *)transaction{
|
|
NSLog(@"unity.log----test ~~xcode~~provideContent!!!---001---");
|
|
UnitySendMessage("ShopPanel", "ProvideContent", [[self transactionInfo:transaction] UTF8String]);
|
|
}
|
|
//购买回掉
|
|
|
|
-(void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions{
|
|
NSLog(@"unity.log----test ~~xcode~~paymentQueue!!!---001---");
|
|
for (SKPaymentTransaction *transaction in transactions) {
|
|
switch (transaction.transactionState) {
|
|
case SKPaymentTransactionStatePurchased:
|
|
[self completeTransaction:transaction];
|
|
// NSLog(@"unity.log----test ~~xcode~~paymentQueue 01!!!---001---");
|
|
break;
|
|
case SKPaymentTransactionStateFailed:
|
|
[self failedTransaction:transaction];
|
|
// NSLog(@"unity.log----test ~~xcode~~paymentQueue 02!!!---001---");
|
|
break;
|
|
case SKPaymentTransactionStateRestored:
|
|
[self restoreTransaction:transaction];
|
|
// NSLog(@"unity.log----test ~~xcode~~paymentQueue 03!!!---001---");
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-(void) completeTransaction:(SKPaymentTransaction *)transaction{
|
|
NSLog(@"unity.log----test ~~xcode~~completeTransaction成功!!!---001---");
|
|
NSLog(@"Comblete transaction : %@",transaction.transactionIdentifier);
|
|
[self provideContent:transaction];
|
|
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
|
|
// NSString * receipt = [transaction.transactionReceipt base64Encoding];
|
|
NSLog(@"unity.log----test ~~xcode~ receipt~ %@",transaction.transactionReceipt);
|
|
|
|
// NSString * str =[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
|
|
//NSLog(@"unity.log----test//////////// receipt~ %@",str);
|
|
//const char* message = [data UTF8String];
|
|
NSString * receipt = [transaction.transactionReceipt base64Encoding];
|
|
const char* message = [receipt UTF8String];
|
|
UnitySendMessage("ShopPanel", "PaySuccessFull",message);
|
|
}
|
|
|
|
|
|
-(void) failedTransaction:(SKPaymentTransaction *)transaction{
|
|
NSLog(@"unity.log----test ~~xcode~~failedTransaction失败!!!---001---");
|
|
NSLog(@"Failed transaction : %@",transaction.transactionIdentifier);
|
|
|
|
if (transaction.error.code != SKErrorPaymentCancelled) {
|
|
NSLog(@"unity.log----test ~~xcode~~购买失败!!!---001---");
|
|
|
|
}
|
|
else{
|
|
NSLog(@"unity.log----test ~~xcode~~取消交易!!!---001---");
|
|
}
|
|
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
|
|
}
|
|
|
|
|
|
-(void) restoreTransaction:(SKPaymentTransaction *)transaction{
|
|
NSLog(@"unity.log----test ~~xcode~~restoreTransaction!!!---001---");
|
|
NSLog(@"Restore transaction : %@",transaction.transactionIdentifier);
|
|
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|