123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- //
- // 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"
- #import "NSBundle+KODLocalizable.h"
- #import "KODCommonConst.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) {
- //票据丢失
- NSString *str1 = [NSBundle KOD_localizedStringForKey:BillLose];
- self.callback(nil, [XSNetwork errorWithCode:-3 message:str1]);
- return;
- }
- if (![transaction.payment.applicationUsername isEqualToString:self.orderId]) {
- //订单未找到
- NSString *str1 = [NSBundle KOD_localizedStringForKey:OrderNoFind];
- self.callback(nil, [XSNetwork errorWithCode:-3 message:str1]);
- return;
- }
-
- self.callback([data base64EncodedStringWithOptions:0], nil);
- }
- - (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response {
-
- if ([response.invalidProductIdentifiers containsObject:self.productId]) {
- //产品暂时不可用,请检查苹果后台
- NSString *str1 = [NSBundle KOD_localizedStringForKey:ProductNoUseCheckAppleDevelop];
- self.callback(nil, [XSNetwork errorWithCode:-3 message:str1]);
- 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.
- //产品未找到, 请查检查后台
- NSString *str1 = [NSBundle KOD_localizedStringForKey:ProductNoFindCheckDevelop];
- self.callback(nil, [XSNetwork errorWithCode:-3 message:str1]);
- }
- - (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray<SKPaymentTransaction *> *)transactions {
- 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
|