1. 程式人生 > >iOS 蘋果內購 In-App Purchase 踩過的坑

iOS 蘋果內購 In-App Purchase 踩過的坑

專案裡面要接蘋果支付,結果我研究了一個小時的apple pay,結果經理說是蘋果內購,當時就感覺被耍了!好了,那就說說In-App Purchase這個吧,前面所有的準備工作經理做完了,我只是碼程式碼,結果購買的回撥都執行,就是介面啥反應都沒有,不知道是前期的工作沒做好,還是我的程式碼有問題?上程式碼:

第一步,匯入標頭檔案籤協議

// 在需要購買的介面裡面匯入,前提是你要將StoreKit框架新增到你的工程裡面
#import <StoreKit/StoreKit.h>
// 簽訂內購需要的兩個協議
@interface VIPViewController ()<SKPaymentTransactionObserver
,SKProductsRequestDelegate>
{ NSInteger _index;// 購買的那件商品 }

第二步,選擇需要購買的商品,並請求商品資訊

// push到支付介面
- (void)pushApplePay:(NSInteger)index{

    _index = index;
    //判斷能不能購買
    // 裡面儲存的是本地化的商品資訊
    VipDataHandle *handle = [VipDataHandle sharedInstence];

    if([SKPaymentQueue canMakePayments]){
        [self
requestProductData:handle.products[index]]; }else{ NSLog(@"不允許程式內付費"); } } //請求商品 - (void)requestProductData:(NSString *)type{ NSLog(@"-------------請求對應的產品資訊----------------"); NSArray *product = [[NSArray alloc] initWithObjects:type,nil]; NSSet *nsset = [NSSet setWithArray:product]; SKProductsRequest *request = [[SKProductsRequest alloc] initWithProductIdentifiers:nsset]; request.delegate
= self; [request start]; } //收到產品返回資訊 - (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response{ NSLog(@"--------------收到產品反饋訊息---------------------"); NSArray *product = response.products; if([product count] == 0){ NSLog(@"--------------沒有商品------------------"); return; } NSLog(@"productID:%@", response.invalidProductIdentifiers); NSLog(@"產品付費數量:%ld",[product count]); SKProduct *p = nil; for (SKProduct *pro in product) { NSLog(@"description:%@", [pro description]); NSLog(@"localizedTitle:%@", [pro localizedTitle]); NSLog(@"localizedDescription:%@", [pro localizedDescription]); NSLog(@"price:%@", [pro price]); NSLog(@"productIdentifier:%@", [pro productIdentifier]); if([pro.productIdentifier isEqualToString:[VipDataHandle sharedInstence].products[_index]]){ p = pro; } } SKPayment *payment = [SKPayment paymentWithProduct:p]; NSLog(@"傳送購買請求"); [[SKPaymentQueue defaultQueue] addPayment:payment]; } //請求失敗 - (void)request:(SKRequest *)request didFailWithError:(NSError *)error{ NSLog(@"------------------錯誤-----------------:%@", error); } //資訊結束 - (void)requestDidFinish:(SKRequest *)request{ NSLog(@"------------反饋資訊結束-----------------"); } -(void) PurchasedTransaction: (SKPaymentTransaction *)transaction{ NSLog(@"-----PurchasedTransaction----"); NSArray *transactions =[[NSArray alloc] initWithObjects:transaction, nil]; [self paymentQueue:[SKPaymentQueue defaultQueue] updatedTransactions:transactions]; }

第三步,監聽購買狀態

//監聽購買結果
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {

    for (SKPaymentTransaction *transaction in transactions){

        switch (transaction.transactionState){

            case SKPaymentTransactionStatePurchased://交易完成

                NSLog(@"transactionIdentifier = %@", transaction.transactionIdentifier);
                [self completeTransaction:transaction];
                break;
            case SKPaymentTransactionStateFailed://交易失敗

                [self failedTransaction:transaction];
                break;
            case SKPaymentTransactionStateRestored://已經購買過該商品

                [self restoreTransaction:transaction];
                break;
            case SKPaymentTransactionStatePurchasing:      //商品新增進列表

                NSLog(@"商品新增進列表");
                break;
            default:
                break;
        }
    }
}
- (void)completeTransaction:(SKPaymentTransaction *)transaction {

    // Your application should implement these two methods.
    NSString * productIdentifier = transaction.payment.productIdentifier;

    if ([productIdentifier length] > 0) {
        // 向自己的伺服器驗證購買憑證

        NSLog(@"驗證購買");
    }
    // Remove the transaction from the payment queue.
    [[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];
}

第四步,拿到蘋果返回來的票據去請求自己的伺服器,校驗是否正確

// 校驗支付真確性的方法
- (void)checkPay:(NSNotification *)note{

    [self appRequest:note.userInfo[@"appleData"]];
}

// 請求伺服器
- (void)appRequest:(NSString *)appleData{

    InAppPayStore *store = [[InAppPayStore alloc]init];
    store.delegate = self;
    store.uid = userId;
    store.appleData = appleData;
    [store request];
}
- (void)success{

    NSLog(@"支付成功");
}
- (void)failure:(NSString *)error{

    NSLog(@"支付失敗");
}

程式碼就是按照上面做的,請求返回的商品id也有,新增購買後,狀態就是新增購買,始終在介面上啥都不顯示,我也是醉了!當然了後面的票據校驗還沒做,前面的還沒通後面沒法做的,換了部手機就可以跳出支付介面,現在還在找原因

大神路過看看哈,請教