什么是內購?
只要在iPhone App上購買的不是實物產品(也就是虛擬產品如qq幣.虎牙幣.電子書......) 都需要走內購流程,蘋果這里面抽走三成。
使用內購需要走的流程。
1,填寫協議,稅務和銀行業務;
2,填寫用戶和職能; 創建內購的項目
3,寫代碼,和后台配合
4,添加項目內購測試賬號;檢驗成果
具體步驟:
接下來的步驟特別繁瑣,請做好准備。
一.填寫協議,稅務和銀行業務配置 https://itunesconnect.apple.com
1.請求合同
填寫地址信息
閱讀同意協議
2.填寫聯系方式
添加新的聯系人
選中聯系人
3.填寫銀行信息
添加銀行卡
選擇銀行卡所在國家
填寫銀行CNAPS Code
貨幣類型 CN銀行賬號如果是對公的賬號,需要填寫公司的英文名稱,如果沒有的話拼音
4.填寫稅務信息
U.S Tax Forms: 美國稅務
Australia Tax Forms:澳大利亞稅務
Canada Tax Forms: 加拿大稅務
第一個問題如下:詢問你是否是美國居民,有沒有美國伙伴關系或者美國公司,如果沒有直接選擇No。
第二個問題如下:詢問你有沒有在美國的商業性活動,沒有也直接選No
然后填寫你的稅務信息,包括以下幾點:
Individual or Organization Name:個人或者組織名稱
Country of incorporation: 所在國家
Type of Beneficial Owner:受益方式,獨立開發者選個人
Permanent Residence:居住地址
Mailing address:郵寄地址
Name of Person Making this Declaration:聲明人
Title:頭銜
填寫完這些信息后就可以提交了
5.等待審核,時間24小時左右。
二.創建內購項目
選中你要添加內購項目的APP然后點擊功能,點擊+創建內購項目。
根據自己APP的需求選擇類型
填寫項目名稱id (ps:應用在首次添加內購買項目時需要與新的應用版本一起提交審核。)
添加沙盒技術測試員
用戶職能>沙箱技術測試員
注意事項:
郵箱隨便寫但是格式要正確(test@163.com),但是不能用已經是AppleID的郵箱
App Store 地區不要亂選。雖然隨便哪個地區都可以用來測試(還沒上線之前app並沒有地區之分),但是在沙盒測試的時候,彈出的購買提示框會根據當前AppleID(沙盒賬號)的地區顯示語言的。
使用沙盒測試一定要用真機 (越獄的不行)
沙盒賬號不能直接登錄AppleStore登錄時會提示"不允許創建iTunes賬戶”
進行沙盒測試時需要退出AppleStore的賬號
三.代碼
#import <Foundation/Foundation.h>
#import "WXApi.h"
typedef NS_ENUM(NSInteger , Payment) {
IAP6 = 6,
IAP25 = 25,
IAP60 = 60
};
@interface PayCenter : NSObject
singleton_interface(PayCenter);
- (void)startInit;
- (void)payWithMoney:(NSInteger)money andType:(PayType)type dataDic:(NSDictionary *)dataDic;
@property (nonatomic, strong) MBProgressHUD * hud;
@property (nonatomic, copy) NSString * rechargeNumber;
@end
#define kProductID_IAP6 @"org.qqhl.nursning.3175"//6
#import "PayCenter.h"
#import "PayHttpRequest.h"
//#import "Order.h"
//#import "DataSigner.h"
//#import <AlipaySDK/AlipaySDK.h>
#import <StoreKit/StoreKit.h>
@interface PayCenter()<SKPaymentTransactionObserver,SKProductsRequestDelegate>
{
int buyType;
NSInteger _money;
}
@property (nonatomic, strong) NSMutableDictionary * dataDic;
@end
@implementation PayCenter
singleton_implementation(PayCenter);
- (void)startInit
{
[WXApi registerApp:kWeChatShareAppKey withDescription:appDefaultTilte];
}
- (void)payWithMoney:(NSInteger)money andType:(PayType)type dataDic:(NSDictionary *)dataDic
{
_money = money;
self.dataDic = [NSMutableDictionary dictionaryWithDictionary:dataDic];
switch (type) {
case PayTypeWX:
[self payTypeWXPay];
break;
case ApplePay:
// [self payTypeAliPay];
[[SKPaymentQueue defaultQueue]addTransactionObserver:self];
if ([SKPaymentQueue canMakePayments]) {
HULog(@"允許程序內付費購買");
[self payTypeApplePay];
}else {
HULog(@"不允許程序內付費購買");
UIAlertView *alerView = [[UIAlertView alloc] initWithTitle:@"提示"
message:@"您的手機沒有打開程序內付費購買"
delegate:nil cancelButtonTitle:NSLocalizedString(@"關閉",nil) otherButtonTitles:nil];
[alerView show];
}
break;
default:
break;
}
}
- (NSString *)payTypeWXPay
{
__block NSString * str = @"";
[PayHttpRequest payRequestWithType:PayTypeWX dataDic:self.dataDic success:^(BOOL success, NSDictionary *dic) {
if (success) {
if (dic != nil) {
NSDictionary * orderDic = [dic objectForKey:@"orderInfo"];
NSMutableString *stamp = [orderDic objectForKey:@"timestamp"];
//調起微信支付
PayReq* req = [[PayReq alloc] init];
req.partnerId = [orderDic objectForKey:@"partnerid"];
req.prepayId = [orderDic objectForKey:@"prepayid"];
req.nonceStr = [orderDic objectForKey:@"noncestr"];
req.timeStamp = stamp.intValue;
req.package = [orderDic objectForKey:@"package"];
req.sign = [orderDic objectForKey:@"sign"];
[WXApi sendReq:req];
}else{
[[PayCenter sharedPayCenter].hud hide:YES];
str = @"服務器返回錯誤,未獲取到json對象";
}
}
}];
return str;
}
- (void)payTypeApplePay{
NSLog(@"---------請求對應的產品信息------------");
NSArray *product = nil;
switch (_money) {
case IAP6:
{
product=[[NSArray alloc] initWithObjects:@"org.qqhl.nursning.6",nil];
NSLog(@"---------IAP6------------");
}
break;
case IAP25:
{
NSLog(@"---------IAP25------------");
}
break;
case IAP60:
{
NSLog(@"---------IAP60------------");
}
break;
default:
break;
}
NSSet *nsSet = [NSSet setWithArray:product];
SKProductsRequest *request = [[SKProductsRequest alloc]initWithProductIdentifiers:nsSet];
request.delegate = self;
[request start];
}
#pragma mark - SKProductsRequestDelegate代理
//返回的在蘋果服務器請求的產品信息
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response{
HULog(@"收到產品信息");
NSArray *myProduct = response.products;
HULog(@"產品id %@",response.invalidProductIdentifiers);
NSLog(@"產品付費數量%d",(int)[myProduct count]);
//populate UI
for (SKProduct *product in myProduct) {
NSLog(@"product info");
NSLog(@"SKProduct 描述信息%@", [product description]);
NSLog(@"產品標題 %@" , product.localizedTitle);
NSLog(@"產品描述信息: %@" , product.localizedDescription);
NSLog(@"價格: %@" , product.price);
NSLog(@"Product id: %@" , product.productIdentifier);
}
SKPayment *payment = nil;
switch (_money) {
case IAP6:
{
payment = [SKPayment paymentWithProductIdentifier:kProductID_IAP6];
}
break;
case IAP25:{
}
default:
break;
}
NSLog(@"發起購買請求");
[[SKPaymentQueue defaultQueue]addPayment:payment];
}
//實現監聽方法
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray<SKPaymentTransaction *> *)transactions{
NSLog(@"調用了幾次這個方法?");
SKPaymentTransaction *transaction = transactions.lastObject;
switch (transaction.transactionState) {
case SKPaymentTransactionStatePurchased: {
NSLog(@"購買完成,向自己的服務器驗證 ---- %@", transaction.payment.applicationUsername);
NSData *data = [NSData dataWithContentsOfFile:[[[NSBundle mainBundle] appStoreReceiptURL] path]];
NSString *receipt = [data base64EncodedStringWithOptions:0];
// [self buySuccessWithReceipt:receipt transaction:transaction];
}
break;
case SKPaymentTransactionStateFailed: {
NSLog(@"交易失敗");
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
}
break;
case SKPaymentTransactionStateRestored: {
NSLog(@"已經購買過該商品");
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
}
break;
case SKPaymentTransactionStatePurchasing: {
NSLog(@"商品添加進列表");
}
break;
default: {
NSLog(@"這是什么情況啊?");
}
break;
}
}
@end