Skip to content

Commit

Permalink
Added unionpay and diner card validation
Browse files Browse the repository at this point in the history
  • Loading branch information
javiersuweijie committed Mar 20, 2021
1 parent 873a19c commit 6523403
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 14 deletions.
12 changes: 8 additions & 4 deletions Xendit.podspec
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
Pod::Spec.new do |s|
s.name = "Xendit"
s.version = "0.1.0"
s.version = "2.1.8"
s.summary = "Xendit is an API for accepting payments online"
s.homepage = "https://www.xendit.co"
s.license = "MIT"
s.author = { "Juan Gonzalez’" => "[email protected]" }
s.social_media_url = "https://www.facebook.com/xendit"
s.platform = :ios, "8.0"
s.source = { :git => 'https://github.com/xendit/xendit-sdk-ios.git', :tag => s.version }
s.ios.vendored_frameworks = 'Xendit.framework'
s.platform = :ios, "9.0"
s.ios.deployment_target = '9.0'
s.swift_versions = ["4", "5"]
s.source = { :git => 'https://github.com/xendit/xendit-sdk-ios-src.git', :tag => s.version }
s.source_files = "Xendit/**/*.{h,m,swift}"
s.pod_target_xcconfig = { 'ONLY_ACTIVE_ARCH' => 'YES', 'EXCLUDED_ARCHS[sdk=iphonesimulator*]' => 'arm64 i386' }
s.user_target_xcconfig = { 'EXCLUDED_ARCHS[sdk=iphonesimulator*]' => 'arm64 i386' }
end
6 changes: 6 additions & 0 deletions Xendit/Models/CardData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public enum CYBCardTypes {
case VISA_ELECTRON
case DANKORT
case MAESTRO
case DINER
case UNIONPAY
case UNKNOWN

func stringValue() -> String {
Expand All @@ -37,6 +39,10 @@ public enum CYBCardTypes {
return "034"
case .MAESTRO:
return "042"
case .DINER:
return "005"
case .UNIONPAY:
return "062"
case .UNKNOWN:
return "0"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ class AuthenticationWebViewController: UIViewController, WKScriptMessageHandler,
view.addSubview(webView)

NSLayoutConstraint.activate([
NSLayoutConstraint(item: webView, attribute: .leading, relatedBy: .equal, toItem: view, attribute: .leading, multiplier: 1, constant: 0),
NSLayoutConstraint(item: webView, attribute: .top, relatedBy: .equal, toItem: view, attribute: .top, multiplier: 1, constant: 0),
NSLayoutConstraint(item: webView, attribute: .trailing, relatedBy: .equal, toItem: view, attribute: .trailing, multiplier: 1, constant: 0),
NSLayoutConstraint(item: webView, attribute: .bottom, relatedBy: .equal, toItem: view, attribute: .bottom, multiplier: 1, constant: 0),
NSLayoutConstraint(item: webView!, attribute: .leading, relatedBy: .equal, toItem: view, attribute: .leading, multiplier: 1, constant: 0),
NSLayoutConstraint(item: webView!, attribute: .top, relatedBy: .equal, toItem: view, attribute: .top, multiplier: 1, constant: 0),
NSLayoutConstraint(item: webView!, attribute: .trailing, relatedBy: .equal, toItem: view, attribute: .trailing, multiplier: 1, constant: 0),
NSLayoutConstraint(item: webView!, attribute: .bottom, relatedBy: .equal, toItem: view, attribute: .bottom, multiplier: 1, constant: 0),
])
}

Expand Down
8 changes: 4 additions & 4 deletions Xendit/WebViewController/WebViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ class WebViewController: UIViewController, WKScriptMessageHandler, WKNavigationD

view.addSubview(webView)
NSLayoutConstraint.activate([
NSLayoutConstraint(item: webView, attribute: .leading, relatedBy: .equal, toItem: view, attribute: .leading, multiplier: 1, constant: 0),
NSLayoutConstraint(item: webView, attribute: .top, relatedBy: .equal, toItem: view, attribute: .top, multiplier: 1, constant: 0),
NSLayoutConstraint(item: webView, attribute: .trailing, relatedBy: .equal, toItem: view, attribute: .trailing, multiplier: 1, constant: 0),
NSLayoutConstraint(item: webView, attribute: .bottom, relatedBy: .equal, toItem: view, attribute: .bottom, multiplier: 1, constant: 0),
NSLayoutConstraint(item: webView!, attribute: .leading, relatedBy: .equal, toItem: view, attribute: .leading, multiplier: 1, constant: 0),
NSLayoutConstraint(item: webView!, attribute: .top, relatedBy: .equal, toItem: view, attribute: .top, multiplier: 1, constant: 0),
NSLayoutConstraint(item: webView!, attribute: .trailing, relatedBy: .equal, toItem: view, attribute: .trailing, multiplier: 1, constant: 0),
NSLayoutConstraint(item: webView!, attribute: .bottom, relatedBy: .equal, toItem: view, attribute: .bottom, multiplier: 1, constant: 0),
])
}

Expand Down
29 changes: 29 additions & 0 deletions Xendit/Xendit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,12 @@ import Foundation
return CYBCardTypes.DANKORT
} else if isCardMaestro(cardNumber: cardNumber) {
return CYBCardTypes.MAESTRO
} else if isCardDiner(cardNumber: cardNumber) {
return CYBCardTypes.DINER
} else if isCardUnionPay(cardNumber: cardNumber) {
return CYBCardTypes.UNIONPAY
}

