//
//  LTStoreHelper.h
//  LTUtility
//
//  Created by 孙泉 on 2017/9/5.
//  Copyright © 2017年 longtugame. All rights reserved.
//

#import <Foundation/Foundation.h>

#import <StoreKit/StoreKit.h>

typedef void(^LTSKProductBlock)(SKProduct *product);

typedef void(^LTStoreErrorBlock)(NSError *error);

typedef void(^LTSKTransactionBlock)(SKPaymentTransaction *transaction);

typedef void(^LTStorePromotBlock)(SKProduct *product, SKPayment *payment);

typedef void(^LTStoreVerifyBlock)(NSDictionary *response);

/**
 IAP充值环境

 - LTStoreTypeSandbox: 沙盒
 - LTStoreTypeProduct: 生产
 */
typedef NS_ENUM(NSUInteger, LTStoreType)
{
    LTStoreTypeSandbox,
    LTStoreTypeProduct,
};

@interface LTStoreHelper : NSObject

/**
 初始化单例对象
 
 @return 单例对象
 */
+ (instancetype)sharedInstance;

+ (void)startObserveTransaction;

/**
 设备能否进行支付
 
 @return 能否进行支付
 */
+ (BOOL)canStore;

/**
 验证商品ID的合法性
 
 @param productID 商品ID
 @param success 验证成功回调SKProduct对象
 @param failure 验证失败回调失败信息
 */
- (void)isProductValid:(NSString *)productID
               success:(LTSKProductBlock)success
               failure:(LTStoreErrorBlock)failure;

/**
 进行IAP支付
 
 @param product 商品
 @param applicationUsername 应用用户名
 @param process 支付进行中回调
 @param success 支付成功回调
 @param failure 支付失败回调
 */
- (void)makeItemProduct:(SKProduct *)product
        applicationUsername:(NSString *)applicationUsername
                    process:(LTSKTransactionBlock)process
                    success:(LTSKTransactionBlock)success
                    failure:(LTSKTransactionBlock)failure;

/**
 获取从AppStore中进行推广内购回调
 
 @param deferred 是否推迟购买执行
 @param completion 推广内购点击回调
 */
- (void)fetchAppStorePromot:(BOOL)deferred
                 completion:(LTStorePromotBlock)completion;

/**
 恢复推广内购
 
 @param payment 保存的中断推广内购
 @param process 支付进行中回调
 @param success 支付成功回调
 @param failure 支付失败回调
 */
- (void)restorePromot:(SKPayment *)storement
              process:(LTSKTransactionBlock)process
              success:(LTSKTransactionBlock)success
              failure:(LTSKTransactionBlock)failure;

/**
 验证票据号
 
 @param type 订单类型(沙盒, 正式)
 @param receipt base64编码后票据号
 @param password 自动续订共享秘钥
 @param success 成功回调
 @param failure 失败回调
 */
+ (void)verifyItemReceipt:(LTStoreType)type
                  receipt:(NSString *)receipt
                 password:(NSString *)password
                  success:(LTStoreVerifyBlock)success
                  failure:(LTStoreErrorBlock)failure;

@end
