服務器模式:
使用這種方式,要提供另外的服務器將產品發送給程序。 服務器交付適用于訂閱、內容類商品和服務,因為商品可以作為數據發送,而不需改動程序束。
例如,一個游戲提供的新的內容(關卡等)。 Store Kit不會對服務器端的設計和交互做出定義,這方面工作需要你來完成。 而且,Store
Kit不提供驗證用戶身份的機制,你需要來設計。 如果你的程序需要以上功能,例如,紀錄特定用戶的訂閱計劃, 你需要自己來設計和實現。
服務器類型的購買過程
1. 程序向服務器發送請求,獲得一份產品列表。
2. 服務器返回包含產品標識符的列表。
3. 程序向App Store發送請求,得到產品的信息。
4. App Store返回產品信息。
5. 程序把返回的產品信息顯示給用戶(App的store界面)
6. 用戶選擇某個產品
7. 程序向App Store發送支付請求
8. App Store處理支付請求并返回交易完成信息。
9. 程序從信息中獲得數據,并發送至服務器。
10. 服務器紀錄數據,并進行審(我們的)查。
11. 服務器將數據發給App Store來驗證該交易的有效性。
12. App Store對收到的數據進行解析,返回該數據和說明其是否有效的標識。
13. 服務器讀取返回的數據,確定用戶購買的內容。
14. 服務器將購買的內容傳遞給程序。
Apple建議在服務器端存儲產品標識,而不要將其存儲在plist中。 這樣就可以在不升級程序的前提下添加新的產品。
在服務器模式下, 你的程序將獲得交易(transaction)相關的信息,并將它發送給服務器。服務器可以驗證收到的數據,并將其解碼以確定需要交付的內容。 這個流程將在“驗證store收據”一節討論。
對于服務器模式,我們有安全性和可靠性方面的顧慮。 你應該測試整個環境來避免威脅。《Secure Coding Guide》文檔中有相關的提示說明。
雖然非消耗性商品可以用內置模式來恢復,訂閱類商品必須通過服務器來恢復。你要負責紀錄訂閱信息、恢復數據。 消耗類商品也可以通過服務器方式來紀錄。例如,由服務器提供的一項服務, 你可能需要用戶在多個設備上重新獲得結果。
蘋果服務端配置指南:
使用IAP內購的準備工作。通常需要經過以下幾個步驟(下面的準備工作是針對真機的Provisioning Profile配置過程,模擬器無法測試IAP內購):
1.在蘋果開發者中心創建支持IAP服務的App ID并指定具體的Bundle ID,假設是“com.tj.xxx”(注意這個Bundle ID就是日后要開發的游戲的Bundle ID)。
2.基于“com.tj.xxx”創建開發者配置文件(或描述文件)并導入對應的設備(創建過程中選擇支持IAP內購服務的App ID,這樣iOS設備在運行指定Boundle ID應用程序就知道此應用支持IAP內購服務)。
3.在iTunes Connect中創建一個應用(假設叫“IAPTest”,這是一款含有內購的游戲)并指定“套裝ID”為之前創建的“com.tj.xxx”,讓應用和這個App關聯(注意這個應用不需要提交)。
4.在iTunes Connect的“用戶和職能”中創建沙盒測試用戶。(測試階段用沙盒用戶可以進行購買,購買任何東西不用擔心被扣錢)。
5.到iTuens Connect中設置“App 內購買項目”,這里仍然以上面的“IAPest”項目為例,假設這個游戲中有一種道具,為“能量瓶”(為玩家提供能量),@“能量瓶”屬于消耗品,用完一次必須再次購買。
6.到iTunes Connect中找到“協議、稅務和銀行業務”增加“iOS Paid Applications”協議,并完成所有配置后等待審核通過(注意這一步如果不設置在應用程序中無法獲得可購買產品)。
在iOS“設置”中找到”iTunes Store與App Store“,在這里可以選擇使用沙盒用戶登錄或者處于注銷狀態,但是一定注意不能使用真實用戶登錄,否則下面的購買測試不會成功,因為到目前為止我們的應用并沒有真正通過蘋果官方審核,所以只能用沙盒測試用戶。
7.有了上面的設置之后保證應用程序Bundle ID和iTunes Connect中的Bundle ID(或者說App ID中配置的Bundle ID)一致即可準備開發。
ios客戶端
#import
@interface JarIAPManager ()
{
id _observer;
}
@end
@implementation JarIAPManager
+ (instancetype)defaultManager{
static JarIAPManager *defaultManager = nil;
static dispatch_once_t onceToken = 0;
dispatch_once(&onceToken, ^{
defaultManager = [[JarIAPManager alloc]? init];
});
return defaultManager;
}
- (instancetype)init{
self = [super init];
if (self) {
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
_observer = [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidEnterBackgroundNotification object:self queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
for (SKPaymentTransaction * paymentTransaction in [SKPaymentQueue defaultQueue].transactions) {
[[SKPaymentQueue defaultQueue] finishTransaction:paymentTransaction];
}
}];
}
return self;
}
- (void)dealloc{
[[SKPaymentQueue defaultQueue] removeTransactionObserver:self];
[[NSNotificationCenter defaultCenter] removeObserver:_observer];
_observer = nil;
}
-(BOOL)iapEnable{
return [SKPaymentQueue canMakePayments];
}
#pragma mark --purchase product
//根據產品標識符去購買產品--[購買結果]
- (void)purchaseProductWithIdenfifier:(NSString *)productIdentifier Order:(NSString *)Order{
//該字符串標識一個特定的產品和用戶原意購買的數量
SKMutablePayment * payment = [[SKMutablePayment alloc] init];
payment.productIdentifier = productIdentifier;
NSData *datas = [Order dataUsingEncoding:NSUTF8StringEncoding];
payment.requestData =datas;
[[SKPaymentQueue defaultQueue] addPayment:payment];
}
//SKPaymentTransactionObserver協議---[更常用的做法還是等待支付隊列告知交易狀態的更新。]
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions{
__weak typeof(self)weakSelf = self;
for (SKPaymentTransaction * paymentTransaction in transactions) {
//小票狀態--》支付交易的狀態
switch (paymentTransaction.transactionState) {
case SKPaymentTransactionStatePurchasing:
{
NSLog(@"JarIAPManager: Transaction is being added to the server queue.");
}
break;
case SKPaymentTransactionStatePurchased:
{
NSLog(@"JarIAPManager: Transaction is in queue, user has been charged.? Client should complete the transaction.");
[weakSelf purchaseSuccessForTransaction:paymentTransaction];
}
break;
case SKPaymentTransactionStateFailed:
{
NSLog(@"JarIAPManager: Transaction was cancelled or failed before being added to the server queue.");
[weakSelf purchaseFailedForTransaction:paymentTransaction];
}
break;
case SKPaymentTransactionStateRestored:
{
NSLog(@"JarIAPManager: Transaction was restored from user's purchase history.? Client should complete the transaction.");
[[SKPaymentQueue defaultQueue] finishTransaction:paymentTransaction];
}
default:
break;
}
}
}
執行
- (void)purchaseFailedForTransaction:(SKPaymentTransaction *)transaction{
NSLog(@"失敗流水--》%@",transaction.transactionIdentifier);
if (transaction != nil) {
[[Toast makeText:@"交易取消。" duration:3000] show:NO];
}
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
}
- (void)purchaseSuccessForTransaction:(SKPaymentTransaction *)transaction{
__weak typeof(self)weakSelf = self;
// 驗證憑證,獲取蘋果返回的交易憑證
// appStoreReceiptURL iOS7.0增加的,購買交易完成后,會將憑證存儲在該地址
NSURL * receiptURL = [[NSBundle mainBundle] appStoreReceiptURL];
// 從沙盒中獲取到購買憑證
NSData * receiptData = [NSData dataWithContentsOfURL:receiptURL];
//base64加密
NSString *encodeStr = [receiptData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
//打包成字典
NSMutableDictionary * parDic = [NSMutableDictionary dictionaryWithCapacity:3];
NSString *Order = [[NSString alloc] initWithData:transaction.payment.requestData encoding:NSUTF8StringEncoding];
DICT_SET_STRING(Order,TAG_ORDER, parDic);
DICT_SET_STRING(encodeStr, TAG_RECEIPT_DATA, parDic);
NSLog(@"下單成功的Order--》%@",Order);
if (Order !=NULL) {
//提前緩存
[[JarIAPSqliteService sharedInstance] insertTestListTransactionWithDic:parDic];
//??????? //檢測是否已經存儲過了
//??????? NSMutableArray *getTestDatas = [[NSMutableArray alloc] init];
//??????? getTestDatas = [[JarIAPSqliteService sharedInstance] getTestList];
//??????? NSLog(@"存儲后的Order個數-->%ld",(unsigned long)getTestDatas.count);
//??????? if (getTestDatas.count != 0) {
//??????????? for (NSMutableDictionary * dic in getTestDatas) {
//??????????????? NSLog(@"存儲后的Order---》%@",dic[TAG_ORDER]);
//??????????? }
//??????? }else{
//??????????? NSLog(@"存儲為0.");
//??????? }
//發送到SDK服務器
[[LoginDataSource sharedInstance] verifyThePurchaseWithDictData:parDic completion:^(NSDictionary *resultData, NSError *error) {
if ([resultData[TAG_RESULT] intValue] == 0 || Order.length != 0) {
[weakSelf actionWithResult:resultData OrderStr:eOrder];//執行刪除
}
}];
}
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
}
參考鏈接:
http://mobile.51cto.com/iphone-410162.htm? 比較全的文檔介紹
http://www.cocoachina.com/ios/20150129/11068.html demo
http://www.2cto.com/kf/201504/389224.html 有訂閱
http://blog.csdn.net/xingchen1106/article/details/45477433
http://blog.jobbole.com/38032/ 唐巧介紹安全
http://blog.csdn.net/fly_fish456/article/details/8955871
http://www.tairan.com/archives/2215/
【后期要理解的安全性,以及沙盒和正式環境】
http://www.360doc.com/content/14/1113/15/12282510_424834793.shtml
http://www.cocoachina.com/special/iap.html
http://www.2cto.com/kf/201504/389224.html