To run the example project, clone the repo, and run pod install
from the Example directory first.
secux-paymentdevicekit is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'secux-paymentdevicekit'
binary "https://maochuns.github.io/secux_paymentdevicekit.json" ~> 2.0.12
import secux_paymentdevicekit
- SecuXPaymentPeripheralManager initialization
scanTimeout: Timeout in seconds for device scan
connTimeout: Timeout in seconds for connection with device
checkRSSI: Scan device within the RSSI value
var peripheralManager = SecuXPaymentPeripheralManager(scanTimeout: 5,
connTimeout: 30,
checkRSSI: -80)
- Get device opeation ivKey
Get the opeation ivKey from the payment device when payment device is in payment / promotion / refund / refill mode.
Note: Call the function in thread. You are allowed to cancel the payment after getting the ivKey.
- 2.1 Get device opeation ivKey from device ID
func doGetIVKey(devID:String) -> (SecuXPaymentPeripheralManagerError, String)
devID: Device ID, e.g. "811c00000016"
- 2.2 Get device opeation ivKey from device ID and opeation nonce
This is recommended to get the ivKey when using P22. Operation nonce can garantee the payment correctness.
func doGetIVKey(devID:String, nonce:[UInt8]) -> (SecuXPaymentPeripheralManagerError,
String)
devID: Device ID, e.g. "811c00000016"
nonce: Payment nonce from payment QR code, convert the nonce hex string to byte array
Payment QR code sample:
{"amount":"5", "coinType":"DCT:SPC", "nonce":"e956013c",
"deviceIDhash":"b0442888f1c9ddb5bb924382f44b0f025e0dc7cd"}
SecuXPaymentPeripheralManagerError shows the operation result, if the result is
.OprationSuccess, the returned String contains device's ivKey,
otherwise String might contain an error message.
- Scan device failed!
- No device coding key info.
- Invalid payment QRCode! QRCode is timeout!
- Device is not activated!
- Set device timeout failed!
let (ret, ivkey) = self.peripheralManager.doGetIVKey(devID: "811c00000016")
print("\(ret) \(ivkey)")
if ret == .OprationSuccess{
...
}else{
self.showMessageInMainThread(title: "Payment failed!",
message: "Get ivkey failed! Error: \(ivkey)")
}
- Cancel operation
Call the function after getting opeation ivKey, will cancel the opeation.
func requestDisconnect()
- Generate encrypted operation data
Source code of the SecuXUtility.swift can be found in secux-paymentdevicekit-test
static func getEncryptMobilePaymentCommand(terminalId:String,
amount:String,
ivKey:String,
cryptKey:String,
currency:String)->Data?
terminalId: An ID string used in device activation
cryptKey: A key string used in device activation
ivKey: Device payment ivKey from doGetIVKey function
amount: Payment amount
currency: Payment currency. For FW v1.0, currency is "SPC";
For FW v2.0, currency is "DCT:SPC"
Ecrypted payment data.
let payData = SecuXUtility.getEncryptMobilePaymentCommand(terminalId: "gkn3p0ec",
amount: "200",
ivKey: ivkey,
cryptKey: "asz2gorm5bxh5nc5ecjjsqqstgnlsxsj")
- Confirm operation
Send the encrypted operation data to the device to confirm the opearation.
Note: call the function in thread.
func doPaymentVerification(encPaymentData:Data) ->
(SecuXPaymentPeripheralManagerError, String)
encPaymentData: The encrypted payment data.
SecuXPaymentPeripheralManagerError shows the operation result, if the result is not
.OprationSuccess, the returned String might contain an error message.
DispatchQueue.global().async {
let payData = SecuXUtility.getEncryptMobilePaymentCommand(terminalId: "gkn3p0ec",
amount: "2",
ivKey: ivkey,
cryptKey: "asz2gorm5bxh5nc5ecjjsqqstgnlsxsj")
let (payret, error) = self.peripheralManager.doPaymentVerification(encPaymentData: payData!)
if payret == .OprationSuccess{
self.showMessageInMainThread(title: "Payment successful!", message: "")
}else{
self.showMessageInMainThread(title: "Payment failed!", message: "Error: \(error)")
}
}
maochuns, [email protected]
secux_paymentdevicekit is available under the MIT license.