Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.86 KB | None | 0 0
  1. package com.ppretail.adyen.business;
  2.  
  3. import com.adyen.jni.clib.*;
  4. import com.ppretail.adyen.lib2.ped.MerchantPed;
  5. import com.ppretail.adyen.lib2.ped.PedCallbackHandler;
  6. import com.ppretail.adyen.lib2.ped.PedCallbackResultHandler;
  7. import com.ppretail.adyen.lib2.ped.PedHandler;
  8. import com.ppretail.adyen.lib2.pos.MerchantPos;
  9. import com.ppretail.adyen.lib2.pos.PosCallbackHandler;
  10. import com.ppretail.adyen.lib2.pos.PosCallbackResultHandler;
  11. import com.ppretail.adyen.lib2.pos.PosHandler;
  12. import lombok.extern.slf4j.Slf4j;
  13.  
  14. @Slf4j
  15. public class PointOfSale {
  16.  
  17.     private static final int retries = 5;
  18.  
  19.     private final MerchantPos pos;
  20.     private final MerchantPed ped;
  21.     private final Environment environment;
  22.  
  23.     private PosHandler posHandler = new PosHandler();
  24.     private PedHandler pedHandler = new PedHandler();
  25.     private PosCallbackResultHandler posCallbackResultHandler = new PosCallbackResultHandler();
  26.     private PedCallbackResultHandler pedCallbackResultHandler = new PedCallbackResultHandler();
  27.     private String merchant;
  28.     private String user;
  29.     private String password;
  30.     private String posId;
  31.  
  32.     private boolean libraryIsNotInit = true;
  33.     private boolean pedIsNotRegistered = true;
  34.  
  35.     public PointOfSale(String posId, String terminalPedName, String environment, String merchant, String user, String password) throws Exception {
  36.         this.posId = posId;
  37.         this.merchant = merchant;
  38.         this.user = user;
  39.         this.password = password;
  40.         this.pos = new MerchantPos();
  41.         this.ped = new MerchantPed(posId, terminalPedName);
  42.         this.environment = Environment.getEnvironment(environment);
  43.  
  44.         addPosCallBackHandler();
  45.         addPedCallbackHandler();
  46.         initLibrary();
  47.         registerPed();
  48.     }
  49.  
  50.     private void callbackWait(boolean condition) {
  51.         while (condition) {
  52.             System.out.println("wait: "  + condition);
  53.             try {
  54.                 synchronized (this) {
  55.                     wait(1000);
  56.                 }
  57.             } catch (InterruptedException e) {
  58.                 e.printStackTrace();
  59.             }
  60.         }
  61.     }
  62.  
  63.     private void addPedCallbackHandler() {
  64.         this.ped.addPedCallbackHandler(new PedCallbackHandler() {
  65.             @Override
  66.             public void pedStateChangeCallbackHandler() {
  67.                 log.info("ResultPedStateChange: " + ped.getResultPedStateChange().getPedState().getNameInClib());
  68.                 if (pedCallbackResultHandler.handlePedStateChangeResult(ped))
  69.                     log.info("Ped: " + ped.getPedName() + " is ready");
  70.             }
  71.  
  72.             @Override
  73.             public void showScreenCallbackHandler() {
  74.                 pedCallbackResultHandler.handleShowScreenResult();
  75.             }
  76.  
  77.             @Override
  78.             public void createTenderCallbackHandler() {
  79.  
  80.                 if (pedCallbackResultHandler.handleCreateTenderResult(ped)) {
  81.                     log.info("tenderCreated on the ped: " + ped);
  82.                 } else {
  83.                     log.error("tenderCreateError on the ped: " + ped);
  84.                     //posGuiCommHandler.updateStatusLabel(PropertyReader.getProperty("tenderCreateError"));
  85.                     refreshPed();
  86.                 }
  87.             }
  88.  
  89.             private void refreshPed() {
  90.                 ped.refreshPEDstate(5);
  91.             }
  92.  
  93.             @Override
  94.             public void progressCallbackHandler() {
  95.                 if (pedCallbackResultHandler.handleProgressResult(ped)) {
  96.                     Transaction.TenderState tenderState = ped.getResultTenderProgress().getTenderState();
  97.                     log.info("progressCallbackHandler - tenderState on ped: {} is: {}", ped, tenderState);
  98.                 } else {
  99.                     log.error("progressCallbackHandler error  on the ped: " + ped);
  100.                     //posGuiCommHandler.updateStatusLabel(PropertyReader.getProperty("progressCBError"));
  101.                 }
  102.             }
  103.  
  104.             @Override
  105.             public void additionalDataCallbackHandler() {
  106.                 if (pedCallbackResultHandler.handleAdditionalDataResult(ped)) {
  107.                     Tender tender = ped.getResultTenderAdditionalData();
  108.                     log.info("additionalDataCallbackHandler on the ped: {} with the tender: {}", ped, tender);
  109.  
  110.                 } else {
  111.                     log.error("additionalDataCallbackHandler error on the ped: {} ", ped);
  112.                 }
  113.             }
  114.  
  115.             @Override
  116.             public void tenderDccCallbackHandler() {
  117.                 pedCallbackResultHandler.handleTenderDccResult(ped);
  118.             }
  119.  
  120.             @Override
  121.             public void tenderPrintReceiptCallbackHandler() {
  122.             }
  123.  
  124.             @Override
  125.             public void tenderCheckSignatureCallbackHandler() {
  126.                 if (pedCallbackResultHandler.handleTenderCheckSignatureResult(ped)) {
  127.                     Tender tender = ped.getResultTenderCheckSignature();
  128.                     log.info("tenderCheckSignatureCallbackHandler on the ped: {} enableCheckSignatureForm: {}", ped,
  129.                             tender.getAdditionalData().get("signature.data"));
  130.                     //posGuiCommHandler.enableCheckSignatureForm(tender.getAdditionalData().get("signature.data"));
  131.                 }
  132.             }
  133.  
  134.             @Override
  135.             public void finalCallbackHandler() {
  136.  
  137.  
  138.                 if (pedCallbackResultHandler.handleFinalResult(ped)) {
  139.                     //check final transaction state
  140.                     Tender tender = ped.getResultTenderPrintReceipt();
  141.  
  142.  
  143.                     if (tender != null) {
  144.                         String finalStatus = tender.getTenderState().getNameInClib();
  145.                         log.info("finalCallbackHandler final status: {}", finalStatus);
  146.  
  147.                         if (finalStatus.equals(Transaction.TenderState.UNDEFINED.getNameInClib()))
  148.                             pedHandler.refreshPedState(ped, retries);
  149.  
  150.                         log.info("finalCallbackHandler final status after pedHandler.refreshPedState: {}", finalStatus);
  151.                     }
  152.                 } else {
  153.                     log.error("transactionError");
  154.                     //posGuiCommHandler.updateStatusLabel(PropertyReader.getProperty("transactionError"));
  155.                     refreshPed();
  156.                 }
  157.             }
  158.  
  159.             @Override
  160.             public void pedExceptionCallbackHandler() {
  161.                 if (!pedCallbackResultHandler.handlePedExceptionResult(ped)) {
  162.                     log.error("Ped exception operation has returned null result with ped: {}", ped);
  163.                     //posGuiCommHandler.updateStatusLabel(PropertyReader.getProperty("pedExceptionNull"));
  164.                 } else {
  165.                     log.error("Ped exception with ped: {}. The error message is: {}", ped, ped.getResultPedException().getAdyenResult().getErrorMessage());
  166.                     //posGuiCommHandler.updateStatusLabel(ped.getResultPedException().getAdyenResult().getErrorMessage());
  167.                 }
  168.  
  169.             }
  170.  
  171.             @Override
  172.             public void txStoreQueryCallbackHandler() {
  173.             }
  174.         });
  175.     }
  176.  
  177.     private void initLibrary() throws Exception {
  178.         log.info("starting initLibrary");
  179.         if (!posHandler.initLibrary(pos, buildInitLibraryRequest())){
  180.             String initLibraryErrorMessage = "initLibrary error with posId: " + this.posId;
  181.             log.error(initLibraryErrorMessage);
  182.             throw new Exception(initLibraryErrorMessage);
  183.         }
  184.         callbackWait(this.libraryIsNotInit);
  185.     }
  186.  
  187.     private void registerPed() throws Exception {
  188.         log.info("starting registration for ped: {}", ped);
  189.         if (!posHandler.registerPed(pos,ped)){
  190.             log.error("Ped registration with ped: {} and pos: {}", ped, posId);
  191.             throw new Exception("Ped registration error");
  192.         }
  193.         callbackWait(this.pedIsNotRegistered);
  194.     }
  195.  
  196.     private InitLibraryRequest buildInitLibraryRequest() {
  197.         return new InitLibraryRequest.InitLibraryRequestBuilder(
  198.                 this.environment, this.posId, null, new LogConfig(LogLevel.DEBUG)).build();
  199.     }
  200.  
  201.     private void addPosCallBackHandler() {
  202.         pos.addCallbackHandler(new PosCallbackHandler() {
  203.             @Override
  204.             public void initLibraryCallbackHandler() {
  205.                 if (posCallbackResultHandler.handleInitLibraryResult(pos))
  206.                     registerPos();
  207.                 else {
  208.                     log.error("initLibError");
  209.                 }
  210.             }
  211.  
  212.             private void registerPos() {
  213.                 log.info("Starting pos registration for posId: " + posId);
  214.                 if (!posHandler.registerPos(pos, buildRegisterPosRequest(posId))) {
  215.                     log.error("Pos registration error for posId: " + posId);
  216.                 }
  217.             }
  218.  
  219.             @Override
  220.             public void registerPosCallbackHandler() {
  221.                 if (posCallbackResultHandler.handleRegisterPosResult(pos)) {
  222.                     log.info("Pos registration finished for posId: " + posId);
  223.                     libraryIsNotInit = false;
  224.                     //posGuiCommHandler.enablePosDependantMenu(true);//enable ped registration
  225.                 } else {
  226.                     log.error("Pos registration finished with error for posId: " + posId);
  227.                     //posGuiCommHandler.enablePosDependantMenu(false);
  228.                 }
  229.             }
  230.  
  231.             @Override
  232.             public void registerPedCallbackHandler() {
  233.                 if (posCallbackResultHandler.handleRegisterPedResult(pos, ped)) {
  234.                     pedIsNotRegistered = false;
  235.                     log.info("Ped registration finished");
  236.                 } else {
  237.                     log.error("Ped registration finished with error");
  238.                 }
  239.             }
  240.  
  241.             @Override
  242.             public void cancelOrRefundAtPspCallbackHandler() {
  243.                 if (posCallbackResultHandler.handleCancelOrRefundAtPspResult(pos)) {
  244.                     log.info("Ped cancelOrRefund finished");
  245.                 } else {
  246.                     log.error("Ped cancelOrRefund finished with error");
  247.                 }
  248.             }
  249.  
  250.             @Override
  251.             public void exitLibraryCallbackHandler() {
  252.                 posCallbackResultHandler.handleExitLibraryResult(pos);
  253.             }
  254.  
  255.             @Override
  256.             public void exceptionCallbackHandler() {
  257.                 //posGuiCommHandler.updateStatusLabel(PropertyReader.getProperty("posExceptionCB") + pos.getResultException().getLibraryResult());
  258.                 posCallbackResultHandler.handleExceptionResult(pos);
  259.             }
  260.         });
  261.     }
  262.  
  263.     private RegisterPosRequest buildRegisterPosRequest(String posId) {
  264.         return new RegisterPosRequest(merchant, user, password, posId);
  265.     }
  266. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement