Cordova plugin for CardFlight
Remember that all cordova code is exposed (client side) and you shouldn't store any private keys in code.
This plugin defines global cardFlight object.
Although in the global scope, it is not available until after the deviceready event.
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
console.log(cardFlight);
}
Note: Plugin requires the following frameworks, although plugin adds them, recheck that are properly referenced in xcode project:
- libstdc++.6.0.9.dylib
- AVFoundation
- AudioToolbox
- MediaPlayer
- MessageUI
- ExternalAccessory
- CoreGraphics //Sometimes this framework is not added. This can be manually added in xcode from Build Phases --> Link Binary With Libraries --> + --> CoreGraphics.framework --> Add
Install using cordova plugin add https://github.com/auctifera-josed/cordova-plugin-cardflight
cardFlight.setAccount('be*********************', 'acc_****************', function(err, res) {
if (err) console.log(err);
else console.log(res);
})
cardFlight.charge(100, 'USD', function(res) {
console.log(res);
}, function(res) {
console.log(res);
})
true = success | false = error (Activate logging to identify error)
- setAccount //Should be called first, calls readerInit
- getAccount
- charge
- refund
- getCurrentCard
- newSwipe
- setCurrency
- cancelTransaction
- addCardTypedView
- removeCardTypedView
- setLogging
- readerInit
- cardFlightEvent
The setAccount(apiKey, accountToken, callback)
function sets the key, token and initializes the reader object.`
you need to set the account before using any other method
The CardFlight's Test/Production API key
The Merchant Account's token
function(err,res){}
The charge(amount, success, error[, currency])
function creates a charge of the given amount to the last swiped/typed credit card. Charge event returns error if card is null
The amount to be charged
Optional string to modify the currency, default is 'USD'
The refund(amount, chargeToken, success, error)
function allows to refund the charge of the given token
The amount to be refunded
The CardFlight's charge token (e.g ch_Wqi19gebijL-********)
The getCurrentCard(success, error)
function returns a string containing the last 4 chars of the current credit card
The newSwipe(success, error)
function sets the system to allow a new swipe
Note: - Convenient method to use if there is a Swipe Error event - Note that this method is called in the readerIsConnected event
The setCurrency(currency, success, error)
function to set the default currency for all future charges
A ISO 4217 Currency Code (e.g USD, EUR)
The getAccount(success, error)
function returns the current account token from the session manager
The cancelTransaction(success, error)
function manually cancel the swipe process before the timeout duration has been reached or cancels an EMV transaction. Additionally it sets to null the reader, paymentView and card objects.
Note: This method should be call anytime the reader shouldn't be expecting a swipe to avoid weird behaviour.
The addCardTypedView(paymentView, success, error)
function adds a CFTPaymentView to accept user input with card information. It takes a paymentView object to create the view.
This method overrides any previous view with the new one.
An object to create a paymentView, e.g:
{
"x": 20,
"y": 20,
"width": 200,
"height": 20,
"keyboardAppearance": "dark",
"border-color": {
"red": 1.0,
"green": 1.0,
"blue": 1.0,
"alpha": 1.0
},
"focus" : true
}
- x*,y*: position on screen
- width*, height*: size
- keyboardApperance options:
- dark
- alert
- default
- light
- focus*: indicates if view should be focus uppon creation
The removeCardTypedView(success, error)
function removes from the super view the last created view with the method 'addCardTypedView'
The setLogging(logging, success, error)
function switches the logging to on/off, convenient method for debugging.
True activates logging, false deactivates it
The readerInit(success, error)
function initializes the reader object to start detecting CardFlight card reader.
This method is called from setAccount function
Listen to reader and card events as cases of the cardFlightEvent event, cases are:
- reader
- attached
- connected //Calls
newSwipe
method - disconnected
- swipe
- card
- Swipe error
- XXXX //4 last card digits
- refund
- charge
- error
Charge and Refund events return a charge object:
{
"amount": xxx,
"amountRefunded": xxx,
"createdDate": "YYYY-MM-DD HH:MM:SS Z",
"isRefunded": bool,
"token": "charge_secret_token"
}
window.addEventListener('cardFlightEvent', function (e) {
switch (e.dataType) {
case 'reader':
break;
case 'card':
break;
case ...:
...
default:
console.log(e);
}
});