Skip to content

Commit

Permalink
Merge branch 'main' into poc-android-integration
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasbark committed Mar 19, 2021
2 parents 6618306 + 35ede4f commit 8670046
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 1,019 deletions.
46 changes: 0 additions & 46 deletions stripe_ios/ios/Classes/ApplePayButtonView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -126,49 +126,3 @@ class FLEmbedView : UIView {
super.layoutSubviews()
}
}

//
//
//@objc(ApplePayButtonView)
//class ApplePayButtonView: UIView {
// var applePayButton: PKPaymentButton?
//
// @objc var onPressAction: RCTDirectEventBlock?
// @objc var type: NSNumber?
// @objc var buttonStyle: NSNumber?
//
// @objc func handleApplePayButtonTapped() {
// if onPressAction != nil {
// onPressAction!(["true": true])
// }
// }
//
// override func didSetProps(_ changedProps: [String]!) {
// if let applePayButton = self.applePayButton {
// applePayButton.removeFromSuperview()
// }
// let paymentButtonType = PKPaymentButtonType(rawValue: self.type as? Int ?? 0) ?? .plain
// let paymentButtonStyle = PKPaymentButtonStyle(rawValue: self.buttonStyle as? Int ?? 2) ?? .black
// self.applePayButton = PKPaymentButton(paymentButtonType: paymentButtonType, paymentButtonStyle: paymentButtonStyle)
//
// if let applePayButton = self.applePayButton {
// applePayButton.addTarget(self, action: #selector(handleApplePayButtonTapped), for: .touchUpInside)
// self.addSubview(applePayButton)
// }
// }
//
// override init(frame: CGRect) {
// super.init(frame: frame)
// }
//
// override func layoutSubviews() {
// if let applePayButton = self.applePayButton {
// applePayButton.frame = self.bounds
// }
// }
//
// required init?(coder: NSCoder) {
// fatalError("init(coder:) has not been implemented")
// }
//}
//
30 changes: 6 additions & 24 deletions stripe_ios/ios/Classes/CardFieldView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ class CardFieldViewFactory: NSObject, FlutterPlatformViewFactory {
class FlutterCardFieldView: NSObject, FlutterPlatformView {
private var _view: UIView



private var cardFieldView: CardFieldView?

private let channel: FlutterMethodChannel
Expand All @@ -62,8 +60,8 @@ class FlutterCardFieldView: NSObject, FlutterPlatformView {
createNativeView()
channel.setMethodCallHandler(handle)
if let arguments = args as? [String: Any] {
onPostalCodeChange(arguments)
onDecorationChange(arguments)
updatePostalCodeEnabled(arguments)
updateDectoration(arguments)
}
}

Expand All @@ -73,12 +71,12 @@ class FlutterCardFieldView: NSObject, FlutterPlatformView {
switch call.method {
case "onPostalCodeEnabledChanged":
if let arguments = call.arguments as? [String : Any] {
onPostalCodeChange(arguments)
updatePostalCodeEnabled(arguments)
}
result(nil)
case "onDecorationChanged":
if let arguments = call.arguments as? [String : Any] {
onDecorationChange(arguments)
updateDectoration(arguments)
}
result(nil)
case "focus":
Expand Down Expand Up @@ -117,13 +115,13 @@ class FlutterCardFieldView: NSObject, FlutterPlatformView {
channel.invokeMethod("onCardChange", arguments: arguments)
}

func onPostalCodeChange(_ arguments: [String: Any]) {
func updatePostalCodeEnabled(_ arguments: [String: Any]) {
if let postalCodeEnabled = arguments["enablePostalCode"] as? Bool {
cardFieldView?.postalCodeEnabled = postalCodeEnabled
}
}

func onDecorationChange(_ arguments: [String: Any]) {
func updateDectoration(_ arguments: [String: Any]) {
var backgroundColor: UIColor?
var textColor: UIColor?
var placeholderColor: UIColor?
Expand Down Expand Up @@ -308,22 +306,6 @@ class CardFieldView: UIView {
return .lightGray
}()

func paymentContext(_ paymentContext: STPPaymentContext, didFailToLoadWithError error: Error) {
//
}

func paymentContextDidChange(_ paymentContext: STPPaymentContext) {
//
}

func paymentContext(_ paymentContext: STPPaymentContext, didCreatePaymentResult paymentResult: STPPaymentResult, completion: @escaping STPPaymentStatusBlock) {
//
}

func paymentContext(_ paymentContext: STPPaymentContext, didFinishWith status: STPPaymentStatus, error: Error?) {
//
}


func focus() {
cardField.becomeFirstResponder()
Expand Down
76 changes: 58 additions & 18 deletions stripe_ios/ios/Classes/Mappers.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Stripe

class Mappers {
class func mapToPKContactField(field: String) -> PKContactField {
switch field {
Expand All @@ -12,7 +11,7 @@ class Mappers {
}
}

class func mapToShippingMethodType(type: String?) -> PKPaymentSummaryItemType {
class func mapToPaymentSummaryItemType(type: String?) -> PKPaymentSummaryItemType {
if let type = type {
switch type {
case "pending": return PKPaymentSummaryItemType.pending
Expand All @@ -32,7 +31,7 @@ class Mappers {
let amount = NSDecimalNumber(string: method["amount"] as? String ?? "")
let identifier = method["identifier"] as! String
let detail = method["detail"] as? String ?? ""
let type = Mappers.mapToShippingMethodType(type: method["type"] as? String)
let type = Mappers.mapToPaymentSummaryItemType(type: method["type"] as? String)
let pm = PKShippingMethod.init(label: label, amount: amount, type: type)
pm.identifier = identifier
pm.detail = detail
Expand All @@ -43,6 +42,46 @@ class Mappers {
return shippingMethodsList
}

class func mapFromShippingMethod(shippingMethod: PKShippingMethod) -> NSDictionary {
let method: NSDictionary = [
"detail": shippingMethod.detail ?? "",
"identifier": shippingMethod.identifier ?? "",
"amount": shippingMethod.amount.stringValue,
"type": shippingMethod.type,
"label": shippingMethod.label
]

return method
}

class func mapFromShippingContact(shippingContact: PKContact) -> NSDictionary {
let name: NSDictionary = [
"familyName": shippingContact.name?.familyName ?? "",
"namePrefix": shippingContact.name?.namePrefix ?? "",
"nameSuffix": shippingContact.name?.nameSuffix ?? "",
"givenName": shippingContact.name?.givenName ?? "",
"middleName": shippingContact.name?.middleName ?? "",
"nickname": shippingContact.name?.nickname ?? "",
]
let contact: NSDictionary = [
"emailAddress": shippingContact.emailAddress ?? "",
"phoneNumber": shippingContact.phoneNumber?.stringValue ?? "",
"name": name,
"postalAddress": [
"city": shippingContact.postalAddress?.city,
"country": shippingContact.postalAddress?.country,
"postalCode": shippingContact.postalAddress?.postalCode,
"state": shippingContact.postalAddress?.state,
"street": shippingContact.postalAddress?.street,
"isoCountryCode": shippingContact.postalAddress?.isoCountryCode,
"subAdministrativeArea": shippingContact.postalAddress?.subAdministrativeArea,
"subLocality": shippingContact.postalAddress?.subLocality,
],
]

return contact
}


class func mapIntentStatus(status: STPPaymentIntentStatus?) -> String {
if let status = status {
Expand Down Expand Up @@ -86,9 +125,9 @@ class Mappers {
class func mapCardParamsToPaymentMethodParams(params: NSDictionary, billingDetails: STPPaymentMethodBillingDetails?) -> STPPaymentMethodParams {
let cardSourceParams = STPCardParams()
cardSourceParams.number = params["number"] as? String
cardSourceParams.cvc = params["cvc"] as? String
cardSourceParams.expMonth = params["expiryMonth"] as! UInt
cardSourceParams.expYear = params["expiryYear"] as! UInt
cardSourceParams.cvc = params["cvc"] as? String
cardSourceParams.expMonth = params["expiryMonth"] as! UInt
cardSourceParams.expYear = params["expiryYear"] as! UInt

let cardParams = STPPaymentMethodCardParams(cardSourceParams: cardSourceParams)
return STPPaymentMethodParams(card: cardParams, billingDetails: billingDetails, metadata: nil)
Expand Down Expand Up @@ -309,9 +348,10 @@ class Mappers {
class func mapCardParams(params: NSDictionary) -> STPPaymentMethodCardParams {
let cardSourceParams = STPCardParams()
cardSourceParams.number = params["number"] as? String
cardSourceParams.cvc = params["cvc"] as? String
cardSourceParams.expMonth = params["expiryMonth"] as! UInt
cardSourceParams.expYear = params["expiryYear"] as! UInt
cardSourceParams.cvc = params["cvc"] as? String
cardSourceParams.expMonth = params["expiryMonth"] as! UInt
cardSourceParams.expYear = params["expiryYear"] as! UInt

return STPPaymentMethodCardParams(cardSourceParams: cardSourceParams)
}

Expand Down Expand Up @@ -394,15 +434,6 @@ class Mappers {
return intent
}

@available(iOS 13.0, *)
class func mapToUserInterfaceStyle(_ style: String) -> PaymentSheet.UserInterfaceStyle {
switch style {
case "alwaysDark": return PaymentSheet.UserInterfaceStyle.alwaysDark
case "alwaysLight": return PaymentSheet.UserInterfaceStyle.alwaysLight
default: return PaymentSheet.UserInterfaceStyle.automatic
}
}

class func mapUICustomization(_ params: NSDictionary) -> STPThreeDSUICustomization {
let uiCustomization = STPThreeDSUICustomization()
if let labelSettings = params["label"] as? Dictionary<String, Any?> {
Expand Down Expand Up @@ -504,4 +535,13 @@ class Mappers {
class func convertDateToUnixTimestamp(date: Date) -> UInt64 {
return UInt64(date.timeIntervalSince1970 * 1000.0)
}

@available(iOS 13.0, *)
class func mapToUserInterfaceStyle(_ style: String) -> PaymentSheet.UserInterfaceStyle {
switch style {
case "alwaysDark": return PaymentSheet.UserInterfaceStyle.alwaysDark
case "alwaysLight": return PaymentSheet.UserInterfaceStyle.alwaysLight
default: return PaymentSheet.UserInterfaceStyle.automatic
}
}
}
32 changes: 30 additions & 2 deletions stripe_ios/ios/Classes/StripePlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ import PassKit


public class StripePlugin: StripeSdk, FlutterPlugin {

private var paymentSheet: PaymentSheet?
private var paymentSheetFlowController: PaymentSheet.FlowController?

private var channel: FlutterMethodChannel

public static func register(with registrar: FlutterPluginRegistrar) {
// Method Channel
let channel = FlutterMethodChannel(name: "flutter.stripe/payments", binaryMessenger: registrar.messenger())
let instance = StripePlugin()
let instance = StripePlugin(channel: channel)
registrar.addMethodCallDelegate(instance, channel: channel)

// Apple Pay Button
Expand All @@ -23,6 +25,11 @@ public class StripePlugin: StripeSdk, FlutterPlugin {
let cardFieldFactory = CardFieldViewFactory(messenger: registrar.messenger())
registrar.register(cardFieldFactory, withId: "flutter.stripe/card_field")
}

public init(channel : FlutterMethodChannel) {
self.channel = channel
super.init()
}

public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
switch call.method {
Expand Down Expand Up @@ -76,6 +83,11 @@ public class StripePlugin: StripeSdk, FlutterPlugin {
result(FlutterError.init(code: code ?? "Failed", message: message, details: error))
}
}

override
func sendEvent(withName name: String, body: [String: Any]) {
channel.invokeMethod(name, arguments: body)
}
}


Expand Down Expand Up @@ -116,6 +128,19 @@ extension StripePlugin {
confirmSetupIntent(setupIntentClientSecret: setupIntentClientSecret, data: data, options: options,resolver: resolver(for: result), rejecter: rejecter(for: result))
}

public func updateApplePaySummaryItems(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
guard let arguments = call.arguments as? Dictionary<String, AnyObject>,
let summaryItems = arguments["summaryItems"] as? NSArray else {
result("Not a valid fields")
return
}
updateApplePaySummaryItems(
summaryItems: summaryItems,
resolver: resolver(for: result),
rejecter: rejecter(for: result)
)
}

public func confirmApplePayPayment(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
guard let arguments = call.arguments as? Dictionary<String, AnyObject>,
let clientSecret = arguments["clientSecret"] as? String else {
Expand Down Expand Up @@ -336,5 +361,8 @@ extension StripePlugin {
}
}


}



Loading

0 comments on commit 8670046

Please sign in to comment.