在iOS開發(fā)中你是否遇到過購買虛擬物品的而無法使用第三方支付的問題,讓不熟悉Apple內(nèi)購的你不知所措,廢話不多說,直接搞起。
第一部分:協(xié)議
CNAPS CODE 查詢地址
https://e.czbank.com/CORPORBANK/query_unionBank_index.jsp
協(xié)議Done,我們現(xiàn)在已經(jīng)和Apple簽訂了協(xié)議,接下來該去上架商品了
第二部分:創(chuàng)建內(nèi)購項(xiàng)目
Apple內(nèi)購的價(jià)格是等級制的,無法自己隨意定價(jià),而且每比訂單成交都要向蘋果繳納百分之30的抽成,坑爹吧!!
第三部分:App代碼集成
介紹一下APP內(nèi)購的步驟:
一般的內(nèi)購分為兩種,一種是我們app有自己的服務(wù)器,一種是本地的,像我們玩的闖關(guān)游戲需要購買關(guān)卡一般都是本地的,像那種聯(lián)網(wǎng)手游,會員VIP的基本都是服務(wù)器的。
服務(wù)器模式:
1.調(diào)用服務(wù)器接口創(chuàng)建一個(gè)商品的訂單
2.請求Apple的商品列表
3.選取商品調(diào)用蘋果支付
4.支付成功(會返回憑證)
5.把支付成功的返回憑證上傳到APP服務(wù)器(帶上訂單的ID,有利于后臺判斷是哪個(gè)訂單支付成功)
6.APP服務(wù)器保存該憑證等數(shù)據(jù)并像蘋果服務(wù)器發(fā)起憑證驗(yàn)證,驗(yàn)證成功則發(fā)送商品
本地模式:
1.請求Apple的商品列表
2.選取商品調(diào)用蘋果支付
3.支付成功(會返回憑證)
4.把憑證與商品發(fā)送狀態(tài)保存到一個(gè)本地的數(shù)據(jù)庫
5.app調(diào)用apple服務(wù)器的驗(yàn)證API
6.驗(yàn)證成功發(fā)送商品并改變數(shù)據(jù)庫的物品發(fā)送狀態(tài)
最后一步了,是不是有些欣喜,最后在代碼中實(shí)現(xiàn)
首先導(dǎo)入StoreKit.framework庫
#import "ApplePayVC.h"
#import <StoreKit/StoreKit.h>
//在內(nèi)購項(xiàng)目中創(chuàng)的商品單號,從itunesConnect里可以看到
#define ProductID_1 @"product1"
#define ProductID_2 @"product2"
#define ProductID_3 @"product3"
#define ProductID_4 @"product4"
#define ProductID_5 @"product5"
@interface ApplePayVC ()
{
NSString *buyProductId;
}
@end
@implementation ApplePayVC
- (void)viewDidLoad {
[super viewDidLoad];
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
[self buyProduct:ProductID_1];
}
-(void)buyProduct:(NSString *)productId
{
buyProductId = productId;
if ([SKPaymentQueue canMakePayments]) {
[self RequestProductData];
NSLog(@"允許程序內(nèi)付費(fèi)購買");
}
else
{
NSLog(@"不允許程序內(nèi)付費(fèi)購買");
UIAlertView *alerView = [[UIAlertView alloc] initWithTitle:@"提示"
message:@"您的手機(jī)沒有打開程序內(nèi)付費(fèi)購買"
delegate:nil cancelButtonTitle:NSLocalizedString(@"關(guān)閉",nil) otherButtonTitles:nil];
[alerView show];
}
}
-(void)RequestProductData
{
NSLog(@"---------請求對應(yīng)的產(chǎn)品信息------------");
NSArray *product = [[NSArray alloc] initWithObjects:buyProductId,nil];
NSSet *nsset = [NSSet setWithArray:product];
SKProductsRequest *request=[[SKProductsRequest alloc] initWithProductIdentifiers: nsset];
request.delegate=self;
[request start];
}
//<SKProductsRequestDelegate> 請求協(xié)議
//收到的產(chǎn)品信息
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response{
NSLog(@"-----------收到產(chǎn)品反饋信息--------------");
NSArray *myProduct = response.products;
NSLog(@"產(chǎn)品Product ID:%@",response.invalidProductIdentifiers);
NSLog(@"產(chǎn)品付費(fèi)數(shù)量: %d", (int)[myProduct count]);
// populate UI
for(SKProduct *product in myProduct){
NSLog(@"product info");
NSLog(@"SKProduct 描述信息%@", [product description]);
NSLog(@"產(chǎn)品標(biāo)題 %@" , product.localizedTitle);
NSLog(@"產(chǎn)品描述信息: %@" , product.localizedDescription);
NSLog(@"價(jià)格: %@" , product.price);
NSLog(@"Product id: %@" , product.productIdentifier);
}
SKPayment *payment = [SKPayment paymentWithProductIdentifier:buyProductId];
NSLog(@"---------發(fā)送購買請求------------");
[[SKPaymentQueue defaultQueue] addPayment:payment];
}
- (void)requestProUpgradeProductData
{
NSLog(@"------請求升級數(shù)據(jù)---------");
NSSet *productIdentifiers = [NSSet setWithObject:@"com.productid"];
SKProductsRequest* productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:productIdentifiers];
productsRequest.delegate = self;
[productsRequest start];
}
//彈出錯(cuò)誤信息
- (void)request:(SKRequest *)request didFailWithError:(NSError *)error{
NSLog(@"-------彈出錯(cuò)誤信息----------");
UIAlertView *alerView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Alert",NULL) message:[error localizedDescription]
delegate:nil cancelButtonTitle:NSLocalizedString(@"Close",nil) otherButtonTitles:nil];
[alerView show];
}
-(void) requestDidFinish:(SKRequest *)request
{
NSLog(@"----------反饋信息結(jié)束--------------");
}
-(void) PurchasedTransaction: (SKPaymentTransaction *)transaction{
NSLog(@"-----PurchasedTransaction----");
NSArray *transactions =[[NSArray alloc] initWithObjects:transaction, nil];
[self paymentQueue:[SKPaymentQueue defaultQueue] updatedTransactions:transactions];
}
//<SKPaymentTransactionObserver> 千萬不要忘記綁定,代碼如下:
//----監(jiān)聽購買結(jié)果
//[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions//交易結(jié)果
{
NSLog(@"-----paymentQueue--------");
for (SKPaymentTransaction *transaction in transactions)
{
switch (transaction.transactionState)
{
case SKPaymentTransactionStatePurchased:{//交易完成
[self completeTransaction:transaction];
NSLog(@"-----交易完成 --------");
UIAlertView *alerView = [[UIAlertView alloc] initWithTitle:@""
message:@"購買成功"
delegate:nil cancelButtonTitle:NSLocalizedString(@"關(guān)閉",nil) otherButtonTitles:nil];
[alerView show];
} break;
case SKPaymentTransactionStateFailed://交易失敗
{ [self failedTransaction:transaction];
NSLog(@"-----交易失敗 --------");
UIAlertView *alerView2 = [[UIAlertView alloc] initWithTitle:@"提示"
message:@"購買失敗,請重新嘗試購買"
delegate:nil cancelButtonTitle:NSLocalizedString(@"關(guān)閉",nil) otherButtonTitles:nil];
[alerView2 show];
}break;
case SKPaymentTransactionStateRestored://已經(jīng)購買過該商品
[self restoreTransaction:transaction];
NSLog(@"-----已經(jīng)購買過該商品 --------");
case SKPaymentTransactionStatePurchasing: //商品添加進(jìn)列表
NSLog(@"-----商品添加進(jìn)列表 --------");
break;
default:
break;
}
}
}
- (void) completeTransaction: (SKPaymentTransaction *)transaction
{
NSLog(@"-----completeTransaction--------");
// Your application should implement these two methods.
NSString *product = transaction.payment.productIdentifier;
if ([product length] > 0) {
NSArray *tt = [product componentsSeparatedByString:@"."];
NSString *bookid = [tt lastObject];
if ([bookid length] > 0) {
[self recordTransaction:bookid];
[self provideContent:bookid];
}
}
// Remove the transaction from the payment queue.
[[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}
//記錄交易
-(void)recordTransaction:(NSString *)product{
NSLog(@"-----記錄交易--------");
}
//處理下載內(nèi)容
-(void)provideContent:(NSString *)product{
NSLog(@"-----下載--------");
}
- (void) failedTransaction: (SKPaymentTransaction *)transaction{
NSLog(@"失敗");
if (transaction.error.code != SKErrorPaymentCancelled)
{
}
[[SKPaymentQueue defaultQueue] finishTransaction: transaction];
}
-(void) paymentQueueRestoreCompletedTransactionsFinished: (SKPaymentTransaction *)transaction{
}
- (void) restoreTransaction: (SKPaymentTransaction *)transaction
{
NSLog(@" 交易恢復(fù)處理");
}
-(void) paymentQueue:(SKPaymentQueue *) paymentQueue restoreCompletedTransactionsFailedWithError:(NSError *)error{
NSLog(@"-------paymentQueue----");
}
#pragma mark connection delegate
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSLog(@"%@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
switch([(NSHTTPURLResponse *)response statusCode]) {
case 200:
case 206:
break;
case 304:
break;
case 400:
break;
case 404:
break;
case 416:
break;
case 403:
break;
case 401:
case 500:
break;
default:
break;
}
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"test");
}
-(void)dealloc
{
[[SKPaymentQueue defaultQueue] removeTransactionObserver:self];//解除監(jiān)聽
}
@end
我們已經(jīng)完成了內(nèi)購的付款操作了,至于如何給到用戶商品就在
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
這個(gè)代理方法里面就行操作
你以為這樣就完成了么?那你就慘了,APP上線以后你就會發(fā)現(xiàn)各種掉單問題,那時(shí)你心中肯定有10000只草泥馬在奔騰,接下來我們來看看如何避免掉單。
附:解決掉單篇
我們先來看看有哪些請況會發(fā)生掉單:
①. 在ApplePay付款成功后由于網(wǎng)絡(luò)或各種原因沒有返回Transaction(SKPaymentTransaction),從而不能得到憑證去Apple服務(wù)器驗(yàn)證訂單的正確性。
②.蘋果服務(wù)器成功返回了Transaction,但是在APP在上傳憑證給服務(wù)器時(shí)發(fā)生了網(wǎng)絡(luò)或各種原因,造成了憑證的丟失,產(chǎn)生了掉單(用戶付了款卻沒有得到相應(yīng)的商品)
[SKPaymentQueue defaultQueue]這個(gè)隊(duì)列里面存著所有的已支付,未支付的訂單,而且需要手動移除,而APP每次啟動的時(shí)候都會去判斷這個(gè)隊(duì)列里面是否為空,如果不為空的話會調(diào)用<SKPaymentTransactionObserver>代理的
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions//交易結(jié)果
所以我們可以把AppDelegate設(shè)置成這個(gè)協(xié)議的代理并實(shí)現(xiàn)這個(gè)方法,當(dāng)然我一般是會寫一個(gè)遵循<SKPaymentTransactionObserver>的工具類單例,畢竟協(xié)議是一對一的,不管是哪里的支付回調(diào),都只走這個(gè)類,統(tǒng)一處理。
上面我們說到每次APP啟動時(shí)都會判斷訂單隊(duì)列是否為空,而且隊(duì)列需要手動移除,所以我們可以在確保商品已經(jīng)成功發(fā)放到用戶手中再做移除操作,這樣就完美了。
移除代碼如下:
[[SKPaymentQueue defaultQueue] finishTransaction: transaction];
iOS 7.0后 我們是用[[NSBundle mainBundle] appStoreReceiptURL]來獲取憑證。
注:蘋果官方內(nèi)購驗(yàn)證文檔
https://developer.apple.com/library/ios/releasenotes/General/ValidateAppStoreReceipt/Chapters/ValidateRemotely.html#//apple_ref/doc/uid/TP40010573-CH104-SW1
到這里可能有些童鞋會懵逼了,他會說我付款后怎么和訂單關(guān)聯(lián)上啊,完全沒有區(qū)分的地方啊!
解決辦法如下:當(dāng)我們創(chuàng)建蘋果訂單初始化SKPayment時(shí)我們應(yīng)該使用SKMutablePayment,這個(gè)類里面有一個(gè)參數(shù)叫applicationUsername的成員變量,我們可以把后臺服務(wù)器的訂單號寫到這里,在付款成功后返回的SKPaymentTransaction里面能拿到這個(gè)參數(shù),然后就帶著它去請求本地服務(wù)器.
我們把內(nèi)購搭建好直接進(jìn)行測試,會提示你購買失敗對吧?內(nèi)購測試我們要到iTunes connection 里去添加沙盒測試員
然后我們測試的時(shí)候換上這個(gè)appleId就能進(jìn)行測試了
perfect!!!!!!
哈哈,第一次寫技術(shù)博客可能寫得不好,猿媛們哪里不明白可以在下面提問!!!