Skip to content

Commit

Permalink
Merge pull request omise#278 from omise/feature/MIT-2260-Readme
Browse files Browse the repository at this point in the history
Readme v5.0.0
  • Loading branch information
Andrei Solovev authored Mar 13, 2024
2 parents 6d3442b + 934aa79 commit b03b87f
Show file tree
Hide file tree
Showing 5 changed files with 222 additions and 177 deletions.
26 changes: 13 additions & 13 deletions ExampleApp/Views/ProductDetailViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,19 @@ class ProductDetailViewController: BaseViewController {
extension ProductDetailViewController: AuthorizingPaymentViewControllerDelegate {
func authorizingPaymentViewController(_ viewController: AuthorizingPaymentViewController, didCompleteAuthorizingPaymentWithRedirectedURL redirectedURL: URL) {
print(redirectedURL)
dismiss(animated: true, completion: nil)
omiseSDK.dismiss()
}

func authorizingPaymentViewControllerDidCancel(_ viewController: AuthorizingPaymentViewController) {
dismiss(animated: true, completion: nil)
omiseSDK.dismiss()
}
}

// MARK: - Custom Credit Card Form View Controller Delegate

extension ProductDetailViewController: CustomCreditCardPaymentControllerDelegate {
func creditCardFormViewController(_ controller: CustomCreditCardPaymentController, didSucceedWithToken token: Token) {
dismissForm {
omiseSDK.dismiss {
let alertController = UIAlertController(
title: "Token Created",
message: "A token with id of \(token.id) was successfully created. Please send this id to server to create a charge.",
Expand All @@ -98,7 +98,7 @@ extension ProductDetailViewController: CustomCreditCardPaymentControllerDelegate
}

func creditCardFormViewController(_ controller: CustomCreditCardPaymentController, didFailWithError error: Error) {
dismissForm {
omiseSDK.dismiss {
let alertController = UIAlertController(
title: "Error",
message: error.localizedDescription,
Expand All @@ -114,7 +114,7 @@ extension ProductDetailViewController: CustomCreditCardPaymentControllerDelegate
/// Processing result of choosing Payment Method screen
extension ProductDetailViewController: ChoosePaymentMethodDelegate {
func choosePaymentMethodDidComplete(with source: Source) {
dismissForm {
omiseSDK.dismiss {
let alertController = UIAlertController(
title: "Source Created\n(\(source.paymentInformation.sourceType.rawValue))",
message: "A source with id of \(source.id) was successfully created. Please send this id to server to create a charge.",
Expand All @@ -127,7 +127,7 @@ extension ProductDetailViewController: ChoosePaymentMethodDelegate {
}

func choosePaymentMethodDidComplete(with token: Token) {
dismissForm {
omiseSDK.dismiss {
let alertController = UIAlertController(
title: "Token Created",
message: "A token with id of \(token.id) was successfully created. Please send this id to server to create a charge.",
Expand All @@ -140,15 +140,15 @@ extension ProductDetailViewController: ChoosePaymentMethodDelegate {
}

func choosePaymentMethodDidComplete(with error: Error) {
dismissForm {
let alertController = UIAlertController(title: "Error", message: error.localizedDescription, preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alertController.addAction(okAction)
self.present(alertController, animated: true, completion: nil)
}
let alertController = UIAlertController(title: "Error", message: error.localizedDescription, preferredStyle: .alert)
let okAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)
alertController.addAction(okAction)

let vc = omiseSDK.presentedViewController ?? self
vc.present(alertController, animated: true, completion: nil)
}

func choosePaymentMethodDidCancel() {
dismissForm()
omiseSDK.dismiss()
}
}
31 changes: 27 additions & 4 deletions OmiseSDK/Sources/OmiseSDK.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public class OmiseSDK {
Country(code: latestLoadedCapability?.countryCode)
}

public private(set) weak var presentedViewController: UIViewController?

/// Creates a new instance of Omise SDK that provides interface to functionallity that SDK provides
///
/// - Parameters:
Expand All @@ -64,6 +66,7 @@ public class OmiseSDK {
///
/// - Parameters:
/// - from: ViewController is used to present Choose Payment Methods
/// - animated: Presents controller with animation if `true`
/// - amount: Payment amount
/// - currency: Payment currency code (ISO 4217)
/// - allowedPaymentMethods: Custom list of payment methods to be shown in the list. Only payment methods presented in the Capabilities will be shown
Expand All @@ -73,6 +76,7 @@ public class OmiseSDK {
/// - completion: Completion handler triggered when payment completes with Token, Source, Error or was Cancelled
public func presentChoosePaymentMethod(
from topViewController: UIViewController,
animated: Bool = true,
amount: Int64,
currency: String,
allowedPaymentMethods: [SourceType] = [],
Expand All @@ -81,6 +85,8 @@ public class OmiseSDK {
handleErrors: Bool = true,
delegate: ChoosePaymentMethodDelegate
) {
dismiss(animated: false)

let paymentFlow = ChoosePaymentCoordinator(
client: client,
amount: amount,
Expand All @@ -106,22 +112,27 @@ public class OmiseSDK {
navigationController.navigationBar.prefersLargeTitles = true
}

topViewController.present(navigationController, animated: true, completion: nil)
topViewController.present(navigationController, animated: animated, completion: nil)
presentedViewController = navigationController
}

/// Creates and presents modal "Credit Card Payment" controller with a given parameters
///
/// - Parameters:
/// - from: ViewController is used to present Choose Payment Methods
/// - countryCode: Delegate to be notified when Source or Token is created
/// - animated: Presents controller with animation if `true`
/// - countryCode: Country to be preselected in the form. If `nil` country from Capabilities will be used instead.
/// - handleErrors: If `true` the controller will show an error alerts in the UI, if `false` the controller will notify delegate
/// - delegate: Delegate to be notified when Source or Token is created
public func presentCreditCardPayment(
from topViewController: UIViewController,
animated: Bool = true,
countryCode: String? = nil,
handleErrors: Bool = true,
delegate: ChoosePaymentMethodDelegate
) {
dismiss(animated: false)

let paymentFlow = ChoosePaymentCoordinator(
client: client,
amount: 0,
Expand All @@ -137,23 +148,28 @@ public class OmiseSDK {
navigationController.navigationBar.prefersLargeTitles = true
}

topViewController.present(navigationController, animated: true, completion: nil)
topViewController.present(navigationController, animated: animated, completion: nil)
presentedViewController = navigationController
}

/// Creates and presents Authorizing Payment controller with a given parameters
///
/// - Parameters:
/// - from: ViewController is used to present Choose Payment Methods
/// - animated: Presents controller with animation if `true`
/// - authorizedURL: The authorized URL given in `Charge` object
/// - expectedReturnURLPatterns: The expected return URL patterns.
/// - delegate: A delegate object that will recieved authorizing payment events.
@available(iOSApplicationExtension, unavailable)
public func presentAuthorizedController(
from topViewController: UIViewController,
animated: Bool = true,
authorizedURL: URL,
expectedReturnURLPatterns: [URLComponents],
delegate: AuthorizingPaymentViewControllerDelegate
) {
dismiss(animated: false)

let viewController = AuthorizingPaymentViewController(nibName: nil, bundle: .omiseSDK)
viewController.authorizedURL = authorizedURL
viewController.expectedReturnURLPatterns = expectedReturnURLPatterns
Expand All @@ -168,7 +184,14 @@ public class OmiseSDK {
navigationController.navigationBar.prefersLargeTitles = true
}

topViewController.present(navigationController, animated: true, completion: nil)
topViewController.present(navigationController, animated: animated, completion: nil)
presentedViewController = navigationController
}

/// Dismiss any presented UI form by OmiseSDK
public func dismiss(animated: Bool = true, completion: (() -> Void)? = nil) {
guard let presentedViewController = presentedViewController else { return }
presentedViewController.dismiss(animated: animated, completion: completion)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,14 @@ class CreditCardPaymentController: UIViewController {
secureCodeTextField.rightView = cvvInfoButton
secureCodeTextField.rightViewMode = .always

let cancelButtonItem = UIBarButtonItem(
image: UIImage(omise: "Close"),
style: .plain,
target: self,
action: #selector(cancelForm)
)
navigationItem.rightBarButtonItem = cancelButtonItem

if let viewModel = viewModel {
bind(to: viewModel)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,51 +71,5 @@ class CreditCardPaymentViewModel: CreditCardPaymentViewModelProtocol, CountryLis
)

delegate?.didSelectCardPayment(card, completion: completion)

/*
viewModel.onSubmitButtonPressed(makeViewContext(), publicKey: publicKey) { [weak self] result in
guard let self = self else { return }
self.stopActivityIndicator()
switch result {
case .success(let token):
os_log("Credit Card Form's Request succeed %{private}@, trying to notify the delegate",
log: uiLogObject,
type: .default,
token.id)
self.delegate?.creditCardFormViewController(self, didSucceedWithToken: token)
case .failure(let error):
self.handleError(error)
}
}
*/

// guard let publicKey = publicKey else {
// os_log("Missing or invalid public key information - %{private}@", log: uiLogObject, type: .error, publicKey ?? "")
// assertionFailure("Missing public key information. Please set the public key before request token.")
// return
// }
//
// os_log("Requesting to create token", log: uiLogObject, type: .info)
//
// let payload = CreateTokenPayload.Card(
// name: viewContext.name,
// number: viewContext.number,
// expirationMonth: viewContext.expirationMonth,
// expirationYear: viewContext.expirationYear,
// securityCode: viewContext.securityCode,
// phoneNumber: nil,
// countryCode: viewContext.countryCode,
// city: viewContext[.city],
// state: viewContext[.state],
// street1: viewContext[.address],
// street2: nil,
// postalCode: viewContext[.postalCode]
// )
//
// let client = OmiseSDK(publicKey: publicKey).client
// client.createToken(payload: payload) { result in
// onComplete(result)
// }
}

}
Loading

0 comments on commit b03b87f

Please sign in to comment.