Skip to content

Commit 48b3aa6

Browse files
committed
Added receipt validation check
1 parent f98aedf commit 48b3aa6

File tree

3 files changed

+42
-10
lines changed

3 files changed

+42
-10
lines changed

BranchSDK/BNCAppleReceipt.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ NS_ASSUME_NONNULL_BEGIN
2121
// this is only available on builds from Apple
2222
- (nullable NSString *)installReceipt;
2323
- (BOOL)isTestFlight;
24+
+ (BOOL)isReceiptValid;
2425

2526
@end
2627

BranchSDK/BNCAppleReceipt.m

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//
88

99
#import "BNCAppleReceipt.h"
10+
#import <CommonCrypto/CommonDigest.h>
1011

1112
@interface BNCAppleReceipt()
1213

@@ -63,4 +64,30 @@ - (BOOL)isTestFlight {
6364
return self.isSandboxReceipt;
6465
}
6566

67+
+ (BOOL)isReceiptValid {
68+
NSURL *receiptURL = [[NSBundle mainBundle] appStoreReceiptURL];
69+
NSData *receiptData = [NSData dataWithContentsOfURL:receiptURL];
70+
71+
if (!receiptData) {
72+
return NO;
73+
}
74+
75+
NSString *receiptHash = [self sha256HashForData:receiptData];
76+
if (receiptHash) {
77+
return YES;
78+
}
79+
80+
return NO;
81+
}
82+
83+
+ (NSString *)sha256HashForData:(NSData *)data {
84+
uint8_t digest[CC_SHA256_DIGEST_LENGTH];
85+
CC_SHA256(data.bytes, (CC_LONG)data.length, digest);
86+
NSMutableString *output = [NSMutableString stringWithCapacity:CC_SHA256_DIGEST_LENGTH * 2];
87+
for (int i = 0; i < CC_SHA256_DIGEST_LENGTH; i++) {
88+
[output appendFormat:@"%02x", digest[i]];
89+
}
90+
return output;
91+
}
92+
6693
@end

BranchSDK/BranchEvent.m

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#import "BNCPartnerParameters.h"
1717
#import "BNCPreferenceHelper.h"
1818
#import "BNCEventUtils.h"
19+
#import "BNCAppleReceipt.h"
1920

2021
#pragma mark BranchStandardEvents
2122

@@ -372,16 +373,19 @@ - (NSString*_Nonnull) description {
372373
#pragma mark - IAP Methods
373374

374375
- (void) logEventWithTransaction:(SKPaymentTransaction *)transaction {
375-
376-
self.transactionID = transaction.transactionIdentifier;
377-
[[BNCEventUtils shared] storeEvent:self];
378-
379-
NSString *productId = transaction.payment.productIdentifier;
380-
SKProductsRequest *productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObject:productId]];
381-
382-
_request = productsRequest;
383-
productsRequest.delegate = self;
384-
[productsRequest start];
376+
if ([BNCAppleReceipt isReceiptValid]) {
377+
self.transactionID = transaction.transactionIdentifier;
378+
[[BNCEventUtils shared] storeEvent:self];
379+
380+
NSString *productId = transaction.payment.productIdentifier;
381+
SKProductsRequest *productsRequest = [[SKProductsRequest alloc] initWithProductIdentifiers:[NSSet setWithObject:productId]];
382+
383+
_request = productsRequest;
384+
productsRequest.delegate = self;
385+
[productsRequest start];
386+
} else {
387+
BNCLogDebug(@"Invalid receipt. Branch Event was not logged.");
388+
}
385389
}
386390

387391
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response {

0 commit comments

Comments
 (0)