Skip to content

secuxtech/secux-paymentdevicekit-framework-sample-ios-master

Repository files navigation

secux_paymentdevicekit

CI Status Version License Platform

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Requirements

Installation

CocoaPods:

secux-paymentdevicekit is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'secux-paymentdevicekit'

Carthage:

binary "https://maochuns.github.io/secux_paymentdevicekit.json" ~> 2.0.12

Add bluetooth privacy permissions in the plist

Screenshot

Import the module

    import secux_paymentdevicekit

Usage

  1. SecuXPaymentPeripheralManager initialization

Parameters

    scanTimeout: Timeout in seconds for device scan
    connTimeout: Timeout in seconds for connection with device
    checkRSSI:   Scan device within the RSSI value

Sample

    var peripheralManager = SecuXPaymentPeripheralManager(scanTimeout: 5, 
                                                          connTimeout: 30, 
                                                          checkRSSI: -80)
  1. 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

Declaration

    func doGetIVKey(devID:String) -> (SecuXPaymentPeripheralManagerError, String)

Parameters

    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.

Declaration

    func doGetIVKey(devID:String, nonce:[UInt8]) -> (SecuXPaymentPeripheralManagerError, 
                                                    String)

Parameters

    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"}

Return value

    SecuXPaymentPeripheralManagerError shows the operation result, if the result is  
    .OprationSuccess, the returned String contains device's ivKey, 
    otherwise String might contain an error message.  

Error message

    - Scan device failed!
    - No device coding key info.
    - Invalid payment QRCode! QRCode is timeout!
    - Device is not activated!
    - Set device timeout failed!

Sample

    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)")
    }
  1. Cancel operation

Call the function after getting opeation ivKey, will cancel the opeation.

Declaration

    func requestDisconnect()
  1. Generate encrypted operation data

Source code of the SecuXUtility.swift can be found in secux-paymentdevicekit-test

Declaration

    static func getEncryptMobilePaymentCommand(terminalId:String, 
                                                   amount:String, 
                                                    ivKey:String, 
                                                 cryptKey:String,
                                                 currency:String)->Data?

Parameters

    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"

Return value

    Ecrypted payment data.

Sample

    let payData = SecuXUtility.getEncryptMobilePaymentCommand(terminalId: "gkn3p0ec", 
                                                amount: "200", 
                                                ivKey: ivkey, 
                                                cryptKey: "asz2gorm5bxh5nc5ecjjsqqstgnlsxsj")
  1. Confirm operation

Send the encrypted operation data to the device to confirm the opearation.

Note: call the function in thread.

Declaration

    func doPaymentVerification(encPaymentData:Data) ->  
                                            (SecuXPaymentPeripheralManagerError, String)

Parameters

    encPaymentData: The encrypted payment data.

Return value

    SecuXPaymentPeripheralManagerError shows the operation result, if the result is not 
    .OprationSuccess, the returned String might contain an error message.  

Sample

    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)")
        }
    }

Author

maochuns, [email protected]

License

secux_paymentdevicekit is available under the MIT license.

About

paymentdevicekit sample code

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published