您好,登錄后才能下訂單哦!
小編給大家分享一下IOS如何實現應用內支付返回新舊Receipt適配,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
ios7.0后ios支付成功返回的票據Receipt的獲取方式有了新的方式,
原來的SKPaymentTransaction中的transactionReceipt屬性獲取票據的方式已經過期,雖然還能使用,但是蘋果官方建議使用新的
新版的獲取Receipt的方式是通過新接口如下
NSURL *receiptURL = [[NSBundle mainBundle] appStoreReceiptURL]; NSData *receipt = [NSData dataWithContentsOfURL:receiptURL];
當然,低于ios7.0的還是需要使用老版本接口,對兩種版本進行適配的代碼如下:
NSData*receipt= nil; if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")){ //ios after 7.0 NSURL *receiptURL = [[NSBundle mainBundle] appStoreReceiptURL]; receipt = [NSData dataWithContentsOfURL:receiptURL]; NSString *receiptURLStr = [receiptURL absoluteString]; NSRange rangeSandbox = [receiptURLStr rangeOfString:@"sandbox"]; if (rangeSandbox.location != NSNotFound){ record[kIAPEnvironment] = [NSNumber numberWithInt:1]; } }else{ //ios 3.0~7.0 receipt = transaction.transactionReceipt; NSDictionary *dict = [NSDictionary dictionaryWithContentsOfData:receipt]; if (dict){ NSString *env = [dict objectForKey:@"environment"]; if ([env isEqualToString:@"Sandbox"]) { record[kIAPEnvironment] = [NSNumber numberWithInt:1]; } } }
判斷是否是沙盒支付,新版本可以直接判斷receiptURL中是否存在“sandbox“
老版本receipt可以解析NSData 查看environment是否為Sandbox來判斷
+ (NSDictionary *)dictionaryWithContentsOfData: (NSData *)data{ CFPropertyListRef plist = CFPropertyListCreateFromXMLData(kCFAllocatorDefault, (__bridge CFDataRef)data, kCFPropertyListImmutable, NULL); if(plist == nil) return nil; if ([(__bridge id)plist isKindOfClass:[NSDictionary class]]){ return (__bridge NSDictionary *)plist; }else{ CFRelease(plist); return nil; } }
客戶端receipt驗證方式:
NSError *error; NSDictionary *requestContents = @{ @"receipt-data": [receipt base64EncodedString] }; NSData *requestData = [NSJSONSerialization dataWithJSONObject:requestContents options:0 error:&error]; if (!requestData) { return; } // Create a POST request with the receipt data. NSURL *storeURL = [NSURL URLWithString:@"https://sandbox.itunes.apple.com/verifyReceipt"];//根據是否是沙盒支付驗證取正確的地址 NSMutableURLRequest *storeRequest = [NSMutableURLRequest requestWithURL:storeURL]; [storeRequest setHTTPMethod:@"POST"]; [storeRequest setHTTPBody:requestData]; // Make a connection to the iTunes Store on a background queue. NSOperationQueue *queue = [[NSOperationQueue alloc] init]; [NSURLConnection sendAsynchronousRequest:storeRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) { if (connectionError) { /* ... Handle error ... */ } else { NSError *error; NSDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error]; if (!jsonResponse) { /* ... Handle error ...*/ } /* ... Send a response back to the device ... */ } }];
老版本返回格式:
{ bid = "com.coodezhang.test"; bvrs = "1.0"; "item_id" = 892617314; "original_purchase_date" = "2017-12-14 07:43:14 Etc/GMT"; "original_purchase_date_ms" = 1626147394550; "original_purchase_date_pst" = "2017-12-14 12:43:14 America/Los_Angeles"; "original_transaction_id" = 1000001127239959; "product_id" = "com.coodezhang.test_coins99M_Tier1"; "purchase_date" = "2017-12-14 07:43:14 Etc/GMT"; "purchase_date_ms" = 1626147394550; "purchase_date_pst" = "2017-12-14 12:43:14 America/Los_Angeles"; quantity = 1; "transaction_id" = 1000001127239959; "unique_identifier" = 0000b0124819; "unique_vendor_identifier" = "ASDGF2DB-DSAD-5A21-9611-642A4B9CASDE7"; }; status = 0; }
新版本返回格式官方文檔:官方文檔
新版本返回格式:
{ environment = Sandbox; receipt = { "adam_id" = 0; "app_item_id" = 0; "application_version" = 1; "bundle_id" = "com.coodezhang.test"; "download_id" = 0; "in_app" = ( { "is_trial_period" = false; "original_purchase_date" = "2017-12-14 07:18:56 Etc/GMT"; "original_purchase_date_ms" = 1513235936000; "original_purchase_date_pst" = "2017-12-13 23:18:56 America/Los_Angeles"; "original_transaction_id" = 1000000359369424; "product_id" = "com.coodezhang.test_coins99M_Tier1"; "purchase_date" = "2017-12-14 07:18:56 Etc/GMT"; "purchase_date_ms" = 1513235936000; "purchase_date_pst" = "2017-12-13 23:18:56 America/Los_Angeles"; quantity = 1; "transaction_id" = 1000000359369424; } ...... 可能存在多條 ); "original_application_version" = "1.0"; "original_purchase_date" = "2013-08-01 07:00:00 Etc/GMT"; "original_purchase_date_ms" = 1375340400000; "original_purchase_date_pst" = "2013-08-01 00:00:00 America/Los_Angeles"; "receipt_creation_date" = "2017-12-14 07:18:56 Etc/GMT"; "receipt_creation_date_ms" = 1513235936000; "receipt_creation_date_pst" = "2017-12-13 23:18:56 America/Los_Angeles"; "receipt_type" = ProductionSandbox; "request_date" = "2017-12-14 07:19:23 Etc/GMT"; "request_date_ms" = 1513235963829; "request_date_pst" = "2017-12-13 23:19:23 America/Los_Angeles"; "version_external_identifier" = 0; }; status = 0; }
值得注意的是,新版中數據結構中的in_app字段,可能包含多個transaction的receipt。當完成transaction后,還沒有成功調用讀取過receipt的接口,那下一次讀取recept時會把所有的都讀取出來,從而出現多條數據。
一般開發商app支付都有自己的支付系統,可能每次下單之前都會創建自己的訂單號,需要與ios支付后返回的receipt一一對應,這種情況下如何處理還需要注意。
以上是“IOS如何實現應用內支付返回新舊Receipt適配”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。