question

trueox avatar image
trueox asked trueox commented

Need help with Cancel OnSaleResponse on clover mini

I have test windows form the button that does this.


var cloverConnector = CloverConnectorFactory.CreateUsbConnector(


"123423482842", // remote application ID

"not yet", // POS name

"20200408" // serial number

);

var ccl = new CloverConnectionListener(cloverConnector);

cloverConnector.AddCloverConnectorListener(ccl);

cloverConnector.InitializeConnection();

while (!ccl.DeviceReady)

{

Thread.Sleep(1000);

}


try

{

var pendingSale = new SaleRequest

{

ExternalId = ExternalIDUtil.GenerateRandomString(13),

Amount = 1000

AutoAcceptSignature = true,

AutoAcceptPaymentConfirmations = true,

DisableDuplicateChecking = true

};

cloverConnector.Sale(pendingSale);

}

catch (Exception e)

{

textboxDisplay.AppendText(e.Message);

}


below is my connector, It works fine as long I do not press a BACK button or Cancel on the clover mini. Once the back button is pressed, the button do not works again. I have to restart my app for it to work again. how do I fix this problem, please? I have tried to follow "https://docs.clover.com/clover-platform/docs/windows " the instruction change method in a different section, it is hard for a newbie like me to follow. I see someone have the same problem but no answer to the question

https://community.clover.com/questions/361/cancel-onsaleresponse.html .

I am using the lasted SDK. 4.03. Any help will be great.

using System;

using com.clover.remotepay.sdk;

using com.clover.remotepay.transport;

using com.clover.sdk.v3.payments;


namespace test

{

// Create an implementation of ICloverConnectorListener

// Create an implementation of ICloverConnectorListener

public class CloverConnectionListener : DefaultCloverConnectorListener

{

public Boolean DeviceReady { get; set; }

public Boolean DeviceConnected { get; set; }


private bool saleDone;


public bool GetsaleDone()

{

return saleDone;

}


public void SetsaleDone(bool value)

{

saleDone = value;

}


public Boolean RefundDone { get; set; }

public String PaymentId { get; set; }

public String OrderId { get; set; }


public CloverConnectionListener(ICloverConnector cloverConnector) : base(cloverConnector)

{

}


public override void OnDeviceReady(MerchantInfo merchantInfo)

{

base.OnDeviceReady(merchantInfo);

//Connected and available to process requests

DeviceReady = true;

}

public override void OnDeviceConnected()

{

base.OnDeviceConnected();

DeviceConnected = true;

// Connected, but not available to process requests


}


public override void OnDeviceDisconnected()

{

base.OnDeviceDisconnected();

Console.WriteLine("Disconnected");

//Disconnected

}


public override void OnSaleResponse(SaleResponse response)

{

base.OnSaleResponse(response);


if ((response.Result).ToString() == "CANCEL")

{

SetsaleDone(true);

}

else

{

SetsaleDone(true);

PaymentId = response.Payment.id;

OrderId = response.Payment.order.id;

}


}


public override void OnConfirmPaymentRequest(ConfirmPaymentRequest request)

{


}


public override void OnRefundPaymentResponse(RefundPaymentResponse response)

{

base.OnRefundPaymentResponse(response);

try

{

if (response.Success)

{

Refund refund = response.Refund;

Console.WriteLine("Refund request successful");

Console.WriteLine(" ID: " + refund.id);

Console.WriteLine(" Amount: " + refund.amount);

Console.WriteLine(" Order ID: " + response.OrderId);

Console.WriteLine(" Payment ID: " + response.PaymentId);

}

else

{

Console.Error.WriteLine("Refund request failed - " + response.Reason);

}

}

catch

{

Console.Error.WriteLine("Error handling sale notification");

}


RefundDone = true;


}


}

}


Sale
2 comments
10 |2000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

David Marginian avatar image David Marginian ♦♦ commented ·

Are you referring to a button in your app? Have you checked your apps logs or tried to debug it? Does OnSaleResponse get called when you hit the back button on the device?

