Deep linking from your iOS mobile app
You can use deep linking to navigate users from your mobile app to payment screens in the AffiniPay, CPACharge, or LawPay mobile app (the target app). Using these payment screens and an optional AffiniPay card reader or manual entry, your users submit card payment information securely in the target app and then return seamlessly to your mobile app. Your backend system then submits the charge.
To take advantage of deep linking:
- You must have an AffiniPay payment integration. You’ll use the credentials you obtained during the OAuth process to submit payment information on behalf of your users, who are AffiniPay merchants.
- Your users must have both your mobile app and the target app installed.
Obtain the payment token
1: Set a scheme name to identify your mobile app
To set a scheme name for your mobile app:
- In Xcode, select your mobile app project in the sidebar.
- Click the Info** tab.
- In the URL Types** section, add a URL type and set the identifier and the URL scheme. In the following examples,
partnerapp
is the scheme name.
2: Create a deep link URL with payment details
You must create the URL that will initiate the deep linking process from your mobile app. Here is an example URL:
ioslawpay://charge?amount=100&publicKey=m_Nz_JZ1wDEoSP7uRTQnIqY&accountName=Operating&demoMode=1&disableCardReader=0&requireCvv=0&requireSwipeCvv=1&trustAccount=0&&requiredPaymentFields=cvv,address1,postal_code&swipeRequiredPaymentFields=cvv,address1,postal_code&continue=partnerapp://
The deep link URL starts with the scheme name for the target app. In this example, ioslawpay
is the scheme name and it provides a deep link to the LawPay app.
- To deep link to the AffiniPay app, use
iosaffinipay
. - To deep link to the CPACharge app, use
ioscpacharge
.
You must include the required parameters publicKey
and continue
in the deep link URL.
Required parameters
Parameter | Type | Description |
---|---|---|
publicKey | String | The merchant’s public key. |
continue | String | The scheme name for your mobile app (partnerapp is used in the examples on this page). |
Optional parameters
Parameter | Type | Description |
---|---|---|
amount | String | The amount to charge, in terms of the currency’s smallest unit. For USD, this is the amount in cents. |
accountName | String | The name of the merchant account. |
demoMode | String | Whether the transaction is a demo transaction (if set to 1 ) or live transaction (if set to 0 ). |
disableCardReader | String | Whether the card reader should be disabled and transactions should be manual entry only (if set to 1 ) or the card reader should be enabled (if set to 0 ). |
requireCvv | String | Whether CVV is required for manual transactions (if set to 1 ) or CVV is not required for manual transactions (if set to 0 ). |
requireSwipeCvv | String | Whether CVV is required for swiper transactions (if set to 1 ) or CVV is not required for swiper transactions (if set to 0 ). |
requiredPaymentFields | String | Which fields are required for manual transactions, such as cvv,address1,postal_code . |
swipeRequiredPaymentFields | String | Which fields are required for swiper transactions, such as cvv,address1,postal_code . |
trustAccount | String | Whether the account type is Trust (if set to 1 ) or not (if set to 0 ). |
3: Send the payment token request to the target app
To send the payment request (including the deep link URL) to the target app:
Add the following lines of code to ViewController.swift.
@IBAction func openapp(_ sender: UIButton) { let originalUrlString = "ioslawpay://charge?amount=100&publicKey=m_Nz_JZ1wDEoSP7uRTQnIqY&accountName=Operating&demoMode=1&disableCardReader=0&requireCvv=0&requireSwipeCvv=1&trustAccount=0&&requiredPaymentFields=cvv,address1,postal_code&swipeRequiredPaymentFields=cvv,address1,postal_code&continue=partnerapp://" let urlString = originalUrlString.replacingOccurrences(of: "", with: "%20") let customURL = URL(string: urlString) if UIApplication.shared.canOpenURL(customURL!) { if #available(iOS 10.0, *) { UIApplication.shared.open(customURL!, options: [:], completionHandler: nil) } else { UIApplication.shared.openURL(customURL!) } } else { //Print alert here } }
Add the following lines of code to AppDelegate.swift.
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool { if (options[UIApplication.OpenURLOptionsKey.sourceApplication] as! String == "com.affinipay.IOSLawPay") { handleResponseFromLawPay(url: url) } return true }
4: Parse the response
After the payment details are processed, the target app returns the following parameters:
Parameter | Type | Description |
---|---|---|
success | String | Whether tokenization was successful (true ) or not (false ). |
reason | String | When the tokenization fails, details are returned, such as “User cancelled the charge” or “The one-time token cannot be created”. |
clientDetailsInfo | JSON String | When a payment token is created, the token ID and the payer details submitted in the target app are returned. Here is an example: {\"id\":\"ONZ-48zBR-SrR1wh3UNcAw\",\"email\":\"abc@aexample.com\",\"phone\":\"(123) 456-7890\",\"reference\":\"Ref Value\",\"city\":\"Austin\",\"country\":\"US\",\"name\":\"John Doe\",\"address1\":\"A/502 Street Lane\",\"address2\":\"Near Cross Road\",\"postalCode\":\"37701\"} |
Add the following code to AppDelegate.swift to parse this data.
func handleResponseFromLawPay(url: URL) {
let queryParams = url.queryParameters
print("query params: ", queryParams!)
if queryParams!["success"] == "true" {
showSuccessAlert(oneTimeToken: queryParams!["oneTimeToken"]!)
}
else if queryParams!["success"] == "true" {
if (queryParams!["reason"] != nil) {
showFailedAlert(errorMessage: queryParams!["reason"]!)
} else {
showFailedAlert(errorMessage: "Unknown error. Please try again.")
}
}
}
func showSuccessAlert(oneTimeToken: String) {
let alert = UIAlertController(title: "Success", message: "One-time-token generated : " + oneTimeToken, preferredStyle: .alert)
alert.addAction(UIAlertAction(
title: "Ok",
style: .cancel,
handler: { (action) in }
))
window?.rootViewController?.present(alert, animated: true, completion: nil)
}
func showFailedAlert(errorMessage: String) {
let alert = UIAlertController(title: "Error", message: errorMessage, preferredStyle: .alert)
alert.addAction(UIAlertAction(
title: "Ok",
style: .cancel,
handler: { (action) in }
))
window?.rootViewController?.present(alert, animated: true, completion: nil)
}
Submit the charge
To submit the charge:
- Hand off the payment token and payment details to your backend server, which is already integrated with AffiniPay.
- Create a charge from your backend server.