This commit is contained in:
2025-12-16 14:14:49 +08:00
parent 1651258eec
commit f10ddd9a31
3 changed files with 43 additions and 42 deletions

View File

@@ -20,6 +20,13 @@ FOUNDATION_EXPORT NSNotificationName const KBIAPDidCompletePurchaseNotification;
- (void)verifyReceipt:(NSString *)receipt - (void)verifyReceipt:(NSString *)receipt
completion:(void (^)(BOOL success, NSString * _Nullable message, NSInteger statusCode))completion; completion:(void (^)(BOOL success, NSString * _Nullable message, NSInteger statusCode))completion;
/// 校验 StoreKit 2 交易的 JWS 签名串
/// - Parameters:
/// - payload: 交易 JWS 字符串
/// - completion: 回调
- (void)verifySignedPayload:(NSString *)payload
completion:(void (^)(BOOL success, NSString * _Nullable message, NSInteger statusCode))completion;
@end @end
NS_ASSUME_NONNULL_END NS_ASSUME_NONNULL_END

View File

@@ -46,6 +46,29 @@ NSNotificationName const KBIAPDidCompletePurchaseNotification = @"KBIAPDidComple
}]; }];
} }
- (void)verifySignedPayload:(NSString *)payload
completion:(void (^)(BOOL success, NSString * _Nullable message, NSInteger statusCode))completion {
if (![payload isKindOfClass:NSString.class] || payload.length == 0) {
if (completion) {
completion(NO, KBLocalized(@"Payload missing"), KBBizCodeReceiptError);
}
return;
}
NSDictionary *params = @{ @"signedPayload": payload ?: @"" };
__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 #pragma mark - FGIAPVerifyTransaction
- (void)pushSuccessTradeReultToServer:(NSString *)receipt - (void)pushSuccessTradeReultToServer:(NSString *)receipt

View File

@@ -51,30 +51,12 @@ final class KBStoreKitBridge: NSObject, StoreKitDelegate {
do { do {
try await self.manager.purchase(productId: productId) try await self.manager.purchase(productId: productId)
let state = self.manager.currentState
switch state { if let payload = await Self.latestJWSPayload(for: productId) {
case .purchaseSuccess(let id) where id == productId: self.verifySignedPayload(payload, completion: completion)
let receipt = try await Self.fetchReceiptData() } else {
self.verifyReceipt(receipt, completion: completion)
case .purchasePending(let id) where id == productId:
await MainActor.run { await MainActor.run {
completion(false, "Purchase pending approval.") completion(false, "Unable to obtain transaction payload.")
}
case .purchaseCancelled(let id) where id == productId:
await MainActor.run {
completion(false, "Purchase cancelled.")
}
case .purchaseFailed(let id, let error) where id == productId:
await MainActor.run {
completion(false, error.localizedDescription)
}
case .error(let error):
await MainActor.run {
completion(false, error.localizedDescription)
}
default:
await MainActor.run {
completion(false, "Purchase failed.")
} }
} }
} catch { } catch {
@@ -119,12 +101,12 @@ final class KBStoreKitBridge: NSObject, StoreKitDelegate {
return true return true
} }
private func verifyReceipt(_ receipt: String, completion: @escaping (Bool, String?) -> Void) { private func verifySignedPayload(_ payload: String, completion: @escaping (Bool, String?) -> Void) {
receiptVerifier.verifyReceipt(receipt) { success, message, _ in receiptVerifier.verifySignedPayload(payload) { success, message, _ in
DispatchQueue.main.async { DispatchQueue.main.async {
if success { if success {
NotificationCenter.default.post( NotificationCenter.default.post(
name: NSNotification.Name(rawValue: NSNotification.Name.KBIAPDidCompletePurchase.rawValue), name: NSNotification.Name.KBIAPDidCompletePurchase,
object: nil object: nil
) )
} }
@@ -133,22 +115,11 @@ final class KBStoreKitBridge: NSObject, StoreKitDelegate {
} }
} }
private static func fetchReceiptData() async throws -> String { private static func latestJWSPayload(for productId: String) async -> String? {
if let receipt = try loadReceiptFromBundle() { guard let result = await Transaction.latest(for: productId) else { return nil }
return receipt if case .verified = result {
return result.jwsRepresentation
} }
try await AppStore.sync() return nil
if let receipt = try loadReceiptFromBundle() {
return receipt
}
throw StoreKitError.verificationFailed
}
private static func loadReceiptFromBundle() throws -> String? {
guard let url = Bundle.main.appStoreReceiptURL else { return nil }
guard FileManager.default.fileExists(atPath: url.path) else { return nil }
let data = try Data(contentsOf: url, options: .alwaysMapped)
guard !data.isEmpty else { return nil }
return data.base64EncodedString()
} }
} }