Skip to content

Commit

Permalink
fix issues mentioned in the comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
ianlin-bbpos committed Nov 20, 2023
1 parent c7ee38d commit 3c12b0b
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.stripeterminalreactnative.ReactNativeConstants
import com.stripeterminalreactnative.createError
import com.stripeterminalreactnative.mapFromOfflineStatus
import com.stripeterminalreactnative.mapFromPaymentIntent
import com.stripeterminalreactnative.nativeMapOf

@OptIn(OfflineMode::class)
class RNOfflineListener(
Expand All @@ -25,6 +26,10 @@ class RNOfflineListener(
override fun onPaymentIntentForwarded(paymentIntent: PaymentIntent, e: TerminalException?) {
context.sendEvent(ReactNativeConstants.FORWARD_PAYMENT_INTENT.listenerName) {
putMap("result", mapFromPaymentIntent(paymentIntent, ""))
putMap("error", nativeMapOf {
putString("code", e?.errorCode.toString())
putString("message", "hahahaha")
})
}
}

Expand Down
71 changes: 17 additions & 54 deletions dev-app/src/screens/CollectCardPaymentScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -200,23 +200,17 @@ export default function CollectCardPaymentScreen() {
paymentIntentError = response.error;
} else {
const offlineStatus = await getOfflineStatus();
let paymentAmountsByCurrency = 0;
let storedPaymentAmount = 0;
for (let currency in offlineStatus.sdk.offlinePaymentAmountsByCurrency) {
if (currency === inputValues.currency) {
paymentAmountsByCurrency =
storedPaymentAmount =
offlineStatus.sdk.offlinePaymentAmountsByCurrency[currency];
}
}
console.log(
'currency = ' +
inputValues.currency +
' paymentAmountsByCurrency = ' +
paymentAmountsByCurrency
);
if (
Number(inputValues.amount) >
Number(inputValues.offlineModeTransactionLimit) ||
paymentAmountsByCurrency >
storedPaymentAmount >
Number(inputValues.offlineModeStoredTransactionLimit)
) {
inputValues.offlineBehavior = 'require_online';
Expand Down Expand Up @@ -392,52 +386,21 @@ export default function CollectCardPaymentScreen() {
return;
}

if (!paymentIntent) {
addLogs({
name: 'Confirm Payment Intent',
events: [
{
name: 'Failed',
description: 'terminal.confirmPaymentIntent',
metadata: {
errorCode: 'no_code',
errorMessage: 'no payment intent id returned!',
},
},
],
});
return;
}

if (paymentIntent.id) {
addLogs({
name: 'Confirm Payment Intent',
events: [
{
name: 'Processed',
description: 'terminal.confirmPaymentIntent',
metadata: {
paymententIntentId: paymentIntent.id,
chargeId: paymentIntent.charges[0].id,
},
},
],
});
} else {
addLogs({
name: 'Confirm Payment Intent',
events: [
{
name: 'Processed',
description: 'terminal.confirmPaymentIntent',
metadata: {
paymententIntentId: '',
chargeId: '',
},
addLogs({
name: 'Confirm Payment Intent',
events: [
{
name: 'Confirmed',
description: 'terminal.confirmPaymentIntent',
metadata: {
paymententIntentId: paymentIntent.id ? paymentIntent.id : 'null',
chargeId: paymentIntent?.charges[0]?.id
? paymentIntent.charges[0].id
: 'null',
},
],
});
}
},
],
});

// Set last successful charge Id in context for refunding later
if (paymentIntent?.charges[0]?.id) {
Expand Down
1 change: 0 additions & 1 deletion dev-app/src/screens/DatabaseScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ export default function DatabaseScreen() {
];
const { getOfflineStatus } = useStripeTerminal();
function getCurrencySymbols(currency: string): string {
console.log('getCurrencySymbols = ' + currency);
let currencySymbol = '$';
currencySymbols.map((a) => {
if (currency === a.value) {
Expand Down
29 changes: 16 additions & 13 deletions dev-app/src/screens/HomeScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,22 @@ export default function HomeScreen() {
Toast.hide(toast);
}, 3000);
},
onDidForwardPaymentIntent(paymentIntent) {
console.log('onDidForwardPaymentIntent = ' + paymentIntent.id);
let toast = Toast.show(
'Payment Intent ' + paymentIntent.id + ' forwarded',
{
duration: Toast.durations.LONG,
position: Toast.positions.BOTTOM,
shadow: true,
animation: true,
hideOnPress: true,
delay: 0,
}
);
onDidForwardPaymentIntent(paymentIntent, error) {
let toastMsg =
'Payment Intent ' +
paymentIntent.id +
' forwarded. ErrorCode' +
error.code +
'. ErrorMsg = ' +
error.message;
let toast = Toast.show(toastMsg, {
duration: Toast.durations.LONG,
position: Toast.positions.BOTTOM,
shadow: true,
animation: true,
hideOnPress: true,
delay: 0,
});

setTimeout(function () {
Toast.hide(toast);
Expand Down
6 changes: 6 additions & 0 deletions src/__tests__/__snapshots__/index.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
exports[`index.ts Public API snapshot ensure there are no unexpected changes to our public exports 1`] = `
Object {
"CHANGE_CONNECTION_STATUS": "CHANGE_CONNECTION_STATUS",
"CHANGE_OFFLINE_STATUS": undefined,
"CHANGE_PAYMENT_STATUS": "CHANGE_PAYMENT_STATUS",
"CommonError": Object {
"Canceled": "Canceled",
Expand All @@ -12,12 +13,16 @@ Object {
"FETCH_TOKEN_PROVIDER": "FETCH_TOKEN_PROVIDER",
"FINISH_DISCOVERING_READERS": "FINISH_DISCOVERING_READERS",
"FINISH_INSTALLING_UPDATE": "FINISH_INSTALLING_UPDATE",
"FORWARD_PAYMENT_INTENT": undefined,
"PaymentIntent": Object {},
"PaymentMethod": Object {
"Android": Object {},
"IOS": Object {},
},
"READER_RECONNECT_FAIL": undefined,
"READER_RECONNECT_SUCCEED": undefined,
"REPORT_AVAILABLE_UPDATE": "REPORT_AVAILABLE_UPDATE",
"REPORT_FORWARDING_ERROR": undefined,
"REPORT_UNEXPECTED_READER_DISCONNECT": "REPORT_UNEXPECTED_READER_DISCONNECT",
"REPORT_UPDATE_PROGRESS": "REPORT_UPDATE_PROGRESS",
"REQUEST_READER_DISPLAY_MESSAGE": "REQUEST_READER_DISPLAY_MESSAGE",
Expand All @@ -31,6 +36,7 @@ Object {
"IOS": Object {},
},
"START_INSTALLING_UPDATE": "START_INSTALLING_UPDATE",
"START_READER_RECONNECT": undefined,
"SetupIntent": Object {
"Android": Object {},
"IOS": Object {},
Expand Down
4 changes: 2 additions & 2 deletions src/components/StripeTerminalProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,9 @@ export function StripeTerminalProvider({
);

const didForwardPaymentIntent = useCallback(
({ result }: EventResult<PaymentIntent.Type>) => {
({ result, error }: { result: PaymentIntent.Type; error: StripeError }) => {
log('didForwardPaymentIntent');
emitter?.emit(FORWARD_PAYMENT_INTENT, result);
emitter?.emit(FORWARD_PAYMENT_INTENT, result, error);
},
[log]
);
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/useStripeTerminal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,8 @@ export function useStripeTerminal(props?: Props) {
);

const didForwardPaymentIntent = useCallback(
({ result }: EventResult<PaymentIntent.Type>) => {
onDidForwardPaymentIntent?.(result);
({ result, error }: { result: PaymentIntent.Type; error: StripeError }) => {
onDidForwardPaymentIntent?.(result, error);
},
[onDidForwardPaymentIntent]
);
Expand Down
6 changes: 6 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ export {
REPORT_UPDATE_PROGRESS,
START_INSTALLING_UPDATE,
UPDATE_DISCOVERED_READERS,
START_READER_RECONNECT,
READER_RECONNECT_SUCCEED,
READER_RECONNECT_FAIL,
CHANGE_OFFLINE_STATUS,
FORWARD_PAYMENT_INTENT,
REPORT_FORWARDING_ERROR,
} from './hooks/useStripeTerminal';

// components
Expand Down
5 changes: 4 additions & 1 deletion src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,10 @@ export type UserCallbacks = {
onDidFailReaderReconnect?(): void;

onDidChangeOfflineStatus?(status: OfflineStatus): void;
onDidForwardPaymentIntent?(paymentIntent: PaymentIntent.Type): void;
onDidForwardPaymentIntent?(
paymentIntent: PaymentIntent.Type,
error: StripeError
): void;
onDidForwardingFailure?(error?: StripeError): void;
};

Expand Down

0 comments on commit 3c12b0b

Please sign in to comment.