Last active
April 6, 2022 14:39
-
-
Save msmfsd/84e3f9b3a9bb0ac318e6d4bbf814442b to your computer and use it in GitHub Desktop.
IAPHUB settings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// VERSIONS | |
// "react-native": "0.63.3" | |
// "react-native-iap": "5.1.3" | |
// "react-native-iaphub": "6.0.3" | |
// OUR CODE | |
import Iaphub from 'react-native-iaphub'; | |
// APP INIT | |
const iaphubInit = async () => { | |
try { | |
await Iaphub.init({ | |
appId: IAPHUB_APP_ID, | |
apiKey: IAPHUB_API_KEY, | |
environment: 'production', | |
}); | |
} catch (error) { | |
logError(error); | |
} | |
}; | |
// USER LOGGED IN | |
... | |
await Iaphub.setUserId(uid); | |
await Iaphub.setUserTags(tags); | |
const userProducts = await Iaphub.getActiveProducts({ | |
includeSubscriptionStates: ['retry_period', 'paused'], | |
}); | |
// if user has the subscription save to async storage | |
... | |
// PRODUCTS SCREEN | |
// return await Iaphub.getProductsForSale(); | |
// we only have one IAP - a yearly renewing subscription | |
const onSubscribeToPremium = async (productSku) => { | |
try { | |
setLog('Connected to App Store. Please remain on this screen'); | |
const transaction = await Iaphub.buy(productSku, { | |
onReceiptProcess: (receipt) => {/* ...log receipt... */}, | |
}); | |
const alert = formatTransactionStatus(transaction); // see helper method below | |
Alert.alert(/* display transaction result */); | |
} catch (error) { | |
if (formatError(error) === 'Transaction not found') { | |
// iOS 14 issue: https://github.com/iaphub/react-native-iaphub/issues/20 | |
setLog('Transaction completed'); | |
} else if (error.code === 'user_cancelled') { | |
return; | |
} else if (error.code === 'product_already_owned') { | |
Alert.alert( | |
'Product already owned', | |
'Please restore your purchase on this device', | |
[ | |
{text: 'Cancel', style: 'cancel'}, | |
{ | |
text: 'Restore', | |
onPress: async () => { | |
await iaphubRestore(uid); | |
setLog('Product restored'); | |
}, | |
}, | |
], | |
); | |
} else { | |
const alert = formatTransactionError(error.code); // see helper method below | |
Alert.alert(/* display transaction result */); | |
setLog(`${alert[1]}`); | |
} | |
} | |
}; | |
// helper methods | |
const formatBillingMessage = (product) => { | |
let message = 'Manage purchases on your device account settings screen'; | |
if (product.isSubscriptionRetryPeriod || product.isSubscriptionGracePeriod) { | |
message = 'URGENT! Please update your billing information'; | |
} | |
return message; | |
}; | |
const formatTransactionStatus = (transaction) => { | |
let status = []; | |
if (transaction.webhookStatus === 'failed') { | |
/* | |
* The purchase has been successful but we need to check that the webhook to our server was successful as well | |
* If the webhook request failed, IAPHUB will send you an alert and retry again in 1 minute, 10 minutes, 1 hour and 24 hours. | |
* You can retry the webhook directly from the dashboard as well | |
*/ | |
status.push( | |
'Purchase successful', | |
'Your purchase was successful and waiting to be validated on our server, this can take a few minutes. If you cannot access premium features please contact support via the link on the Profile screen', | |
); | |
} else { | |
status.push( | |
'Purchase successful', | |
'Your purchase has been processed successfully! All premium features are now unlocked. Your account screen may take a few minutes to reflect your subscription', | |
); | |
} | |
return status; | |
}; | |
const formatTransactionError = (errorCode) => { | |
let errMessage = []; | |
if (errorCode === 'deferred_payment') { | |
errMessage.push( | |
'Purchase awaiting approval', | |
'Your purchase is awaiting approval from the parental control', | |
); | |
} else if (errorCode === 'receipt_validation_failed') { | |
errMessage.push( | |
'We‘re having trouble validating your transaction', | |
'Give us some time, we‘ll retry to validate your transaction ASAP!', | |
); | |
} else if (errorCode === 'receipt_invalid') { | |
errMessage.push( | |
'Purchase error', | |
'We were not able to process your purchase, if you‘ve been charged please contact support via the link on the Profile screen', | |
); | |
} else if (errorCode === 'receipt_request_failed') { | |
errMessage.push( | |
'Purchase awaiting approval', | |
'Your purchase was successful and waiting to be validated on our server, this can take a few minutes. If you cannot access premium features please contact support via the link on the Profile screen', | |
); | |
} else { | |
errMessage.push( | |
'Purchase error', | |
'We were not able to process your purchase, please try again later or contact support via the link on the Profile screen', | |
); | |
} | |
return errMessage; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment