#import "EInAppPurchasing.h"

#import

#if TARGET_IPHONE_SIMULATOR

// 开发时模拟器使用的验证服务器地址

#define ITMS_VERIFY_RECEIPT_URL    @""

#elif TARGET_OS_IPHONE

//真机验证的服务器地址

#define ITMS_VERIFY_RECEIPT_URL        @""

#endif

@interface EInAppPurchasing ()

@property (nonatomic, strong) SKProductsRequest *request;

@end

@implementationEInAppPurchasing

+ (instancetype)sharedInstance

{

staticEInAppPurchasing* instance =NULL;

staticdispatch_once_tonceToken;

dispatch_once(&onceToken, ^{

instance = [EInAppPurchasingnew];

});

returninstance;

}

- (instancetype)init

{

self= [superinit];

if(self) {

[[SKPaymentQueue defaultQueue] addTransactionObserver:self];

}

return self;

}

- (void)dealloc

{

[self releaseRequest];

[[SKPaymentQueue defaultQueue] removeTransactionObserver:self];

}

- (void)releaseRequest

{

if(_request) {

[_requestcancel];

_request.delegate=nil;

_request=nil;

}

}

- (void)identifyCanMakePayments:(NSArray*)requestArray

{

if(requestArray.count==0) {

if(self.delegate&& [self.delegaterespondsToSelector:@selector(updatedTransactions:)]) {

[self.delegate updatedTransactions:EPaymentTransactionStateAddPaymentFailed];

}

return;

}

if ([SKPaymentQueue canMakePayments]) {

[self releaseRequest];

self.request = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithArray:requestArray]];

_request.delegate=self;

[_requeststart];

}

else

{

if(self.delegate&& [self.delegaterespondsToSelector:@selector(updatedTransactions:)]) {

[self.delegate updatedTransactions:EPaymentTransactionStateNoPaymentPermission];

}

}

}

#pragma mark - SKProductsRequestDelegate

- (void)productsRequest:(SKProductsRequest*)request didReceiveResponse:(SKProductsResponse*)response

{

NSLog(@"-----------收到产品反馈信息-------------- 产品ID:%@ 产品数量:%ld",response.invalidProductIdentifiers,response.products.count);

NSArray * myProducts = response.products;

for(SKProduct*productinmyProducts){

NSLog(@"SKProduct 描述信息%@", [productdescription]);

NSLog(@"产品标题 %@", product.localizedTitle);

NSLog(@"产品描述信息: %@" , product.localizedDescription);

NSLog(@"价格: %@", product.price);

NSLog(@"Product id: %@" , product.productIdentifier);

}

if(myProducts && myProducts.count>0) {

SKProduct*product = [myProductsobjectAtIndex:0];

if(self.delegate&& [self.delegaterespondsToSelector:@selector(isProductIdentifierAvailable:)]) {

if ([self.delegate isProductIdentifierAvailable:product.productIdentifier]) {

SKPayment*payment = [SKPaymentpaymentWithProduct:product];

[[SKPaymentQueue defaultQueue] addPayment:payment];

return;

}

}

}

if(self.delegate&& [self.delegaterespondsToSelector:@selector(updatedTransactions:)]) {

[self.delegate updatedTransactions:EPaymentTransactionStateAddPaymentFailed];

}

}

/** 请求失败 */

- (void)request:(SKRequest*)request didFailWithError:(NSError*)error{

NSLog(@"---购买失败");

}

#pragma mark - SKPaymentTransactionObserver

/** 监听购买结果 */

- (void)paymentQueue:(SKPaymentQueue*)queue updatedTransactions:(NSArray *)transactions

