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

sPOS offline mode support #649

Merged
merged 20 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from 16 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
48 changes: 46 additions & 2 deletions android/src/main/java/com/stripeterminalreactnative/Mappers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import com.facebook.react.bridge.WritableArray
import com.facebook.react.bridge.WritableMap
import com.facebook.react.bridge.WritableNativeArray
import com.stripe.stripeterminal.external.CollectInputs
import com.stripe.stripeterminal.external.OfflineMode
import com.stripe.stripeterminal.external.models.Address
import com.stripe.stripeterminal.external.models.AmountDetails
import com.stripe.stripeterminal.external.models.CardDetails
import com.stripe.stripeterminal.external.models.CardPresentDetails
import com.stripe.stripeterminal.external.models.CartLineItem
Expand All @@ -20,6 +22,8 @@ import com.stripe.stripeterminal.external.models.Location
import com.stripe.stripeterminal.external.models.LocationStatus
import com.stripe.stripeterminal.external.models.NetworkStatus
import com.stripe.stripeterminal.external.models.NumericResult
import com.stripe.stripeterminal.external.models.OfflineCardPresentDetails
import com.stripe.stripeterminal.external.models.OfflineDetails
import com.stripe.stripeterminal.external.models.OfflineStatus
import com.stripe.stripeterminal.external.models.PaymentIntent
import com.stripe.stripeterminal.external.models.PaymentIntentStatus
Expand Down Expand Up @@ -141,6 +145,8 @@ internal fun mapFromDeviceType(type: DeviceType): String {
DeviceType.WISEPAD_3S -> "wisePad3s"
DeviceType.WISEPOS_E_DEVKIT -> "wisePosEDevkit"
DeviceType.STRIPE_S700_DEVKIT -> "stripeS700Devkit"
DeviceType.STRIPE_S700 -> "stripeS700"
Copy link
Collaborator

Choose a reason for hiding this comment

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

This case is already covered. Suggest sorting this list alphabetically to make dupes easier to find.

DeviceType.ETNA -> "etna"
}
}

Expand All @@ -163,6 +169,7 @@ internal fun mapToDiscoveryMethod(method: String?): DiscoveryMethod? {
}
}

@OptIn(OfflineMode::class)
internal fun mapFromPaymentIntent(paymentIntent: PaymentIntent, uuid: String): ReadableMap = nativeMapOf {
putInt("amount", paymentIntent.amount.toInt())
putString("currency", paymentIntent.currency)
Expand All @@ -173,6 +180,7 @@ internal fun mapFromPaymentIntent(paymentIntent: PaymentIntent, uuid: String): R
putString("created", convertToUnixTimestamp(paymentIntent.created))
putString("sdkUuid", uuid)
putString("paymentMethodId", paymentIntent.paymentMethodId)
putMap("offlineDetails", mapFromOfflineDetails(paymentIntent?.offlineDetails))
}

internal fun mapFromSetupIntent(setupIntent: SetupIntent, uuid: String): ReadableMap = nativeMapOf {
Expand Down Expand Up @@ -517,6 +525,42 @@ private fun mapFromCardPresentDetails(cardPresentDetails: CardPresentDetails?):
)
}

private fun mapFromOfflineDetails(offlineDetails: OfflineDetails?): ReadableMap? =
offlineDetails?.let {
nativeMapOf {
putString("storedAt", offlineDetails.storedAt.toString())
putBoolean("requiresUpload", offlineDetails.requiresUpload)
putMap(
"cardPresentDetails",
mapFromOfflineCardPresentDetails(offlineDetails.cardPresentDetails)
)
putMap("amountDetails", mapFromAmountDetails(offlineDetails.amountDetails))
}
}

private fun mapFromAmountDetails(amountDetails: AmountDetails?): ReadableMap? =
amountDetails?.let {
nativeMapOf {
putMap("tip", nativeMapOf { putInt("amount", amountDetails.tip?.amount?.toInt() ?: 0) })
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: we may want putIntOrNull here because "0" is different from "tip was skipped".

}
}

