处理storkit2

This commit is contained in:
2025-12-16 13:49:08 +08:00
parent fd0ddfd45a
commit 1651258eec
9 changed files with 245 additions and 75 deletions

View File

@@ -4,13 +4,12 @@
#import "KBJfPay.h"
#import "KBJfPayCell.h"
#import "FGIAPProductsFilter.h"
#import "FGIAPManager.h"
#import "PayVM.h"
#import "KBPayProductModel.h"
#import "KBBizCode.h"
#import "KBShopVM.h"
#import "IAPVerifyTransactionObj.h"
#import "keyBoard-Swift.h"
static NSString * const kKBJfPayCellId = @"kKBJfPayCellId";
@interface KBJfPay () <UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@@ -40,7 +39,6 @@ static NSString * const kKBJfPayCellId = @"kKBJfPayCellId";
@property (nonatomic, strong) NSArray<KBPayProductModel *> *data; // In-App
@property (nonatomic, assign) NSInteger selectedIndex; //
@property (nonatomic, strong) FGIAPProductsFilter *filter;
@property (nonatomic, strong) PayVM *payVM;
@property (nonatomic, strong) KBShopVM *shopVM;
@@ -50,7 +48,6 @@ static NSString * const kKBJfPayCellId = @"kKBJfPayCellId";
- (void)viewDidLoad {
[super viewDidLoad];
self.filter = [[FGIAPProductsFilter alloc] init];
self.payVM = [PayVM new];
self.shopVM = [KBShopVM new];
[[NSNotificationCenter defaultCenter] addObserver:self
@@ -272,11 +269,25 @@ static NSString * const kKBJfPayCellId = @"kKBJfPayCellId";
self.data = products ?: @[];
self.selectedIndex = self.data.count > 0 ? 0 : NSNotFound;
[self.collectionView reloadData];
[self prepareStoreKitWithProducts:self.data];
[self selectItemAtCurrentIndexAnimated:NO];
});
}];
}
- (void)prepareStoreKitWithProducts:(NSArray<KBPayProductModel *> *)products {
if (![products isKindOfClass:NSArray.class] || products.count == 0) { return; }
NSMutableArray<NSString *> *ids = [NSMutableArray array];
for (KBPayProductModel *item in products) {
if (![item isKindOfClass:KBPayProductModel.class]) { continue; }
if (item.productId.length) {
[ids addObject:item.productId];
}
}
if (ids.count == 0) { return; }
[[KBStoreKitBridge shared] prepareWithProductIds:ids completion:nil];
}
- (KBPayProductModel *)currentSelectedProductItem {
if (self.selectedIndex == NSNotFound) { return nil; }
if (self.selectedIndex < 0 || self.selectedIndex >= self.data.count) { return nil; }
@@ -315,28 +326,12 @@ static NSString * const kKBJfPayCellId = @"kKBJfPayCellId";
}
[KBHUD show];
__weak typeof(self) weakSelf = self;
[self.filter requestProductsWith:[NSSet setWithObject:productId] completion:^(NSArray<SKProduct *> * _Nonnull products) {
dispatch_async(dispatch_get_main_queue(), ^{
__strong typeof(weakSelf) self = weakSelf;
if (!self) { return; }
SKProduct *match = nil;
for (SKProduct *product in products) {
if ([product.productIdentifier isEqualToString:productId]) {
match = product;
break;
}
}
if (!match) {
[KBHUD dismiss];
[KBHUD showInfo:KBLocalized(@"Unable to load product information")];
return;
}
[[FGIAPManager shared].iap buyProduct:match onCompletion:^(NSString * _Nonnull message, FGIAPManagerPurchaseRusult result) {
dispatch_async(dispatch_get_main_queue(), ^{
// [KBHUD dismiss];
});
}];
});
[[KBStoreKitBridge shared] purchaseWithProductId:productId completion:^(BOOL success, NSString * _Nullable message) {
__strong typeof(weakSelf) self = weakSelf;
NSString *tip = message.length ? message : (success ? KBLocalized(@"Payment successful") : KBLocalized(@"Payment failed"));
[KBHUD dismiss];
[KBHUD showInfo:tip];
(void)self;
}];
}

View File

@@ -11,9 +11,8 @@
#import "KBVipReviewListCell.h"
#import "PayVM.h"
#import "KBPayProductModel.h"
#import "FGIAPProductsFilter.h"
#import "FGIAPManager.h"
#import "KBBizCode.h"
#import "keyBoard-Swift.h"
static NSString * const kKBVipHeaderId = @"kKBVipHeaderId";
static NSString * const kKBVipSubscribeCellId = @"kKBVipSubscribeCellId";
@@ -33,7 +32,6 @@ static NSString * const kKBVipReviewListCellId = @"kKBVipReviewListCellId";
@property (nonatomic, strong) UILabel *agreementLabel; //
@property (nonatomic, strong) UIButton *agreementButton; // Embership Agreement
@property (nonatomic, strong) PayVM *payVM;
@property (nonatomic, strong) FGIAPProductsFilter *filter;
@end
@@ -54,7 +52,6 @@ static NSString * const kKBVipReviewListCellId = @"kKBVipReviewListCellId";
make.height.mas_equalTo(224);
}];
self.payVM = [PayVM new];
self.filter = [[FGIAPProductsFilter alloc] init];
self.plans = @[];
self.selectedIndex = NSNotFound;
@@ -120,11 +117,25 @@ static NSString * const kKBVipReviewListCellId = @"kKBVipReviewListCellId";
self.plans = products ?: @[];
self.selectedIndex = self.plans.count > 0 ? 0 : NSNotFound;
[self.collectionView reloadData];
[self prepareStoreKitWithPlans:self.plans];
[self selectCurrentPlanAnimated:NO];
});
}];
}
- (void)prepareStoreKitWithPlans:(NSArray<KBPayProductModel *> *)plans {
if (![plans isKindOfClass:NSArray.class] || plans.count == 0) { return; }
NSMutableArray<NSString *> *ids = [NSMutableArray array];
for (KBPayProductModel *plan in plans) {
if (![plan isKindOfClass:KBPayProductModel.class]) { continue; }
if (plan.productId.length) {
[ids addObject:plan.productId];
}
}
if (ids.count == 0) { return; }
[[KBStoreKitBridge shared] prepareWithProductIds:ids completion:nil];
}
- (void)selectCurrentPlanAnimated:(BOOL)animated {
if (self.selectedIndex == NSNotFound) { return; }
if (self.selectedIndex < 0 || self.selectedIndex >= self.plans.count) { return; }
@@ -191,28 +202,14 @@ static NSString * const kKBVipReviewListCellId = @"kKBVipReviewListCellId";
}
[KBHUD show];
__weak typeof(self) weakSelf = self;
[self.filter requestProductsWith:[NSSet setWithObject:productId] completion:^(NSArray<SKProduct *> * _Nonnull products) {
dispatch_async(dispatch_get_main_queue(), ^{
__strong typeof(weakSelf) self = weakSelf;
if (!self) { return; }
SKProduct *match = nil;
for (SKProduct *product in products) {
if ([product.productIdentifier isEqualToString:productId]) {
match = product;
break;
}
}
if (!match) {
[KBHUD dismiss];
[KBHUD showInfo:KBLocalized(@"Unable to load product information")];
return;
}
[[FGIAPManager shared].iap buyProduct:match onCompletion:^(NSString * _Nonnull message, FGIAPManagerPurchaseRusult result) {
dispatch_async(dispatch_get_main_queue(), ^{
// [KBHUD dismiss];
});
}];
});
[[KBStoreKitBridge shared] purchaseWithProductId:productId completion:^(BOOL success, NSString * _Nullable message) {
__strong typeof(weakSelf) self = weakSelf;
NSString *tip = message.length ? message : (success ? KBLocalized(@"Payment successful") : KBLocalized(@"Payment failed"));
[KBHUD dismiss];
[KBHUD showInfo:tip];
if (!self || !success) { return; }
// UI
[self selectCurrentPlanAnimated:NO];
}];
}
- (void)agreementButtonAction{