Skip to content

Commit 22348a0

Browse files
hyochanclaude
andauthored
feat: add offer code redemption support for iOS and Android (#90)
## 🎟 Offer Code Redemption (Closes #87) Adds offer code redemption support for both platforms. ### βœ… Changes * **iOS**: Exposes `presentCodeRedemptionSheet` (works on real devices only) * **Android**: Adds `openRedeemOfferCodeAndroid` to open Play Store redeem page * **Example**: New screen showing usage on both platforms * **Docs**: Added guide with usage and testing instructions ### πŸ§ͺ Testing * iOS: Test with promo codes via TestFlight or App Store * Android: Use promo codes from Google Play Console --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent 90e454d commit 22348a0

File tree

12 files changed

+2742
-17937
lines changed

12 files changed

+2742
-17937
lines changed

β€Ž.claude/settings.local.jsonβ€Ž

Lines changed: 0 additions & 11 deletions
This file was deleted.

β€Ž.gitignoreβ€Ž

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,6 @@ yarn-error.log
5858

5959
# Expo
6060
.expo/*
61+
62+
# Claude settings
63+
.claude/settings.local.json
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
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

β€Ždocs/sidebars.tsβ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ const sidebars: SidebarsConfig = {
3535
items: [
3636
'guides/purchases',
3737
'guides/lifecycle',
38+
'guides/offer-code-redemption',
3839
'guides/troubleshooting',
3940
'guides/faq',
4041
'guides/migration',

0 commit comments

Comments
Β (0)