处理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

@@ -9,10 +9,17 @@
NS_ASSUME_NONNULL_BEGIN
FOUNDATION_EXPORT NSString * const KBIAPDidCompletePurchaseNotification;
FOUNDATION_EXPORT NSNotificationName const KBIAPDidCompletePurchaseNotification;
@interface IAPVerifyTransactionObj : NSObject <FGIAPVerifyTransaction>
/// 校验票据StoreKit 2 入口)
/// - Parameters:
/// - receipt: Base64 编码的票据
/// - completion: 回调success 表示验签成功statusCode 为后端状态码
- (void)verifyReceipt:(NSString *)receipt
completion:(void (^)(BOOL success, NSString * _Nullable message, NSInteger statusCode))completion;
@end
NS_ASSUME_NONNULL_END

View File

@@ -9,7 +9,7 @@
//#import "KBLoginSheetViewController.h"
#import "KBBizCode.h"
NSString * const KBIAPDidCompletePurchaseNotification = @"KBIAPDidCompletePurchaseNotification";
NSNotificationName const KBIAPDidCompletePurchaseNotification = @"KBIAPDidCompletePurchaseNotification";
@interface IAPVerifyTransactionObj ()
@property (nonatomic, strong) PayVM *payVM;
@end
@@ -23,6 +23,29 @@ NSString * const KBIAPDidCompletePurchaseNotification = @"KBIAPDidCompletePurcha
return self;
}
- (void)verifyReceipt:(NSString *)receipt
completion:(void (^)(BOOL success, NSString * _Nullable message, NSInteger statusCode))completion {
if (![receipt isKindOfClass:NSString.class] || receipt.length == 0) {
if (completion) {
completion(NO, KBLocalized(@"Receipt missing"), KBBizCodeReceiptError);
}
return;
}
NSDictionary *params = @{ @"receipt": receipt ?: @"" };
__weak typeof(self) weakSelf = self;
[self.payVM applePayReqWithParams:params needShow:NO completion:^(NSInteger sta, NSString * _Nullable msg) {
dispatch_async(dispatch_get_main_queue(), ^{
__strong typeof(weakSelf) self = weakSelf;
(void)self;
BOOL success = (sta == KBBizCodeSuccess);
NSString *tip = msg ?: (success ? KBLocalized(@"Success") : KBLocalized(@"Payment failed"));
if (completion) {
completion(success, tip, sta);
}
});
}];
}
#pragma mark - FGIAPVerifyTransaction
- (void)pushSuccessTradeReultToServer:(NSString *)receipt
@@ -40,34 +63,23 @@ NSString * const KBIAPDidCompletePurchaseNotification = @"KBIAPDidCompletePurcha
#endif
NSInteger type = 0;
#if DEBUG
type = 0;
#else
type = 1;
#endif
// , @"type": @(type)
NSDictionary *params = @{ @"receipt": receipt ?: @""};
__weak typeof(self) weakSelf = self;
[KBHUD showWithStatus:@"请稍等..."];
[self.payVM applePayReqWithParams:params needShow:NO completion:^(NSInteger sta, NSString * _Nullable msg) {
// [KBHUD dismiss];
// [KBHUD showInfo:(sta == !KBBizCodeSuccess ? KBLocalized(@"Payment failed") : KBLocalized(@"Payment successful"))];
if (sta == KBBizCodeSuccess) {
[self verifyReceipt:receipt completion:^(BOOL success, NSString * _Nullable message, NSInteger statusCode) {
__strong typeof(weakSelf) self = weakSelf;
(void)self;
if (success) {
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
[KBHUD showInfo:@"Success"];
[[NSNotificationCenter defaultCenter] postNotificationName:KBIAPDidCompletePurchaseNotification object:nil];
if (handler) handler(KBLocalized(@"Success"), nil);
}else if(sta == KBBizCodeReceiptError){
} else if (statusCode == KBBizCodeReceiptError) {
[KBHUD dismiss];
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
}else {
[KBHUD showError:@"Failed"];
if (handler) handler(KBLocalized(@"Failed"), nil);
} else {
[KBHUD showError:message ?: KBLocalized(@"Failed")];
if (handler) handler(message ?: KBLocalized(@"Failed"), nil);
}
(void)weakSelf; // keep self during block life if needed
}];
}

View File

@@ -2,3 +2,4 @@
// Use this file to import your target's public headers that you would like to expose to Swift.
//
#import "IAPVerifyTransactionObj.h"