Skip to content
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
35 changes: 18 additions & 17 deletions gr4vy-iOS/Gr4vyUtility.swift
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ struct Gr4vyUtility {
guard let data = payload["data"] as? [String: Any],
let countryCode = data["countryCode"] as? String,
let currencyCode = data["currencyCode"] as? String,
let supportedNetworks = data["supportedNetworks"] as? [String],
let total = data["total"] as? [String: Any],
let value = merchantName ?? total["label"] as? String,
let amount = total["amount"] as? String else {
Expand All @@ -176,25 +177,25 @@ struct Gr4vyUtility {

let paymentItem = PKPaymentSummaryItem.init(label: value, amount: NSDecimalNumber(string: amount))

let paymentNetworks = [
PKPaymentNetwork.amex,
PKPaymentNetwork.cartesBancaires,
PKPaymentNetwork.discover,
PKPaymentNetwork.eftpos,
PKPaymentNetwork.electron,
PKPaymentNetwork.elo,
PKPaymentNetwork.interac,
PKPaymentNetwork.JCB,
PKPaymentNetwork.mada,
PKPaymentNetwork.maestro,
PKPaymentNetwork.masterCard,
PKPaymentNetwork.privateLabel,
PKPaymentNetwork.visa,
PKPaymentNetwork.vPay
let networksMap: [String: PKPaymentNetwork] = [
"amex": .amex,
"cartesbancaires": .cartesBancaires,
"discover": .discover,
"eftpos": .eftpos,
"electron": .electron,
"elo": .elo,
"interac": .interac,
"jcb": .JCB,
"mada": .mada,
"maestro": .maestro,
"mastercard": .masterCard,
"privatelabel": .privateLabel,
"visa": .visa,
"vpay": .vPay
Comment on lines +180 to +194
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Create a map so it's easier to reference and update networks

]

guard deviceSupportsApplePay(paymentNetworks: paymentNetworks) else {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This guard completely prevented the payment sheet to open. Since in this case it checked for supported networks, we're ok to let it open and display the Apple UI error instead for unsupported networks instead.

return nil
let paymentNetworks = supportedNetworks.compactMap { network in
networksMap[network.lowercased()]
}

let request = PKPaymentRequest()
Expand Down
30 changes: 19 additions & 11 deletions gr4vy-iOSTests/gr4vy_iOSTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -748,26 +748,18 @@ class gr4vy_iOSTests: XCTestCase {
}

func testHandleAppleStartSucceeds() {
var payload: [String: Any] = ["data": ["countryCode": "countryCode", "currencyCode": "currencyCode", "total": ["label": "label", "amount": "123"]]]
var payload: [String: Any] = ["data": ["supportedNetworks": ["VISA", "MASTERCARD"], "countryCode": "countryCode", "currencyCode": "currencyCode", "total": ["label": "label", "amount": "123"]]]
let merchantId: String = "merchantID"

var sut = Gr4vyUtility.handleAppleStartSession(from: payload, merchantId: merchantId, merchantName: nil)
XCTAssertNotNil(sut)

sut = Gr4vyUtility.handleAppleStartSession(from: payload, merchantId: merchantId, merchantName: "Test")
XCTAssertNotNil(sut)

payload = ["data": ["supportedNetworks": ["VISA", "MASTERCARD"], "countryCode": "countryCode", "currencyCode": "currencyCode", "total": ["label": "label", "amount": "123"]]]

sut = Gr4vyUtility.handleAppleStartSession(from: payload, merchantId: merchantId, merchantName: nil)
XCTAssertNotNil(sut)

sut = Gr4vyUtility.handleAppleStartSession(from: payload, merchantId: merchantId, merchantName: "Test")
XCTAssertNotNil(sut)
}

func testHandleAppleStartFails() {
var payload: [String: Any] = ["data": ["countryCode": "countryCode", "currencyCode": "currencyCode", "total": ["label": "label", "amount": "123"]]]
var payload: [String: Any] = ["data": ["supportedNetworks": ["VISA", "MASTERCARD"], "countryCode": "countryCode", "currencyCode": "currencyCode", "total": ["label": "label", "amount": "123"]]]
let merchantId: String = "merchantID"

var sut = Gr4vyUtility.handleAppleStartSession(from: payload, merchantId: merchantId, merchantName: nil)
Expand All @@ -776,6 +768,22 @@ class gr4vy_iOSTests: XCTestCase {
sut = Gr4vyUtility.handleAppleStartSession(from: payload, merchantId: merchantId, merchantName: "Test")
XCTAssertNotNil(sut)

payload = ["data": ["supportedNetworks": [], "countryCode": "countryCode", "currencyCode": "currencyCode", "total": ["label": "label", "amount": "123"]]]

sut = Gr4vyUtility.handleAppleStartSession(from: payload, merchantId: merchantId, merchantName: nil)
XCTAssertNotNil(sut)

sut = Gr4vyUtility.handleAppleStartSession(from: payload, merchantId: merchantId, merchantName: "Test")
XCTAssertNotNil(sut)

payload = ["data": ["countryCode": "countryCode", "currencyCode": "currencyCode", "total": ["label": "label", "amount": "123"]]]

sut = Gr4vyUtility.handleAppleStartSession(from: payload, merchantId: merchantId, merchantName: nil)
XCTAssertNil(sut)

sut = Gr4vyUtility.handleAppleStartSession(from: payload, merchantId: merchantId, merchantName: "Test")
XCTAssertNil(sut)

payload = ["data": ["currencyCode": "currencyCode", "total": ["label": "label", "amount": "123"]]]

sut = Gr4vyUtility.handleAppleStartSession(from: payload, merchantId: merchantId, merchantName: nil)
Expand Down Expand Up @@ -808,7 +816,7 @@ class gr4vy_iOSTests: XCTestCase {
sut = Gr4vyUtility.handleAppleStartSession(from: payload, merchantId: merchantId, merchantName: "Test")
XCTAssertNil(sut)

payload = ["data": ["countryCode": "countryCode", "currencyCode": "currencyCode", "total": ["amount": "123"]]]
payload = ["data": ["supportedNetworks": [], "countryCode": "countryCode", "currencyCode": "currencyCode", "total": ["amount": "123"]]]

sut = Gr4vyUtility.handleAppleStartSession(from: payload, merchantId: merchantId, merchantName: nil)
XCTAssertNil(sut)
Expand Down
2 changes: 1 addition & 1 deletion gr4vy-ios.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'gr4vy-ios'
s.version = '2.4.4'
s.version = '2.5.0'
s.license = 'MIT'
s.summary = 'Quickly embed Gr4vy in your iOS app to store card details, authorize payments, and capture a transaction.'
s.homepage = 'https://github.com/gr4vy/gr4vy-ios'
Expand Down