import {SuccessState, Message, Data} from './Messages';
//
// Represents the 3 Pairing statuses that the Spi instanxce can be in.
//
export const SpiStatus =
{
//
// Paired and Connected
//
PairedConnected: 'PairedConnected',
//
// Paired but trying to establish a connection
//
PairedConnecting: 'PairedConnecting',
//
// Unpaired
//
Unpaired: 'Unpaired'
};
//
// The Spi instance can be in one of these flows at any point in time.
//
export enum SpiFlow {
//
// Currently going through the Pairing Process Flow.
// Happens during the Unpaired SpiStatus.
//
Pairing = "Pairing",
//
// Currently going through the transaction Process Flow.
// Cannot happen in the Unpaired SpiStatus.
//
Transaction = "Transaction",
//
// Not in any of the other states.
//
Idle = "Idle"
}
//
// Represents the Pairing Flow State during the pairing process
//
export class PairingFlowState
{
public Message: string;
public AwaitingCheckFromEftpos: boolean;
public AwaitingCheckFromPos: boolean;
public ConfirmationCode: string;
public Finished: boolean;
public Successful: boolean;
constructor(state) {
//
// Some text that can be displayed in the Pairing Process Screen
// that indicates what the pairing process is up to.
//
this.Message = null;
//
// When true, it means that the EFTPOS is shoing the confirmation code,
// and your user needs to press YES or NO on the EFTPOS.
//
this.AwaitingCheckFromEftpos = null;
//
// When true, you need to display the YES/NO buttons on you pairing screen
// for your user to confirm the code.
//
this.AwaitingCheckFromPos = null;
//
// This is the confirmation code for the pairing process.
//
this.ConfirmationCode = null;
//
// Indicates whether the Pairing Flow has finished its job.
//
this.Finished = null;
//
// Indicates whether pairing was successful or not.
//
this.Successful = null;
if(state) {
Object.assign(this, state);
}
}
}
export const TransactionType =
{
AccountVerify: 'AccountVerify',
CashoutOnly: 'CashoutOnly',
GetTransaction: 'GetTransaction',
GetLastTransaction: 'GetLastTransaction',
MOTO: 'MOTO',
Preauth: 'Preauth',
Purchase: 'Purchase',
Refund: 'Refund',
Reversal: 'Reversal',
Settle: 'Settle',
SettlementEnquiry: 'SettlementEnquiry',
ZipPurchase: 'ZipPurchase',
ZipRefund: 'ZipRefund',
};
//
// Used as a return in the InitiateTx methods to signify whether
// the transaction was initiated or not, and a reason to go with it.
//
export class InitiateTxResult
{
public Initiated: boolean;
public Message: string;
constructor(initiated: boolean, message: string)
{
//
// Whether the tx was initiated.
// When true, you can expect updated to your registered callback.
// When false, you can retry calling the InitiateX method.
//
this.Initiated = initiated;
//
// Text that gives reason for the Initiated flag, especially in case of false.
//
this.Message = message;
}
}
//
// Used as a return in calls mid transaction to let you know
// whether the call was valid or not.
// These attributes work for COM interop.
//
export class MidTxResult
{
public Valid: boolean;
public Message: string;
//
// This default stucture works for COM interop.
//
constructor(valid: boolean, message: string)
{
this.Valid = valid;
this.Message = message;
}
}
//
// Represents the State during a TransactionFlow
//
export class TransactionFlowState
{
public PosRefId: string;
public Id: string;
public Type: string;
public DisplayMessage: string;
public AmountCents: number;
public RequestSent: boolean;
public RequestTime: number;
public LastStateRequestTime: number;
public GtRequestId: string;
public AttemptingToCancel: boolean;
public AwaitingSignatureCheck: boolean;
public AwaitingPhoneForAuth: boolean;
public Finished: boolean;
public Success: string;
public Response: boolean;
public SignatureRequiredMessage: boolean;
public PhoneForAuthRequiredMessage: boolean;
public CancelAttemptTime: number;
public CancelElapsedTime: number;
public Request: Message;
public AwaitingGtResponse: boolean;
public CompletedTime: number;
constructor(posRefId: string, type: string, amountCents: number, message: Message, msg: string)
{
//
// The id given to this transaction
//
this.PosRefId = posRefId;
this.Id = posRefId; // obsolete, but let's maintain it for now, to mean same as PosRefId.
//
// Purchase/Refund/Settle/...
//
this.Type = type;
//
// A text message to display on your Transaction Flow Screen
//
this.DisplayMessage = msg;
//
// Amount in cents for this transaction
//
this.AmountCents = amountCents;
//
// Whther the request has been sent to the EFTPOS yet or not.
// In the PairedConnecting state, the transaction is initiated
// but the request is only sent once the connection is recovered.
//
this.RequestSent = false;
//
// The time when the request was sent to the EFTPOS.
//
this.RequestTime = null;
//
// The time when we last asked for an update, including the original request at first
//
this.LastStateRequestTime = null;
//
// The id of the gt request message that was sent. Used to match with the response.
//
this.GtRequestId = null;
//
// Whether we're currently attempting to Cancel the transaction.
//
this.AttemptingToCancel = null;
//
// When this flag is on, you need to display the dignature accept/decline buttons in your
// transaction flow screen.
//
this.AwaitingSignatureCheck = false;
//
// When this flag is on, you need to show your user the phone number to call to get the authorisation code.
// Then you need to provide your user means to enter that given code and submit it via SubmitAuthCode().
//
this.AwaitingPhoneForAuth = null;
//
// Whether this transaction flow is over or not.
//
this.Finished = false;
//
// The success state of this transaction. Starts off as Unknown.
// When finished, can be Success, Failed OR Unknown.
//
this.Success = SuccessState.Unknown;
//
// The response at the end of the transaction.
// Might not be present in all edge cases.
// You can then turn this Message into the appropriate structure,
// such as PurchaseResponse, RefundResponse, etc
//
this.Response = null;
//
// The message the we received from EFTPOS that told us that signature is required.
//
this.SignatureRequiredMessage = null;
//
// The message the we received from EFTPOS that told us that Phone For Auth is required.
//
this.PhoneForAuthRequiredMessage = null;
//
// The time when the cancel attempt was made.
//
this.CancelAttemptTime = null;
//
// The timer used to detect when to give up waiting for
// cancellation response from the terminal
//
this.CancelElapsedTime = null;
//
// The request message that we are sending/sent to the server.
//
this.Request = message;
//
// Whether we're currently waiting for a Get Transaction Response to get an update.
//
this.AwaitingGtResponse = null;
///
/// The time when the transaction was completed
///
this.CompletedTime = null;
}
Sent(msg)
{
this.RequestSent = true;
this.RequestTime = Date.now();
this.LastStateRequestTime = Date.now();
this.DisplayMessage = msg;
}
Cancelling(msg)
{
this.AttemptingToCancel = true;
this.CancelAttemptTime = Date.now();
this.CancelElapsedTime = Date.now();
this.DisplayMessage = msg;
}
CancelFailed(msg)
{
this.AttemptingToCancel = false;
this.DisplayMessage = msg;
}
CallingGt(gtRequestId)
{
this.AwaitingGtResponse = true;
this.LastStateRequestTime = Date.now();
this.GtRequestId = gtRequestId;
}
GotGtResponse()
{
this.AwaitingGtResponse = false;
this.CancelElapsedTime = this.CancelElapsedTime ? Date.now() : null;
}
Failed(response, msg)
{
this.Success = SuccessState.Failed;
this.Finished = true;
this.Response = response;
this.DisplayMessage = msg;
this.CompletedTime = Date.now();
}
SignatureRequired(spiMessage, msg)
{
this.SignatureRequiredMessage = spiMessage;
this.AwaitingSignatureCheck = true;
this.DisplayMessage = msg;
}
SignatureResponded(msg)
{
this.AwaitingSignatureCheck = false;
this.DisplayMessage = msg;
}
PhoneForAuthRequired(spiMessage, msg)
{
this.PhoneForAuthRequiredMessage = spiMessage;
this.AwaitingPhoneForAuth = true;
this.DisplayMessage = msg;
}
AuthCodeSent(msg)
{
this.AwaitingPhoneForAuth = false;
this.DisplayMessage = msg;
}
Completed(state, response, msg)
{
this.Success = state;
this.Response = response;
this.Finished = true;
this.AttemptingToCancel = false;
this.AwaitingGtResponse = false;
this.AwaitingSignatureCheck = false;
this.AwaitingPhoneForAuth = false;
this.DisplayMessage = msg;
this.CompletedTime = Date.now();
}
UnknownCompleted(msg)
{
this.Success = SuccessState.Unknown;
this.Response = null;
this.Finished = true;
this.AttemptingToCancel = false;
this.AwaitingGtResponse = false;
this.AwaitingSignatureCheck = false;
this.AwaitingPhoneForAuth = false;
this.DisplayMessage = msg;
this.CompletedTime = Date.now();
}
}
//
// Used as a return in the SubmitAuthCode method to signify whether Code is valid
//
export class SubmitAuthCodeResult
{
public ValidFormat: boolean;
public Message: string;
constructor(validFormat: boolean, message: string)
{
this.ValidFormat = validFormat;
//
// Text that gives reason for Invalidity
//
this.Message = message;
}
}
export class SpiConfig
{
public PrintMerchantCopy: boolean;
public PromptForCustomerCopyOnEftpos: boolean;
public SignatureFlowOnEftpos: boolean;
public EnabledPrintMerchantCopy: boolean;
public EnabledPromptForCustomerCopyOnEftpos: boolean;
public EnabledSignatureFlowOnEftpos: boolean;
constructor() {
this.PrintMerchantCopy = false;
this.PromptForCustomerCopyOnEftpos = false;
this.SignatureFlowOnEftpos = false;
this.EnabledPrintMerchantCopy = false;
this.EnabledPromptForCustomerCopyOnEftpos = false;
this.EnabledSignatureFlowOnEftpos = false;
}
AddReceiptConfig(messageData)
{
if (this.PromptForCustomerCopyOnEftpos && this.EnabledPromptForCustomerCopyOnEftpos)
{
messageData.prompt_for_customer_copy = this.PromptForCustomerCopyOnEftpos;
}
if (this.SignatureFlowOnEftpos && this.EnabledSignatureFlowOnEftpos)
{
messageData.print_for_signature_required_transactions = this.SignatureFlowOnEftpos;
}
if (this.PrintMerchantCopy && this.EnabledPrintMerchantCopy)
{
messageData.print_merchant_copy = this.PrintMerchantCopy;
}
return messageData;
}
ToString()
{
return `PromptForCustomerCopyOnEftpos:${this.PromptForCustomerCopyOnEftpos} SignatureFlowOnEftpos:${this.SignatureFlowOnEftpos} PrintMerchantCopy: ${this.PrintMerchantCopy}`;
}
}
interface MessageData {
customer_receipt_header?: string
customer_receipt_footer?: string
merchant_receipt_header?: string
merchant_receipt_footer?: string
pos_ref_id?: string
cash_amount?: number,
surcharge_amount?: number
}
export class TransactionOptions
{
public _customerReceiptHeader: string;
public _customerReceiptFooter: string;
public _merchantReceiptHeader: string;
public _merchantReceiptFooter: string;
constructor() {
this._customerReceiptHeader = "";
this._customerReceiptFooter = "";
this._merchantReceiptHeader = "";
this._merchantReceiptFooter = "";
}
SetCustomerReceiptHeader(customerReceiptHeader: string)
{
this._customerReceiptHeader = customerReceiptHeader;
}
SetCustomerReceiptFooter(customerReceiptFooter: string)
{
this._customerReceiptFooter = customerReceiptFooter;
}
SetMerchantReceiptHeader(merchantReceiptHeader: string)
{
this._merchantReceiptHeader = merchantReceiptHeader;
}
SetMerchantReceiptFooter(merchantReceiptFooter: string)
{
this._merchantReceiptFooter = merchantReceiptFooter;
}
AddOptions(messageData: MessageData)
{
messageData.customer_receipt_header = this._customerReceiptHeader;
messageData.customer_receipt_footer = this._customerReceiptFooter;
messageData.merchant_receipt_header = this._merchantReceiptHeader;
messageData.merchant_receipt_footer = this._merchantReceiptFooter;
return messageData;
}
}