From 4014f68ebd69540ae964c8c276a0d460ec4fdc38 Mon Sep 17 00:00:00 2001 From: Jason Divock Date: Tue, 15 Mar 2022 14:15:52 -0600 Subject: [PATCH 1/2] getListLocations -> getLocations --- .../StripeTerminalReactNativeModule.kt | 4 +- example/ios/Podfile.lock | 2 +- example/src/screens/LocationListScreen.tsx | 8 +- ios/Mappers.swift | 114 +++++++++--------- ios/StripeTerminalReactNative.m | 2 +- ios/StripeTerminalReactNative.swift | 12 +- src/StripeTerminalSdk.tsx | 4 +- src/functions.ts | 11 +- src/hooks/useStripeTerminal.tsx | 8 +- src/types/index.ts | 4 +- 10 files changed, 84 insertions(+), 85 deletions(-) diff --git a/android/src/main/java/com/stripeterminalreactnative/StripeTerminalReactNativeModule.kt b/android/src/main/java/com/stripeterminalreactnative/StripeTerminalReactNativeModule.kt index 7fb921cd..625d5345 100644 --- a/android/src/main/java/com/stripeterminalreactnative/StripeTerminalReactNativeModule.kt +++ b/android/src/main/java/com/stripeterminalreactnative/StripeTerminalReactNativeModule.kt @@ -555,7 +555,7 @@ class StripeTerminalReactNativeModule(reactContext: ReactApplicationContext) : @ReactMethod @Suppress("unused") - fun getListLocations(params: ReadableMap, promise: Promise) { + fun getLocations(params: ReadableMap, promise: Promise) { val listParameters = ListLocationsParameters.Builder() listParameters.endingBefore = getStringOr(params, "endingBefore") listParameters.startingAfter = getStringOr(params, "startingAfter") @@ -565,7 +565,7 @@ class StripeTerminalReactNativeModule(reactContext: ReactApplicationContext) : override fun onSuccess(locations: List, hasMore: Boolean) { val list = mapFromListLocations(locations) val result = WritableNativeMap() - result.putArray("locationsList", list) + result.putArray("locations", list) result.putBoolean("hasMore", hasMore) promise.resolve(result) } diff --git a/example/ios/Podfile.lock b/example/ios/Podfile.lock index 8515eda2..96663fa9 100644 --- a/example/ios/Podfile.lock +++ b/example/ios/Podfile.lock @@ -490,6 +490,6 @@ SPEC CHECKSUMS: StripeTerminal: 2c7d261881b0039693a6ba30c00e1e6133e6d393 Yoga: 32a18c0e845e185f4a2a66ec76e1fd1f958f22fa -PODFILE CHECKSUM: 617d5d5a75b2b359fc53061af7f2624d7349ec04 +PODFILE CHECKSUM: 7dc744410dfefabb790965a6dbea3cfd4138e31c COCOAPODS: 1.11.2 diff --git a/example/src/screens/LocationListScreen.tsx b/example/src/screens/LocationListScreen.tsx index 52e7d210..3a897feb 100644 --- a/example/src/screens/LocationListScreen.tsx +++ b/example/src/screens/LocationListScreen.tsx @@ -15,14 +15,14 @@ export default function LocationListScreen() { const navigation = useNavigation(); const { params } = useRoute>(); - const { getListLocations, loading } = useStripeTerminal(); + const { getLocations, loading } = useStripeTerminal(); const [list, setList] = useState([]); useEffect(() => { async function init() { - const { locationsList } = await getListLocations({ limit: 20 }); - if (locationsList) { - setList(locationsList); + const { locations } = await getLocations({ limit: 20 }); + if (locations) { + setList(locations); } } init(); diff --git a/ios/Mappers.swift b/ios/Mappers.swift index a3c20883..a00a67ea 100644 --- a/ios/Mappers.swift +++ b/ios/Mappers.swift @@ -3,15 +3,15 @@ import StripeTerminal class Mappers { class func mapFromReaders(_ readers: [Reader]) -> [NSDictionary] { var readersList: [NSDictionary] = [] - + for reader in readers { let result = mapFromReader(reader) readersList.append(result) } - + return readersList } - + class func mapFromReader(_ reader: Reader) -> NSDictionary { let result: NSDictionary = [ "label": reader.label ?? NSNull(), @@ -32,7 +32,7 @@ class Mappers { ] return result } - + class func mapFromLocationStatus(_ status: LocationStatus) -> String { switch status { case LocationStatus.notSet: return "notSet" @@ -41,7 +41,7 @@ class Mappers { default: return "unknown" } } - + class func mapFromReaderNetworkStatus(_ status: ReaderNetworkStatus) -> String { switch status { case ReaderNetworkStatus.offline: return "offline" @@ -49,7 +49,7 @@ class Mappers { default: return "unknown" } } - + class func mapFromBatteryStatus(_ status: BatteryStatus) -> String { switch status { case BatteryStatus.critical: return "critical" @@ -59,7 +59,7 @@ class Mappers { default: return "unknown" } } - + class func mapFromDeviceType(_ type: DeviceType) -> String { switch type { case DeviceType.chipper1X: return "chipper1X" @@ -72,21 +72,21 @@ class Mappers { default: return "unknown" } } - - + + class func mapToCartLineItem(_ cartLineItem: NSDictionary) -> CartLineItem? { guard let displayName = cartLineItem["displayName"] as? String else { return nil } guard let quantity = cartLineItem["quantity"] as? NSNumber else { return nil } guard let amount = cartLineItem["amount"] as? NSNumber else { return nil } - + let lineItem = CartLineItem(displayName: displayName, quantity: Int(truncating: quantity), amount: Int(truncating: amount)) - + return lineItem } - + class func mapToCartLineItems(_ cartLineItems: NSArray) -> [CartLineItem] { var items = [CartLineItem]() - + cartLineItems.forEach { if let item = $0 as? NSDictionary { if let lineItem = Mappers.mapToCartLineItem(item) { @@ -96,20 +96,20 @@ class Mappers { } return items } - - + + class func mapToDiscoveryMethod(_ discoveryMethod: String?) -> DiscoveryMethod { if let method = discoveryMethod { switch method { case "bluetoothProximity": return DiscoveryMethod.bluetoothProximity case "bluetoothScan": return DiscoveryMethod.bluetoothScan - case "internet": return DiscoveryMethod.internet + case "internet": return DiscoveryMethod.internet default: return DiscoveryMethod.internet } } - return DiscoveryMethod.internet + return DiscoveryMethod.internet } - + class func mapFromPaymentIntent(_ paymentIntent: PaymentIntent) -> NSDictionary { let result: NSDictionary = [ "amount": paymentIntent.amount, @@ -121,7 +121,7 @@ class Mappers { ] return result } - + class func mapFromSetupIntent(_ setupIntent: SetupIntent) -> NSDictionary { let result: NSDictionary = [ "id": setupIntent.stripeId, @@ -132,7 +132,7 @@ class Mappers { ] return result } - + class func mapFromSetupIntentUsage(_ usage: SetupIntentUsage) -> String { switch usage { case SetupIntentUsage.offSession: return "offSession" @@ -140,7 +140,7 @@ class Mappers { default: return "unknown" } } - + class func mapFromSetupAttempt(_ attempt: SetupAttempt?) -> NSDictionary? { guard let unwrappedAttempt = attempt else { return nil @@ -158,7 +158,7 @@ class Mappers { ] return result } - + class func mapFromSetupAttemptPaymentMethodDetails(_ details: SetupAttemptPaymentMethodDetails?) -> NSDictionary? { guard let unwrappedDetails = details else { return nil @@ -170,7 +170,7 @@ class Mappers { ] return result } - + class func mapFromSetupAttemptCardPresentDetails(_ details: SetupAttemptCardPresentDetails?) -> NSDictionary? { guard let unwrappedDetails = details else { return nil @@ -181,8 +181,8 @@ class Mappers { ] return result } - - + + class func mapFromSetupIntentStatus(_ status: SetupIntentStatus) -> String { switch status { case SetupIntentStatus.canceled: return "canceled" @@ -194,7 +194,7 @@ class Mappers { default: return "unknown" } } - + class func mapFromPaymentIntentStatus(_ status: PaymentIntentStatus) -> String { switch status { case PaymentIntentStatus.canceled: return "canceled" @@ -206,7 +206,7 @@ class Mappers { default: return "unknown" } } - + class func mapFromReaderDisplayMessage(_ displayMessage: ReaderDisplayMessage) -> String { switch displayMessage { case ReaderDisplayMessage.insertCard: return "insertCard" @@ -220,11 +220,11 @@ class Mappers { default: return "unknown" } } - + class func mapFromReaderInputOptions(_ inputOptions: ReaderInputOptions) -> NSMutableArray { let array = inputOptions.rawValue.bitComponents() let mappedOptions: NSMutableArray = [] - + array.forEach { item in switch item { case 0: return @@ -234,21 +234,21 @@ class Mappers { default: return } } - + return mappedOptions } - + class func mapFromCharges(_ charges: [Charge]) -> [NSDictionary] { var list: [NSDictionary] = [] - + for charge in charges { let result = mapFromCharge(charge) list.append(result) } - + return list } - + class func mapFromReaderSoftwareUpdate(_ update: ReaderSoftwareUpdate?) -> NSDictionary? { guard let unwrappedUpdate = update else { return nil @@ -260,7 +260,7 @@ class Mappers { ] return result } - + class func mapFromUpdateTimeEstimate(_ time: UpdateTimeEstimate) -> String { switch time { case UpdateTimeEstimate.estimate1To2Minutes: return "estimate1To2Minutes" @@ -270,21 +270,21 @@ class Mappers { default: return "unknown" } } - - - class func mapFromLocationsList(_ locationsList: [Location]) -> [NSDictionary] { + + + class func mapFromLocationsList(_ locations: [Location]) -> [NSDictionary] { var list: [NSDictionary] = [] - - for location in locationsList { + + for location in locations { let result = mapFromLocation(location) if let result = result { list.append(result) } } - + return list } - + class func mapFromLocation(_ location: Location?) -> NSDictionary? { guard let unwrappedLocation = location else { return nil @@ -297,7 +297,7 @@ class Mappers { ] return result } - + class func mapFromAddress(_ address: Address?) -> NSDictionary? { if let address = address { let result: NSDictionary = [ @@ -313,7 +313,7 @@ class Mappers { return nil } } - + class func mapFromCharge(_ charge: Charge) -> NSDictionary { let result: NSDictionary = [ "amount": charge.amount, @@ -324,7 +324,7 @@ class Mappers { ] return result } - + class func convertDateToUnixTimestamp(date: Date?) -> String? { if let date = date { let value = date.timeIntervalSince1970 * 1000.0 @@ -332,7 +332,7 @@ class Mappers { } return nil } - + class func mapToSimulateReaderUpdate(_ update: String) -> SimulateReaderUpdate { switch update { case "available": return SimulateReaderUpdate.available @@ -343,7 +343,7 @@ class Mappers { default: return SimulateReaderUpdate.none } } - + class func mapFromCardPresent(_ cardPresent: CardPresentDetails) -> NSDictionary { let result: NSDictionary = [ "last4": cardPresent.last4, @@ -361,7 +361,7 @@ class Mappers { ] return result } - + class func mapFromPaymentMethodDetailsType(_ type: PaymentMethodType) -> String { switch type { case PaymentMethodType.card: return "card" @@ -370,7 +370,7 @@ class Mappers { default: return "unknown" } } - + class func mapFromPaymentMethodDetails(_ paymentMethodDetails: PaymentMethodDetails) -> NSDictionary { var cardPresentMapped: NSDictionary? if let cardPresent = paymentMethodDetails.cardPresent{ @@ -380,7 +380,7 @@ class Mappers { if let interacPresent = paymentMethodDetails.interacPresent{ interacPresentMapped = mapFromCardPresent(interacPresent) } - + let result: NSDictionary = [ "type": mapFromPaymentMethodDetailsType(paymentMethodDetails.type), "cardPresent": cardPresentMapped ?? NSNull(), @@ -388,7 +388,7 @@ class Mappers { ] return result } - + class func mapFromRefund(_ refund: Refund) -> NSDictionary { var paymentMethodDetailsMapped: NSDictionary? if let paymentMethodDetails = refund.paymentMethodDetails{ @@ -408,7 +408,7 @@ class Mappers { ] return result } - + class func mapFromRefundStatus(_ type: RefundStatus) -> String { switch type { case RefundStatus.failed: return "failed" @@ -418,7 +418,7 @@ class Mappers { default: return "unknown" } } - + class func mapFromCardDetails(_ cardDetails: CardDetails) -> NSDictionary { let result: NSDictionary = [ "brand": cardDetails.brand, @@ -431,7 +431,7 @@ class Mappers { ] return result } - + class func mapFromPaymentMethod(_ paymentMethod: PaymentMethod) -> NSDictionary { let result: NSDictionary = [ "id": paymentMethod.stripeId, @@ -442,7 +442,7 @@ class Mappers { ] return result } - + class func mapFromPaymentStatus(_ paymentStatus: PaymentStatus) -> String { switch paymentStatus { case PaymentStatus.notReady: return "notReady" @@ -452,7 +452,7 @@ class Mappers { default: return "unknown" } } - + class func mapFromConnectionStatus(_ connectionStatus: ConnectionStatus) -> String { switch connectionStatus { case ConnectionStatus.connected: return "connected" @@ -475,7 +475,7 @@ extension UInt { init(bitComponents : [UInt]) { self = bitComponents.reduce(0, +) } - + func bitComponents() -> [UInt] { return (0 ..< 8*MemoryLayout.size).map( { 1 << $0 }).filter( { self & $0 != 0 } ) } diff --git a/ios/StripeTerminalReactNative.m b/ios/StripeTerminalReactNative.m index 2e313ca4..3dcec06e 100644 --- a/ios/StripeTerminalReactNative.m +++ b/ios/StripeTerminalReactNative.m @@ -62,7 +62,7 @@ @interface RCT_EXTERN_MODULE(StripeTerminalReactNative, RCTEventEmitter) ) RCT_EXTERN_METHOD( - getListLocations:(NSDictionary *)params + getLocations:(NSDictionary *)params resolver: (RCTPromiseResolveBlock)resolve rejecter: (RCTPromiseRejectBlock)reject ) diff --git a/ios/StripeTerminalReactNative.swift b/ios/StripeTerminalReactNative.swift index b8192438..bd2f1c8b 100644 --- a/ios/StripeTerminalReactNative.swift +++ b/ios/StripeTerminalReactNative.swift @@ -332,20 +332,20 @@ class StripeTerminalReactNative: RCTEventEmitter, DiscoveryDelegate, BluetoothRe } } - @objc(getListLocations:resolver:rejecter:) - func getListLocations(params: NSDictionary, resolver resolve: @escaping RCTPromiseResolveBlock, rejecter reject: @escaping RCTPromiseRejectBlock) { + @objc(getLocations:resolver:rejecter:) + func getLocations(params: NSDictionary, resolver resolve: @escaping RCTPromiseResolveBlock, rejecter reject: @escaping RCTPromiseRejectBlock) { let limit = params["limit"] as? NSNumber let endingBefore = params["endingBefore"] as? String let startingAfter = params["startingAfter"] as? String let listParameters = ListLocationsParameters(limit: limit, endingBefore: endingBefore, startingAfter: startingAfter) - Terminal.shared.listLocations(parameters: listParameters) { locationsList, hasMore, error in + Terminal.shared.listLocations(parameters: listParameters) { locations, hasMore, error in if let error = error { resolve(Errors.createError(code: CommonErrorType.Failed.rawValue, message: error.localizedDescription)) - } else if let locationsList = locationsList { - let list = Mappers.mapFromLocationsList(locationsList) - resolve(["locationsList": list, "hasMore": hasMore]) + } else if let locations = locations { + let list = Mappers.mapFromLocationsList(locations) + resolve(["locations": list, "hasMore": hasMore]) } } } diff --git a/src/StripeTerminalSdk.tsx b/src/StripeTerminalSdk.tsx index 7a0731ec..7447564f 100644 --- a/src/StripeTerminalSdk.tsx +++ b/src/StripeTerminalSdk.tsx @@ -92,9 +92,7 @@ type StripeTerminalSdkType = { // Cancel Setup Intent cancelSetupIntent(paymentIntentId: string): Promise; // List of locations belonging to the merchant - getListLocations( - params: ListLocationsParams - ): Promise; + getLocations(params: ListLocationsParams): Promise; // Confirm Setup Intent confirmSetupIntent(paymentIntentId: string): Promise; simulateReaderUpdate(update: Reader.SimulateUpdateType): Promise; diff --git a/src/functions.ts b/src/functions.ts index 4e72f577..a4ef18ae 100644 --- a/src/functions.ts +++ b/src/functions.ts @@ -253,22 +253,23 @@ export async function retrievePaymentIntent( } } -export async function getListLocations( +export async function getLocations( params: ListLocationsParams ): Promise { try { - const { error, locationsList, hasMore } = - await StripeTerminalSdk.getListLocations(params); + const { error, locations, hasMore } = await StripeTerminalSdk.getLocations( + params + ); if (error) { return { error, - locationsList: undefined, + locations: undefined, hasMore: undefined, }; } return { - locationsList: locationsList!, + locations: locations!, hasMore: hasMore!, error: undefined, }; diff --git a/src/hooks/useStripeTerminal.tsx b/src/hooks/useStripeTerminal.tsx index 9e921ebc..cc5d402a 100644 --- a/src/hooks/useStripeTerminal.tsx +++ b/src/hooks/useStripeTerminal.tsx @@ -24,7 +24,7 @@ import { createPaymentIntent, collectPaymentMethod, retrievePaymentIntent, - getListLocations, + getLocations, processPayment, createSetupIntent, cancelPaymentIntent, @@ -411,11 +411,11 @@ export function useStripeTerminal(props?: Props) { [setLoading] ); - const _getListLocations = useCallback( + const _getLocations = useCallback( async (params: ListLocationsParams) => { setLoading(true); - const response = await getListLocations(params); + const response = await getLocations(params); setLoading(false); @@ -653,7 +653,7 @@ export function useStripeTerminal(props?: Props) { createPaymentIntent: _createPaymentIntent, collectPaymentMethod: _collectPaymentMethod, retrievePaymentIntent: _retrievePaymentIntent, - getListLocations: _getListLocations, + getLocations: _getLocations, processPayment: _processPayment, createSetupIntent: _createSetupIntent, cancelPaymentIntent: _cancelPaymentIntent, diff --git a/src/types/index.ts b/src/types/index.ts index 463185d7..ac81377a 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -174,12 +174,12 @@ export type SetupIntentResultType = export type ListLocationsResultType = | { - locationsList: Location[]; + locations: Location[]; hasMore: boolean; error?: undefined; } | { - locationsList?: undefined; + locations?: undefined; hasMore?: undefined; error: StripeError; }; From be1e0888ef7e9d6b94ec26ff774c64270f1e56c6 Mon Sep 17 00:00:00 2001 From: Jason Divock Date: Tue, 15 Mar 2022 14:20:56 -0600 Subject: [PATCH 2/2] rename params as well --- src/StripeTerminalSdk.tsx | 6 +++--- src/functions.ts | 8 ++++---- src/hooks/useStripeTerminal.tsx | 4 ++-- src/types/index.ts | 4 ++-- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/StripeTerminalSdk.tsx b/src/StripeTerminalSdk.tsx index 7447564f..96adf09a 100644 --- a/src/StripeTerminalSdk.tsx +++ b/src/StripeTerminalSdk.tsx @@ -18,8 +18,8 @@ import type { SetupIntentResultType, CreateSetupIntentParams, ClearReaderDisplayResultType, - ListLocationsParams, - ListLocationsResultType, + GetLocationsParams, + GetLocationsResultType, RefundParams, CollectRefundPaymentMethodType, ProcessRefundResultType, @@ -92,7 +92,7 @@ type StripeTerminalSdkType = { // Cancel Setup Intent cancelSetupIntent(paymentIntentId: string): Promise; // List of locations belonging to the merchant - getLocations(params: ListLocationsParams): Promise; + getLocations(params: GetLocationsParams): Promise; // Confirm Setup Intent confirmSetupIntent(paymentIntentId: string): Promise; simulateReaderUpdate(update: Reader.SimulateUpdateType): Promise; diff --git a/src/functions.ts b/src/functions.ts index a4ef18ae..6ca2314b 100644 --- a/src/functions.ts +++ b/src/functions.ts @@ -13,8 +13,8 @@ import type { CreatePaymentIntentParams, CollectSetupIntentPaymentMethodParams, PaymentIntentResultType, - ListLocationsParams, - ListLocationsResultType, + GetLocationsParams, + GetLocationsResultType, StripeError, Cart, CreateSetupIntentParams, @@ -254,8 +254,8 @@ export async function retrievePaymentIntent( } export async function getLocations( - params: ListLocationsParams -): Promise { + params: GetLocationsParams +): Promise { try { const { error, locations, hasMore } = await StripeTerminalSdk.getLocations( params diff --git a/src/hooks/useStripeTerminal.tsx b/src/hooks/useStripeTerminal.tsx index cc5d402a..98f477d0 100644 --- a/src/hooks/useStripeTerminal.tsx +++ b/src/hooks/useStripeTerminal.tsx @@ -6,7 +6,7 @@ import type { ConnectInternetReaderParams, CreatePaymentIntentParams, ConnectBluetoothReaderParams, - ListLocationsParams, + GetLocationsParams, Cart, CreateSetupIntentParams, CollectSetupIntentPaymentMethodParams, @@ -412,7 +412,7 @@ export function useStripeTerminal(props?: Props) { ); const _getLocations = useCallback( - async (params: ListLocationsParams) => { + async (params: GetLocationsParams) => { setLoading(true); const response = await getLocations(params); diff --git a/src/types/index.ts b/src/types/index.ts index ac81377a..7971acdd 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -26,7 +26,7 @@ export type DiscoverReadersParams = { discoveryMethod: Reader.DiscoveryMethod; }; -export type ListLocationsParams = { +export type GetLocationsParams = { limit?: number; endingBefore?: string; startingAfter?: string; @@ -172,7 +172,7 @@ export type SetupIntentResultType = error: StripeError; }; -export type ListLocationsResultType = +export type GetLocationsResultType = | { locations: Location[]; hasMore: boolean;