samsung-iap
v1.0.0
Published
A package to integrate Samsung in-app purchases in your application
Maintainers
Readme
samsung-iap
A package to integrate Samsung in-app purchases in your application.
Installation
npm install samsung-iapSDK Setup
This package is a TypeScript wrapper around the Samsung IAP SDK. The actual Samsung IAP SDK aar file must be provided by your application.
Setting Up the Samsung IAP SDK
Download the Samsung IAP SDK AAR file from the Samsung Developers website
In your application's Android project:
- Create a
libsdirectory in your app's root directory if it doesn't exist - Place the Samsung IAP SDK AAR file in the
libsdirectory - The file structure should look like this:
your-app/ ├── android/ │ ├── libs/ │ │ └── samsung-iap-6.4.0.aar // Your SDK file │ └── build.gradle └── ...
- Create a
Add the following to your app's
build.gradle:repositories { flatDir { dirs 'libs' } } dependencies { implementation(name: 'samsung-iap-6.4.0', ext: 'aar') // Replace with your actual aar file name }Sync your project with Gradle files:
- In Android Studio: Click "Sync Project with Gradle Files"
- Or run:
./gradlew clean build
Verification
To verify that the SDK is properly integrated:
- Check the build output for any errors
- Try to import the SDK classes in your Java/Kotlin code:
If there are no import errors, the SDK is properly integrated.import com.samsung.android.sdk.iap.lib.helper.IapHelper;
Expo Setup
Development Build
To use this package in an Expo project, you need to create a development build:
- Install EAS CLI:
npm install -g eas-cli- Configure your project for development builds:
eas build:configure- Add the following to your
app.json:
{
"expo": {
"plugins": [
[
"expo-build-properties",
{
"android": {
"compileSdkVersion": 33,
"targetSdkVersion": 33,
"buildToolsVersion": "33.0.0"
}
}
]
]
}
}- Install the required dependencies:
npx expo install expo-build-properties- Create a development build:
eas build --profile development --platform androidConfig Plugin
Add the following to your app.json to configure the Samsung IAP SDK:
{
"expo": {
"plugins": [
[
"samsung-iap",
{
"sdkVersion": "6.0.0" // Specify your Samsung IAP SDK version
}
]
]
}
}Native Module Setup
- Create a
pluginsdirectory in your project root - Create
samsung-iap.jsin thepluginsdirectory:
const { withPlugins } = require('@expo/config-plugins');
const withSamsungIap = (config) => {
return withPlugins(config, [
// Add any additional plugins if needed
]);
};
module.exports = withSamsungIap;- Update your
app.config.js:
const { withPlugins } = require('@expo/config-plugins');
const withSamsungIap = require('./plugins/samsung-iap');
module.exports = ({ config }) => {
return withPlugins(config, [
withSamsungIap,
// Add other plugins as needed
]);
};Usage in Expo
import SamsungIap from 'samsung-iap';
import { Platform } from 'react-native';
// Check if running on a Samsung device
const isSamsungDevice = Platform.OS === 'android' &&
Platform.constants.Manufacturer.toLowerCase().includes('samsung');
// Initialize Samsung IAP
const initSamsungIap = async () => {
if (!isSamsungDevice) {
console.log('Not a Samsung device, Samsung IAP will not work');
return;
}
try {
// Set operation mode (0 for test, 1 for production)
await SamsungIap.setOperationMode(0);
console.log('Samsung IAP initialized successfully');
} catch (error) {
console.error('Failed to initialize Samsung IAP:', error);
}
};
// Purchase an item
const purchaseItem = async (itemId) => {
if (!isSamsungDevice) {
throw new Error('Samsung IAP is only available on Samsung devices');
}
try {
const result = await SamsungIap.startPayment(itemId);
console.log('Purchase successful:', result);
return result;
} catch (error) {
console.error('Purchase failed:', error);
throw error;
}
};
// Get product details
const getProducts = async (itemIds) => {
if (!isSamsungDevice) {
throw new Error('Samsung IAP is only available on Samsung devices');
}
try {
const products = await SamsungIap.getProductsDetails(itemIds);
console.log('Products:', products);
return products;
} catch (error) {
console.error('Failed to get products:', error);
throw error;
}
};
// Example usage in a component
const MyComponent = () => {
useEffect(() => {
initSamsungIap();
}, []);
const handlePurchase = async () => {
try {
const result = await purchaseItem('your_item_id');
// Handle successful purchase
} catch (error) {
// Handle purchase error
}
};
return (
<View>
<Button title="Purchase Item" onPress={handlePurchase} />
</View>
);
};Error Handling
The package provides specific error codes for different scenarios:
const handleError = (error) => {
switch (error.code) {
case 'SET_OPERATION_MODE_ERROR':
console.error('Failed to set operation mode');
break;
case 'PAYMENT_ERROR':
console.error('Payment failed:', error.message);
break;
case 'GET_PRODUCTS_ERROR':
console.error('Failed to get products:', error.message);
break;
case 'GET_OWNED_LIST_ERROR':
console.error('Failed to get owned items:', error.message);
break;
case 'CONSUME_ITEMS_ERROR':
console.error('Failed to consume items:', error.message);
break;
case 'ACKNOWLEDGE_PURCHASES_ERROR':
console.error('Failed to acknowledge purchases:', error.message);
break;
case 'CHANGE_SUBSCRIPTION_PLAN_ERROR':
console.error('Failed to change subscription plan:', error.message);
break;
case 'GET_PROMOTION_ELIGIBILITY_ERROR':
console.error('Failed to get promotion eligibility:', error.message);
break;
default:
console.error('Unknown error:', error);
}
};Testing
For testing purposes, you can use the test mode:
// Set test mode
await SamsungIap.setOperationMode(0); // 0 for test mode
// Test purchase flow
try {
const result = await SamsungIap.startPayment('test_item_id');
console.log('Test purchase successful:', result);
} catch (error) {
console.error('Test purchase failed:', error);
}Troubleshooting
SDK not found error
- Make sure the Samsung IAP SDK aar file is in the correct location
- Check that the build.gradle file is properly configured
Device compatibility
- Samsung IAP only works on Samsung devices
- Check device compatibility before making API calls
Network issues
- Ensure the device has a stable internet connection
- Check if the Samsung account is properly logged in
Test mode issues
- Make sure you're using the correct test item IDs
- Verify that the test mode is properly set
License
MIT