private fun mapFromOfflineCardPresentDetails(offlineCardPresentDetails: OfflineCardPresentDetails?): ReadableMap? =
offlineCardPresentDetails?.let {
nativeMapOf {
putString("brand", offlineCardPresentDetails?.brand)
putString("cardholderName", offlineCardPresentDetails?.cardholderName)
putIntOrNull(this, "expMonth", offlineCardPresentDetails?.expMonth)
putIntOrNull(this, "expYear", offlineCardPresentDetails?.expYear)
putString("last4", offlineCardPresentDetails?.last4)
putString("readMethod", offlineCardPresentDetails?.readMethod)
putMap(
"receiptDetails",
mapFromReceiptDetails(offlineCardPresentDetails?.receiptDetails)
)
}
}

internal fun mapFromWallet(wallet: Wallet?): ReadableMap =
nativeMapOf {
putString("type", wallet?.type)
Expand All @@ -537,8 +581,8 @@ fun mapFromReceiptDetails(receiptDetails: ReceiptDetails?): ReadableMap =
putString("authorizationResponseCode", receiptDetails?.authorizationResponseCode)
putString("cvm", receiptDetails?.cvm)
putString("dedicatedFileName", receiptDetails?.dedicatedFileName)
putString("tsi", receiptDetails?.tsi)
putString("tvr", receiptDetails?.tvr)
putString("transactionStatusInformation", receiptDetails?.tsi)
putString("terminalVerificationResult", receiptDetails?.tvr)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Reminder to add these name changes to a CHANGELOG.

}

