Skip to content

open-pay/openpay-swift-ios

Repository files navigation

Openpay iOS

iOS swift library for tokenizing credit/debit card and collect device information Current version: v3.0.1

Please refer to the following documentation sections for field documentation:

Requirements

  • iOS SDK 14.3+

Installation

Usage

import OpenpayKit

Create a instance object

For create an instance Openpay needs:

  • MerchantId
  • Public API Key
static let MERCHANT_ID = "merchantId"
static let API_KEY = "apiKey"

var openpay : Openpay!

func myFunction() {
	openpay = Openpay(withMerchantId: MERCHANT_ID, andApiKey: API_KEY, isProductionMode: false, isDebug: false,countryCode: "MX")
}

//You can instantiate with a country code. Currently supported codes are: MX for Mexico, CO for Colombia and PE for Peru. Any other code will do a fallback to MX an MX is the default if you dont specify a country code.

Production Mode

Use isProductionMode = true

var openpay : Openpay!

func myFunction() {
	openpay = Openpay(withMerchantId: MERCHANT_ID, andApiKey: API_KEY, isProductionMode: true, isDebug: false)
}

Create a SessionID

The framework contains a function for generate a device sessionID. The following parameters are required: -Function to call when SessionID is generated -Function to call when error occurs

var openpay : Openpay!

func myFunction() {
        openpay = Openpay(withMerchantId: MERCHANT_ID, andApiKey: API_KEY, isProductionMode: false, isDebug: false)
        openpay.createDeviceSessionId(successFunction: successSessionID, failureFunction: failSessionID)
}

func successSessionID(sessionID: String) {
        print("SessionID: \(sessionID)")
}

func failSessionID(error: NSError) {
        print("\(error.code) - \(error.localizedDescription)")
}

Display Card Form

The framework contains a form for the user directly capture his card's minimum required data. For display the form you need to pass the following parameters: -Current UIViewController -Function to call when capture ends -Function to call when error occurs -The title to display at the top of form

var openpay : Openpay!

func myFunction() {
        openpay = Openpay(withMerchantId: MERCHANT_ID, andApiKey: API_KEY, isProductionMode: false, isDebug: false)
        openpay.loadCardForm(in: self, successFunction: successCard, failureFunction: failCard, formTitle: "Openpay")
}

func successCard() {

}

func failCard(error: NSError) {
	print("\(error.code) - \(error.localizedDescription)")
}

Create a token

For more information about how to create a token, please refer to [Create a token] (http://www.openpay.mx/docs/api/#crear-un-nuevo-token)

With only required fields captured by the user
var openpay : Openpay!

func myFunction() {
        openpay = Openpay(withMerchantId: MERCHANT_ID, andApiKey: API_KEY, isProductionMode: false, isDebug: false)
        openpay.loadCardForm(in: self, successFunction: successCard, failureFunction: failCard, formTitle: "Openpay")
}

func successCard() {
	openpay.createTokenWithCard(address: nil, successFunction: successToken, failureFunction: failToken)
}

func failCard(error: NSError) {
	print("\(error.code) - \(error.localizedDescription)")
}

func successToken(token: OPToken) {
        print("TokenID: \(token.id)")
}

func failToken(error: NSError) {
        print("\(error.code) - \(error.localizedDescription)")
}
With custom address
func successCard() {
print("Success Card Capture...")

        let addressDictionary: Dictionary<String, Any> = [
            "postal_code":"76000",
            "line1":"Av 5 de Febrero",
            "line2":"Roble 207",
            "line3":"Col. Felipe",
            "city":"Querétaro",
            "state":"Querétaro",
            "country_code":"MX"
        ]
        
        // create token with card info from default form
        openpay.createTokenWithCard(address: OPAddress(with: addressDictionary), successFunction: successToken, failureFunction: failToken)
    }
With custom form
 let card = TokenizeCardRequest(cardNumber: cardNumber,holderName:holderName, expirationYear: expirationYear, expirationMonth: expirationMonth, cvv2: cvv2)
        
// request token by card model
openpay.tokenizeCard(card: card) { (OPToken) in
    print(OPToken.id)            
} failureFunction: { (NSError) in
    print(NSError)
}