不管有没有用 参考一下 总没错。。
推广购买-跳转:itms-services://?action=purchaseIntent&bundleId=boundleID&productIdentifier=内购ID
/********一下是iap内购*************/
-(void)buy:(NSString*)coin
{
//这个只是下面拼接的数据,可以自己给
NSString*strcoin = [coinsubstringFromIndex:1];
NSIntegerintcoin = [strcoinintValue];
NSIntegermoney = intcoin;
if(intcoin!=1)
{
intcoin *=10;
}
else
{
intcoin =20;
}
//1.判断当前设备是否允许IAP支付
if(![SKPaymentQueuecanMakePayments]) {
// MBProgressHUD 只是一个弹窗方法
[MBProgressHUD showError:@"用户已禁止用户取消了支付"];
return;
}
//2.请求商品信息
//这里采用内置产品类型,商品ID写死在客户端中。
//请求获取订单号
//这个是你们后台给的接口
NSString*oidurl = @“xxxxxxxx”
NSURL*ordurl = [NSURL URLWithString:oidurl];
NSURLRequest*oidRequest = [NSURLRequest requestWithURL:ordurl];
NSURLResponse*response =nil;
NSError*error =nil;
NSData*data = [NSURLConnection sendSynchronousRequest:oidRequest returningResponse:&response error:&error];
NSDictionary*ordRes = [dataJSONValue];
self.OrderNo= [[ordRes valueForKey:@"data"]valueForKey:@"info"];
//内购参数
NSString*setStr = [NSString stringWithFormat:@“xxxxxxxxxxx%ld",(long)intcoin];
NSSet*set = [[NSSet alloc]initWithObjects:setStr,nil];
self.request= [[SKProductsRequest alloc]initWithProductIdentifiers:set];
self.request.delegate=self;
[MBProgressHUD showMessage:@"请稍等"];
[self.request start];
}
- (void)productsRequest:(SKProductsRequest*)request didReceiveResponse:(SKProductsResponse*)response
{
// NSLog(@"Did received products results");
self.products= response.products;
self.request=nil;
for(SKProduct *product inresponse.products) {
NSLog(@"已获取到产品信息%@,%@,%@",product.localizedTitle,product.localizedDescription,product.price);
self.product= product;
}
if(!self.product) {
[MBProgressHUD hideHUD];
[selfshowAlertView:@"无法获取商品信息"];
return;
}
//3.获取到产品信息,加入支付队列
SKPayment*payment = [SKPaymentpaymentWithProduct:self.product];
[[SKPaymentQueuedefaultQueue]addTransactionObserver:self];
[[SKPaymentQueuedefaultQueue]addPayment:payment];
}
- (void)recordTransaction:(SKPaymentTransaction*)transaction {
// Optional: Record the transaction on the server side...
//记录当前购买成功的商品
// NSLog(@"recordTransaction");
}
- (void)provideContent:(NSString*)productIdentifier {
// NSLog(@"provideContent %@", productIdentifier);
//针对购买的商品,提供不同的服务。
[_purchasedProductsaddObject:productIdentifier];
}
- (void)paymentQueue:(SKPaymentQueue*)queue updatedTransactions:(NSArray*)transactions
{
for(SKPaymentTransaction*transactionintransactions)
{
switch(transaction.transactionState)
{
case SKPaymentTransactionStatePurchased:
[selfcompleteTransaction:transaction];
break;
case SKPaymentTransactionStateFailed:
[selffailedTransaction:transaction];
break;
case SKPaymentTransactionStateRestored:
[selfrestoreTransaction:transaction];
default:
break;
}
}
}
- (void)completeTransaction:(SKPaymentTransaction*)transaction {
//NSLog(@"completeTransaction...");
[selfrecordTransaction: transaction];
[selfprovideContent: transaction.payment.productIdentifier];
[selfverifyReceipt:transaction];
[[SKPaymentQueuedefaultQueue]finishTransaction: transaction];
}
#pragma mark服务器验证购买凭据
- (void) verifyReceipt:(SKPaymentTransaction*)transaction
{
NSMutableURLRequest*request = [NSMutableURLRequestrequestWithURL:[NSURLURLWithString:@“xxxxxxxx”]cachePolicy:NSURLRequestUseProtocolCachePolicytimeoutInterval:10.0f];
request.HTTPMethod=@"POST";
NSString*encodeStr = [transaction.transactionReceiptbase64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
intisSandBox =0;
#ifdef TEST_SANDBOX
isSandBox=1;
#endif
NSString*payload = [NSStringstringWithFormat:@"{\"receipt-data\" : \"%@\",\"sandbox\":%d,\"out_trade_no\" : \"%@\"}", encodeStr,isSandBox,self.OrderNo];
//把bodyString转换为NSData数据
NSData*bodyData = [payloaddataUsingEncoding:NSUTF8StringEncodingallowLossyConversion:YES];//把bodyString转换为NSData数据
[requestsetHTTPBody:bodyData];
//提交验证请求,并获得官方的验证JSON结果
NSData*result = [NSURLConnectionsendSynchronousRequest:requestreturningResponse:nilerror:nil];
NSString*debugaa = [[NSStringalloc]initWithData:resultencoding:NSUTF8StringEncoding];
//官方验证结果为空
[MBProgressHUDhideHUD];
if(result ==nil) {
[MBProgressHUDhideHUD];
[MBProgressHUDshowError:@"验证失败"];
}
else
{
NSDictionary*dict = [NSJSONSerializationJSONObjectWithData:resultoptions:NSJSONReadingAllowFragmentserror:nil];
if(dict==nil)
{
[MBProgressHUDshowError:@"请查看网站是否开启了调试模式"];
return;
}
if([[dictvalueForKey:@"status"]isEqualToString:@"success"]) {
//比对字典中以下信息基本上可以保证数据安全
[MBProgressHUDshowSuccess:@"充值成功"];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2*NSEC_PER_SEC)),dispatch_get_main_queue(), ^{
[selfdoReturn];
});
}
else
{
[MBProgressHUDshowError:[dictvalueForKey:@"info"] ];
}
}
}
- (void)restoreTransaction:(SKPaymentTransaction*)transaction {
//NSLog(@"restoreTransaction...");
[selfrecordTransaction: transaction];
[selfprovideContent: transaction.originalTransaction.payment.productIdentifier];
[[SKPaymentQueuedefaultQueue]finishTransaction: transaction];
[selfshowAlertView:@"用户已恢复购买"];
[MBProgressHUDhideHUD];
}
- (void)failedTransaction:(SKPaymentTransaction*)transaction {
[MBProgressHUDhideHUD];
if(transaction.error.code!=SKErrorPaymentCancelled)
{
[selfshowAlertView:transaction.error.localizedDescription];
NSLog(@"Transaction error: %@", transaction.error.localizedDescription);
}else{
[selfshowAlertView:@"用户取消了支付"];
}
[[SKPaymentQueuedefaultQueue]finishTransaction: transaction];
}
- (void)showAlertView:(NSString*)message
{
UIAlertView*alert = [[UIAlertViewalloc]initWithTitle:@"提示"message:messagedelegate:nilcancelButtonTitle:@"好的"otherButtonTitles:nil];
[alertshow];
}
/******************内购结束********************/