// // IAPAgnet.m // XenonSDK // // Created by SAGESSE on 2019/5/31. // Copyright © 2019 SAGESSE. All rights reserved. // #if SDK_HAS_IAP_PAYMENT #import "IAPAgnet.h" #import "XSNetwork.h" @interface IAPAgnet () @end @implementation IAPAgnet - (void)prepare { [SKPaymentQueue.defaultQueue addTransactionObserver: self]; } - (void)clean { [SKPaymentQueue.defaultQueue removeTransactionObserver: self]; } - (void)finishTransaction:(SKPaymentTransaction*)transaction { [SKPaymentQueue.defaultQueue finishTransaction:transaction]; } - (void)failure:(SKPaymentTransaction*)transaction { //... [self finishTransaction:transaction]; if (self.callback == nil) { return; } if (transaction.error != nil) { if (transaction.error.code == SKErrorPaymentCancelled) { self.callback(nil, [XSNetwork errorWithCode:-3 message:@"取消购买"]); } else { self.callback(nil, transaction.error); } } } - (void)success:(SKPaymentTransaction*)transaction { [self finishTransaction:transaction]; if (self.callback == nil) { return; } NSURL* url = NSBundle.mainBundle.appStoreReceiptURL; NSData* data = [NSData dataWithContentsOfURL:url]; if (data == nil) { self.callback(nil, [XSNetwork errorWithCode:-3 message:@"票据丢失"]); return; } if (![transaction.payment.applicationUsername isEqualToString:self.orderId]) { self.callback(nil, [XSNetwork errorWithCode:-3 message:@"订单未找到"]); return; } self.callback([data base64EncodedStringWithOptions:0], nil); } //查询成功后的回调 - (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response { if ([response.invalidProductIdentifiers containsObject:self.productId]) { self.callback(nil, [XSNetwork errorWithCode:-3 message:@"产品暂时不可用,请检查苹果后台"]); return; } for (SKProduct* product in response.products) { SKMutablePayment* payment = [SKMutablePayment paymentWithProduct:product];; payment.applicationUsername = self.orderId; [SKPaymentQueue.defaultQueue addPayment:payment]; // Only one is allowed. return; } // No in-app purchase found, purchase failed. self.callback(nil, [XSNetwork errorWithCode:-3 message:@"产品未找到, 请查检查后台"]); } //回调 - (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions { //NSLog(@"调了几次这个方法~~"); for (SKPaymentTransaction* transaction in transactions) { switch (transaction.transactionState) { case SKPaymentTransactionStatePurchased: [self success:transaction]; break; case SKPaymentTransactionStateFailed: [self failure:transaction]; break; case SKPaymentTransactionStateRestored: [self success:transaction]; break; default: break; } } } @end #endif