omise/omise-ios
[Omise](https://docs.omise.co/) is a payment service provider operating
Security Warning
Please do NOT use Omise iOS SDK versions less than 3.2.0, as they are outdated and have security vulnerabilities.
Requirements
- Omise API public key. Register for an Omise account to obtain your API keys.
- iOS 14 or higher deployment target.
- Xcode 15.0 or higher (Xcode 26 is recommended)
- Swift 5.0 or higher (Swift 5.3 is recommended)
Merchant compliance
Card data should never transit through your server. We recommend that you follow our guide on how to safely collect credit information.
To be authorized to create tokens on your server, you must have a currently valid PCI-DSS Attestation of Compliance (AoC) delivered by a certified QSA Auditor. This SDK provides the means to tokenize card data through the end user's mobile phone without this data having to go through your server.
Installation
To integrate the Omise SDK into your Xcode project using the Swift Package Manager, proceed with the following steps:
- In Xcode, select
File>Swift Packages>Add Package Dependency... - Enter the URL for this repository
https://github.com/omise/omise-ios.git - Choose the latest available
5.6.0release - Select your target, go to
Frameworks, Libraries, and Embedded Content, and set OmiseSDK toEmbed & Sign
Usage
The Omise iOS SDK provides an easy-to-use library for calling the
Omise API and presenting UI forms.
The main class and protocol for the Omise iOS SDK are `OmiseSDK` and `ClientProtocol` through
which all requests to the Omise API will be sent.
To start working with OmiseSDK, you must create a new instance of the `OmiseSDK` class with an Omise public key.
```swift
import OmiseSDK
let omiseSDK = OmiseSDK(publicKey: "omise_public_key")
```
You can also set up and use a `shared` instance of `OmiseSDK` in your code:
```swift
OmiseSDK.shared = omiseSDK
```
If you cloned this project to your local hard drive, you can check out `ExampleApp.xcodeproj`.
#### ApplePay
To enable Apple Pay in your app, configure your Apple Pay Merchant Identifier using the setupApplePay method.
Refer to the ExampleApp sample for a complete implementation.
For more information, please review at [Omise Apple Pay Documentation](https://docs.omise.co/applepay/singapore).
```swift
omiseSDK.setupApplePay(for: "your_merchant_id", requiredBillingAddress: true)
```
Replace "your_merchant_id" with your actual Apple Pay Merchant Identifier.
### Omise API
The SDK currently
supports two main categories of requests: **Tokenizing a
Card** and **Creating a Payment Source**.
#### Creating a card token
Normally, merchants must not send credit or debit card data to their own
servers. To collect a card payment from a
customer, merchants must first *tokenize* the card data using the
Omise API and then use the generated token in place of the card
data. You can tokenize card data by creating and initializing
a `CreateTokenPayload.Card` as follows:
```swift
let createTokenPayload = CreateTokenPayload.Card(
name: "JOHN DOE",
number: "4242424242424242",
expirationMonth: 11,
expirationYear: 2022,
securityCode: "123"
)
let createTokenPayloadWithAddress = CreateTokenPayload.Card(
name: "JOHN DOE",
number: "4242424242424242",
expirationMonth: 11,
expirationYear: 2022,
securityCode: "123",
phoneNumber: "0123456789",
countryCode: "TH",
city: "Bangkok",
state: "Bangkok",
street1: "Sukhumvit",
street2: "",
postalCode: "10110"
)
```
#### Creating a payment source
Omise supports many payment methods other than cards. You
may request a payment with one of those supported payment methods from
a customer by calling the `CreateSource` API. You need to specify
the parameters (e.g., payment amount and currency) of the source you
want to create by creating and initializing a `CreateSourcePayload` with
the `Payment Information` object:
```swift
let createSourcePayload = CreateSourcePayload(
amount: amount,
currency: currency,
details: .sourceType(.mobileBankingBBL) // Bangkok Bank mobile banking payment method
)
```
After creating the token or the payment source, create the completion handler, as follows:
#### Creating the completion handler
A simple completion handler for a token, for example, is as follows:
```swift
func tokenCompletionHandler(tokenResult: Result<Token, Error>) -> Void {
switch tokenResult {
case .success(let token):
// do something with Token id
print(token.id)
case .failure(let error):
print(error)
}
}
```
Now, send the request:
#### Sending the request
Whether you are charging a source or a card, sending the
request is the same. Use the `ClientProtocol` to perform one-off API calls
with the completion handler block.
```swift
let client = omiseSDK.client
client.createToken(payload: createTokenPayload, tokenCompletionHandler)
client.createSource(payload: createSourcePayload, sourceCompletionHandler)
client.capability(capabilityCompletionHandler)
client.token(tokenID: "tokenID", tokenInfoCompletionHandler)
client.observeChargeStatus(chargeStatusCompletionHandler)
```
### Using built-in forms
Omise iOS SDK provides easy-to-use drop-in UI forms for both Tokenizing a Card and Creating a Payment Source, which
you can easily integrate into your application.
#### Card form
The `omiseSDK.presentCreditCardPayment()` provides a pre-made card form that will automatically
[tokenize card information](https://docs.omise.co/security-best-practices) for you.
##### Use Omise card form
To use the controller in your application, modify your view controller with the following additions:
```swift
import OmiseSDK
class ViewController: UIViewController {
private let omiseSDK = OmiseSDK("pkey_test_123")
@IBAction func displayCreditCardPayment() {
omiseSDK.presentCreditCardPayment(from: self, delegate: self)
}
}
```
You can provide extra parameters:
- `countryCode` to preselect the country in the UI form
- `handleErrors` to process and display errors (by default, is `true`); if `false`, then the delegate
method `choosePaymentMethodDidComplete(with error: Error)` will be called instead.
```swift
omiseSDK.presentCreditCardPayment(
from: self,
countryCode: "TH", // if `nil` it will use country from Capability API
handleErrors: true, // by default `true`
delegate: self
)
```
Then implement the delegate to receive the `Token` object after the user has entered the card data:
```swift
extension ViewController: ChoosePaymentMethodDelegate {
func choosePaymentMethodDidComplete(with token: Token) {
// Send `Token` to your server to create a charge or a customer object.
print(token.id) // prints Token ID
}
func choosePaymentMethodDidComplete(with error: Error) {
// Only called if we set `handleErrors = false`.
// You can send errors to a logging service or display them here to the user.
}
}
```
You can call `OmiseSDK.dismiss(animated:completion:)` to close UI form presented by OmiseSDK:
```swift
func choosePaymentMethodDidComplete(with token: Token) {
omiseSDK.dismiss {
// present another screen
}
}
```
##### Creating a custom card form
You can create your card form, but please remember you must not send the card information to your server.
Omise iOS SDK provides the following built-in card UI components to make it easier to create your card form:
* `CardNumberTextField` - Provides basic number grouping as the user types.
* `CardNameTextField` - Cardholder name field.
* `CardExpiryDateTextField` - Provides card expiration date input and styling
* `CardExpiryDatePicker` - `UIPickerView` implementation with a month and year column.
* `CardCVVTextField` - CVV number field.
Additionally, fields automatically turn red if their content fails
basic validation (e.g., alphabetic characters in the number field,
content with wrong length), and come in two supported styles: plain
and border.
#### Built-in Choose Payment Methods controller
The `presentChoosePaymentMethod` function of `OmiseSDK` presents a pre-made form that lets customers choose how they want to make a payment.
Please note that the presented controller is designed to be used as-is, and you should not push your view controllers into its navigation controller stack.
You can configure it to display either specified payment method options or a default list based on your country.
##### Use Choose Payment Methods controller
To use the controller in your application, call `presentChoosePaymentMethod`.
```swift
omiseSDK.presentChoosePaymentMethod(
from: self,
amount: paymentAmount,
currency: paymentCurrencyCode,
delegate: self
)
```
You can provide extra parameters:
- `allowedPaymentMethods` to display given payment methods (if presented in Capability API)
- `forcePaymentMethods` to override payment methods from Capability and display only `allowedPaymentMethods`
- `isCardPaymentAllowed` to display or hide the Credit Card payment method
- `handleErrors` to process and display errors (by default, is `true`); if `false`, then the delegate
method `choosePaymentMethodDidComplete(with error: Error)` will be called instead.
```swift
let allowedPaymentMethods = SourceType.availableByDefaultInThailand
omiseSDK.presentChoosePaymentMethod(
from: self,
amount: paymentAmount,
currency: paymentCurrencyCode,
allowedPaymentMethods: allowedPaymentMethods,
forcePaymentMethods: true,
isCardPaymentAllowed: true,
handleErrors: true,
delegate: self
)
```
Then implement the delegate to receive the `Source` or `Token` object after the user has selected:
```swift
extension ProductDetailViewController: ChoosePaymentMethodDelegate {
func choosePaymentMethodDidComplete(with source: Source) {
omiseSDK.dismiss {
print(source.id) // prints Source ID
}
}
func choosePaymentMethodDidComplete(with token: Token) {
omiseSDK.dismiss {
print(token.id) // prints Token ID
}
}
func choosePaymentMethodDidComplete(with error: Error) {
// Only called if we set `handleErrors = false`.
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() {
omiseSDK.dismiss()
}
}
```Objective-C compatibility
This version of Omise iOS SDK does not support Objective-C. For full Objective-C support, use this version.
Contributing
Pull requests, issues, and bug fixes are welcome!
License
Package Metadata
Repository: omise/omise-ios
Default branch: master
README: README.md