Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/add android tap and pay #123

Draft
wants to merge 16 commits into
base: beta
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ dependencies {
testImplementation "junit:junit:$junitVersion"
androidTestImplementation "androidx.test.ext:junit:$androidxJunitVersion"
androidTestImplementation "androidx.test.espresso:espresso-core:$androidxEspressoCoreVersion"
implementation "com.stripe:stripeterminal:2.17.1"
implementation "com.stripe:stripeterminal:2.20.1"
implementation "com.stripe:stripeterminal-core:2.20.1"
implementation "com.stripe:stripeterminal-localmobile:2.20.1"
implementation("com.squareup.moshi:moshi:1.14.0")
implementation "com.google.code.gson:gson:2.8.8"
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public class StripeTerminal
Cancelable pendingCollectPaymentMethod = null;
ConnectionTokenCallback pendingConnectionTokenCallback = null;
String lastCurrency = null;
Boolean tapToPaySupported = null;

ReaderSoftwareUpdate currentUpdate = null;
PaymentIntent currentPaymentIntent = null;
Expand Down Expand Up @@ -883,6 +884,7 @@ public void getSimulatorConfiguration(@NonNull final PluginCall call) {
public void setSimulatorConfiguration(@NonNull final PluginCall call) {
Integer availableReaderUpdateInt = call.getInt("availableReaderUpdate");
Integer simulatedCardInt = call.getInt("simulatedCard");
Long simulatedTipAmount = call.getLong("simulatedTipAmount");

SimulatorConfiguration currentConfig = Terminal
.getInstance()
Expand All @@ -903,7 +905,8 @@ public void setSimulatorConfiguration(@NonNull final PluginCall call) {

SimulatorConfiguration newConfig = new SimulatorConfiguration(
availableReaderUpdate,
simulatedCard
simulatedCard,
simulatedTipAmount
);

Terminal.getInstance().setSimulatorConfiguration(newConfig);
Expand Down Expand Up @@ -936,6 +939,53 @@ public void onFailure(@NonNull TerminalException e) {
}
}

@PluginMethod
public void tapToPaySupported(final PluginCall call) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not really a fan of this method of determining if Tap to Pay is supported.

This will say that Tap to Pay is not supported if discoverReaders is already in progress. It would also prevent discoverReaders from happening while it is checking. Both of those could be pretty problematic in an app that auto-connects to readers on startup.

I really wish that the Terminal SDK had a built in method... When I implemented this for Tap to Pay on iOS, I just checked the iPhone model and iOS version number to "assume" if it was supported or not. Unfortunately, its not that easy on Android with different phone brands...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, unfortunately this is the official Stripe way of checking.

In this instance I have added a value that is set on first run of the method call, in order to speed up subsequent calls without requiring a call to discoverReaders. Although not optimal, scanning for the compatibility/LocalMobileReader is fast (generally less than a second).

I have also added a call to cancel discoverReaders if it's already running and I have also added a call to the pendingDiscoverReaders.cancel() method within cancelDiscoverReaders before nulling pendingDiscoverReaders as Stripe includes calling that method in their docs.

If there's anything further that you would like me to change, let me know. I happy to remove this method entirely if it irks you, as it is more a nice to have check rather than a requirement.

JSObject ret = new JSObject();

if (tapToPaySupported != null) {
ret.put("supported", tapToPaySupported);
call.resolve(ret);
return;
}

DiscoveryConfiguration config = new DiscoveryConfiguration(
0,
DiscoveryMethod.LOCAL_MOBILE,
false
);

// first attempt to cancel any pending discoverReaders calls
cancelDiscoverReaders();

pendingDiscoverReaders =
Terminal
.getInstance()
.discoverReaders(
config,
readers -> {},
new Callback() {
private void returnValue(boolean supported) {
if (pendingDiscoverReaders != null) {
pendingDiscoverReaders = null;
}
ret.put("supported", supported);
call.resolve(ret);
}

@Override
public void onSuccess() {
returnValue(true);
}

@Override
public void onFailure(@NonNull TerminalException e) {
returnValue(false);
}
}
);
}

@Override
public void fetchConnectionToken(
@NonNull ConnectionTokenCallback connectionTokenCallback
Expand Down Expand Up @@ -1111,4 +1161,9 @@ public void onReaderReconnectFailed(@NonNull Reader reader) {
pendingReaderAutoReconnect = null;
notifyListeners("didFailReaderReconnect", null);
}

@Override
public void onStop() {
cancelDiscoverReaders();
}
}
8 changes: 8 additions & 0 deletions src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,7 @@ export interface ListLocationsParameters {
export interface SimulatorConfiguration {
availableReaderUpdate?: SimulateReaderUpdate
simulatedCard?: SimulatedCardType
simulatedTipAmount?: number
}

/**
Expand Down Expand Up @@ -977,6 +978,8 @@ export interface StripeTerminalInterface {
checkPermissions(): Promise<PermissionStatus>
requestPermissions(): Promise<PermissionStatus>

tapToPaySupported(): Promise<boolean>

addListener(
eventName: 'requestConnectionToken',
listenerFunc: () => void
Expand Down Expand Up @@ -1032,4 +1035,9 @@ export interface StripeTerminalInterface {
eventName: string,
listenerFunc: Function
): Promise<PluginListenerHandle> & PluginListenerHandle

addListener(
eventName: 'didCancelDiscoverReaders',
listenerFunc: (data: null) => void
): Promise<PluginListenerHandle> & PluginListenerHandle
}
4 changes: 4 additions & 0 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1016,6 +1016,10 @@ export class StripeTerminalPlugin {
return await StripeTerminal.requestPermissions()
}

public static async tapToPaySupported(): Promise<boolean> {
return await StripeTerminal.tapToPaySupported()
}

/**
* This should not be used directly. It will not behave correctly when using `Internet` and `Both` discovery methods
*
Expand Down
4 changes: 4 additions & 0 deletions src/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -692,4 +692,8 @@ export class StripeTerminalWeb
// no equivalent
console.warn('cancelAutoReconnect is only available for Bluetooth readers.')
}

async tapToPaySupported(): Promise<boolean> {
return await this.tapToPaySupported()
}
}