iOS Keyed Payments Guide
Requirements
- Xcode 8+
- iOS 9+
- Omni Webpayments Token
Installation
Use CocoaPods to install the Fattmerchant iOS SDK.
- Install CocoaPods
- Add
pod 'Fattmerchant'
to yourPodfile
- Run
pod install
Create a PaymentMethod
To accept a payment, you’ll need to collect information from the customer and tokenize it to create an Omni PaymentMethod
. You will then be able to use this PaymentMethod
with Fattmerchant’s Omni API to run the transaction.
Setup
You’ll first need to setup the FattmerchantApi
for usage. Set the webPaymentsToken
field on the shared FattmerchantConfiguration
instance. All new FattmerchantApi
instances will then use that configuration by default.
import UIKit
import Fattmerchant
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
FattmerchantConfiguration.shared.webPaymentsToken = "mywebpaymentstoken"
// ...
return true
}
}
Alternatively, you may create a configuration object and pass it to the new FattmerchantApi
instance as you need it.
let configuration = FattmerchantConfiguration(webPaymentsToken: "mywebpaymentstoken")
let fattClient = FattmerchantApi(configuration: configuration)
Collect payment information
You first want to collect card information and populate a CreditCard
object.
let card = CreditCard(personName: "Joan Parsnip",
cardNumber: "4111111111111111",
cardExp: "1220",
addressZip: "32814")
Create the PaymentMethod
Once you have a CreditCard
object, call the tokenize(:)
method on FattmerchantClient
object and pass a listener to be notified once tokenization is complete.
let fattClient = FattmerchantApi(webPaymentsToken: "mywebpaymentstoken")
fattClient.tokenize(card) { (response) in
if case let .success(paymentMethod) = response {
// You can now use `paymentMethod` with the Fattmerchant Omni API
}
}
Testing
If you’d like to try tokenization without real payment information, you can use the CreditCard.testCreditCard()
method to get a test credit card.
let creditCard = CreditCard.testCreditCard()
If you want to test failures, you can use the following method
let failingCreditCard = CreditCard.failingTestCreditCard()
Or you can create the CreditCard
object with the following testing payment information:
Credit card numbers
Card Type | Good Card | Bad Card |
---|---|---|
VISA | 4111111111111111 | 4012888888881881 |
Mastercard | 5555555555554444 | 5105105105105100 |
Amex | 378282246310005 | 371449635398431 |
Discover | 6011111111111117 | 6011000990139424 |
JCB | 3569990010030400 | 3528327757705979 |
Diners Club | 30569309025904 | 30207712915383 |
Use any CVV number for the above
Taking a Payment
Now that you have the token representing the payment method, you can use the POST /charge
resource on the Omni API. this will allow you to create a transaction with the payment method. payment_method_id
is a required field, where you will need to pass in the id of the payment method that you received from the tokenize(:)
method. You can use this PaymentMethod
anywhere that you need in the Omni API.
Here is an example:
/// This is a sample request body for Omni POST /charge
struct ChargeRequestBody: Codable {
struct Meta: Codable {
var tax: Double
var subtotal: Double
}
var total: Double
var payment_method_id: String
var pre_auth: Bool = false
var meta: Meta
}
func takePayment(dollars: Double, paymentMethodId: String) {
// Create URLRequest
let chargeRoute = URL(string: "https://apiprod.fattlabs.com/charge")!
var request = URLRequest(url: chargeRoute)
request.httpMethod = "POST"
// Add headers
request.setValue("Bearer \(apiKey)", forHTTPHeaderField: "Authorization")
request.setValue("application/json", forHTTPHeaderField: "Accept")
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
// Add body
let body = ChargeRequestBody(total: dollars,
payment_method_id: paymentMethodId,
meta: ChargeRequestBody.Meta(tax: 0.0, subtotal: dollars))
request.httpBody = try! JSONEncoder().encode(body)
// Send request
let task = URLSession(configuration: .ephemeral).dataTask(with: request) { (data, response, error) in
if let data = data {
// data should have the PaymentMethod
}
}
task.resume()
}