{

for(SKPaymentTransaction*transactionintransactions)

{

EPaymentTransactionState state;

switch(transaction.transactionState){

case SKPaymentTransactionStatePurchasing:

{

// 连接appStore

state =EPaymentTransactionStatePurchasing;

}

break;

case SKPaymentTransactionStatePurchased:

{

state =EPaymentTransactionStatePurchased;

//交易完成

if(isServiceVerify) {

[selfcompleteTransaction:transaction];

}

else

{

//本地作校验

[selfverifyPurchase:transaction];

}

}

break;

case SKPaymentTransactionStateFailed:

{

//交易失败

if(transaction.error.code!=SKErrorPaymentCancelled)

{

state =EPaymentTransactionStateFailed;

}else

{

state =EPaymentTransactionStateCancel;

}

[selffinshTransaction:transaction];

}

break;

case SKPaymentTransactionStateRestored:

{

state =EPaymentTransactionStateRestored;

//已经购买过该商品

[selffinshTransaction:transaction];

}

break;

case SKPaymentTransactionStateDeferred:

{

state =EPaymentTransactionStateDeferred;

}

break;

default:

break;

}

if(self.delegate&& [self.delegaterespondsToSelector:@selector(updatedTransactions:)]) {

[self.delegate updatedTransactions:state];

}

}

}

// Sent when transactions are removed from the queue (via finishTransaction:).

- (void)paymentQueue:(SKPaymentQueue*)queue removedTransactions:(NSArray *)transactions

{

NSLog(@"---removedTransactions");

}

// 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(@"restoreCompletedTransactionsFailedWithError");

}

// Sent when all transactions from the user's purchase history have successfully been added back to the queue.

- (void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue*)queue

{

NSLog(@"paymentQueueRestoreCompletedTransactionsFinished");

}

// Sent when the download state has changed.

- (void)paymentQueue:(SKPaymentQueue*)queue updatedDownloads:(NSArray *)downloads

{

NSLog(@"updatedDownloads");

}

#pragma mark - Private

#pragma mark 验证购买

// 验证购买,在每一次购买完成之后,需要对购买的交易进行验证

// 所谓验证,是将交易的凭证进行"加密",POST请求传递给苹果的服务器,苹果服务器对"加密"数据进行验证之后,

// 会返回一个json数据,供开发者判断凭据是否有效

// 有些“内购助手”同样会拦截验证凭据,返回一个伪造的验证结果

// 所以在开发时,对凭据的检验要格外小心

- (void)verifyPurchase:(SKPaymentTransaction*)transaction

{

//ios7开始支持

NSURL *receiptURL = [[NSBundle mainBundle] appStoreReceiptURL];

NSData*receiptData = [NSDatadataWithContentsOfURL:receiptURL];

NSString *encodeStr = [receiptData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];

NSURL *url = [NSURL URLWithString:ITMS_VERIFY_RECEIPT_URL];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0f];

NSString *payload = [NSString stringWithFormat:@"{\"receipt-data\" : \"%@\"}", encodeStr];

NSData *payloadData = [payload dataUsingEncoding:NSUTF8StringEncoding];

[requestsetHTTPBody:payloadData];

[requestsetHTTPMethod:@"POST"];

NSURLResponse*response =nil;

// 此请求返回的是一个json结果  将数据反序列化为数据字典

NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];

if(data ==nil) {

return;

}

NSDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];

if(jsonResponse !=nil) {

if([[jsonResponseobjectForKey:@"status"]intValue] ==0)

{

//通常需要校验:bid,product_id,purchase_date,status

}

else

{

//验证失败,检查你的机器是否越狱

}

}

//结束交易

[selffinshTransaction:transaction];

}

- (void)completeTransaction:(SKPaymentTransaction*)transaction

{

//服务器校验

}

- (void)finshTransaction:(SKPaymentTransaction*)transaction

{

//结束交易

[[SKPaymentQueue defaultQueue] finishTransaction: transaction];

}

@end

