1. 程式人生 > >iOS開發--In-app Purchase內購驗證方法

iOS開發--In-app Purchase內購驗證方法

IOS7開始:AppStore增加了驗證內購(In App Purchasement)的方法, 以確保此次支付是有效地.

下面是蘋果提供的驗證地址:

開發測試用:

https://sandbox.itunes.apple.com/verifyReceipt

產品用:

https://buy.itunes.apple.com/verifyReceipt

當購買成功時,會得到蘋果返回的一個收據(receipt), 蘋果推薦的方法是將收據發給開發者的server,server像上述地址post http訊息,進行驗證, 蘋果將結果返回.用來判斷到底是真正的購買還是虛假的購買.

當然,我們也可以在客戶端來驗證,程式碼如下:

#define ITMS_SANDBOX_VERIFY_RECEIPT_URL     @"https://sandbox.itunes.apple.com/verifyReceipt"

#pragma mark - VerifyFinishedTransaction
-(void)verifyFinishedTransaction:(SKPaymentTransaction *)transaction{
    if(transaction.transactionState == SKPaymentTransactionStatePurchased){
        NSData *transactionReceipt  = transaction.transactionReceipt;
        //將transactionIdentifer和加密後的transactionReceipt資料傳送給server端
//        NSString* receipent = [NSString stringWithFormat:@"%s", transactionReceipt.bytes];//用來POST給server
        NSDictionary *requestContents = @{
                                          @"receipt-data": [self encode:(uint8_t *)transactionReceipt.bytes length:transactionReceipt.length]};
        
        NSLog(@"receipent = %@", requestContents);
        
        // 在app上做驗證, 僅用於測試
        NSData *requestData = [NSJSONSerialization dataWithJSONObject:requestContents
                                                              options:0
                                                                error:nil];
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:ITMS_SANDBOX_VERIFY_RECEIPT_URL]];
        [request setHTTPMethod:@"POST"];
        [request setHTTPBody:requestData];
        NSError* err;
        NSURLResponse *theResponse = nil;
        NSData *data=[NSURLConnection sendSynchronousRequest:request
                                           returningResponse:&theResponse
                                                       error:&err];
        NSError *jsonParsingError = nil;
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&jsonParsingError];
        NSLog(@"requestDict: %@", dict);
    }
}
//Base64加密
- (NSString *)encode:(const uint8_t *)input length:(NSInteger)length {
    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];
}


正確地返回資料應該是:

{

    receipt =     {

        bid = APPBundleID

        bvrs = AppVersion

        "item_id" = 1011063440;

        "original_purchase_date" = "2015-06-25 02:40:46 Etc/GMT";

        "original_purchase_date_ms" = 1435200046685;

        "original_purchase_date_pst" = "2015-06-24 19:40:46 America/Los_Angeles";

        "original_transaction_id" = 1000000160740781;

        "product_id" = ProductID

        "purchase_date" = "2015-06-25 02:40:46 Etc/GMT";

        "purchase_date_ms" = 1435200046685;

        "purchase_date_pst" = "2015-06-24 19:40:46 America/Los_Angeles";

        quantity = 1;

        "transaction_id" = 1000000160740781;

        "unique_identifier" = bea76003f25d87d1572ac452f600ef2575af0c7f;

        "unique_vendor_identifier" = "7C4CE200-847F-4F5C-BE32-04FAEE6C64E7";

    };

    status = 0;

}


其中: status=0,表示支付有效.

下面是其他的一些狀態碼:

Status Code

Description

21000

The App Store could not read the JSON object you provided.

21002

The data in the receipt-data property was malformed or missing.

21003

The receipt could not be authenticated.

21004

The shared secret you provided does not match the shared secret on file for your account.

Only returned for iOS 6 style transaction receipts for auto-renewable subscriptions.

21005

The receipt server is not currently available.

21006

This receipt is valid but the subscription has expired. When this status code is returned to your server, the receipt data is also decoded and returned as part of the response.

Only returned for iOS 6 style transaction receipts for auto-renewable subscriptions.

21007

This receipt is from the test environment, but it was sent to the production environment for verification. Send it to the test environment instead.

21008

This receipt is from the production environment, but it was sent to the test environment for verification. Send it to the production environment instead.

相關推薦

iOS開發--In-app Purchase驗證方法

IOS7開始:AppStore增加了驗證內購(In App Purchasement)的方法, 以確保此次支付是有效地. 下面是蘋果提供的驗證地址: 開發測試用: https://sandbox.itunes.apple.com/verifyReceipt 產品用: https://buy.itunes.a

iOS In-App Purchase 之 問題總結

1、內購流程 1、在 AppStore 中建立相應的物品,建立內購沙盒測試賬號2、客戶端從後臺獲取相應的物品 ID (當然也可以再客戶端寫死,但後期擴充套件性就受限制了)3、依據相應的物品 ID 請求商品的相關資訊4、依據商品資訊建立訂單請求交易5、依據返回

iOS In-App Purchase 之使用產品元資料

Working with Your Product’s Metadata 你可以改變除了產品ID和型別之外的產品的任何元資料。有些變化需要蘋果公司的審查。提交前的變化。如果你的程式內建購買產品從未被提交給蘋果稽核,您可以更改產品的詳細資訊。參考名稱。您可以隨時更改產品的名

iOS 蘋果 In-App Purchase 踩過的坑

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

