Skip to content

Commit

Permalink
Merge pull request #27 from MadBrains/ios_fix
Browse files Browse the repository at this point in the history
[2.2.3] Fix nil arguments
  • Loading branch information
mit-73 authored Jun 29, 2021
2 parents 61e1732 + 6a15e6e commit 6be19a3
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 27 deletions.
4 changes: 4 additions & 0 deletions packages/mad_pay/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.2.3

* [iOS] Fix nil arguments

## 2.2.1-2.2.2

* [Android] Fix release build
Expand Down
4 changes: 2 additions & 2 deletions packages/mad_pay/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: mad_pay
description: Mad Pay will help you make payments through Apple Pay and Google Pay
version: 2.2.2
version: 2.2.3
repository: https://github.com/MadBrains/Mad-Pay-Flutter
issue_tracker: https://github.com/MadBrains/Mad-Pay-Flutter/issues
homepage: https://madbrains.ru/
Expand All @@ -14,7 +14,7 @@ dependencies:
sdk: flutter
mad_pay_platform_interface: ^2.2.0
mad_pay_android: ^2.2.2
mad_pay_ios: ^2.2.0
mad_pay_ios: ^2.2.1

# For development. Remove/comment before publishing to pub.
# dependency_overrides:
Expand Down
4 changes: 4 additions & 0 deletions packages/mad_pay_ios/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.2.1

* Fix nil arguments

## 2.2.0