internal fun mapFromNetworkStatus(status: NetworkStatus): String {
Expand Down
1 change: 1 addition & 0 deletions dev-app/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export type RouteParamList = {
CollectCardPayment: {
simulated: boolean;
discoveryMethod: Reader.DiscoveryMethod;
deviceType: Reader.DeviceType;
};
RefundPayment: {
simulated: boolean;
Expand Down
23 changes: 18 additions & 5 deletions dev-app/src/screens/CollectCardPaymentScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export default function CollectCardPaymentScreen() {
const [tipEligibleAmount, setTipEligibleAmount] = useState('');
const { params } =
useRoute<RouteProp<RouteParamList, 'CollectCardPayment'>>();
const { simulated, discoveryMethod } = params;
const { simulated, discoveryMethod, deviceType } = params;
const { addLogs, clearLogs, setCancel } = useContext(LogContext);
const navigation = useNavigation();

Expand Down Expand Up @@ -165,7 +165,7 @@ export default function CollectCardPaymentScreen() {
};
let paymentIntent: PaymentIntent.Type | undefined;
let paymentIntentError: StripeError<CommonError> | undefined;
if (discoveryMethod === 'internet') {
if (deviceType === 'verifoneP400') {
const resp = await api.createPaymentIntent({
amount: Number(inputValues.amount),
currency: inputValues.currency,
Expand Down Expand Up @@ -205,21 +205,34 @@ export default function CollectCardPaymentScreen() {
paymentIntentError = response.error;
} else {
const offlineStatus = await getOfflineStatus();
let storedPaymentAmount = 0;
let sdkStoredPaymentAmount = 0;
for (let currency in offlineStatus.sdk.offlinePaymentAmountsByCurrency) {
if (currency === inputValues.currency) {
storedPaymentAmount =
sdkStoredPaymentAmount =
offlineStatus.sdk.offlinePaymentAmountsByCurrency[currency];
}
}
let readerStoredPaymentAmount = 0;
if (offlineStatus.reader) {
for (let currency in offlineStatus.reader
.offlinePaymentAmountsByCurrency) {
if (currency === inputValues.currency) {
readerStoredPaymentAmount =
offlineStatus.reader.offlinePaymentAmountsByCurrency[currency];
}
}
}
if (
Number(inputValues.amount) >
Number(inputValues.offlineModeTransactionLimit) ||
storedPaymentAmount >
sdkStoredPaymentAmount >
Number(inputValues.offlineModeStoredTransactionLimit) ||
readerStoredPaymentAmount >
Number(inputValues.offlineModeStoredTransactionLimit)
) {
inputValues.offlineBehavior = 'require_online';
}

const response = await createPaymentIntent({
amount: Number(inputValues.amount),
currency: inputValues.currency,
Expand Down
45 changes: 40 additions & 5 deletions dev-app/src/screens/DatabaseScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,43 @@ export default function DatabaseScreen() {

return (
<ScrollView style={styles.container}>
<List bolded={false} topSpacing={false} title="PUBLIC INTERFACE SUMMARY">
<List bolded={false} topSpacing={false} title="READER SUMMARY">
{offlinePaymentStatus &&
offlinePaymentStatus.reader &&
offlinePaymentStatus.reader.offlinePaymentsCount > 0 ? (
Object.keys(
offlinePaymentStatus.reader.offlinePaymentAmountsByCurrency
).map((key) => (
<ListItem
title={
getCurrencySymbols(key) +
' ' +
(
Number(
offlinePaymentStatus.reader!
.offlinePaymentAmountsByCurrency[key]
) / 100
).toFixed(2)
}
/>
))
) : (
<></>
)}
</List>
<Text style={styles.infoText}>
{' '}
{String(
offlinePaymentStatus &&
offlinePaymentStatus.reader &&
offlinePaymentStatus.reader.offlinePaymentsCount
? offlinePaymentStatus.reader.offlinePaymentsCount
: 0
) +
' payment intent(s) for ' +
account?.settings?.dashboard.display_name}{' '}
</Text>
<List bolded={false} topSpacing={false} title="SDK SUMMARY">
{offlinePaymentStatus &&
offlinePaymentStatus.sdk.offlinePaymentsCount > 0 ? (
Object.keys(
Expand All @@ -55,9 +91,8 @@ export default function DatabaseScreen() {
' ' +
(
Number(
offlinePaymentStatus.sdk.offlinePaymentAmountsByCurrency[
key
]
offlinePaymentStatus.reader!
Copy link
Collaborator

Choose a reason for hiding this comment

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

Shouldn't this be sdk?

.offlinePaymentAmountsByCurrency[key]
) / 100
).toFixed(2)
}
Expand All @@ -70,7 +105,7 @@ export default function DatabaseScreen() {
<Text style={styles.infoText}>
{' '}
{String(
offlinePaymentStatus
offlinePaymentStatus && offlinePaymentStatus.sdk
? offlinePaymentStatus.sdk.offlinePaymentsCount
: 0
) +
Expand Down
17 changes: 10 additions & 7 deletions dev-app/src/screens/HomeScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,15 @@ export default function HomeScreen() {
}, 3000);
},
onDidForwardPaymentIntent(paymentIntent, error) {
let toastMsg =
'Payment Intent ' +
paymentIntent.id +
' forwarded. ErrorCode' +
error?.code +
'. ErrorMsg = ' +
error?.message;
let toastMsg = 'Payment Intent ' + paymentIntent.id + ' forwarded. ';
if (error) {
toastMsg +
'ErrorCode = ' +
error.code +
'. ErrorMsg = ' +
error.message;
}
console.log(toastMsg);
let toast = Toast.show(toastMsg, {
duration: Toast.durations.LONG,
position: Toast.positions.BOTTOM,
Expand Down Expand Up @@ -140,6 +142,7 @@ export default function HomeScreen() {
navigation.navigate('CollectCardPaymentScreen', {
simulated,
discoveryMethod,
online,
});
}}
/>
Expand Down
70 changes: 70 additions & 0 deletions ios/Mappers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ class Mappers {
case DeviceType.wisePosE: return "wisePosE"
case DeviceType.wisePosEDevKit: return "wisePosEDevkit"
case DeviceType.stripeS700DevKit: return "stripeS700Devkit"
case DeviceType.stripeS700: return "stripeS700"
case DeviceType.appleBuiltIn: return "appleBuiltIn"
case DeviceType.etna: return "etna"
default: return "unknown"
}
}
Expand Down Expand Up @@ -138,6 +140,10 @@ class Mappers {


class func mapFromPaymentIntent(_ paymentIntent: PaymentIntent, uuid: String) -> NSDictionary {
var offlineDetailsMap: NSDictionary?
if let offlineDetails = paymentIntent.offlineDetails {
offlineDetailsMap = mapFromOfflineDetails(offlineDetails)
}
let result: NSDictionary = [
"amount": paymentIntent.amount,
"charges": mapFromCharges(paymentIntent.charges),
Expand All @@ -147,6 +153,7 @@ class Mappers {
"id": paymentIntent.stripeId,
"sdkUuid": uuid,
"paymentMethodId": paymentIntent.paymentMethodId,
"offlineDetails": offlineDetailsMap ?? NSNull()
]
return result
}
Expand Down Expand Up @@ -420,6 +427,58 @@ class Mappers {
]
return result
}

class func mapFromOfflineDetails(_ offlineDetails: OfflineDetails) -> NSDictionary {
var offlineCardPresentDetails: NSDictionary?
if let cardPresentDetails = offlineDetails.cardPresentDetails {
offlineCardPresentDetails = mapFromOfflineCardPresentDetails(cardPresentDetails)
}

var amountDetails: NSDictionary?
if let offlineAmountDetails = offlineDetails.amountDetails {
amountDetails = mapFromAmountDetails(offlineAmountDetails)
}

let result: NSDictionary = [
"storedAt": convertDateToUnixTimestamp(date: offlineDetails.collectedAt) ?? NSNull(),
"requiresUpload": offlineDetails.requiresUpload,
"cardPresentDetails": offlineCardPresentDetails ?? NSNull(),
"amountDetails": amountDetails ?? NSNull()
]

return result
}

class func mapFromAmountDetails(_ amountDetails: SCPAmountDetails?) -> NSDictionary {
let amount: NSDictionary = [
"amount": amountDetails?.tip ?? NSNull(),
]

let result: NSDictionary = [
"tip": amount
]

return result
}

class func mapFromOfflineCardPresentDetails(_ offlineCardPresentDetails: OfflineCardPresentDetails) -> NSDictionary {
var receiptDetailsMap: NSDictionary?
if let receiptDetails = offlineCardPresentDetails.receiptDetails {
receiptDetailsMap = mapFromReceiptDetails(receiptDetails)
}

let result: NSDictionary = [
"brand": offlineCardPresentDetails.brand,
"cardholderName": offlineCardPresentDetails.cardholderName ?? NSNull(),
"expMonth": offlineCardPresentDetails.expMonth,
"expYear": offlineCardPresentDetails.expYear,
"last4": offlineCardPresentDetails.last4 ?? NSNull(),
"readMethod": mapFromReadMethod(offlineCardPresentDetails.readMethod),
"receiptDetails": receiptDetailsMap ?? NSNull()
]

return result
}

class func mapFromCardPresentDetailsWallet(_ wallet: SCPWallet) -> NSDictionary {
let result: NSDictionary = [
Expand Down Expand Up @@ -639,6 +698,17 @@ class Mappers {
}
}

class func mapFromReadMethod(_ readMethod: SCPReadMethod) -> String {
switch readMethod {
case SCPReadMethod.contactEMV: return "contactEMV"
case SCPReadMethod.contactlessEMV: return "contactlessEMV"
case SCPReadMethod.contactlessMagstripeMode: return "contactlessMagstripeMode"
case SCPReadMethod.magneticStripeFallback: return "magneticStripeFallback"
case SCPReadMethod.magneticStripeTrack2: return "magneticStripeTrack2"
default: return "unknown"
}
}

class func mapFromCollectInputs(_ results: [CollectInputsResult]) -> NSDictionary {
var collectInputResults: [String : Any] = [:]
for result in results {
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useStripeTerminal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -869,7 +869,7 @@ export function useStripeTerminal(props?: Props) {
throw Error(NOT_INITIALIZED_ERROR_MESSAGE);
}
const response = await getOfflineStatus();
if (response.reader?.networkStatus) {
if (!response.reader?.networkStatus) {
response.reader = undefined;
}
return response;
Expand Down
3 changes: 2 additions & 1 deletion src/types/PaymentIntent.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Charge, PaymentMethod } from './';
import type { Charge, OfflineDetails, PaymentMethod } from './';

export namespace PaymentIntent {
export interface Type {
Expand All @@ -11,6 +11,7 @@ export namespace PaymentIntent {
sdkUuid: string;
paymentMethodId: string;
paymentMethod: PaymentMethod.Type;
offlineDetails: OfflineDetails;
}

export type Status =
Expand Down
4 changes: 3 additions & 1 deletion src/types/Reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,10 @@ export namespace Reader {
| 'wisePad3s'
| 'wisePadEDevkit'
| 'stripeS700Devkit'
| 'stripeS700'
| 'cotsDevice'
| 'appleBuiltIn';
| 'appleBuiltIn'
| 'etna';

export type InputOptions = 'insertCard' | 'swipeCard' | 'tapCard';

Expand Down
Loading