0 Likes 0 ·
trueox avatar image trueox commented ·

A button on my app executes the code in the first section. It does call OnsaleResponse without issue. the dollar amount shows up. I press the back arrow button, that basically Cancelled the sale. so on the clove mini, it is back to the welcoming screen. I press the button again, it will not call OnsaleResponse again. It stuck on "public Boolean DeviceReady { get; set; }" and never get beyond that because the device is not in the ready status anymore and my while loop keeps looping and waiting for the ready status. how do I get past that?

0 Likes 0 ·
David Marginian avatar image
David Marginian Deactivated answered trueox commented

It is unclear from your code, but my guess is that each time the button in your "app" is clicked you create a new CloverConnector (CloverConnectorFactory.CreateUsbConnector). Is that correct? If so, that is your problem. You are creating a NEW instance of the Connector per button click. The second connector instance (second button click) will never be able to establish a connection because the Connector from the first button click is already connected to the device and has not been properly disposed of.

We generally recommend that you initialize a connection to the device and use the same connection until your application is closed, at which time you should properly dispose of the connection by calling Dispose on the Clover Connector. To fix this you can:

  • Recommended - Establish/manage the connection to the device at the application level. Move the connection initialization code to the application level and only initiate a new Sale when the button is clicked. When your application is closed, make sure you properly clean-up the connection by calling Dispose.
  • Or (not recommended), manage the connection state inside your button click handler (assuming a new instance is not created per button click). Only perform the CloverConnector initialization if you are not currently connected.
  • Or (not recommended), Dispose of the Connector in your button handler before you start a new transaction.

You should read our best practices - https://docs.clover.com/clover-platform/docs/remote-pay-sdk-best-practices.

You should look at the examples - https://github.com/clover/remote-pay-windows/tree/master/examples.

6 comments
10 |2000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

trueox avatar image trueox commented ·

David,

what you said is correct, my button initializes a connection each time and I am aware of that problem. I got that code from your starter example and that is what it does base on my understanding. I can't figure out how to reuse the established connection. I am quite new at this. do you have a simple example on how I can fix the issue?

0 Likes 0 ·
David Marginian avatar image David Marginian ♦♦ trueox commented ·

That example illustrates how to do a one time sale - not build a POS application. I provided you with a bulleted list of recommendations in my answer above, can you explain what you don't understand about these recommendations? If you would like to see an example of a POS application please see this example - https://github.com/clover/remote-pay-windows/tree/master/examples/CloverExamplePOS.

0 Likes 0 ·
trueox avatar image trueox David Marginian ♦♦ commented ·

can you please a create solution base on my example? basically how to reuse the connection in the scenario. thanks in advance. I am new to this so please excuse me for asking simple questions.


0 Likes 0 ·
Show more comments
trueox avatar image trueox commented ·

I am trying to do this.

"

Recommended - Establish/manage the connection to the device at the application level. Move the connection initialization code to the application level and only initiate a new Sale when the button is clicked. "

I want to use your recommended solution but I do not know-how.

I can do you not recommended one but I want to do it properly.

can you create a quite example based on my initial posting?

it is basic windows form with a button on it. the 1st section is the code behind the button.

the 2nd section is the class file. I named it Cloverconnectionlistener.cs.


installed windows clover windows SDK 4.03 clover NuGet packages.

run the app, and click on the button.

a 10.00 amount show up on the clover mini, press the back "<-" allow on the clover mini.

press the button again.


I know I can do your "not recommended" way by disposing of the connection each time but I want to reuse the connection instead and dispose it if the application is closed.

that the part I do not know how to do.


0 Likes 0 ·
trueox avatar image
trueox answered

This can be closed, the sample file from

https://community.clover.com/questions/22051/please-tell-me-how-to-make-one-c-file-which-includ.html.

works for me.

that is the sample code I was asking for, sorry if I was not clear.

10 |2000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Welcome to the
Clover Developer Community