return CYBCardTypes.UNKNOWN
}

Expand Down Expand Up @@ -276,6 +281,30 @@ import Foundation
return false
}

// Validate Card for type Diner
private static func isCardDiner(cardNumber: String) -> Bool{
if cardNumber.count > 2 {
let index3 = cardNumber.index(cardNumber.startIndex, offsetBy: 3)
let startingNumber3 = Int(cardNumber[..<index3])!
let index2 = cardNumber.index(cardNumber.startIndex, offsetBy: 2)
let startingNumber2 = Int(cardNumber[..<index2])!
return startingNumber2 == 36 ||
startingNumber3 == 309 ||
(startingNumber3 >= 300 && startingNumber3 <= 305)
}
return false
}

// Validate Card for type UnionPay
private static func isCardUnionPay(cardNumber: String) -> Bool{
if cardNumber.count > 2 {
let index = cardNumber.index(cardNumber.startIndex, offsetBy: 2)
let startingNumber = Int(cardNumber[..<index])!
return startingNumber == 62
}
return false
}

// MARK: - Networking

private static let WEBAPI_FLEX_BASE_URL = "https://sandbox.webapi.visa.com"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class CreateTokenViewController: UIViewController {
super.viewDidLoad()

// Set Publishable Key
Xendit.publishableKey = "xnd_public_development_LH4fWw2s6eAxVR9mHAsSJZi0hx3jWEUb9dIN8lm4It4MPVNl86LIk1Hh1nDUG"
Xendit.publishableKey = "xnd_public_development_9fB0J1Ase70afEL6FPJTBrpIc5NfJCu6evsAxiHSECvUDiz6ZAKWryQObfkS"

}

Expand All @@ -45,7 +45,7 @@ class CreateTokenViewController: UIViewController {
cardData.amount = NSNumber(value: int!)
}

Xendit.createToken(fromViewController: self, cardData: cardData, shouldAuthenticate: true, onBehalfOf: "5cd8d52d9b60c752da69b9ec") { (token, error) in
Xendit.createToken(fromViewController: self, cardData: cardData, shouldAuthenticate: true) { (token, error) in
if let token = token {
// Handle successful tokenization. Token is of type XenditCCToken
let message = String(format: "TokenID - %@, Status - %@, MaskedCardNumber - %@, Should_3DS - %@", token.id, token.status, token.maskedCardNumber ?? "n/a", token.should3DS?.description ?? "n/a")
Expand Down

0 comments on commit 6523403

Please sign in to comment.