一、Agreements, Tax, and Banking Information (填寫協(xié)議、稅務(wù)和銀行卡信息)
https://developer.apple.com/library/content/technotes/tn2259/_index.html#//apple_ref/doc/uid/DTS40009578 官方
http://www.itdecent.cn/p/f7bff61e0b31 IOS內(nèi)購IAP,設(shè)置及使用&填寫協(xié)議、稅務(wù)
http://www.itdecent.cn/p/0d919415df20 【2020年1月更新】蘋果內(nèi)購銀行、稅務(wù)信息后臺配置
http://www.itdecent.cn/p/77b589728d74?utm_campaign=hugo 蘋果開發(fā)者后臺添加稅務(wù)、銀行信息
二、Certificates, Identifiers & Profiles (證書設(shè)置)
安裝好項目證書后,進(jìn)入Capabilities 設(shè)置界面,將In-App-Purchase 開關(guān)打開就OK.
三、iTunes Connect (添加沙箱測試技術(shù)員、添加應(yīng)用內(nèi)購商品信息)
1、iTunes Connect —>用戶和職能 —>沙盒測試技術(shù)員:添加沙盒測試賬號
2、我的App—>準(zhǔn)提交的項目—>功能—>App內(nèi)購買項目:添加內(nèi)購商品信息
【注】蘋果內(nèi)購時,需要先退出手機(jī)的AppStore登錄賬號
【注】提交審核前需要上傳購買界面截圖,供蘋果審核
四、代碼實現(xiàn)
//
// AppleIAPService.h
// OneMate
//
// Created by ChenWenHan on 2017/8/30.
// Copyright ? 2017年 OneMate1314. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "PayProtocol.h"
typedef NS_ENUM(NSInteger, IAPPurchaseStatus)
{
IAPPurchaseFailed, // Indicates that the purchase was unsuccessful
IAPPurchaseSucceeded, // Indicates that the purchase was successful
IAPRestoredFailed, // Indicates that restoring products was unsuccessful
IAPRestoredSucceeded, // Indicates that restoring products was successful
};
@interface AppleIAPService : NSObject <PayProtocol>
+ (AppleIAPService *)sharedInstance;
@end
//
// AppleIAPService.m
// OneMate
//
// Created by ChenWenHan on 2017/8/30.
// Copyright ? 2017年 OneMate1314. All rights reserved.
//
#import "AppleIAPService.h"
#import <StoreKit/StoreKit.h>
#import "SkyApiManager.h"
@interface AppleIAPService()<SKProductsRequestDelegate, SKPaymentTransactionObserver>
@property (nonatomic, copy) MsgBlock resultBlock;
@end
@implementation AppleIAPService
//類裝載的時候系統(tǒng)調(diào)用該方法
+ (void)load
{
[[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidFinishLaunchingNotification object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
[AppleIAPService sharedInstance];
}];
}
+ (AppleIAPService *)sharedInstance
{
static dispatch_once_t onceToken;
static AppleIAPService * sharedInstance;
dispatch_once(&onceToken, ^{
sharedInstance = [[AppleIAPService alloc] init];
});
return sharedInstance;
}
- (instancetype)init
{
self = [super init];
if (self) {
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
}
return self;
}
-(void)purchase:(NSString *)product_id resultBlock:(MsgBlock)resultBlock{
self.resultBlock = resultBlock;
if ([SKPaymentQueue canMakePayments]) {//用戶允許支付
NSSet *set = [NSSet setWithObjects:product_id, nil];
SKProductsRequest *request = [[SKProductsRequest alloc] initWithProductIdentifiers:set];
request.delegate = self;
[request start];
} else {
NSError *error = [NSError errorWithDomain:@"IAP"
code:-1
userInfo:@{ NSLocalizedDescriptionKey : @"檢查是否允許支付功能或者該設(shè)備是否支持支付." }];
if(self.resultBlock) self.resultBlock(nil,error);
}
}
#pragma mark - SKProductsRequestDelegate
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response {
NSArray *products = response.products;
if (products.count == 0) {
NSError *error = [NSError errorWithDomain:@"IAP"
code:-2
userInfo:@{ NSLocalizedDescriptionKey : @"商品信息無效,請聯(lián)系客服。" }];
if(self.resultBlock) self.resultBlock(nil,error);
return;
}
SKProduct *product = products.firstObject;
NSLog(@"請求購買:%@",product.productIdentifier);
SKPayment * payment = [SKPayment paymentWithProduct:product];
[[SKPaymentQueue defaultQueue] addPayment:payment];
}
- (void)requestDidFinish:(SKRequest *)request{
NSLog(@"%s",__FUNCTION__);
}
- (void)request:(SKRequest *)request didFailWithError:(NSError *)error{
NSLog(@"%s:%@",__FUNCTION__,error.localizedDescription);
if(self.resultBlock) self.resultBlock(nil,error);
}
#pragma mark - SKPaymentTransactionObserver
//監(jiān)聽購買結(jié)果
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{
for (SKPaymentTransaction *transaction in transactions)
{
//NSLog(@"transaction_id:%@",transaction.transactionIdentifier);
switch (transaction.transactionState)
{
case SKPaymentTransactionStatePurchasing: // 0 購買事務(wù)進(jìn)行中
break;
case SKPaymentTransactionStatePurchased: // 1 交易完成
[self completeTransaction:transaction forStatus:IAPPurchaseSucceeded];
break;
case SKPaymentTransactionStateFailed: // 2 交易失敗
[self completeTransaction:transaction forStatus:IAPPurchaseFailed];
break;
case SKPaymentTransactionStateRestored: // 3 恢復(fù)交易成功:從用戶的購買歷史中恢復(fù)了交易
[self completeTransaction:transaction forStatus:IAPRestoredSucceeded];
break;
default:
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];//結(jié)束支付事務(wù)
break;
}
}
}
// Sent when transactions are removed from the queue (via finishTransaction:).
- (void)paymentQueue:(SKPaymentQueue *)queue removedTransactions:(NSArray<SKPaymentTransaction *> *)transactions
{
for(SKPaymentTransaction *transaction in transactions){
NSLog(@"%@ was removed from the payment queue.", transaction.payment.productIdentifier);
}
}
// Sent when an error is encountered while adding transactions from the user's purchase history back to the queue.
- (void)paymentQueue:(SKPaymentQueue *)queue restoreCompletedTransactionsFailedWithError:(NSError *)error
{
NSLog(@"%s error:%@",__FUNCTION__,error.localizedDescription);
}
// Sent when all transactions from the user's purchase history have successfully been added back to the queue.
- (void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue
{
NSLog(@"%s",__FUNCTION__);
}
#pragma mark Complete transaction
-(void)completeTransaction:(SKPaymentTransaction *)transaction forStatus:(IAPPurchaseStatus)status
{
//Do not send any notifications when the user cancels the purchase
if (transaction.error.code != SKErrorPaymentCancelled){
// Notify the user
switch (status) {
case IAPPurchaseSucceeded:
case IAPRestoredSucceeded:
{
[self uploadReceipt:transaction];
}
break;
case IAPPurchaseFailed:
case IAPRestoredFailed:
default:
{
if(self.resultBlock) self.resultBlock(nil,transaction.error);
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];//結(jié)束支付事務(wù)
}
break;
}
}else{
NSError *error = [NSError errorWithDomain:@"IAP"
code:-3
userInfo:@{ NSLocalizedDescriptionKey : @"已取消支付。" }];
if(self.resultBlock) self.resultBlock(nil,error);
// Remove the transaction from the queue for purchased and restored statuses
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];//結(jié)束支付事務(wù)
}
}
/**
上傳支付憑證到后臺
@param transaction 支付事務(wù)
*/
//附加:官方文檔:向蘋果校驗支付結(jié)果https://developer.apple.com/library/content/releasenotes/General/ValidateAppStoreReceipt/Chapters/ValidateRemotely.html
-(void)uploadReceipt:(SKPaymentTransaction *)transaction
{
NSURL *receiptURL = [[NSBundle mainBundle] appStoreReceiptURL];
NSData *receipt = [NSData dataWithContentsOfURL:receiptURL];
if (!receipt) { /* No local receipt -- handle the error. */
NSLog(@"receipt 本地數(shù)據(jù)不存在");
return;
}
NSString *base64_receipt = [receipt base64EncodedStringWithOptions:0];
NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
[params setObject:base64_receipt forKey:@"receipt"];
[params setObject:transaction.transactionIdentifier forKey:@"transaction_id"];
wSelf(self);
[SkyApiManager fetch:params url:kUrlIAPSuccessNofity requester:nil block:^(id result, NSError *error) {
if (!error) {
NSInteger code = [[result objectForKey:kResponseCode] integerValue];
NSString *msg = [result objectForKey:kResponseMsg];
MessageObj *msgObj = [[MessageObj alloc] init];
msgObj.code = code;
msgObj.msg = msg;
if(wSelf.resultBlock) wSelf.resultBlock(msgObj,nil);
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
}else{
if(wSelf.resultBlock) wSelf.resultBlock(nil,error);
}
}];
}
- (void)dealloc
{
NSLog(@"%s銷毀",__FUNCTION__);
[[SKPaymentQueue defaultQueue] removeTransactionObserver:self];
}
@end
五、receipt-data 支付憑證校驗
https://developer.apple.com/library/content/releasenotes/General/ValidateAppStoreReceipt/Chapters/ValidateRemotely.html 官方文檔:向蘋果校驗支付憑證
服務(wù)器端開發(fā)處理流程
http://www.itdecent.cn/p/7e7c3a918946?utm_campaign=hugo&utm_medium=reader_share&utm_content=note&utm_source=qq
http://blog.csdn.net/teng_ontheway/article/details/47023119 漏單處理
蘋果反饋的狀態(tài)碼;
21000App Store無法讀取你提供的JSON數(shù)據(jù)
21002 收據(jù)數(shù)據(jù)不符合格式
21003 收據(jù)無法被驗證
21004 你提供的共享密鑰和賬戶的共享密鑰不一致
21005 收據(jù)服務(wù)器當(dāng)前不可用
21006 收據(jù)是有效的,但訂閱服務(wù)已經(jīng)過期。當(dāng)收到這個信息時,解碼后的收據(jù)信息也包含在返回內(nèi)容中
21007 收據(jù)信息是測試用(sandbox),但卻被發(fā)送到產(chǎn)品環(huán)境中驗證 【請求sandbox校驗支付憑證】
21008 收據(jù)信息是產(chǎn)品環(huán)境中使用,但卻被發(fā)送到測試環(huán)境中驗證
六、注意事項&附加資源
https://developer.apple.com/library/content/samplecode/sc1991/Introduction/Intro.html#//apple_ref/doc/uid/DTS40014726-Intro-DontLinkElementID_2 StoreKitSuite 官方Demo
個人碰到的坑:
類裝載的時候?qū)嵗痆AppleIAPService sharedInstance];重復(fù)注冊[[SKPaymentQueue defaultQueue] addTransactionObserver:self];導(dǎo)致重復(fù)接監(jiān)聽支付結(jié)果- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions;最終導(dǎo)致重復(fù)向后臺服務(wù)器上傳相同的支付憑證。??
被蘋果拒絕:內(nèi)購類型問題
消耗類型: 例如:金幣、道具等。
非續(xù)期訂閱: non-renewable subscription 例如:VIP
內(nèi)購信息信息被拒,下面是正常實例:
標(biāo)題:400金幣
描述:充值40元人民幣獲取400金幣。
附
鄧白氏申請流程
iOS開發(fā)內(nèi)購全套圖文教程
iOS應(yīng)用程序內(nèi)購/內(nèi)付費(fèi)
[iOS]應(yīng)用內(nèi)支付(內(nèi)購)的個人開發(fā)過程及坑!
蘋果IAP支付時序圖:

圖片來源:https://www.processon.com/view/link/598c062be4b02e9a26eed69a