Skip to content

Commit

Permalink
Add support for dynamic currency conversion (beta) (#700)
Browse files Browse the repository at this point in the history
* Add support for dynamic currency conversion (beta)

* fix lint issues

* move dcc option into transaction features

* tie dcc to update pi in example app
  • Loading branch information
henryx-stripe authored May 21, 2024
1 parent 411c93c commit b49645c
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,11 @@ class StripeTerminalReactNativeModule(reactContext: ReactApplicationContext) :
getBoolean(params, "enableCustomerCancellation")
)
}
if (params.hasKey("requestDynamicCurrencyConversion")) {
configBuilder.setRequestDynamicCurrencyConversion(
getBoolean(params, "requestDynamicCurrencyConversion")
)
}
val config = configBuilder.build()

collectPaymentMethodCancelable = terminal.collectPaymentMethod(
Expand Down
20 changes: 19 additions & 1 deletion dev-app/src/screens/CollectCardPaymentScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export default function CollectCardPaymentScreen() {
useState(false);
const [enableCustomerCancellation, setEnableCustomerCancellation] =
useState(false);
const [requestDcc, setRequestDcc] = useState(false);
const [tipEligibleAmount, setTipEligibleAmount] = useState('');
const { params } =
useRoute<RouteProp<RouteParamList, 'CollectCardPayment'>>();
Expand Down Expand Up @@ -330,6 +331,7 @@ export default function CollectCardPaymentScreen() {
: undefined,
updatePaymentIntent: enableUpdatePaymentIntent,
enableCustomerCancellation: enableCustomerCancellation,
requestDynamicCurrencyConversion: requestDcc,
});

if (error) {
Expand Down Expand Up @@ -707,7 +709,12 @@ export default function CollectCardPaymentScreen() {
<Switch
testID="enable-update-paymentIntent"
value={enableUpdatePaymentIntent}
onValueChange={(value) => setEnableUpdatePaymentIntent(value)}
onValueChange={(value) => {
setEnableUpdatePaymentIntent(value);
if (!value) {
setRequestDcc(false);
}
}}
/>
}
/>
Expand All @@ -725,6 +732,17 @@ export default function CollectCardPaymentScreen() {
/>
}
/>
<ListItem
title="Request DCC (requires Update PaymentIntent)"
rightElement={
<Switch
disabled={!enableUpdatePaymentIntent}
testID="request-dynamic-currency-conversion"
value={requestDcc}
onValueChange={(value) => setRequestDcc(value)}
/>
}
/>
</List>
)}

Expand Down
18 changes: 10 additions & 8 deletions ios/StripeTerminalReactNative.swift
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ class StripeTerminalReactNative: RCTEventEmitter, DiscoveryDelegate, BluetoothRe
let simulated = params["simulated"] as? Bool
let discoveryMethod = params["discoveryMethod"] as? String
let timeout = params["timeout"] as? UInt ?? 0

let config: DiscoveryConfiguration
do {
config = try Mappers.mapToDiscoveryConfiguration(discoveryMethod, simulated: simulated ?? false, timeout: timeout)
Expand Down Expand Up @@ -517,11 +517,13 @@ class StripeTerminalReactNative: RCTEventEmitter, DiscoveryDelegate, BluetoothRe
let skipTipping = params["skipTipping"] as? Bool ?? false
let updatePaymentIntent = params["updatePaymentIntent"] as? Bool ?? false
let enableCustomerCancellation = params["enableCustomerCancellation"] as? Bool ?? false
let requestDynamicCurrencyConversion = params["requestDynamicCurrencyConversion"] as? Bool ?? false

let collectConfigBuilder = CollectConfigurationBuilder()
.setSkipTipping(skipTipping)
.setUpdatePaymentIntent(updatePaymentIntent)
.setEnableCustomerCancellation(enableCustomerCancellation)
.setRequestDynamicCurrencyConversion(requestDynamicCurrencyConversion)

if let eligibleAmount = params["tipEligibleAmount"] as? Int {
do {
Expand Down Expand Up @@ -949,7 +951,7 @@ class StripeTerminalReactNative: RCTEventEmitter, DiscoveryDelegate, BluetoothRe

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 All @@ -958,9 +960,9 @@ class StripeTerminalReactNative: RCTEventEmitter, DiscoveryDelegate, BluetoothRe
resolve(Errors.createError(code: CommonErrorType.InvalidRequiredParameter, message: "You must provide \(invalidParams!) parameters."))
return
}

let collectInputsParameters: CollectInputsParameters

var inputs: [Input] = []
let collectInputs = params["collectInputs"] as? [NSDictionary]
if let collectInputs = collectInputs {
Expand Down Expand Up @@ -1173,14 +1175,14 @@ class StripeTerminalReactNative: RCTEventEmitter, DiscoveryDelegate, BluetoothRe
}
}
}

do {
collectInputsParameters = try CollectInputsParametersBuilder(inputs: inputs).build()
} catch {
resolve(Errors.createError(nsError: error as NSError))
return
}

DispatchQueue.main.async {
self.collectInputsCancellable = Terminal.shared.collectInputs(collectInputsParameters) { collectInputResults, error in
if let error = error as NSError? {
Expand All @@ -1191,7 +1193,7 @@ class StripeTerminalReactNative: RCTEventEmitter, DiscoveryDelegate, BluetoothRe
}
}
}

@objc(cancelCollectInputs:rejecter:)
func cancelCollectInputs(resolver resolve: @escaping RCTPromiseResolveBlock, rejecter reject: @escaping RCTPromiseRejectBlock) {
guard let cancelable = collectInputsCancellable else {
Expand Down Expand Up @@ -1241,7 +1243,7 @@ class StripeTerminalReactNative: RCTEventEmitter, DiscoveryDelegate, BluetoothRe
}
}
}

@objc(supportsReadersOfType:resolver:rejecter:)
func supportsReadersOfType(params: NSDictionary, resolver resolve: @escaping RCTPromiseResolveBlock, rejecter reject: @escaping RCTPromiseRejectBlock) {
let invalidParams = Errors.validateRequiredParameters(params: params, requiredParams: ["deviceType", "discoveryMethod"])
Expand Down
1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ export type CollectPaymentMethodParams = {
tipEligibleAmount?: number;
updatePaymentIntent?: boolean;
enableCustomerCancellation?: boolean;
requestDynamicCurrencyConversion?: boolean;
};

export type CollectSetupIntentPaymentMethodParams = {
Expand Down

0 comments on commit b49645c

Please sign in to comment.