Skip to content

Commit

Permalink
Merge branch 'main' into bbpos/add-missing-reader-callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
nazli-stripe authored May 29, 2024
2 parents 1c84d8e + 811b553 commit 017f170
Show file tree
Hide file tree
Showing 18 changed files with 121 additions and 10 deletions.
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def getExtOrIntegerDefault(name) {
return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties['StripeTerminalReactNative_' + name]).toInteger()
}

def terminalAndroidSdkVersion = '3.5.0'
def terminalAndroidSdkVersion = '3.6.0'
def reactNativeSdkVersion = getVersionFromNpm()

android {
Expand Down
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 @@ -796,6 +797,18 @@ class StripeTerminalReactNativeModule(reactContext: ReactApplicationContext) :
promise.resolve(mapFromOfflineStatus(terminal.offlineStatus))
}

@ReactMethod
@Suppress("unused")
fun getPaymentStatus(promise: Promise) {
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
2 changes: 1 addition & 1 deletion bitrise.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ workflows:
set -e
# This is a terrible hack, as I haven't worked out how Bitrise's `pod install` step interacts with the rbenv set in this app. You definitely shouldn't copy this.
cd dev-app/ios && asdf install ruby 3.2.3 && bundle install && \
gem install cocoapods -v 1.14.2 && pod install && cd - && \
gem install cocoapods -v 1.15.2 && pod install && cd - && \
npm rebuild detox
echo "Checking for diffs in pod lockfile, if this fails please ensure all dependencies are up to date" && \
git diff --exit-code
Expand Down
8 changes: 4 additions & 4 deletions dev-app/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -440,8 +440,8 @@ PODS:
- SocketRocket (0.6.1)
- stripe-terminal-react-native (0.0.1-beta.18):
- React-Core
- StripeTerminal (~> 3.5.0)
- StripeTerminal (3.5.0)
- StripeTerminal (~> 3.6.0)
- StripeTerminal (3.6.0)
- Yoga (1.14.0)

DEPENDENCIES:
Expand Down Expand Up @@ -640,8 +640,8 @@ SPEC CHECKSUMS:
RNGestureHandler: 32a01c29ecc9bb0b5bf7bc0a33547f61b4dc2741
RNScreens: 3c2d122f5e08c192e254c510b212306da97d2581
SocketRocket: f32cd54efbe0f095c4d7594881e52619cfe80b17
stripe-terminal-react-native: d4d8536646e1d16ae8a906867e79107b02e8fc1f
StripeTerminal: 107cb1815738accbaa54179411cd916ed129b044
stripe-terminal-react-native: ca2785cc6ec7efc9b2a4a5b7fdb6ef9b7084ce24
StripeTerminal: d480f58ed3a170695a92954a0f64e97383983a26
Yoga: 4c3aa327e4a6a23eeacd71f61c81df1bcdf677d5

PODFILE CHECKSUM: 8b6a9ec02d405ae261b816f227c75d2138b656d5
Expand Down
2 changes: 2 additions & 0 deletions dev-app/src/AppContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export const AppContext = React.createContext<IAppContext>({
setLastSuccessfulChargeId: (_id) => null,
lastSuccessfulPaymentIntentId: null,
setLastSuccessfulPaymentIntentId: (_id) => null,
lastSuccessfulAmount: null,
setLastSuccessfulAmount: (_a) => null,
autoReconnectOnUnexpectedDisconnect: false,
setAutoReconnectOnUnexpectedDisconnect: (_b) => null,
cachedLocations: [],
Expand Down
5 changes: 5 additions & 0 deletions dev-app/src/Root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ export default function Root() {
>(null);
const [lastSuccessfulPaymentIntentId, setLastSuccessfulPaymentIntentId] =
useState<string | null>(null);
const [lastSuccessfulAmount, setLastSuccessfulAmount] = useState<
string | null
>(null);
const [
autoReconnectOnUnexpectedDisconnect,
setAutoReconnectOnUnexpectedDisconnect,
Expand Down Expand Up @@ -96,6 +99,8 @@ export default function Root() {
setLastSuccessfulPaymentIntentId: (id) =>
setLastSuccessfulPaymentIntentId(id),
lastSuccessfulPaymentIntentId,
setLastSuccessfulAmount: (a) => setLastSuccessfulAmount(a),
lastSuccessfulAmount,
autoReconnectOnUnexpectedDisconnect,
setAutoReconnectOnUnexpectedDisconnect: (b) =>
setAutoReconnectOnUnexpectedDisconnect(b),
Expand Down
2 changes: 2 additions & 0 deletions dev-app/src/screens/CollectCardPaymentScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export default function CollectCardPaymentScreen() {
api,
setLastSuccessfulChargeId,
setLastSuccessfulPaymentIntentId,
setLastSuccessfulAmount,
account,
} = useContext(AppContext);

Expand Down Expand Up @@ -478,6 +479,7 @@ export default function CollectCardPaymentScreen() {
if (paymentIntent?.charges[0]?.id) {
setLastSuccessfulChargeId(paymentIntent.charges[0].id);
setLastSuccessfulPaymentIntentId(paymentIntent.id);
setLastSuccessfulAmount(paymentIntent.amount.toString());
}

if (paymentIntent?.status === 'succeeded') {
Expand Down
9 changes: 6 additions & 3 deletions dev-app/src/screens/RefundPaymentScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ import type { RouteParamList } from '../App';
import { Picker } from '@react-native-picker/picker';

export default function RefundPaymentScreen() {
const { lastSuccessfulChargeId, lastSuccessfulPaymentIntentId } =
useContext(AppContext);
const {
lastSuccessfulAmount,
lastSuccessfulChargeId,
lastSuccessfulPaymentIntentId,
} = useContext(AppContext);
const [inputValues, setInputValues] = useState<{
chargeId: string;
paymentIntentId: string;
Expand All @@ -34,7 +37,7 @@ export default function RefundPaymentScreen() {
}>({
chargeId: lastSuccessfulChargeId || '',
paymentIntentId: lastSuccessfulPaymentIntentId || '',
amount: '100',
amount: lastSuccessfulAmount || '',
currency: 'CAD',
refundApplicationFee: false,
reverseTransfer: false,
Expand Down
2 changes: 2 additions & 0 deletions dev-app/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export type IAppContext = {
setLastSuccessfulChargeId: (id: string) => void;
lastSuccessfulPaymentIntentId: string | null;
setLastSuccessfulPaymentIntentId: (id: string) => void;
lastSuccessfulAmount: string | null;
setLastSuccessfulAmount: (amount: string) => void;
autoReconnectOnUnexpectedDisconnect: boolean | false;
setAutoReconnectOnUnexpectedDisconnect: (b: boolean) => void;
cachedLocations: Array<Location>;
Expand Down
10 changes: 10 additions & 0 deletions ios/StripeTerminalReactNative.m
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,16 @@ @interface RCT_EXTERN_MODULE(StripeTerminalReactNative, RCTEventEmitter)
rejecter: (RCTPromiseRejectBlock)reject
)

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

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

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

resolve(result)
}

@objc(getPaymentStatus:rejecter:)
func getPaymentStatus(resolver resolve: @escaping RCTPromiseResolveBlock, rejecter reject: @escaping RCTPromiseRejectBlock) {
let result = Mappers.mapFromPaymentStatus(Terminal.shared.paymentStatus)

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) {
Expand Down
4 changes: 4 additions & 0 deletions src/StripeTerminalSdk.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import type {
OfflineStatus,
CollectInputsParameters,
CollectInputsResults,
PaymentStatus,
ConnectionStatus,
} from './types';

const { StripeTerminalReactNative } = NativeModules;
Expand Down Expand Up @@ -145,6 +147,8 @@ export interface StripeTerminalSdkType {
error?: StripeError;
}>;
getOfflineStatus(): Promise<OfflineStatus>;
getPaymentStatus(): Promise<PaymentStatus>;
getConnectionStatus(): Promise<ConnectionStatus>;
getReaderSettings(): Promise<Reader.ReaderSettings>;
setReaderSettings(
params: Reader.ReaderSettingsParameters
Expand Down
2 changes: 2 additions & 0 deletions src/__tests__/__snapshots__/functions.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ Object {
"createSetupIntent": [Function],
"disconnectReader": [Function],
"discoverReaders": [Function],
"getConnectionStatus": [Function],
"getLocations": [Function],
"getOfflineStatus": [Function],
"getPaymentStatus": [Function],
"getReaderSettings": [Function],
"initialize": [Function],
"installAvailableUpdate": [Function],
Expand Down
28 changes: 28 additions & 0 deletions src/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ import type {
OfflineStatus,
CollectInputsParameters,
CollectInputsResults,
PaymentStatus,
ConnectionStatus,
} from './types';

export async function initialize(
Expand Down Expand Up @@ -774,6 +776,32 @@ export async function getOfflineStatus(): Promise<OfflineStatus> {
}, 'getOfflineStatus')();
}

export async function getPaymentStatus(): Promise<PaymentStatus> {
return Logger.traceSdkMethod(async () => {
try {
const paymentStatus = await StripeTerminalSdk.getPaymentStatus();
return paymentStatus;
} catch (error) {
return {
error: error as any,
};
}
}, '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
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ Object {
"discoverReaders": [Function],
"discoveredReaders": Array [],
"emitter": undefined,
"getConnectionStatus": [Function],
"getLocations": [Function],
"getOfflineStatus": [Function],
"getPaymentStatus": [Function],
"getReaderSettings": [Function],
"initialize": [Function],
"installAvailableUpdate": [Function],
Expand Down
22 changes: 22 additions & 0 deletions src/hooks/useStripeTerminal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ import {
cancelCollectInputs,
cancelReaderReconnection,
supportsReadersOfType,
getPaymentStatus,
getConnectionStatus,
} from '../functions';
import { StripeTerminalContext } from '../components/StripeTerminalContext';
import { useListener } from './useListener';
Expand Down Expand Up @@ -906,6 +908,24 @@ export function useStripeTerminal(props?: Props) {
return response;
}, [_isInitialized]);

const _getPaymentStatus = useCallback(async () => {
if (!_isInitialized()) {
console.error(NOT_INITIALIZED_ERROR_MESSAGE);
throw Error(NOT_INITIALIZED_ERROR_MESSAGE);
}
const response = await getPaymentStatus();
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 @@ -1026,6 +1046,8 @@ export function useStripeTerminal(props?: Props) {
connectLocalMobileReader: _connectLocalMobileReader,
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 @@ -315,6 +315,8 @@ export type OfflineStatus = {

export type ReaderEvent = 'cardInserted' | 'cardRemoved';

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

/**
* @ignore
*/
Expand Down
2 changes: 1 addition & 1 deletion stripe-terminal-react-native.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ Pod::Spec.new do |s|
s.source_files = 'ios/**/*.{h,m,mm,swift}'

s.dependency 'React-Core'
s.dependency 'StripeTerminal', '~> 3.5.0'
s.dependency 'StripeTerminal', '~> 3.6.0'
end

0 comments on commit 017f170

Please sign in to comment.