Skip to content

Commit

Permalink
update iOS glue code
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasbark committed Jul 25, 2023
1 parent 26a955e commit a4115dd
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 181 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,6 @@ extension StripeSdk : PKPaymentAuthorizationViewControllerDelegate, STPApplePayC
didSelect shippingMethod: PKShippingMethod,
handler: @escaping (PKPaymentRequestShippingMethodUpdate) -> Void
) {
if (self.hasLegacyApplePayListeners) {
// Legacy, remove when useApplePay hook is removed
sendEvent(withName: "onDidSetShippingMethod", body: ["shippingMethod": Mappers.mapFromShippingMethod(shippingMethod: shippingMethod)])
}
if let callback = self.shippingMethodUpdateJSCallback {
self.shippingMethodUpdateCompletion = handler
callback(["shippingMethod": Mappers.mapFromShippingMethod(shippingMethod: shippingMethod)])
Expand All @@ -141,10 +137,6 @@ extension StripeSdk : PKPaymentAuthorizationViewControllerDelegate, STPApplePayC
didSelectShippingContact contact: PKContact,
handler: @escaping (PKPaymentRequestShippingContactUpdate) -> Void
) {
if (self.hasLegacyApplePayListeners) {
// Legacy, remove when useApplePay hook is removed
sendEvent(withName: "onDidSetShippingContact", body: ["shippingContact": Mappers.mapFromShippingContact(shippingContact: contact)])
}
if let callback = self.shippingContactUpdateJSCallback {
self.shippingContactUpdateCompletion = handler
callback(["shippingContact": Mappers.mapFromShippingContact(shippingContact: contact)])
Expand All @@ -170,10 +162,7 @@ extension StripeSdk : PKPaymentAuthorizationViewControllerDelegate, STPApplePayC
} else if let clientSecret = self.confirmApplePaySetupClientSecret {
completion(clientSecret, nil)
} else {
self.applePayCompletionCallback = completion
let method = Mappers.mapFromPaymentMethod(paymentMethod.splitApplePayAddressByNewline())
self.deprecatedApplePayRequestResolver?(Mappers.createResult("paymentMethod", method))
self.deprecatedApplePayRequestRejecter = nil
RCTMakeAndLogError("Tried to complete Apple Pay payment, but no client secret was found.", nil, nil)
}
}

Expand Down Expand Up @@ -233,44 +222,28 @@ extension StripeSdk : PKPaymentAuthorizationViewControllerDelegate, STPApplePayC
}
}
}

} else {
deprecatedConfirmApplePayPaymentResolver?([])
}
break
case .error:
if let resolve = self.confirmApplePayResolver {
resolve(Errors.createError(ErrorType.Failed, error as NSError?))
} else {
let message = "Payment not completed"
deprecatedApplePayCompletionRejecter?(ErrorType.Failed, message, nil)
deprecatedApplePayRequestRejecter?(ErrorType.Failed, message, nil)
}
break
case .userCancellation:
let message = "The payment has been canceled"
if let resolve = self.confirmApplePayResolver {
resolve(Errors.createError(ErrorType.Canceled, message))
} else {
deprecatedApplePayCompletionRejecter?(ErrorType.Canceled, message, nil)
deprecatedApplePayRequestRejecter?(ErrorType.Canceled, message, nil)
}
break
@unknown default:
if let resolve = self.confirmApplePayResolver {
resolve(Errors.createError(ErrorType.Unknown, error as NSError?))
} else {
let message = "Payment not completed"
deprecatedApplePayCompletionRejecter?(ErrorType.Unknown, message, nil)
deprecatedApplePayRequestRejecter?(ErrorType.Unknown, message, nil)
}
break
}
confirmApplePayResolver = nil
confirmApplePayPaymentClientSecret = nil
confirmApplePaySetupClientSecret = nil
deprecatedApplePayCompletionRejecter = nil
deprecatedApplePayRequestRejecter = nil
}

}
Expand Down
110 changes: 100 additions & 10 deletions packages/stripe_ios/ios/Classes/Stripe Sdk/StripeSdk+PaymentSheet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import Foundation
import StripePaymentSheet

