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

Add rules to sort list of Payment Methods coming from Capabilities (v4.27.0) #256

Merged
merged 5 commits into from
Jan 23, 2024
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
16 changes: 16 additions & 0 deletions OmiseSDK/Helpers/Extensions/Array+Helpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,19 @@ extension Array {
return self[index]
}
}

extension Array where Element: Equatable {
func reorder(by preferredOrder: [Element]) -> [Element] {
self.sorted { (a, b) -> Bool in
guard let first = preferredOrder.firstIndex(of: a) else {
return false
}

guard let second = preferredOrder.firstIndex(of: b) else {
return true
}

return first < second
}
}
}
81 changes: 57 additions & 24 deletions OmiseSDK/PaymentChooserViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,74 @@ import UIKit
import os

enum PaymentChooserOption: CaseIterable, Equatable, CustomStringConvertible {
case creditCard
case installment
case internetBanking
case mobileBanking
case tescoLotus
case conbini
case payEasy
case netBanking
case alipay
case alipayCN
case alipayHK
case atome
case boost
case citiPoints
case conbini
case creditCard
case dana
case duitNowOBW
case duitNowQR
case fpx
case gcash
case grabPay
case grabPayRms
case installment
case internetBanking
case kakaoPay
case touchNGoAlipayPlus
case promptpay
case maybankQRPay
case mobileBanking
case netBanking
case ocbcDigital
case ocbcPao
case payEasy
case paynow
case truemoney
case truemoneyJumpApp
case citiPoints
case fpx
case payPay
case promptpay
case rabbitLinepay
case ocbcPao
case ocbcDigital
case grabPay
case boost
case shopeePay
case shopeePayJumpApp
case maybankQRPay
case duitNowQR
case duitNowOBW
case tescoLotus
case touchNGo
case grabPayRms
case payPay
case touchNGoAlipayPlus
case truemoney
case truemoneyJumpApp

static var alphabetical: [PaymentChooserOption] {
PaymentChooserOption.allCases.sorted {
$0.description.localizedCaseInsensitiveCompare($1.description) == .orderedAscending
}
}

static let topList: [PaymentChooserOption] = [
.creditCard,
.paynow,
.promptpay,
.truemoney,
.truemoneyJumpApp,
.mobileBanking,
.internetBanking,
.alipay,
.installment,
.ocbcDigital,
.ocbcPao,
.rabbitLinepay,
.shopeePay,
.shopeePayJumpApp,
.alipayCN,
.alipayHK
]

static var sorting: [PaymentChooserOption] {
var sorted = topList
for item in alphabetical where !sorted.contains(item) {
sorted.append(item)
}
return sorted
}

var description: String {
switch self {
Expand Down Expand Up @@ -580,7 +613,7 @@ class PaymentChooserViewController: AdaptableStaticTableViewController<PaymentCh
var paymentMethodsToShow = paymentOptions(from: allowedPaymentMethods)
paymentMethodsToShow = appendCreditCardPayment(paymentOptions: paymentMethodsToShow)
paymentMethodsToShow = filterTrueMoney(paymentOptions: paymentMethodsToShow)
showingValues = paymentMethodsToShow
showingValues = paymentMethodsToShow.reorder(by: PaymentChooserOption.sorting)

os_log("Payment Chooser: Showing options - %{private}@",
log: uiLogObject,
Expand Down
90 changes: 90 additions & 0 deletions OmiseSDKTests/Views/PaymentChooserViewControllerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,94 @@ class PaymentChooserViewControllerTests: XCTestCase {
XCTAssertFalse(vc.showingValues.contains(.truemoneyJumpApp))
XCTAssertFalse(vc.showingValues.contains(.truemoney))
}

func testAlphabetSorting() {
let vc = PaymentChooserViewController()
vc.loadView()

let sorted: [PaymentChooserOption] = [
.alipay,
.alipayCN,
.alipayHK,
.atome,
.boost,
.citiPoints,
.conbini,
.creditCard,
.dana,
.duitNowOBW,
.duitNowQR,
.fpx,
.gcash,
.grabPay,
.grabPayRms,
.installment,
.internetBanking,
.kakaoPay,
.maybankQRPay,
.mobileBanking,
.netBanking,
.ocbcDigital,
.ocbcPao,
.payEasy,
.paynow,
.payPay,
.promptpay,
.rabbitLinepay,
.shopeePay,
.shopeePayJumpApp,
.tescoLotus,
.touchNGoAlipayPlus, // TNG eWallet
.touchNGo,
.truemoneyJumpApp, // TrueMoney
.truemoney // TrueMoney Wallet
]

XCTAssertEqual(PaymentChooserOption.alphabetical, sorted)
}

func testFilteringAndSorting() {
let filteredAndSorted: [PaymentChooserOption] = [
.creditCard,
.paynow,
.promptpay,
.truemoneyJumpApp, // TrueMoney
.mobileBanking,
.internetBanking,
.alipay,
.installment,
.ocbcDigital,
.ocbcPao,
.rabbitLinepay,
.shopeePay,
.shopeePayJumpApp,
.alipayCN,
.alipayHK,
.atome,
.boost,
.citiPoints,
.conbini,
.dana,
.duitNowOBW,
.duitNowQR,
.fpx,
.gcash,
.grabPay,
.grabPayRms,
.kakaoPay,
.maybankQRPay,
.netBanking,
.payEasy,
.payPay,
.tescoLotus,
.touchNGoAlipayPlus, // TNG eWallet
.touchNGo
]

let vc = PaymentChooserViewController()
vc.loadView()

vc.allowedPaymentMethods = allSourceTypes
XCTAssertEqual(vc.showingValues, filteredAndSorted)
}
}