* Initial `mad_pay_ios` module.
50 changes: 26 additions & 24 deletions packages/mad_pay_ios/ios/Classes/SwiftMadPayIosPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ import PassKit
@available(iOS 10.0, *)
public class SwiftMadPayIosPlugin: NSObject, FlutterPlugin {
private var activeResult: FlutterResult?

public static func register(with registrar: FlutterPluginRegistrar) {
let messenger = registrar.messenger()

let channel = FlutterMethodChannel(name: Constants.channel, binaryMessenger: messenger)
registrar.addMethodCallDelegate(SwiftMadPayIosPlugin(), channel: channel)

let buttonFactory = ApplePayButtonViewFactory(messenger: messenger)
registrar.register(buttonFactory, withId: Constants.buttonChannel)
}

func invokeSuccessResult(success: Bool = true, data: Data? = nil) {
guard (try? activeResult?(Response.with({ (res) in
res.success = success
Expand All @@ -26,7 +26,7 @@ public class SwiftMadPayIosPlugin: NSObject, FlutterPlugin {
return
}
}

func invokeErrorResult(success: Bool = false, errorCode: String? = nil, message: String? = nil) {
guard (try? activeResult?(Response.with({ (res) in
res.success = success
Expand All @@ -40,42 +40,44 @@ public class SwiftMadPayIosPlugin: NSObject, FlutterPlugin {
return
}
}

public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
activeResult = result

guard let arguments = (call.arguments as? Flutter.FlutterStandardTypedData?)??.data else {

let arguments = (call.arguments as? Flutter.FlutterStandardTypedData?)??.data

if arguments == nil && (call.method == Constants.checkActiveCard || call.method == Constants.payment) {
invokeErrorResult(errorCode: Constants.invalidParametersCode, message: "Invalid parameters. \"Arguments\" is null")
return
}

switch call.method {
case Constants.checkPayments: checkPayments()
case Constants.checkActiveCard: try? checkActiveCard(arguments: CheckActiveCardRequest(serializedData: arguments))
case Constants.payment: try? payment(arguments: PaymentRequest(serializedData: arguments))
case Constants.checkActiveCard: try? checkActiveCard(arguments: CheckActiveCardRequest(serializedData: arguments!))
case Constants.payment: try? payment(arguments: PaymentRequest(serializedData: arguments!))
default:
invokeErrorResult(errorCode: Constants.notImplementedCode, message: "Method \(call.method) not implemented")
}
}

func checkPayments() {
let canMakePayment = PKPaymentAuthorizationController.canMakePayments()
invokeSuccessResult(success: canMakePayment)
}

func checkActiveCard(arguments: CheckActiveCardRequest) {
let canMakePayments = PKPaymentAuthorizationController.canMakePayments(
usingNetworks: PaymentNetworkHelper.getPaymentNetworks(arguments.allowedPaymentNetworks))
usingNetworks: PaymentNetworkHelper.getPaymentNetworks(arguments.allowedPaymentNetworks))

invokeSuccessResult(success: canMakePayments)
}

func payment(arguments: PaymentRequest) {
if (arguments.parameters == nil) {
invokeErrorResult(errorCode: Constants.invalidParametersCode, message: "Invalid Payment parameters. \"Apple\" parameter required")
return
}

if (arguments.apple.merchantIdentifier.isEmpty || arguments.currencyCode.isEmpty || arguments.countryCode.isEmpty) {
invokeErrorResult(errorCode: Constants.invalidParametersCode, message: """
Invalid Payment parameters.
Expand All @@ -85,10 +87,10 @@ public class SwiftMadPayIosPlugin: NSObject, FlutterPlugin {
""")
return
}

var paymentNetworks = PaymentNetworkHelper.getPaymentNetworks(arguments.allowedPaymentNetworks)
paymentNetworks = paymentNetworks.isEmpty ? PKPaymentRequest.availableNetworks() : paymentNetworks

let paymentRequest = PKPaymentRequest()
paymentRequest.paymentSummaryItems = PaymentNetworkHelper.getPaymentSummaryItem(arguments.paymentItems)
paymentRequest.supportedNetworks = paymentNetworks
Expand All @@ -100,11 +102,11 @@ public class SwiftMadPayIosPlugin: NSObject, FlutterPlugin {
paymentRequest.shippingContact = PaymentNetworkHelper.getContact(arguments.apple.shippingContact)
paymentRequest.shippingMethods = PaymentNetworkHelper.getShippingMethods(arguments.apple.shippingMethods)
paymentRequest.shippingType = PaymentNetworkHelper.getShippingType(arguments.apple.shippingType)

if !arguments.apple.applicationData.isEmpty {
paymentRequest.applicationData = arguments.apple.applicationData
}

if #available(iOS 11.0, *) {
if !arguments.apple.requiredBillingContactFields.isEmpty {
paymentRequest.requiredBillingContactFields = PaymentNetworkHelper.getContactFields(arguments.apple.requiredBillingContactFields)
Expand All @@ -113,12 +115,12 @@ public class SwiftMadPayIosPlugin: NSObject, FlutterPlugin {
paymentRequest.requiredShippingContactFields = PaymentNetworkHelper.getContactFields(arguments.apple.requiredShippingContactFields)
}
}

let paymentController = PKPaymentAuthorizationController(paymentRequest: paymentRequest)
paymentController.delegate = self
paymentController.present(completion: nil)
}

private func paymentResult(pkPayment: PKPayment?) {
if let payment = pkPayment {
let jsonEncoder = JSONEncoder()
Expand All @@ -140,7 +142,7 @@ extension SwiftMadPayIosPlugin: PKPaymentAuthorizationControllerDelegate {
paymentResult(pkPayment: nil)
controller.dismiss(completion: nil)
}

@available(iOS 11.0, *)
public func paymentAuthorizationController(_ controller: PKPaymentAuthorizationController, didAuthorizePayment payment: PKPayment, handler completion: @escaping (PKPaymentAuthorizationResult) -> Void) {
paymentResult(pkPayment: payment)
Expand Down
2 changes: 1 addition & 1 deletion packages/mad_pay_ios/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: mad_pay_ios
description: A plug-in to add support for payments on the iOS side of Mad Pay
version: 2.2.0
version: 2.2.1
repository: https://github.com/MadBrains/Mad-Pay-Flutter
issue_tracker: https://github.com/MadBrains/Mad-Pay-Flutter/issues
homepage: https://madbrains.ru/
Expand Down

0 comments on commit 6be19a3

Please sign in to comment.