extension StripeSdk {
internal func buildPaymentSheetConfiguration(params: NSDictionary, orderTrackingCallback: RCTResponseSenderBlock? = nil) -> (error: NSDictionary?, configuration: PaymentSheet.Configuration?) {
internal func buildPaymentSheetConfiguration(
params: NSDictionary
) -> (error: NSDictionary?, configuration: PaymentSheet.Configuration?) {
var configuration = PaymentSheet.Configuration()

configuration.primaryButtonLabel = params["primaryButtonLabel"] as? String
Expand All @@ -29,7 +31,7 @@ extension StripeSdk {
merchantCountryCode: applePayParams["merchantCountryCode"] as? String,
paymentSummaryItems: applePayParams["cartItems"] as? [[String : Any]],
buttonType: applePayParams["buttonType"] as? NSNumber,
customHandlers: buildCustomerHandlersForPaymentSheet(applePayParams: applePayParams, orderTrackingCallback: orderTrackingCallback)
customHandlers: buildCustomerHandlersForPaymentSheet(applePayParams: applePayParams)
)
} catch {
return(error: Errors.createError(ErrorType.Failed, error.localizedDescription), configuration: nil)
Expand Down Expand Up @@ -96,8 +98,13 @@ extension StripeSdk {
return (nil, configuration)
}

internal func preparePaymentSheetInstance(params: NSDictionary, configuration: PaymentSheet.Configuration, resolve: @escaping RCTPromiseResolveBlock) {

internal func preparePaymentSheetInstance(
params: NSDictionary,
configuration: PaymentSheet.Configuration,
resolve: @escaping RCTPromiseResolveBlock
) {
self.paymentSheetFlowController = nil

func handlePaymentSheetFlowControllerResult(result: Result<PaymentSheet.FlowController, Error>, stripeSdk: StripeSdk?) {
switch result {
case .failure(let error):
Expand Down Expand Up @@ -146,12 +153,91 @@ extension StripeSdk {
resolve([])
}
} else {
resolve(Errors.createError(ErrorType.Failed, "You must provide either paymentIntentClientSecret or setupIntentClientSecret"))
guard let intentConfiguration = params["intentConfiguration"] as? NSDictionary else {
resolve(Errors.createError(ErrorType.Failed, "One of `paymentIntentClientSecret`, `setupIntentClientSecret`, or `intentConfiguration` is required"))
return
}
guard let modeParams = intentConfiguration["mode"] as? NSDictionary else {
resolve(Errors.createError(ErrorType.Failed, "One of `paymentIntentClientSecret`, `setupIntentClientSecret`, or `intentConfiguration.mode` is required"))
return
}
if (intentConfiguration.object(forKey: "confirmHandler") == nil) {
resolve(Errors.createError(ErrorType.Failed, "You must provide `intentConfiguration.confirmHandler` if you are not passing an intent client secret"))
return
}
let captureMethodString = intentConfiguration["captureMethod"] as? String
let intentConfig = buildIntentConfiguration(
modeParams: modeParams,
paymentMethodTypes: intentConfiguration["paymentMethodTypes"] as? [String],
captureMethod: mapCaptureMethod(captureMethodString)
)

if params["customFlow"] as? Bool == true {
PaymentSheet.FlowController.create(intentConfiguration: intentConfig, configuration: configuration) { [weak self] result in
handlePaymentSheetFlowControllerResult(result: result, stripeSdk: self)
}
} else {
self.paymentSheet = PaymentSheet(
intentConfiguration: intentConfig,
configuration: configuration
)
resolve([])
}
}
}

private func mapCaptureMethod(_ captureMethod: String?) -> PaymentSheet.IntentConfiguration.CaptureMethod {
if let captureMethod = captureMethod {
switch captureMethod {
case "Automatic": return PaymentSheet.IntentConfiguration.CaptureMethod.automatic
case "Manual": return PaymentSheet.IntentConfiguration.CaptureMethod.manual
case "AutomaticAsync": return PaymentSheet.IntentConfiguration.CaptureMethod.automaticAsync
default: return PaymentSheet.IntentConfiguration.CaptureMethod.automatic
}
}
return PaymentSheet.IntentConfiguration.CaptureMethod.automatic
}

private func buildIntentConfiguration(
modeParams: NSDictionary,
paymentMethodTypes: [String]?,
captureMethod: PaymentSheet.IntentConfiguration.CaptureMethod
) -> PaymentSheet.IntentConfiguration {
var mode: PaymentSheet.IntentConfiguration.Mode
if let amount = modeParams["amount"] as? Int {
mode = PaymentSheet.IntentConfiguration.Mode.payment(
amount: amount,
currency: modeParams["currencyCode"] as? String ?? "",
setupFutureUsage: modeParams["setupFutureUsage"] != nil
? (modeParams["setupFutureUsage"] as? String == "OffSession" ? .offSession : .onSession)
: nil,
captureMethod: captureMethod
)
} else {
mode = PaymentSheet.IntentConfiguration.Mode.setup(
currency: modeParams["currencyCode"] as? String,
setupFutureUsage: modeParams["setupFutureUsage"] as? String == "OffSession" ? .offSession : .onSession
)
}

return PaymentSheet.IntentConfiguration.init(
mode: mode,
paymentMethodTypes: paymentMethodTypes,
confirmHandler: { paymentMethod, shouldSavePaymentMethod, intentCreationCallback in
if (self.hasEventListeners) {
self.paymentSheetIntentCreationCallback = intentCreationCallback
self.sendEvent(withName: "onConfirmHandlerCallback", body: [
"paymentMethod": Mappers.mapFromPaymentMethod(paymentMethod) ?? NSNull(),
"shouldSavePaymentMethod": shouldSavePaymentMethod
])
} else {
RCTMakeAndLogError("Tried to call confirmHandler, but no callback was found. Please file an issue: https://github.com/stripe/stripe-react-native/issues", nil, nil)
}
})
}

private func buildCustomerHandlersForPaymentSheet(applePayParams: NSDictionary, orderTrackingCallback: RCTResponseSenderBlock?) -> PaymentSheet.ApplePayConfiguration.Handlers? {
if (applePayParams["request"] == nil && orderTrackingCallback == nil) {
private func buildCustomerHandlersForPaymentSheet(applePayParams: NSDictionary) -> PaymentSheet.ApplePayConfiguration.Handlers? {
if (applePayParams["request"] == nil) {
return nil
}
return PaymentSheet.ApplePayConfiguration.Handlers(paymentRequestHandler: { request in
Expand All @@ -163,9 +249,13 @@ extension StripeSdk {
}
return request
}, authorizationResultHandler: { result, completion in
if let orderTrackingCallback = orderTrackingCallback {
self.orderTrackingHandler = (result, completion)
orderTrackingCallback(nil)
if applePayParams.object(forKey: "setOrderTracking") != nil {
if (self.hasEventListeners) {
self.orderTrackingHandler = (result, completion)
self.sendEvent(withName: "onOrderTrackingCallback", body: [:])
} else {
RCTMakeAndLogError("Order tracking is enabled, but no callback was found. Please file an issue: https://github.com/stripe/stripe-react-native/issues", nil, nil)
}
} else {
completion(result)
}
Expand Down
Loading

0 comments on commit a4115dd

Please sign in to comment.