Requesting a Payment

The following steps summarize an in-app payment request:

  • 3rd party backend uses the Scan to Pay API to create a transaction code (which contains the amount, references, etc.).
  • The 3rd party app calls Scan to PayKit, passing in this newly created code.
  • The Scan to Pay in-app library will facilitate the payment and call a delegate method to return a result and reference indicating the payment status.
  • The 3rd backend will then query the Scan to Pay system with the returned reference to guarantee the payment status. Alternatively, the 3rd backend will query itself to see if Scan to Pay has notified it of a new payment.

📘

Note

It is imperative that the 3rd party app does not trust the library's result as the final status. A backend API call must be done to verify the payment status from the notification.

Calling the Checkout Method

The following snippet illustrates how to invoke the payment request from Swift:

import Scan to PayKit
@IBAction func checkoutAction(sender: AnyObject) {
let code = "6799782555"
let apiKey = "1234567890"
let system = MPSystem.test

let Scan to Pay = MPScan to Pay(); Scan to Pay.checkout(withCode:code,
apiKey:apiKey, system: system, controller: self, delegate:self)
//or
let preMsisdn = "27123456789"

Scan to Pay.checkout(withCode:code, apiKey:apiKey,
system: system, controller: self, delegate:self, preMSISDN: preMsisdn)
}

Parameters passed:

NameTypeDescription
codeStringThis is the code created by calling the Scan to Pay API representing the transaction.
apiKeyStringThis is the API key provided by Scan to Pay that will enable the library to be used. This can be found on the Scan to Pay Portal under the Lib Lite Tokens menu item.
systemMPSystemThis value can be Live or Test, representing the Scan to Pay backend system to connect to.
controllerUIViewControllerThe presenting controller.
delegateMPScan to PayDelegateThe callback delegate that will receive the results.
preMsisdnStringThis is an optional field if you know the client’s mobile number.

Calling the Preregister Method

The following snippet illustrates how to invoke the pre-register request from Swift:

import Scan to PayKit

@IBAction func preRegister(sender: AnyObject) {
let apiKey = "1234567890"
let system = MPSystem.test

let Scan to Pay = MPScan to Pay(); Scan to Pay.preRegister(apiKey,
system: system, controller: self, delegate:self)
//or
let preMsisdn = "27123456789"
Scan to Pay.preRegister(apiKey,
system: system, controller: self, delegate:self, preMSISDN: preMsisdn)
}

Parameters passed:

NameTypeDescription
apiKeyStringThis is the API key provided by Scan to Pay that will enable the library to be used. This can be found on the Scan to Pay Portal under the Lib Lite Tokens menu item.
systemMPSystemThis value can be Live or Test, representing the Scan to Pay backend system to connect to.
controllerUIViewControllerThe presenting controller.
delegateMPScan to PayDelegateThe callback delegate that will receive the results.
preMsisdnStringThis is an optional field if you know the client’s mobile number

Calling the Manage Card List

This method allows a 3rd party app to send a request to the Scan to Pay SDK in order for the cardholder to
“manage” their card list. Once invoked, the user is then able to add or delete bank cards.

import Scan to PayKit

@IBAction func manageCardList(sender: AnyObject) {
let apiKey = "1234567890"
let system = MPSystem.test

let Scan to Pay = MPScan to Pay(); Scan to Pay.walletManagement(apiKey,
system: system, controller: self, delegate: self);
//or
let preMsisdn = "27123456789"
Scan to Pay.walletManagement(apiKey,
system: system, controller: self, delegate: self,
preMSISDN: preMsisdn);
}

Parameters passed:

NameTypeDescription
apiKeyStringThis is the API key provided by Scan to Pay that will enable the library to be used. This can be found on the Scan to Pay Portal under the Lib Lite Tokens menu item.
systemMPSystemThis value can be Live or Test, representing the Scan to Pay backend system to connect to.
controllerUIViewControllerThe presenting controller.
delegateMPScan to PayDelegateThe callback delegate that will receive the results.
preMsisdnStringThis is an optional field if you know the client’s mobile number

Handling the Response

The following snippet illustrates how to implement the delegate to handle the response:

import UIKit
import Scan to PayKit
class ViewController: UIViewController, MPScan to PayDelegate 
{


func Scan to PayInvalidCode() { let alert = UIAlertController(
title: "Scan to Pay Resutl", message: "Invalid Code", preferredStyle: .Alert)
alert.addAction(UIAlertAction( title: "Cancel",
style: .Cancel, handler: nil));
self.presentViewController( alert,
animated: true, completion: nil);
}

func Scan to PayError(error: MPError) { let alert = UIAlertController(
title: "Scan to Pay Resutl", message: "Error", preferredStyle: .Alert)
alert.addAction(UIAlertAction( title: "Cancel",
style: .Cancel, handler: nil));
self.presentViewController( alert,
animated: true, completion: nil);
}

func Scan to PayPaymentFailedWithTransactionReference( transactionReference: String!) 
{
let alert = UIAlertController( title: "Scan to Pay Resutl",
message: "Payment Failed. Ref: \(transactionReference)", preferredStyle: .Alert)
alert.addAction(UIAlertAction( title: "Cancel",
style: .Cancel, handler: nil));
self.presentViewController( alert,
animated: true, completion: nil);
}

func Scan to PayPaymentSucceededWithTransactionReference( transactionReference: String!) 
{
let alert = UIAlertController( title: "Scan to Pay Resutl",
message: "Payment Succeeded. Ref: \(transactionReference)", preferredStyle: .Alert)
alert.addAction(UIAlertAction( title: "Cancel",
style: .Cancel, handler: nil));
self.presentViewController( alert,
 
animated: true, completion: nil);
}

func Scan to PayUserDidCancel() { let alert = UIAlertController(
title: "Scan to Pay Resutl", message: "User did cancel", preferredStyle: .Alert)
alert.addAction(UIAlertAction( title: "Cancel",
style: .Cancel, handler: nil));
self.presentViewController( alert,
animated: true, completion: nil);
}
func Scan to PayUserRegistered() {
let alert = UIAlertController( title: "Scan to Pay Result", message: "User registered", preferredStyle: .alert)
alert.addAction(UIAlertAction( title: "Cancel",
style: .cancel, handler: nil));
self.present( alert, animated: true,
completion: nil);
}

func Scan to PayUserCompletedWallet() {
let alert = UIAlertController( title: "Scan to Pay Result",
message: "User Done with Wallet", preferredStyle: .alert)
alert.addAction(UIAlertAction( title: "Cancel",
style: .cancel, handler: nil));
self.present( alert, animated: true,
completion: nil);
}
}

The following is a breakdown of the delegate callback methods:

Result CodeDescriptionExtra Values
Scan to PayErrorReturned if an error has occurred before the paymentMPError – This enum indicates the type of error that occurred. See the error table below
Scan to PayPaymentSucceededWithTransactionReferenceReturned on payment successtransactionReference - This is the reference for the transaction. This reference will be used to tie up the transaction on the 3rd party backend system.
Scan to PayPaymentFailedWithTransactionReferenceReturned if they payment failedtransactionReference - This is the reference for the failed transaction in the event the 3rd party needs to request more information regarding the failure.
Scan to PayUserDidCancelReturned if the user aborted the processNone.
Scan to PayInvalidCodeReturned if the code that was passed is invalidNone.