Skip to content

Instantly share code, notes, and snippets.

@vivekvardhanadepu
Created December 31, 2021 11:05
Show Gist options
  • Save vivekvardhanadepu/156e5b3b6e9a804ad0f85b867f2e4088 to your computer and use it in GitHub Desktop.
Save vivekvardhanadepu/156e5b3b6e9a804ad0f85b867f2e4088 to your computer and use it in GitHub Desktop.
Initialising Stripe Terminal
import { Platform } from "react-native";
import StripeTerminal from "react-native-stripe-terminal";
import RNAndroidLocationEnabler from "react-native-android-location-enabler";
import { PERMISSIONS, request, RESULTS } from "react-native-permissions";
// can be similarly done for componentDidMount
useEffect(() => {
request(Platform.select({
android: PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION,
ios: PERMISSIONS.IOS.LOCATION_ALWAYS,
}))
.then((result) => {
if(result === RESULTS.GRANTED){
if (Platform.OS === "android") {
return RNAndroidLocationEnabler.promptForEnableLocationIfNeeded({
interval: 10000,
fastInterval: 5000,
})
}
return Promise.resolve(100); // dummy resolved promise
} else {
throw 'Permission is denied';
}
})
.then(() => {
if(data){
StripeTerminal.initialize({
fetchConnectionToken: () => {
return axios.post(
"https://your.backend.com/connection_token",
{}
)
.then((res) => {
return res.secret;
})
.catch((err) => {
console.log(err);
});
},
})
.then((data) => {
console.log('Stripe Terminal initialized');
})
.catch((err) => {
console.log(err);
});
} else {
throw 'error enabling location';
}
})
.catch((err) => {
console.log('You need to enable permission to use Stripe Terminal');
});
},[]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment