//需要遵守的代理
<SKStoreProductViewControllerDelegate,UIGestureRecognizerDelegate,SKPaymentTransactionObserver,SKProductsRequestDelegate>
#import <StoreKit/StoreKit.h>
//沙盒测试环境验证
#define SANDBOX @"https://sandbox.itunes.apple.com/verifyReceipt"
//正式环境验证
#define AppStore @"https://buy.itunes.apple.com/verifyReceipt"
- (void)buyBtnAction {
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
NSArray* transactions = [SKPaymentQueue defaultQueue].transactions;
for (SKPaymentTransaction *transaction in transactions) {
NSLog(@"transactionState:%ld", transaction.transactionState);
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
}
if([SKPaymentQueue canMakePayments]){
NSLog(@"-------------请求对应的产品信息----------------");
NSArray *product = [[NSArray alloc] initWithObjects:self.product_id,nil];
NSSet *nsset = [NSSet setWithArray:product];
SKProductsRequest *request = [[SKProductsRequest alloc] initWithProductIdentifiers:nsset];
request.delegate = self;
[request start];
}else{
NSLog(@"不允许程序内付费");
}
}
//收到产品返回信息
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response{
NSLog(@"--------------收到产品反馈消息---------------------");
NSArray *product = response.products;
if([product count] == 0){
NSLog(@"--------------没有商品------------------");
return;
}
NSLog(@"productID:%@", response.invalidProductIdentifiers);
NSLog(@"产品付费数量:%lu",(unsigned long)[product count]);
SKProduct *p = nil;
for (SKProduct *pro in product) {
NSLog(@"%@", [pro description]);
NSLog(@"%@", [pro localizedTitle]);
NSLog(@"%@", [pro localizedDescription]);
NSLog(@"%@", [pro price]);
NSLog(@"%@", [pro productIdentifier]);
if([pro.productIdentifier isEqualToString:self.product_id]){
p = pro;
}
}
SKPayment *payment = [SKPayment paymentWithProduct:p];
NSLog(@"发送购买请求");
[[SKPaymentQueue defaultQueue] addPayment:payment];
}
//请求失败
- (void)request:(SKRequest *)request didFailWithError:(NSError *)error{
NSLog(@"------------------错误-----------------:%@", error);
}
- (void)requestDidFinish:(SKRequest *)request{
NSLog(@"------------反馈信息结束-----------------");
}
/**
* 验证购买,避免越狱软件模拟苹果请求达到非法购买问题
*
*/
-(void)verifyPurchaseWithPaymentTransaction{
//从沙盒中获取交易凭证并且拼接成请求体数据
NSURL *receiptUrl=[[NSBundle mainBundle] appStoreReceiptURL];
NSData *receiptData=[NSData dataWithContentsOfURL:receiptUrl];
NSString *receiptString=[receiptData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];//转化为base64字符串
NSString *bodyString = [NSString stringWithFormat:@"{\"receipt-data\" : \"%@\"}", receiptString];//拼接请求数据
NSData *bodyData = [bodyString dataUsingEncoding:NSUTF8StringEncoding];
//创建请求到苹果官方进行购买验证
NSURL *url = [NSURL URLWithString:AppStore];
NSMutableURLRequest *requestM = [NSMutableURLRequest requestWithURL:url];
requestM.HTTPBody = bodyData;
requestM.HTTPMethod = @"POST";
//创建连接并发送同步请求
NSError *error = nil;
NSData *responseData = [NSURLConnection sendSynchronousRequest:requestM returningResponse:nil error:&error];
if (error) {
NSLog(@"Appstore验证购买过程中发生错误,错误信息:%@",error.localizedDescription);
return;
}
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingAllowFragments error:nil];
NSLog(@"%@",dic);
if([dic[@"status"] intValue] == 0){
NSLog(@"Appstore下购买成功!");
[self rechargeSuccess];
NSArray *items = @[MMItemMake(@"确定", MMItemTypeHighlight, nil)];
[[[MMAlertView alloc] initWithTitle:@"充值成功!" detail:nil items:items] showWithBlock:nil];
}else{
NSLog(@"Appstore下购买失败,未通过验证!");
//创建请求到苹果官方进行购买验证
NSURL *url2 = [NSURL URLWithString:SANDBOX];
NSMutableURLRequest *requestM2 = [NSMutableURLRequest requestWithURL:url2];
requestM2.HTTPBody = bodyData;
requestM2.HTTPMethod = @"POST";
//创建连接并发送同步请求
NSError *error2 = nil;
NSData *responseData2 = [NSURLConnection sendSynchronousRequest:requestM2 returningResponse:nil error:&error2];
if (error2) {
NSLog(@"SandBox验证购买过程中发生错误,错误信息:%@",error2.localizedDescription);
return;
}
NSDictionary *dic2 = [NSJSONSerialization JSONObjectWithData:responseData2 options:NSJSONReadingAllowFragments error:nil];
NSLog(@"%@",dic2);
if([dic2[@"status"] intValue] == 0){
NSLog(@"SandBox下购买成功!");
[self rechargeSuccess];
NSArray *items = @[MMItemMake(@"确定", MMItemTypeHighlight, nil)];
[[[MMAlertView alloc] initWithTitle:@"充值成功!" detail:nil items:items] showWithBlock:nil];
}else{
NSLog(@"SandBox下购买失败,未通过验证!");
}
}
}
//监听购买结果
- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transaction{
for(SKPaymentTransaction *tran in transaction){
switch (tran.transactionState) {
case SKPaymentTransactionStatePurchased:{
NSLog(@"交易完成");
[[SKPaymentQueue defaultQueue] finishTransaction:tran];
[self verifyPurchaseWithPaymentTransaction];
}
break;
case SKPaymentTransactionStatePurchasing:
NSLog(@"商品添加进列表");
break;
case SKPaymentTransactionStateRestored:{
NSLog(@"已经购买过商品");
[[SKPaymentQueue defaultQueue] finishTransaction:tran];
}
break;
case SKPaymentTransactionStateFailed:{
NSLog(@"交易失败");
[[SKPaymentQueue defaultQueue] finishTransaction:tran];
}
break;
default:
break;
}
}
}
//交易结束
- (void)paymentQueue:(SKPaymentQueue *)queue removedTransactions:(NSArray<SKPaymentTransaction *> *)transactions {
[[SKPaymentQueue defaultQueue] removeTransactionObserver:self];
NSLog(@"交易结束");
}
- (void)dealloc {
[[SKPaymentQueue defaultQueue] removeTransactionObserver:self];
}
iOS 内购充值代码
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...