苹果服务器维护时间表2019,ios 内购详解(2019)相关推荐

  1. iOS 内购详解-代码篇

    内购项目-代码篇 一.分步骤说明 1.获取商品列表 2.苹果服务器返回的可购买商品 3.下单购买商品 4.购买队列状态变化,判断购买状态是否成功 5.交易验证 6.拿到的收据信息是,此App所有购买的 ...

  2. 苹果服务器维护时间表2019,苹果更新电脑键盘服务维修计划机型 含2019最新款

    今天苹果发布了全新的2019款Macbook Pro 13英寸和15英寸版,为其配备了最新的第八代和第九代英特尔处理器.新的MacBook Pro机型还采用了一种修改后的键盘设计,同时升级触控条至13 ...

  3. 【iOS内购支付】Uniapp拉起苹果内购支付注意事项、实现步骤以及踩过的坑(手把手教程)

    前言 Hello!又是很长时间没有写博客了,因为最近又开始从事新项目,也是第一次接触关于uniapp开发原生IOS应用的项目,在这里做一些关于我在项目中使用苹果内购支付所实现的方式以及要注意的事项,希 ...

  4. ios内购二次验证安全性问题_苹果IOS内购二次验证返回state为21002的坑

    项目是三四年前的老项目,之前有IOS内购二次验证的接口,貌似很久都没用了,然而最近IOS的妹子说接口用不了,让我看看啥问题.接口流程时很简单的,就是前端IOS在购买成功之后,接收到receipt后进行 ...

  5. java集成ios内购\与ios退款通知处理

    使用ios内购,需在项目数据库建立虚拟币相关表(虚拟币余额表.充值面额表.充值订单表等)上代码 苹果IAP内购验证工具类 IosVerifyUtil import javax.net.ssl.*; i ...

  6. iOS 内购StoreKit 框架介绍

    StoreKit 框架介绍 一.StoreKit 能做什么? In-App Purchase 提供和促进内容和服务的应用内购买. Apple Music 检查用户的Apple Music功能并提供订阅 ...

  7. iOS内购-防越狱破解刷单

    ---------------------------2018.10.16更新--------------------------- 最近我们公司丢单率上涨,尤其是10月份比9月份来说丢单率翻了3倍, ...

  8. IOS内购验证 (Java版)

    此处给各位贴出apple官方文档 App 内购买项目配置流程 apple 收据文档 apple 收据responseBody字段释义 IOS内购逻辑图 IOS内购验证相关代码 package xxxx ...

  9. IOS内购经常遇到的一些问题,和一些容易混淆的点。

    Q1:内购和Apple Pay的区别? A1:内购是内购,Apple Pay是Apple Pay.我不知道有多少人第一次接触时,会把这俩概念混淆掉,这里你可以简单这么理解,虚拟的物品就是用内购,实际的 ...

最新文章

  1. 8088/8086的功能结构
  2. java序列化和反序列化工具_Java 序列化和反序列化工具类并解决StreamCorruptedException问题 | 学步园...
  3. Html,xhtml,xml的定义和区别
  4. IT PRO的职业生涯
  5. 四、物理优化(2)索引视图
  6. 测度论相关概念(吐)
  7. 网页设计布局选择:固定,流行和弹性布局 (2010-12-14 13:07:35)
  8. P4055-[JSOI2009]游戏【网络流,博弈】
  9. 类库,随机数,属性,值类型和引用类型
  10. comsol线圈不能加电流激励_comsol里面如何绘制三维的激励线圈?
  11. Spring Cloud Gateway (六) 自定义 Global Filter
  12. 微信小程序转发功能详解
  13. 怎样夸学计算机的人,学学古人是怎样夸人有才的
  14. matlab 菱形符号,matlab 图形符号
  15. 使用linux,导出mysql数据库信息,连接聚石塔
  16. php去除首尾符号,PHP如何去除头尾字符
  17. 服务器bios上传文件,巧改BIOS设置 让文件服务器性能提升10%
  18. MATLAB(九)数值微积分
  19. h5 实现一键复制到粘贴板 兼容iOS
  20. 【社媒营销】四大跨境营销渠道分析

热门文章

  1. 机器人旋转关节非线性摩擦辨识
  2. 安全检测80端口Web服务攻击痕迹(上)
  3. 师生使用计算机不得,师生文明上网行为规范
  4. 如何学习爬虫技术(问答版)
  5. 微型计算机原理与接口技术考试附录
  6. 【ora】+eclp+OpM错误提示、特殊处理
  7. 网站QQ客服链接代码
  8. FFmpeg提取视频(mp4)中的音频(m4a)
  9. Cortex-M3处理器
  10. swoole 捕捉php错误,常见错误 - swoole - yoyo