Created
December 31, 2021 11:05
-
-
Save vivekvardhanadepu/156e5b3b6e9a804ad0f85b867f2e4088 to your computer and use it in GitHub Desktop.
Initialising Stripe Terminal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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