Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.ppretail.adyen.business;
- import com.adyen.jni.clib.*;
- import com.ppretail.adyen.lib2.ped.MerchantPed;
- import com.ppretail.adyen.lib2.ped.PedCallbackHandler;
- import com.ppretail.adyen.lib2.ped.PedCallbackResultHandler;
- import com.ppretail.adyen.lib2.ped.PedHandler;
- import com.ppretail.adyen.lib2.pos.MerchantPos;
- import com.ppretail.adyen.lib2.pos.PosCallbackHandler;
- import com.ppretail.adyen.lib2.pos.PosCallbackResultHandler;
- import com.ppretail.adyen.lib2.pos.PosHandler;
- import lombok.extern.slf4j.Slf4j;
- @Slf4j
- public class PointOfSale {
- private static final int retries = 5;
- private final MerchantPos pos;
- private final MerchantPed ped;
- private final Environment environment;
- private PosHandler posHandler = new PosHandler();
- private PedHandler pedHandler = new PedHandler();
- private PosCallbackResultHandler posCallbackResultHandler = new PosCallbackResultHandler();
- private PedCallbackResultHandler pedCallbackResultHandler = new PedCallbackResultHandler();
- private String merchant;
- private String user;
- private String password;
- private String posId;
- private boolean libraryIsNotInit = true;
- private boolean pedIsNotRegistered = true;
- public PointOfSale(String posId, String terminalPedName, String environment, String merchant, String user, String password) throws Exception {
- this.posId = posId;
- this.merchant = merchant;
- this.user = user;
- this.password = password;
- this.pos = new MerchantPos();
- this.ped = new MerchantPed(posId, terminalPedName);
- this.environment = Environment.getEnvironment(environment);
- addPosCallBackHandler();
- addPedCallbackHandler();
- initLibrary();
- registerPed();
- }
- private void callbackWait(boolean condition) {
- while (condition) {
- System.out.println("wait: " + condition);
- try {
- synchronized (this) {
- wait(1000);
- }
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- private void addPedCallbackHandler() {
- this.ped.addPedCallbackHandler(new PedCallbackHandler() {
- @Override
- public void pedStateChangeCallbackHandler() {
- log.info("ResultPedStateChange: " + ped.getResultPedStateChange().getPedState().getNameInClib());
- if (pedCallbackResultHandler.handlePedStateChangeResult(ped))
- log.info("Ped: " + ped.getPedName() + " is ready");
- }
- @Override
- public void showScreenCallbackHandler() {
- pedCallbackResultHandler.handleShowScreenResult();
- }
- @Override
- public void createTenderCallbackHandler() {
- if (pedCallbackResultHandler.handleCreateTenderResult(ped)) {
- log.info("tenderCreated on the ped: " + ped);
- } else {
- log.error("tenderCreateError on the ped: " + ped);
- //posGuiCommHandler.updateStatusLabel(PropertyReader.getProperty("tenderCreateError"));
- refreshPed();
- }
- }
- private void refreshPed() {
- ped.refreshPEDstate(5);
- }
- @Override
- public void progressCallbackHandler() {
- if (pedCallbackResultHandler.handleProgressResult(ped)) {
- Transaction.TenderState tenderState = ped.getResultTenderProgress().getTenderState();
- log.info("progressCallbackHandler - tenderState on ped: {} is: {}", ped, tenderState);
- } else {
- log.error("progressCallbackHandler error on the ped: " + ped);
- //posGuiCommHandler.updateStatusLabel(PropertyReader.getProperty("progressCBError"));
- }
- }
- @Override
- public void additionalDataCallbackHandler() {
- if (pedCallbackResultHandler.handleAdditionalDataResult(ped)) {
- Tender tender = ped.getResultTenderAdditionalData();
- log.info("additionalDataCallbackHandler on the ped: {} with the tender: {}", ped, tender);
- } else {
- log.error("additionalDataCallbackHandler error on the ped: {} ", ped);
- }
- }
- @Override
- public void tenderDccCallbackHandler() {
- pedCallbackResultHandler.handleTenderDccResult(ped);
- }
- @Override
- public void tenderPrintReceiptCallbackHandler() {
- }
- @Override
- public void tenderCheckSignatureCallbackHandler() {
- if (pedCallbackResultHandler.handleTenderCheckSignatureResult(ped)) {
- Tender tender = ped.getResultTenderCheckSignature();
- log.info("tenderCheckSignatureCallbackHandler on the ped: {} enableCheckSignatureForm: {}", ped,
- tender.getAdditionalData().get("signature.data"));
- //posGuiCommHandler.enableCheckSignatureForm(tender.getAdditionalData().get("signature.data"));
- }
- }
- @Override
- public void finalCallbackHandler() {
- if (pedCallbackResultHandler.handleFinalResult(ped)) {
- //check final transaction state
- Tender tender = ped.getResultTenderPrintReceipt();
- if (tender != null) {
- String finalStatus = tender.getTenderState().getNameInClib();
- log.info("finalCallbackHandler final status: {}", finalStatus);
- if (finalStatus.equals(Transaction.TenderState.UNDEFINED.getNameInClib()))
- pedHandler.refreshPedState(ped, retries);
- log.info("finalCallbackHandler final status after pedHandler.refreshPedState: {}", finalStatus);
- }
- } else {
- log.error("transactionError");
- //posGuiCommHandler.updateStatusLabel(PropertyReader.getProperty("transactionError"));
- refreshPed();
- }
- }
- @Override
- public void pedExceptionCallbackHandler() {
- if (!pedCallbackResultHandler.handlePedExceptionResult(ped)) {
- log.error("Ped exception operation has returned null result with ped: {}", ped);
- //posGuiCommHandler.updateStatusLabel(PropertyReader.getProperty("pedExceptionNull"));
- } else {
- log.error("Ped exception with ped: {}. The error message is: {}", ped, ped.getResultPedException().getAdyenResult().getErrorMessage());
- //posGuiCommHandler.updateStatusLabel(ped.getResultPedException().getAdyenResult().getErrorMessage());
- }
- }
- @Override
- public void txStoreQueryCallbackHandler() {
- }
- });
- }
- private void initLibrary() throws Exception {
- log.info("starting initLibrary");
- if (!posHandler.initLibrary(pos, buildInitLibraryRequest())){
- String initLibraryErrorMessage = "initLibrary error with posId: " + this.posId;
- log.error(initLibraryErrorMessage);
- throw new Exception(initLibraryErrorMessage);
- }
- callbackWait(this.libraryIsNotInit);
- }
- private void registerPed() throws Exception {
- log.info("starting registration for ped: {}", ped);
- if (!posHandler.registerPed(pos,ped)){
- log.error("Ped registration with ped: {} and pos: {}", ped, posId);
- throw new Exception("Ped registration error");
- }
- callbackWait(this.pedIsNotRegistered);
- }
- private InitLibraryRequest buildInitLibraryRequest() {
- return new InitLibraryRequest.InitLibraryRequestBuilder(
- this.environment, this.posId, null, new LogConfig(LogLevel.DEBUG)).build();
- }
- private void addPosCallBackHandler() {
- pos.addCallbackHandler(new PosCallbackHandler() {
- @Override
- public void initLibraryCallbackHandler() {
- if (posCallbackResultHandler.handleInitLibraryResult(pos))
- registerPos();
- else {
- log.error("initLibError");
- }
- }
- private void registerPos() {
- log.info("Starting pos registration for posId: " + posId);
- if (!posHandler.registerPos(pos, buildRegisterPosRequest(posId))) {
- log.error("Pos registration error for posId: " + posId);
- }
- }
- @Override
- public void registerPosCallbackHandler() {
- if (posCallbackResultHandler.handleRegisterPosResult(pos)) {
- log.info("Pos registration finished for posId: " + posId);
- libraryIsNotInit = false;
- //posGuiCommHandler.enablePosDependantMenu(true);//enable ped registration
- } else {
- log.error("Pos registration finished with error for posId: " + posId);
- //posGuiCommHandler.enablePosDependantMenu(false);
- }
- }
- @Override
- public void registerPedCallbackHandler() {
- if (posCallbackResultHandler.handleRegisterPedResult(pos, ped)) {
- pedIsNotRegistered = false;
- log.info("Ped registration finished");
- } else {
- log.error("Ped registration finished with error");
- }
- }
- @Override
- public void cancelOrRefundAtPspCallbackHandler() {
- if (posCallbackResultHandler.handleCancelOrRefundAtPspResult(pos)) {
- log.info("Ped cancelOrRefund finished");
- } else {
- log.error("Ped cancelOrRefund finished with error");
- }
- }
- @Override
- public void exitLibraryCallbackHandler() {
- posCallbackResultHandler.handleExitLibraryResult(pos);
- }
- @Override
- public void exceptionCallbackHandler() {
- //posGuiCommHandler.updateStatusLabel(PropertyReader.getProperty("posExceptionCB") + pos.getResultException().getLibraryResult());
- posCallbackResultHandler.handleExceptionResult(pos);
- }
- });
- }
- private RegisterPosRequest buildRegisterPosRequest(String posId) {
- return new RegisterPosRequest(merchant, user, password, posId);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement