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

Support getting ConnectionStatus value from Terminal #712

Merged
merged 3 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import com.stripe.stripeterminal.external.models.CardPresentRoutingOptionParamet
import com.stripe.stripeterminal.external.models.Cart
import com.stripe.stripeterminal.external.models.CollectConfiguration
import com.stripe.stripeterminal.external.models.CollectInputsParameters
import com.stripe.stripeterminal.external.models.ConnectionStatus
import com.stripe.stripeterminal.external.models.CreateConfiguration
import com.stripe.stripeterminal.external.models.DiscoveryConfiguration
import com.stripe.stripeterminal.external.models.EmailInput
Expand Down Expand Up @@ -802,6 +803,12 @@ class StripeTerminalReactNativeModule(reactContext: ReactApplicationContext) :
promise.resolve(mapFromPaymentStatus(terminal.paymentStatus))
}

@ReactMethod
@Suppress("unused")
fun getConnectionStatus(promise: Promise) {
promise.resolve(mapFromConnectionStatus(terminal.connectionStatus))
}

@ReactMethod
@Suppress("unused")
fun getReaderSettings(promise: Promise) {
Expand Down
5 changes: 5 additions & 0 deletions ios/StripeTerminalReactNative.m
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,11 @@ @interface RCT_EXTERN_MODULE(StripeTerminalReactNative, RCTEventEmitter)
rejecter: (RCTPromiseRejectBlock)reject
)

RCT_EXTERN_METHOD(
getConnectionStatus:(RCTPromiseResolveBlock)resolve
rejecter: (RCTPromiseRejectBlock)reject
)

RCT_EXTERN_METHOD(
collectInputs:(NSDictionary *)params
resolver: (RCTPromiseResolveBlock)resolve
Expand Down
7 changes: 7 additions & 0 deletions ios/StripeTerminalReactNative.swift
Original file line number Diff line number Diff line change
Expand Up @@ -959,6 +959,13 @@ class StripeTerminalReactNative: RCTEventEmitter, DiscoveryDelegate, BluetoothRe
resolve(result)
}

@objc(getConnectionStatus:rejecter:)
func getConnectionStatus(resolver resolve: @escaping RCTPromiseResolveBlock, rejecter reject: @escaping RCTPromiseRejectBlock) {
let result = Mappers.mapFromConnectionStatus(Terminal.shared.connectionStatus)

resolve(result)
}

@objc(collectInputs:resolver:rejecter:)
func collectInputs(_ params: NSDictionary, resolver resolve: @escaping RCTPromiseResolveBlock, rejecter reject: @escaping RCTPromiseRejectBlock) {
let invalidParams = Errors.validateRequiredParameters(params: params, requiredParams: ["collectInputs"])
Expand Down
2 changes: 2 additions & 0 deletions src/StripeTerminalSdk.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import type {
CollectInputsParameters,
CollectInputsResults,
PaymentStatus,
ConnectionStatus,
} from './types';

const { StripeTerminalReactNative } = NativeModules;
Expand Down Expand Up @@ -147,6 +148,7 @@ export interface StripeTerminalSdkType {
}>;
getOfflineStatus(): Promise<OfflineStatus>;
getPaymentStatus(): Promise<PaymentStatus>;
getConnectionStatus(): Promise<ConnectionStatus>;
getReaderSettings(): Promise<Reader.ReaderSettings>;
setReaderSettings(
params: Reader.ReaderSettingsParameters
Expand Down
14 changes: 14 additions & 0 deletions src/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import type {
CollectInputsParameters,
CollectInputsResults,
PaymentStatus,
ConnectionStatus,
} from './types';

export async function initialize(
Expand Down Expand Up @@ -788,6 +789,19 @@ export async function getPaymentStatus(): Promise<PaymentStatus> {
}, 'getPaymentStatus')();
}

export async function getConnectionStatus(): Promise<ConnectionStatus> {
return Logger.traceSdkMethod(async () => {
try {
const connectionStatus = await StripeTerminalSdk.getConnectionStatus();
return connectionStatus;
} catch (error) {
return {
error: error as any,
};
}
}, 'getConnectionStatus')();
}

export async function getReaderSettings(): Promise<Reader.ReaderSettings> {
return Logger.traceSdkMethod(async () => {
try {
Expand Down
11 changes: 11 additions & 0 deletions src/hooks/useStripeTerminal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ import {
cancelReaderReconnection,
supportsReadersOfType,
getPaymentStatus,
getConnectionStatus,
} from '../functions';
import { StripeTerminalContext } from '../components/StripeTerminalContext';
import { useListener } from './useListener';
Expand Down Expand Up @@ -887,6 +888,15 @@ export function useStripeTerminal(props?: Props) {
return response;
}, [_isInitialized]);

const _getConnectionStatus = useCallback(async () => {
if (!_isInitialized()) {
console.error(NOT_INITIALIZED_ERROR_MESSAGE);
throw Error(NOT_INITIALIZED_ERROR_MESSAGE);
}
const response = await getConnectionStatus();
return response;
}, [_isInitialized]);

const _getReaderSettings = useCallback(async () => {
if (!_isInitialized()) {
console.error(NOT_INITIALIZED_ERROR_MESSAGE);
Expand Down Expand Up @@ -1008,6 +1018,7 @@ export function useStripeTerminal(props?: Props) {
setSimulatedCard: _setSimulatedCard,
getOfflineStatus: _getOfflineStatus,
getPaymentStatus: _getPaymentStatus,
getConnectionStatus: _getConnectionStatus,
getReaderSettings: _getReaderSettings,
setReaderSettings: _setReaderSettings,
collectInputs: _collectInputs,
Expand Down
2 changes: 2 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,8 @@ export type OfflineStatus = {
reader?: OfflineStatusDetails;
};

export type ConnectionStatus = 'notConnected' | 'connecting' | 'connected';

/**
* @ignore
*/
Expand Down