ios 應用支付(In-App Purchase,沙盒測試,後臺驗證

1.蘋果iTunes Connect內購產品資訊錄入。 1)建立app內購買專案(Create New),選擇型別: 1.消耗型專案 對於消耗型 App 內購買專案,使用者每次下載時都必須進行購買。一次性服務通常屬於消耗型專案,例如釣魚App 中的魚餌。 2.非消耗型

ios 應用支付(In-App Purchase,沙盒測試,後臺驗證

1.蘋果iTunes Connect內購產品資訊錄入。 1)建立app內購買專案(Create New),選擇型別: 1.消耗型專案 對於消耗型 App 內購買專案,使用者每次下載時都必須進行購買。一次性服務通常屬於消耗型專案,例如釣魚App 中的魚餌。 2.非消耗型專案

ios(iap)關於問題“您已經購買了此程式購買(In App Purchase)專案,但尚未下載"的解決方案

近日在專案內購中遇到“您已經購買了此程式內購買(In App Purchase)專案,但尚未下載”的問題,查閱google和百度各種文章,沒找到合適的解決方案,折騰一整天,終於解決,解決方案貼出來,供大家參考。也是為了驗證這個方案的正確性。歡迎拍磚! 首先說說出現這個問題的

iOS應用建付費 In-App Purchase 詳細介紹(IAP詳解)

In App Purchase(程式內購買)為蘋果開發人員們打開了一個新的盈利渠道,如果您對此並不瞭解,下面這段 CocoaChina 會員“leon”翻譯的 In App Purchase 詳細介紹一定不能錯過。 一、In App Purchase概覽 Store

in-app purchase 開發的的3個坑 Error Domain=SKErrorDomain Code=0 "無法連接到 iTunes Store

不能 使用 view build 獲取 p s blog 一點 圖片 首先,完整的代碼我這裏就不提供了,這裏提供一個下載鏈接 http://code.cocoachina.com/view/130335 有個這個code,你調試也不能通過,原因主要有三個

iOS In-App Purchase中涉及到的貨幣單位

iOS In-App Purchase中涉及到的貨幣單位 發表於 2014 年 2 月 18 日 由 三石 現在iOS的AppStore上各類應用非常豐富,有付費的也有免費的,而免費的應用也可以支援IAP (In-App Purchase,應用內支付) 。 對於應

iPhone In App Purchase購買完成時驗證transactionReceipt

  最近正在做一個iphone遊戲內購買的專案,所以瞭解了一些In App Purchase相關的技術。   根據Apple官方文件,In App Purchase(IAP)有兩種模型:內建模型(Built-in Model)和伺服器模型(Server Model)。由於我做的專案需要用自己的伺服器管理

In-App Purchase 被拒絕!IOS App上傳App Store 由於使用第三方支付而被拒絕的解決方案

Guideline 3.1.1 - In-App Purchase We noticed that your app contains a payment mechanism other than in-app purchase for digital content

IOS驗證

客戶端在沙箱環境下購買成功之後,需要進行二次驗證 當應用向Apple伺服器請求購買成功之後,Apple會返回資料給應用,如下所示: 產品識別符號: product Identifier[在itunes store應用內定義的產品ID,例如com.公司名.產品名.道具

IOS開發-提升app性能的25條建議和技巧

contents oar profile 討論 遊戲開發 when plist 數據庫 formats 前言 這篇文章介紹了作者開發工作中總結的25個iOS開發ti

APP蘋果支付

$user_id = $_SESSION['user_id'];/* = 1024 */ if ($user_id <= 0) { return new ecjia_error(100, 'Invalid session'); } $url_buy = "https:/

解決某APP遊戲

對某APP內的道具購買進行破解 學習筆記三:對一款存在道具、關卡內購的APP進行破解使其道具、關卡購買免費化 一、將該未進行處理的APP通過模擬器安裝使用 通過安裝後的使用(購買其中的道具)發現該APP總體上通過手機發送簡訊的付費的方式進行支付操作。第二張圖又說

2018年十月份蘋果iOS退款方法流出,不看追悔莫及!

說起IOS手遊退款現在可能還是不為大眾所知,其實就是你充值進遊戲裡面的錢通過一些渠道從蘋果那裡退回來,此為大意,此退款只針對蘋果使用者,不是蘋果使用者的是無法返還你之前所充值的,所以蘋果也一直很受到遊戲玩家的喜愛,除了系統頂級流暢之外,還可以充值退款也是玩家看中

iOS開發APP內部切換語言

前言 iOS開發中,隨著APP越來越完善,很多APP都做了國際化,也實現了APP內部切換語言。 原理 國際化都會走到NSBundle的- (NSString *)localizedStringForKey:(NSString *)key value:(nullable NSString *)value

IOS開發8---APP應用程式圖示 及 程式啟動畫面設定

我們著重看下 Add Icon files in Info.plist這一項。 首先:我們必須開啟xx.Info.plist,對其進行編輯。 空白區域右鍵點選,選擇Add Row,選擇Icon files。 繼續新增條目: Iphone 和Ipad有不同,可

iOS開發APP撥打電話、發簡訊、發郵件功能

在iOS開發過程中,經常會用到點選公司簡介的電話號碼,會直接撥打電話;或者點選網址連結發郵件,點擊發簡訊按鈕直接發簡訊等,這些功能雖小,但很實用。 一、撥打電話功能: 通過app撥打電話有三種方式,具體程式碼如下所示: 1.直接跳轉到撥打電話介面,不彈出撥打電話提示框,打完