1.0.1 • Published 1 month ago

@soundpayscorp/soundpay-sdk v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
1 month ago

@piavan/soundpaysreact

React Native wrapper for the SoundPays SDK, enabling sound-based payments and interactions in your React Native applications.

Installation

Add the package to your project's dependencies:

"dependencies": {
  "@piavan/soundpaysreact": "^1.0.0"
}

Or install using npm/yarn:

npm install @piavan/soundpaysreact
# or
yarn add @piavan/soundpaysreact

iOS Setup

  1. Add the following to your ios/Podfile:
require_relative '../node_modules/react-native/scripts/react_native_pods'
require_relative '../node_modules/@react-native-community/cli-platform-ios/native_modules'

platform :ios, '16.0'
prepare_react_native_project!

target 'YourAppName' do
  config = use_native_modules!
  use_react_native!(
    :path => config[:reactNativePath],
    :hermes_enabled => true,
    :fabric_enabled => true
  )

  pod 'soundpaysreact', :path => '../node_modules/@piavan/soundpaysreact'
end
  1. Run pod install:
cd ios && pod install

Usage Example

Here's a complete example of how to use the SoundPays SDK in your React Native app:

import React, {useState, useEffect} from 'react';
import {
  SafeAreaView,
  StyleSheet,
  Text,
  TouchableOpacity,
  View,
  Alert,
} from 'react-native';
import SoundPaysReact from '@piavan/soundpaysreact';

const App = () => {
  const [isInitialized, setIsInitialized] = useState(false);
  const [isScanning, setIsScanning] = useState(false);
  const [scanResult, setScanResult] = useState(null);

  useEffect(() => {
    initializeSoundPays();
  }, []);

  const initializeSoundPays = async () => {
    try {
      await SoundPaysReact.setup('sandbox');
      setIsInitialized(true);
      console.log('SoundPays initialized successfully');
    } catch (error) {
      console.error('Failed to initialize SoundPays:', error);
      Alert.alert('Error', 'Failed to initialize SoundPays');
    }
  };

  const handleLogin = async () => {
    try {
      const result = await SoundPaysReact.login('test@example.com');
      console.log('Login result:', result);
      Alert.alert('Success', 'Verification needed. Check your email.');
    } catch (error) {
      console.error('Login failed:', error);
      Alert.alert('Error', 'Login failed');
    }
  };

  const handleSilentAuth = async () => {
    try {
      const result = await SoundPaysReact.silentAuth();
      console.log('Silent auth result:', result);
      Alert.alert('Success', 'Silent authentication successful');
    } catch (error) {
      console.error('Silent auth failed:', error);
      Alert.alert('Error', 'Silent authentication failed');
    }
  };

  const handleStartScan = async () => {
    try {
      const hasPermission = await SoundPaysReact.checkMicPermission();
      if (!hasPermission) {
        Alert.alert('Error', 'Microphone permission required');
        return;
      }

      setIsScanning(true);
      const result = await SoundPaysReact.startScan();
      setScanResult(JSON.parse(result));
      console.log('Scan result:', result);
    } catch (error) {
      console.error('Scan failed:', error);
      Alert.alert('Error', 'Scan failed');
    } finally {
      setIsScanning(false);
    }
  };

  const handleStopScan = async () => {
    try {
      await SoundPaysReact.stopScan();
      setIsScanning(false);
    } catch (error) {
      console.error('Stop scan failed:', error);
    }
  };

  const handleSignOut = async () => {
    try {
      await SoundPaysReact.signout();
      Alert.alert('Success', 'Signed out successfully');
    } catch (error) {
      console.error('Sign out failed:', error);
      Alert.alert('Error', 'Sign out failed');
    }
  };

  return (
    <SafeAreaView style={styles.container}>
      <Text style={styles.title}>SoundPays Sample App</Text>
      
      <View style={styles.buttonContainer}>
        <TouchableOpacity
          style={styles.button}
          onPress={handleLogin}
          disabled={!isInitialized}>
          <Text style={styles.buttonText}>Login</Text>
        </TouchableOpacity>

        <TouchableOpacity
          style={styles.button}
          onPress={handleSilentAuth}
          disabled={!isInitialized}>
          <Text style={styles.buttonText}>Silent Auth</Text>
        </TouchableOpacity>

        <TouchableOpacity
          style={[styles.button, isScanning && styles.scanningButton]}
          onPress={isScanning ? handleStopScan : handleStartScan}
          disabled={!isInitialized}>
          <Text style={styles.buttonText}>
            {isScanning ? 'Stop Scan' : 'Start Scan'}
          </Text>
        </TouchableOpacity>

        <TouchableOpacity
          style={styles.button}
          onPress={handleSignOut}
          disabled={!isInitialized}>
          <Text style={styles.buttonText}>Sign Out</Text>
        </TouchableOpacity>
      </View>

      {scanResult && (
        <View style={styles.resultContainer}>
          <Text style={styles.resultTitle}>Scan Result:</Text>
          <Text style={styles.resultText}>
            {JSON.stringify(scanResult, null, 2)}
          </Text>
        </View>
      )}
    </SafeAreaView>
  );
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
    padding: 20,
    backgroundColor: '#f5f5f5',
  },
  title: {
    fontSize: 24,
    fontWeight: 'bold',
    textAlign: 'center',
    marginBottom: 20,
  },
  buttonContainer: {
    gap: 10,
  },
  button: {
    backgroundColor: '#007AFF',
    padding: 15,
    borderRadius: 8,
    alignItems: 'center',
  },
  buttonText: {
    color: 'white',
    fontSize: 16,
    fontWeight: 'bold',
  },
  scanningButton: {
    backgroundColor: '#FF3B30',
  },
  resultContainer: {
    marginTop: 20,
    padding: 10,
    backgroundColor: 'white',
    borderRadius: 8,
  },
  resultTitle: {
    fontSize: 18,
    fontWeight: 'bold',
    marginBottom: 10,
  },
  resultText: {
    fontSize: 14,
  },
});

export default App;

API Reference

setup(environment: string)

Initialize SoundPays with the specified environment.

  • environment: 'sandbox' or 'production'

login(email: string)

Start the login process with email.

  • Returns: "verification-needed" on success

silentAuth()

Perform silent authentication.

  • Returns: "201" on success

checkMicPermission()

Check if microphone permission is granted.

  • Returns: boolean

startScan()

Start scanning for sound-based interactions.

  • Returns: JSON string containing scan results

stopScan()

Stop the scanning process.

getToken()

Get the current authentication token.

  • Returns: string

signout()

Sign out the current user.

License

MIT

1.0.1

1 month ago

1.0.0

1 month ago