|
| 1 | +--- |
| 2 | +title: Offer Code Redemption |
| 3 | +sidebar_label: Offer Code Redemption |
| 4 | +--- |
| 5 | + |
| 6 | +# Offer Code Redemption |
| 7 | + |
| 8 | +This guide explains how to implement offer code redemption functionality in your app using expo-iap. |
| 9 | + |
| 10 | +## Overview |
| 11 | + |
| 12 | +Offer codes (also known as promo codes or redemption codes) allow users to redeem special offers for in-app purchases and subscriptions. The implementation differs between iOS and Android platforms. |
| 13 | + |
| 14 | +## iOS Implementation |
| 15 | + |
| 16 | +On iOS, expo-iap provides a native method to present Apple's code redemption sheet directly within your app. |
| 17 | + |
| 18 | +### Usage |
| 19 | + |
| 20 | +```typescript |
| 21 | +import { presentCodeRedemptionSheet } from 'expo-iap'; |
| 22 | + |
| 23 | +// Present the code redemption sheet |
| 24 | +try { |
| 25 | + const result = await presentCodeRedemptionSheet(); |
| 26 | + if (result) { |
| 27 | + console.log('Code redemption sheet presented successfully'); |
| 28 | + // The system will handle the redemption process |
| 29 | + // Listen for purchase updates via purchaseUpdatedListener |
| 30 | + } |
| 31 | +} catch (error) { |
| 32 | + console.error('Failed to present code redemption sheet:', error); |
| 33 | +} |
| 34 | +``` |
| 35 | + |
| 36 | +### Important Notes |
| 37 | + |
| 38 | +- This method only works on real iOS devices (not simulators) |
| 39 | +- Not available on tvOS |
| 40 | +- The redemption sheet is handled by the iOS system |
| 41 | +- After successful redemption, purchase updates will be delivered through your existing `purchaseUpdatedListener` |
| 42 | + |
| 43 | +## Android Implementation |
| 44 | + |
| 45 | +Google Play does not provide a direct API to redeem codes within the app. Instead, users must redeem codes through the Google Play Store app or website. |
| 46 | + |
| 47 | +### Usage |
| 48 | + |
| 49 | +```typescript |
| 50 | +import { openRedeemOfferCodeAndroid } from 'expo-iap'; |
| 51 | + |
| 52 | +// Open Google Play Store redemption page |
| 53 | +try { |
| 54 | + await openRedeemOfferCodeAndroid(); |
| 55 | + // This will open the Play Store where users can enter their codes |
| 56 | +} catch (error) { |
| 57 | + console.error('Failed to open Play Store:', error); |
| 58 | +} |
| 59 | +``` |
| 60 | + |
| 61 | +### Alternative Approach |
| 62 | + |
| 63 | +You can also direct users to redeem codes via a custom deep link: |
| 64 | + |
| 65 | +```typescript |
| 66 | +import { Linking } from 'react-native'; |
| 67 | + |
| 68 | +const redeemCode = async (code: string) => { |
| 69 | + const url = `https://play.google.com/redeem?code=${code}`; |
| 70 | + await Linking.openURL(url); |
| 71 | +}; |
| 72 | +``` |
| 73 | + |
| 74 | +## Complete Example |
| 75 | + |
| 76 | +Here's a complete example that handles both platforms: |
| 77 | + |
| 78 | +```typescript |
| 79 | +import { Platform } from 'react-native'; |
| 80 | +import { |
| 81 | + presentCodeRedemptionSheet, |
| 82 | + openRedeemOfferCodeAndroid, |
| 83 | + purchaseUpdatedListener |
| 84 | +} from 'expo-iap'; |
| 85 | + |
| 86 | +const handleRedeemCode = async () => { |
| 87 | + try { |
| 88 | + if (Platform.OS === 'ios') { |
| 89 | + // Present native iOS redemption sheet |
| 90 | + const result = await presentCodeRedemptionSheet(); |
| 91 | + if (result) { |
| 92 | + console.log('Redemption sheet presented'); |
| 93 | + } |
| 94 | + } else if (Platform.OS === 'android') { |
| 95 | + // Open Play Store for Android |
| 96 | + await openRedeemOfferCodeAndroid(); |
| 97 | + } |
| 98 | + } catch (error) { |
| 99 | + console.error('Error redeeming code:', error); |
| 100 | + } |
| 101 | +}; |
| 102 | + |
| 103 | +// Set up listener for purchase updates after redemption |
| 104 | +useEffect(() => { |
| 105 | + const subscription = purchaseUpdatedListener((purchase) => { |
| 106 | + console.log('Purchase updated after redemption:', purchase); |
| 107 | + // Handle the new purchase/subscription |
| 108 | + }); |
| 109 | + |
| 110 | + return () => { |
| 111 | + subscription.remove(); |
| 112 | + }; |
| 113 | +}, []); |
| 114 | +``` |
| 115 | + |
| 116 | +## Best Practices |
| 117 | + |
| 118 | +1. **User Experience**: Clearly communicate to users where they can find and how to use offer codes |
| 119 | +2. **Error Handling**: Always wrap redemption calls in try-catch blocks |
| 120 | +3. **Platform Detection**: Use platform-specific methods appropriately |
| 121 | +4. **Purchase Validation**: Always validate purchases on your server after redemption |
| 122 | + |
| 123 | +## Testing |
| 124 | + |
| 125 | +### iOS Testing |
| 126 | +- Offer codes can only be tested on real devices |
| 127 | +- Use TestFlight or App Store Connect to generate test codes |
| 128 | +- Sandbox environment supports offer code testing |
| 129 | + |
| 130 | +### Android Testing |
| 131 | +- Test with promo codes generated in Google Play Console |
| 132 | +- Ensure your app is properly configured for in-app purchases |
| 133 | + |
| 134 | +## Troubleshooting |
| 135 | + |
| 136 | +### iOS Issues |
| 137 | +- **"Not available on simulator"**: Use a real device for testing |
| 138 | +- **Sheet doesn't appear**: Ensure StoreKit is properly configured |
| 139 | +- **User cancellation**: This is normal behavior and doesn't throw an error |
| 140 | + |
| 141 | +### Android Issues |
| 142 | +- **Play Store doesn't open**: Check if Play Store is installed and updated |
| 143 | +- **Invalid code**: Verify the code format and validity in Play Console |
0 commit comments