应用内支付

http://www.cocoachina.com/bbs/read.php?tid-24738-page-1.html

http://wenku.it168.com/d_000651373.shtml

http://blog.csdn.net/shenjie12345678/article/details/40978977

#import "ViewController.h"
#import <StoreKit/StoreKit.h>
@interface ViewController ()<SKProductsRequestDelegate,SKPaymentTransactionObserver>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.view.backgroundColor = [UIColor whiteColor];
    //监听购买结果
    [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
    
    //判断是否允许应用内支付
    if ([SKPaymentQueue canMakePayments]){
        [self getProductInfo];
    }
    else{
    
        NSLog(@"失败,用户禁止应用内付费购买");
    }
}

-(void)viewDidUnload{
    [super viewDidLoad];
    [[SKPaymentQueue defaultQueue] removeTransactionObserver:self];
}

-(void)getProductInfo
{
    NSSet *set = [NSSet setWithArray:@[@"ProductId"]];
    SKProductsRequest *request = [[SKProductsRequest alloc]initWithProductIdentifiers:set];
    request.delegate = self;
    [request start];

}
#pragma mark 查询回调函数 
-(void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response{
    NSArray *myProuduct = response.products;
    if (myProuduct.count == 0){
        NSLog(@"无法获取产品信息,购买失败");
        return;
    }
    SKPayment *payment = [SKPayment paymentWithProduct:myProuduct[0]];
    [[SKPaymentQueue defaultQueue] addPayment:payment];
}

#pragma mark 操作有结果时 回调函数
-(void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray<SKPaymentTransaction *> *)transactions{
    for (SKPaymentTransaction *tramsaction in transactions){
        switch (tramsaction.transactionState) {
            case SKPaymentTransactionStatePurchased://交易完成
                [self completeTransaction:tramsaction];
                break;
            case SKPaymentTransactionStateFailed://交易失败
                [self failedTransaction:tramsaction];
                break;
            case SKPaymentTransactionStatePurchasing://商品添加进列表
                NSLog(@"商品添加进列表");
                break;;
            case SKPaymentTransactionStateRestored://已经购买过该商品
                [self restoreTransaction:tramsaction];
                break;
            default:
                break;
        }
        
    }

}
-(void)completeTransaction:(SKPaymentTransaction *)transaction{
    NSString *productIdentifier = transaction.payment.productIdentifier;
    NSString *receipt = [transaction.transactionReceipt base64Encoding];
    if ([productIdentifier length]>0){
    //向自己的服务器验证购买凭证
    }
    [[SKPaymentQueue defaultQueue]finishTransaction:transaction];
}
-(void)failedTransaction:(SKPaymentTransaction *)transaction{
    if (transaction.error.code != SKErrorPaymentCancelled){
        NSLog(@"购买失败");
    }else{
        NSLog(@"用户取消交易");
    }
    [[SKPaymentQueue defaultQueue] finishTransaction:transaction];
}
-(void)restoreTransaction:(SKPaymentTransaction *)transaction{
    //对于已购商品,处理恢复购买的逻辑
    [[SKPaymentQueue defaultQueue]finishTransaction:transaction];
    
}

@end


你可能感兴趣的:(应用内支付)