Get ticket object in payment processor

No it’s not true elsewhere.

thats a shame, would be great is script had this ticket object available for current open ticket.

If your accessing a currently open ticket then yes you can access it without loading it but it all depends on when you are accessing it and what process is injecting the script.

Interesting, never seen that before. Is it like a global var? Above shows it as an input variable? So what needs to go in the call?

So he is confusing two different tutorials from two different use cases. The tutorial he referenced that showed the Ticket Object was the Credit Card Payment integration module tutorial. THe one he is wanting to actually use is a script invoked from a Payment Processor attached to a payment type. Those are two different scenarios. IF you use the Payment Processor and you use Execute Script then it is exposing the ticket already. The key is its not a Automation/Scripts script its specifically from Execute Script payment processor type.

Understood, shame, that would make scripts even more useful as wouldnt have to feed in so many variables all the time.

I am using payment processor to process mpesa payment. I have a script that is able to get telephone number and read the tendered amount. Now I need to get more information to enable track the transaction on the mpesa. like current user, the department and ticket number. How can I get these variables on the script side executed as payment processor script?

function sayHello() {
var mpesaStyle = dlg.AskQuestion(“Choose Mpesa”, “Push=stkPush,Check=c2bCheck,CANCEL=CANCEL”);

if (mpesaStyle == “CANCEL”) {
Data.Set(“canContinue”, false);
dlg.ShowMessage(“Payment Cancelled”);
return 1;
}

if (mpesaStyle == “stkPush”) {
var phoneNo = askPhoneNumberStrict254();
var amt = Data.Get(“tenderedAmount”);
//Push(phoneNo,amt);
var pushResult=mpesa_stk_push(phoneNo,amt);

Data.Set("canContinue", false);
dlg.ShowMessage(pushResult);
return 1;

}

if (mpesaStyle == “c2bCheck”) {

Data.Set("canContinue", false);
dlg.ShowMessage("Check Selected");
return 1;

}
}

function askPhoneNumberStrict254() {
var attempts = 0;
var phone = “”;

while (attempts < 2) {
phone = dlg.EditValue(“Enter Phone Number;254[0-9]{9};;ON”, “”);
if (!phone) return null; // user cancelled

// Validate again just in case
if (/254[0-9]{9}/.test(phone)) {
  dlg.ShowMessage(phone);

  var amt = Data.Get("tenderedAmount");
  dlg.ShowMessage("Do Mpesa request for Number: " + phone + " to pay: " + amt);
  return phone;
}

dlg.ShowMessage("❌ Invalid format.\r\nPhone must be 12 digits and start with 254.");
attempts++;

}

dlg.ShowMessage(“:warning: Too many failed attempts. Cancelling.”);
return null;
}

function mpesa_stk_push(phone,amount)
{

var mpesaQuarry = new Object();
mpesaQuarry.phone = phone;
mpesaQuarry.amount = amount;
mpesaQuarry.source = "SambaPOS";
// System-acquired info
mpesaQuarry.user = Data.Get("CurrentUserName") || "UnknownUser";
mpesaQuarry.ticketId = Data.Get("TicketId") || "UnknownTicket";
mpesaQuarry.terminal = Data.Get("Terminal") || "UnknownTerminal";
mpesaQuarry.department = Data.Get("DepartmentName") || "UnknownDept";
mpesaQuarry.time = getISOTimestamp()//new Date().toISOString();  // ISO time format

var api = JSON.stringify(mpesaQuarry);

  
 
 var url = "http://127.0.0.1:3600/mpesa";
 var username = 'sambapos';
 var password = 'xxxxxxxxxxxxxxxx';
 
 
 var response = web.PostJson(url,api,username,password);
 
 var responseObject = JSON.parse(response);
 return response;
}

function getISOTimestamp() {
var now = new Date();

function pad(n) {
return n < 10 ? “0” + n : n;
}

var year = now.getFullYear();
var month = pad(now.getMonth() + 1);
var day = pad(now.getDate());
var hours = pad(now.getHours());
var minutes = pad(now.getMinutes());
var seconds = pad(now.getSeconds());

// SambaPOS doesn’t support milliseconds or timezone easily
return year + “-” + month + “-” + day + “T” + hours + “:” + minutes + “:” + seconds;
}

User, department, ticket id/# must be passed as variables into the function.

For date time with ms and tz, you can get that from .NET:

Change UtcNow to Now if you want local time.

You can also specify your own format in ToString() e.g. yyyy-MM-ddTHH:mm:ss.fffzzz for less fractional precision. See C# date and time string format.

function getIso8601NowDate()
{
    var lib = host.lib("mscorlib");
    var now = lib.System.DateTimeOffset.UtcNow;
    
    return now.ToString('o');
}

For user, department and ticket id#. How do I get what to pass to the function. I it only works when I pass it by executing an action command. But then I will not be able to access payment variables like tendered amount, and canContinue. Kindly advise I am so confused.

That’s the only way you’ll be able to get that data into the function. I’m not aware of being able to access payment information from within the script engine. You may need to process something after a payment and get those details via SQL queries.

Hi, Memo, I am very grateful to you for the help you have been giving me. I am torn in between two scenarios. This is what I want. I want to process mpesa payment by user clicking mpesa button as a payment option and then I use the script to process what goes on there after. Now I have more than one department so I need to send the department name to my mpesa server for trace logs. Now my challenge is when I use the payment processed rule, I can pass variables, like current user, department, ticked ID, and others. but I do not have control of the payment process like if the process fails I can not stop the process. On the other hand, If i use payment processor which is available under ticket and execute the script under process handler. I have control of the payment process. I can stop the payment if it does not complete using canContinue set to false. But then I can not get variables like current user and department and ticket Id. So I was asking is there a way I could set variable that I can access like I use ticket opened event set these variable and then access them when in payment process. Or can I use pre process handlers. Or is there a way I can set some variable to help in this case. or Is there away to access current user, current department, current ticket id from database? I have been able to access all that I want at different scenarios and i can not marry the two? Please help

Man, the only way I can think of to have the data available from within a payment processor is to set local variables for the data you need when initiating a payment (open payment screen, quick pay, etc.)

There’s Data.Get() and also a GQL query getLocalSetting that might work. But to use the latter requires an API licence. I’m also not sure if the local setting retrived is local to the terminal or the message server.

Have a peek at this old thread for some ideas:

Payment processing is a complex beast; I’ve all but given up on it.