-
Notifications
You must be signed in to change notification settings - Fork 184
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Listen for purchase events + optional promise support + other updates #128
base: master
Are you sure you want to change the base?
Changes from 14 commits
d2bb4ed
bac9483
8b93ee6
19765eb
97fef8b
dad0ce4
ffa363f
6a0c3bd
a4879fb
08a7bd4
b131fa9
895456d
01ea503
2edc791
138c183
bc48565
78ea07a
b15792e
5ff369b
cc34229
b506624
138c6d3
eac175b
80fe9ef
4960c56
c8cb54c
1eea062
252464b
9f2f0af
a171c6f
04b1523
2e08e5a
77afb0f
8679bae
65d2b69
a13ff6e
794d32f
8d33665
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ @implementation InAppUtils | |
{ | ||
NSArray *products; | ||
NSMutableDictionary *_callbacks; | ||
bool hasPurchaseCompletedListeners; | ||
} | ||
|
||
- (instancetype)init | ||
|
@@ -19,13 +20,33 @@ - (instancetype)init | |
return self; | ||
} | ||
|
||
-(void)startObserving { | ||
hasPurchaseCompletedListeners = YES; | ||
} | ||
|
||
-(void)stopObserving { | ||
hasPurchaseCompletedListeners = NO; | ||
} | ||
|
||
- (dispatch_queue_t)methodQueue | ||
{ | ||
return dispatch_get_main_queue(); | ||
} | ||
|
||
RCT_EXPORT_MODULE() | ||
|
||
- (NSArray<NSString *> *)supportedEvents | ||
{ | ||
return @[@"PurchaseCompleted"]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done! |
||
} | ||
|
||
// Transactions initiated from App Store | ||
- (BOOL)paymentQueue:(SKPaymentQueue *)queue | ||
shouldAddStorePayment:(SKPayment *)payment | ||
forProduct:(SKProduct *)product { | ||
return true; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done! |
||
} | ||
|
||
- (void)paymentQueue:(SKPaymentQueue *)queue | ||
updatedTransactions:(NSArray *)transactions | ||
{ | ||
|
@@ -46,10 +67,12 @@ - (void)paymentQueue:(SKPaymentQueue *)queue | |
case SKPaymentTransactionStatePurchased: { | ||
NSString *key = RCTKeyForInstance(transaction.payment.productIdentifier); | ||
RCTResponseSenderBlock callback = _callbacks[key]; | ||
NSDictionary *purchase = [self getPurchaseData:transaction]; | ||
if (callback) { | ||
NSDictionary *purchase = [self getPurchaseData:transaction]; | ||
callback(@[[NSNull null], purchase]); | ||
[_callbacks removeObjectForKey:key]; | ||
} else if (hasPurchaseCompletedListeners) { | ||
[self sendEventWithName:@"PurchaseCompleted" body:purchase]; | ||
} else { | ||
RCTLogWarn(@"No callback registered for transaction with state purchased."); | ||
} | ||
|
@@ -105,7 +128,7 @@ - (void) doPurchaseProduct:(NSString *)productIdentifier | |
[[SKPaymentQueue defaultQueue] addPayment:payment]; | ||
_callbacks[RCTKeyForInstance(payment.productIdentifier)] = callback; | ||
} else { | ||
callback(@[@"invalid_product"]); | ||
callback(@[RCTMakeError(@"invalid_product", nil, nil)]); | ||
} | ||
} | ||
|
||
|
@@ -118,13 +141,13 @@ - (void)paymentQueue:(SKPaymentQueue *)queue | |
switch (error.code) | ||
{ | ||
case SKErrorPaymentCancelled: | ||
callback(@[@"user_cancelled"]); | ||
callback(@[RCTMakeError(@"user_cancelled", nil, nil)]); | ||
break; | ||
default: | ||
callback(@[@"restore_failed"]); | ||
callback(@[RCTJSErrorFromNSError(error)]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is going to be a breaking change. I think we can avoid making a breaking change with this PR. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm still leaving this for now in case we do make other breaking changes, then we might as well add this too. |
||
break; | ||
} | ||
|
||
[_callbacks removeObjectForKey:key]; | ||
} else { | ||
RCTLogWarn(@"No callback registered for restore product request."); | ||
|
@@ -166,7 +189,7 @@ - (void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue | |
NSString *restoreRequest = @"restoreRequest"; | ||
_callbacks[RCTKeyForInstance(restoreRequest)] = callback; | ||
if(!username) { | ||
callback(@[@"username_required"]); | ||
callback(@[RCTMakeError(@"username_required", nil, nil)]); | ||
return; | ||
} | ||
[[SKPaymentQueue defaultQueue] restoreCompletedTransactionsWithApplicationUsername:username]; | ||
|
@@ -185,15 +208,15 @@ - (void)paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue | |
RCT_EXPORT_METHOD(canMakePayments: (RCTResponseSenderBlock)callback) | ||
{ | ||
BOOL canMakePayments = [SKPaymentQueue canMakePayments]; | ||
callback(@[@(canMakePayments)]); | ||
callback(@[[NSNull null], @(canMakePayments)]); | ||
} | ||
|
||
RCT_EXPORT_METHOD(receiptData:(RCTResponseSenderBlock)callback) | ||
{ | ||
NSURL *receiptUrl = [[NSBundle mainBundle] appStoreReceiptURL]; | ||
NSData *receiptData = [NSData dataWithContentsOfURL:receiptUrl]; | ||
if (!receiptData) { | ||
callback(@[@"not_available"]); | ||
callback(@[RCTMakeError(@"receipt_not_available", nil, nil)]); | ||
} else { | ||
callback(@[[NSNull null], [receiptData base64EncodedStringWithOptions:0]]); | ||
} | ||
|
@@ -252,7 +275,7 @@ - (NSDictionary *)getPurchaseData:(SKPaymentTransaction *)transaction { | |
purchase[@"originalTransactionDate"] = @(originalTransaction.transactionDate.timeIntervalSince1970 * 1000); | ||
purchase[@"originalTransactionIdentifier"] = originalTransaction.transactionIdentifier; | ||
} | ||
|
||
return purchase; | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { | ||
NativeEventEmitter, | ||
NativeModules, | ||
Platform, | ||
} from 'react-native'; | ||
|
||
const { InAppUtils } = NativeModules; | ||
|
||
const InAppUtilsEmitter = new NativeEventEmitter(InAppUtils); | ||
|
||
const promisify = fn => (...args) => new Promise((resolve, reject) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wdyt about using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Personally I prefer using promises but others might still want to use callbacks. Doing it js side gives you the option to use both. Are there any advantages using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd vote to just standardize on Promises, using |
||
fn(...args, (err, res) => err ? reject(err) : resolve(res)); | ||
}); | ||
|
||
const IAU = Platform.select({ | ||
ios: { | ||
loadProducts: (products, cb) => cb | ||
? InAppUtils.loadProducts(products, cb) | ||
: promisify(InAppUtils.loadProducts)(products), | ||
|
||
canMakePayments: cb => cb | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @superandrew213 - If const promisifyBool = fn => (...args) => new Promise((resolve, reject) => {
fn(...args, res => res === true || false ? resolve(res) : reject(res))
}) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about this:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks good. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great! Thanks for catching that! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @superandrew213 just realized that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm ... it would be best if we enforce an error type. We can use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we have to do a breaking change, lets keep it simple and just change |
||
? InAppUtils.canMakePayments(cb) | ||
: promisify(InAppUtils.canMakePayments)(), | ||
|
||
purchaseProduct: (productIdentifier, cb) => cb | ||
? InAppUtils.purchaseProduct(productIdentifier, cb) | ||
: promisify(InAppUtils.purchaseProduct)(productIdentifier), | ||
|
||
restorePurchases: cb => cb | ||
? InAppUtils.restorePurchases(cb) | ||
: promisify(InAppUtils.restorePurchases)(), | ||
|
||
receiptData: cb => cb | ||
? InAppUtils.receiptData(cb) | ||
: promisify(InAppUtils.receiptData)(), | ||
|
||
addListener: InAppUtilsEmitter.addListener, | ||
}, | ||
|
||
android: {}, | ||
}); | ||
|
||
export default IAU; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we default to
NO
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!