Skip to content

Commit

Permalink
docs
Browse files Browse the repository at this point in the history
  • Loading branch information
jdivock-stripe committed Mar 30, 2022
1 parent 78dd6e8 commit 4089433
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 2 deletions.
44 changes: 42 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ Stripe Terminal enables you to build your own in-person checkout to accept payme
- [Manifest](#manifest)
- [Usage With Expo](#usage-with-expo)
- [Android](#android-2)
- [build.gradle](#buildgradle)
- [Permissions](#permissions-2)
- [iOS](#ios-2)
- [Configuring the SDK](#configuring-the-sdk)
- [Build](#build)
- [Example Code](#example-code)
Expand Down Expand Up @@ -205,6 +208,8 @@ Please read the [Android documentation](https://developer.android.com/about/vers
### Android

#### build.gradle

For android you'll need to massage your build files in order to properly compile. First in `android/build.gradle` by updating both `compileSdkVersion` and `targetSdkVersion` to at least `31`:

```
Expand All @@ -231,6 +236,37 @@ android.jetifier.blacklist=moshi-1.13.0.jar

Depending on the version of jetifier in use.

#### Permissions

Android will still require runtime permissions checks for location and BT access, we've provided an expo specific util to run these checks as follows:

```
import { requestNeededExpoAndroidPermissions } from 'stripe-terminal-react-native';
try {
const granted = await requestNeededExpoAndroidPermissions({
accessFineLocation: {
title: 'Location Permission',
message: 'Stripe Terminal needs access to your location',
buttonPositive: 'Accept',
},
});
if (granted) {
// init SDK
} else {
console.error(
'Location and BT services are required in order to connect to a reader.'
);
}
} catch (e) {
console.error(e);
}
```

### iOS

No special steps are required for iOS!

### Configuring the SDK

After [installing](#installation) the SDK, add the [config plugin](https://docs.expo.io/guides/config-plugins/) to the [`plugins`](https://docs.expo.io/versions/latest/config/app/#plugins) array of your `app.json` or `app.config.js`:
Expand Down Expand Up @@ -258,9 +294,13 @@ After [installing](#installation) the SDK, add the [config plugin](https://docs.
Rebuild your app as described in the ['Adding custom native code'](https://docs.expo.io/workflow/customizing/) guide with:

```
expo run:ios
> expo prebuild
and then
> expo run:ios
or
expo run:android
> expo run:android
```

# Example Code
Expand Down
29 changes: 29 additions & 0 deletions docs/set-up-your-sdk.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ Please read the [Android documentation](https://developer.android.com/about/vers
### Android

#### build.gradle

For android you'll need to massage your build files in order to properly compile. First in `android/build.gradle` by updating both `compileSdkVersion` and `targetSdkVersion` to at least `31`:

```
Expand All @@ -180,6 +182,33 @@ android.jetifier.blacklist=moshi-1.13.0.jar

Depending on the version of jetifier in use.

#### Permissions

Android will still require runtime permissions checks for location and BT access, we've provided an expo specific util to run these checks as follows:

```
import { requestNeededExpoAndroidPermissions } from 'stripe-terminal-react-native';
try {
const granted = await requestNeededExpoAndroidPermissions({
accessFineLocation: {
title: 'Location Permission',
message: 'Stripe Terminal needs access to your location',
buttonPositive: 'Accept',
},
});
if (granted) {
// init SDK
} else {
console.error(
'Location and BT services are required in order to connect to a reader.'
);
}
} catch (e) {
console.error(e);
}
```

### Configuring the SDK

After [installing](#installation) the SDK, add the [config plugin](https://docs.expo.io/guides/config-plugins/) to the [`plugins`](https://docs.expo.io/versions/latest/config/app/#plugins) array of your `app.json` or `app.config.js`:
Expand Down
55 changes: 55 additions & 0 deletions src/utils/androidExpoPermissionsUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { PermissionsAndroid, Platform } from 'react-native';
import { PERMISSIONS, RESULTS, request } from 'react-native-permissions';

const defaultFineLocationParams = {
title: 'Location Permission',
message: 'Stripe Terminal needs access to your location',
buttonPositive: 'Accept',
};

type PermissionsProps = {
accessFineLocation?: {
title: string;
message: string;
buttonPositive: string;
};
};

const isAndroid12orHigher = () =>
Platform.OS === 'android' && Platform.Version >= 31;

export async function requestNeededExpoAndroidPermissions({
accessFineLocation = defaultFineLocationParams,
}: PermissionsProps | undefined = {}): Promise<boolean> {
let hasGrantedLocationPermissions = false;
let hasGrantedBluetoothPermissions = false;

const grantedFineLocation = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
accessFineLocation || defaultFineLocationParams
);

if (hasGrantedPermission(grantedFineLocation)) {
hasGrantedLocationPermissions = true;

if (isAndroid12orHigher()) {
// otherwise within expo we have to make use of react-native-permissions
const grantedBT = await request(PERMISSIONS.ANDROID.BLUETOOTH_CONNECT);
const grantedBTScan = await request(PERMISSIONS.ANDROID.BLUETOOTH_SCAN);

if (
hasGrantedPermission(grantedBT) &&
hasGrantedPermission(grantedBTScan)
) {
hasGrantedBluetoothPermissions = true;
}
} else {
hasGrantedBluetoothPermissions = true;
}
}
return hasGrantedBluetoothPermissions && hasGrantedLocationPermissions;
}

const hasGrantedPermission = (status: string) => {
return status === RESULTS.GRANTED;
};

0 comments on commit 4089433

Please sign in to comment.