Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fixes ApplePay button rendering logic #685

Merged
merged 3 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Debug App/Sources/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,4 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
UINavigationBar.appearance().scrollEdgeAppearance = navigationBarAppearance
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,9 @@ class MerchantSessionAndSettingsViewController: UIViewController {
applePayOptions: PrimerApplePayOptions(
merchantIdentifier: "merchant.checkout.team",
merchantName: merchantNameTextField.text ?? "Primer Merchant",
isCaptureBillingAddressEnabled: false)),
isCaptureBillingAddressEnabled: false,
showApplePayForUnsupportedDevice: false,
checkProvidedNetworks: false)),
uiOptions: uiOptions,
debugOptions: PrimerDebugOptions(is3DSSanityCheckEnabled: false)
)
Expand Down Expand Up @@ -552,7 +554,9 @@ class MerchantSessionAndSettingsViewController: UIViewController {
applePayOptions: PrimerApplePayOptions(
merchantIdentifier: "merchant.checkout.team",
merchantName: merchantNameTextField.text ?? "Primer Merchant",
isCaptureBillingAddressEnabled: false)),
isCaptureBillingAddressEnabled: false,
showApplePayForUnsupportedDevice: false,
checkProvidedNetworks: false)),
uiOptions: nil,
debugOptions: PrimerDebugOptions(is3DSSanityCheckEnabled: false)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,26 @@


import Foundation
import PassKit

extension PrimerHeadlessUniversalCheckout {

public class PaymentMethod: NSObject {

static var availablePaymentMethods: [PrimerHeadlessUniversalCheckout.PaymentMethod] {
let availablePaymentMethods = PrimerAPIConfiguration.paymentMethodConfigs?
var availablePaymentMethods = PrimerAPIConfiguration.paymentMethodConfigs?
.compactMap({ $0.type })
.compactMap({ PrimerHeadlessUniversalCheckout.PaymentMethod(paymentMethodType: $0) })

if PrimerSettings.current.paymentMethodOptions.applePayOptions?.showApplePayForUnsupportedDevice != true {
if !PKPaymentAuthorizationViewController.canMakePayments() {
// Filter out Apple pay from payment methods
availablePaymentMethods = availablePaymentMethods?.filter({ (method: PrimerHeadlessUniversalCheckout.PaymentMethod) in
return method.paymentMethodType != "APPLE_PAY"
})
}
}

return availablePaymentMethods ?? []
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,14 @@ extension Response.Body {
?? []

let supportedNetworks = PaymentNetwork.iOSSupportedPKPaymentNetworks
if !PKPaymentAuthorizationViewController.canMakePayments(usingNetworks: supportedNetworks) {
var canMakePayment: Bool
if PrimerSettings.current.paymentMethodOptions.applePayOptions?.checkProvidedNetworks == true {
canMakePayment = PKPaymentAuthorizationViewController.canMakePayments(usingNetworks: supportedNetworks)
} else {
canMakePayment = PKPaymentAuthorizationViewController.canMakePayments()
}

if !canMakePayment {
if let applePayViewModel = viewModels.filter({ $0.config.type == PrimerPaymentMethodType.applePay.rawValue }).first,
let applePayViewModelIndex = viewModels.firstIndex(where: { $0 == applePayViewModel }) {
viewModels.remove(at: applePayViewModelIndex)
Expand Down
14 changes: 12 additions & 2 deletions Sources/PrimerSDK/Classes/Data Models/PrimerSettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,21 @@ public class PrimerApplePayOptions: Codable {
let merchantIdentifier: String
let merchantName: String
let isCaptureBillingAddressEnabled: Bool

public init(merchantIdentifier: String, merchantName: String, isCaptureBillingAddressEnabled: Bool = false) {
/// If in some cases you dont want to present ApplePay option if the device is not supporting it set this to `false`. Default value is `true`.
let showApplePayForUnsupportedDevice: Bool
/// Due to merchant report about ApplePay flow which was not presenting because canMakePayments(usingNetworks:) was returning false if there were no cards in the Wallet, we introduced this flag to continue supporting the old behaviour. Default value is `true`.
let checkProvidedNetworks: Bool

public init(merchantIdentifier: String,
merchantName: String,
isCaptureBillingAddressEnabled: Bool = false,
showApplePayForUnsupportedDevice: Bool = true,
checkProvidedNetworks: Bool = true) {
self.merchantIdentifier = merchantIdentifier
self.merchantName = merchantName
self.isCaptureBillingAddressEnabled = isCaptureBillingAddressEnabled
self.showApplePayForUnsupportedDevice = showApplePayForUnsupportedDevice
self.checkProvidedNetworks = checkProvidedNetworks
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,14 @@ class ApplePayTokenizationViewModel: PaymentMethodTokenizationViewModel {
)

let supportedNetworks = PaymentNetwork.iOSSupportedPKPaymentNetworks
if PKPaymentAuthorizationViewController.canMakePayments(usingNetworks: supportedNetworks) {
var canMakePayment: Bool
if PrimerSettings.current.paymentMethodOptions.applePayOptions?.checkProvidedNetworks == true {
canMakePayment = PKPaymentAuthorizationViewController.canMakePayments(usingNetworks: supportedNetworks)
} else {
canMakePayment = PKPaymentAuthorizationViewController.canMakePayments()
}

if canMakePayment {
let request = PKPaymentRequest()
let isBillingContactFieldsRequired = PrimerSettings.current.paymentMethodOptions.applePayOptions?.isCaptureBillingAddressEnabled == true
request.requiredBillingContactFields = isBillingContactFieldsRequired ? [.postalAddress] : []
Expand Down Expand Up @@ -232,10 +239,18 @@ class ApplePayTokenizationViewModel: PaymentMethodTokenizationViewModel {
}

} else {
log(logLevel: .error, title: "APPLE PAY", message: "Cannot make payments on the provided networks")
let err = PrimerError.unableToMakePaymentsOnProvidedNetworks(userInfo: ["file": #file, "class": "\(Self.self)", "function": #function, "line": "\(#line)"], diagnosticsId: UUID().uuidString)
ErrorHandler.handle(error: err)
seal.reject(err)
if PrimerSettings.current.paymentMethodOptions.applePayOptions?.checkProvidedNetworks == true {
log(logLevel: .error, title: "APPLE PAY", message: "Cannot run ApplePay on this device")
let err = PrimerError.unableToMakePaymentsOnProvidedNetworks(userInfo: ["file": #file, "class": "\(Self.self)", "function": #function, "line": "\(#line)"], diagnosticsId: UUID().uuidString)
ErrorHandler.handle(error: err)
seal.reject(err)
} else {
log(logLevel: .error, title: "APPLE PAY", message: "Cannot run ApplePay on this device")
let err = PrimerError.unableToPresentPaymentMethod(paymentMethodType: "APPLE_PAY", userInfo: ["message:": "Cannot run ApplePay on this device", "file": #file, "class": "\(Self.self)", "function": #function, "line": "\(#line)"], diagnosticsId: UUID().uuidString)
ErrorHandler.handle(error: err)
seal.reject(err)
}

}
}
}
Expand Down