123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365 |
- #import "KDIAPManager.h"
- #import <StoreKit/StoreKit.h>
- #import "XSNetwork.h"
- @interface KDIAPManager() <SKPaymentTransactionObserver,SKProductsRequestDelegate> {
- IAPCompletionHandle _handle;
- }
- @property (nonatomic, strong) NSString* productId;
- @property (nonatomic, strong) NSString* orderId;
- @end
- @implementation KDIAPManager
- + (instancetype)shareIAPManager {
- static KDIAPManager *IAPManager = nil;
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^{
- IAPManager = [[KDIAPManager alloc] init];
- });
- return IAPManager;
- }
- - (instancetype)init {
- if (self = [super init]) {
- [[SKPaymentQueue defaultQueue] addTransactionObserver:self];
- }
- return self;
- }
- - (void)dealloc {
- [[SKPaymentQueue defaultQueue] removeTransactionObserver:self];
- }
- #pragma mark -- Method
- + (void)checkOrderStatus {
- NSDictionary *orderInfo = [KDIAPManager getReceiptData];
-
- if (orderInfo != nil) {
- NSString *orderId = orderInfo[@"orderId"];
- id receipt = orderInfo[@"receipt"];
- [[KDIAPManager shareIAPManager] verifyPurchaseForServiceWithOrderId:orderId receipt:receipt];
- }
- }
- #pragma mark -- 结束上次未完成的交易
- - (void)removeAllUncompleteTransactionsBeforeNewPurchase {
- NSArray* transactions = [SKPaymentQueue defaultQueue].transactions;
- if (transactions.count >= 1) {
- for (SKPaymentTransaction* transaction in transactions) {
- if (transaction.transactionState == SKPaymentTransactionStatePurchased ||
- transaction.transactionState == SKPaymentTransactionStateRestored) {
- [[SKPaymentQueue defaultQueue]finishTransaction:transaction];
- }
- }
- } else {
- NSLog(@"没有历史未消耗订单");
- }
- }
- - (void)startIAPWithOrderId:(NSString *)orderId productID:(NSString *)productID completeHandle: (IAPCompletionHandle)handle {
- _handle = handle;
- if(productID && productID.length > 0) {
- if ([SKPaymentQueue canMakePayments]) {
- [self removeAllUncompleteTransactionsBeforeNewPurchase];
- self.orderId = orderId;
-
- self.productId = productID;
- NSSet *set = [NSSet setWithObjects:productID, nil];
- SKProductsRequest *request = [[SKProductsRequest alloc] initWithProductIdentifiers:set];
- request.delegate = self;
-
- [request start];
- } else {
-
- [self handleActionWithType:IAPResultNotArrow data:nil];
- }
- } else {
- NSLog(@"内购项目ID错误");
- [XSNetwork errorWithCode:-3 message:@"产品暂时不可用,请检查苹果后台"];
- [self handleActionWithType:IAPResultIDError data:nil];
- }
-
- }
- - (void)handleActionWithType:(IAPResultType)type data:(NSData *)data{
- switch (type) {
- case IAPResultSuccess:
- NSLog(@"购买成功");
- break;
- case IAPResultFailed:
- NSLog(@"购买失败");
- break;
- case IAPResultCancle:
- NSLog(@"用户取消购买");
- break;
- case IAPResultVerFailed:
- NSLog(@"订单校验失败");
- break;
- case IAPResultVerSuccess:
- NSLog(@"订单校验成功");
- break;
- case IAPResultNotArrow:
- NSLog(@"不允许程序内付费");
- break;
- default:
- break;
- }
- if(_handle){
- _handle(type, data);
- }
- }
- #pragma mark -- SKProductsRequestDelegate
- - (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response {
-
-
- NSArray *productArr = response.products;
- if (productArr.count > 0) {
- for (SKProduct *product in productArr) {
- if ([product.productIdentifier isEqualToString:self.productId]) {
- SKMutablePayment *payment = [SKMutablePayment paymentWithProduct:product];
- payment.quantity = 1;
-
-
- [[SKPaymentQueue defaultQueue] addPayment:payment];
- break;
- }
- }
- } else {
- [self handleActionWithType:IAPResultIDError data:nil];
- }
-
-
-
- }
- #pragma mark -- SKPaymentTransactionObserver
- - (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray<SKPaymentTransaction *> *)transactions {
-
-
- for (SKPaymentTransaction *trans in transactions) {
- switch (trans.transactionState) {
- case SKPaymentTransactionStatePurchasing:
- NSLog(@"商品添加进列表");
- break;
- case SKPaymentTransactionStatePurchased:
- {
- NSLog(@"交易完成");
- [self completeTransaction:trans];
- [[SKPaymentQueue defaultQueue] finishTransaction:trans];
-
-
- }
- break;
- case SKPaymentTransactionStateFailed:
- NSLog(@"交易失败");
- [self failedTransaction:trans];
- [[SKPaymentQueue defaultQueue] finishTransaction:trans];
- break;
- case SKPaymentTransactionStateRestored:
- NSLog(@"已经购买过商品");
- [[SKPaymentQueue defaultQueue] finishTransaction:trans];
- break;
- case SKPaymentTransactionStateDeferred:
-
- break;
- default:
- break;
- }
- }
- }
- - (void)completeTransaction:(SKPaymentTransaction *)transaction {
- NSString *productIdentifier = transaction.payment.productIdentifier;
- NSURL *recepitURL = [[NSBundle mainBundle] appStoreReceiptURL];
- NSData *data = [NSData dataWithContentsOfURL:recepitURL];
- id receiptString = [data base64EncodedStringWithOptions:0];
-
- if ([productIdentifier length] > 0 && data) {
-
- if (receiptString && self.orderId.length > 0) {
-
- [self saveReceiptData:@{@"receipt":receiptString,
- @"orderId":self.orderId ?:@""}];
-
-
- [self verifyPurchaseForServiceWithOrderId:self.orderId
- receipt:receiptString];
- }
-
- } else {
- [self handleActionWithType:IAPResultVerFailed data:nil];
- }
-
-
- }
- - (void)verifyPurchaseForServiceWithOrderId:(NSString *)orderId
- receipt:(id)receiptString {
- if (orderId == nil && !receiptString) {
- [self handleActionWithType:IAPResultIDError data:nil];
- return;
- }
-
-
- [XSNetwork payWithReceipt:receiptString orderId:orderId complete:^(id object, NSError *error) {
-
-
-
- if (error != nil) {
-
- [self handleActionWithType:IAPResultVerFailed data:nil];
- }else{
-
-
- [self handleActionWithType:IAPResultSuccess data:nil];
- [self removeLocReceiptData];
- }
- }];
- }
- - (void)failedTransaction:(SKPaymentTransaction *)transaction {
- if (transaction.error.code != SKErrorPaymentCancelled) {
- [self handleActionWithType:IAPResultFailed data:nil];
- } else {
- [self handleActionWithType:IAPResultCancle data:nil];
- }
- }
- #pragma mark -- 本地保存一次支付凭证
- static NSString *const kSaveReceiptData = @"kSaveReceiptData";
- - (void)saveReceiptData:(NSDictionary *)receiptData {
- [[NSUserDefaults standardUserDefaults] setValue:receiptData forKey:kSaveReceiptData];
- [[NSUserDefaults standardUserDefaults]synchronize];
- }
- + (NSDictionary *)getReceiptData {
- return [[NSUserDefaults standardUserDefaults] valueForKey:kSaveReceiptData];
- }
- - (void)removeLocReceiptData {
- [[NSUserDefaults standardUserDefaults] removeObjectForKey:kSaveReceiptData];
- [[NSUserDefaults standardUserDefaults] synchronize];
- }
- @end
|