public void init(ServletConfig servletConfig) throws ServletException { // ##Load Configuration // Load SDK configuration for // the resource. This intialization code can be // done as Init Servlet. InputStream is = VoidAuthorizationServlet.class.getResourceAsStream("/sdk_config.properties"); try { PayPalResource.initConfig(is); } catch (PayPalRESTException e) { LOGGER.fatal(e.getMessage()); } }
// ##VoidAuthorization // Sample showing how to void an Authorization @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // ###AccessToken // Retrieve the access token from // OAuthTokenCredential by passing in // ClientID and ClientSecret APIContext apiContext = null; String accessToken = null; try { accessToken = GenerateAccessToken.getAccessToken(); // ### Api Context // Pass in a `ApiContext` object to authenticate // the call and to send a unique request id // (that ensures idempotency). The SDK generates // a request id if you do not pass one explicitly. apiContext = new APIContext(accessToken); // Use this variant if you want to pass in a request id // that is meaningful in your application, ideally // a order id. /* * String requestId = Long.toString(System.nanoTime(); APIContext * apiContext = new APIContext(accessToken, requestId )); */ // ###Authorization // Retrieve a Authorization object // by making a Payment with intent // as 'authorize' Authorization authorization = getAuthorization(apiContext); // Void an Authorization // by POSTing to // URI v1/payments/authorization/{authorization_id}/void Authorization returnAuthorization = authorization.doVoid(apiContext); req.setAttribute("response", Authorization.getLastResponse()); LOGGER.info( "Authorization id = " + returnAuthorization.getId() + " and status = " + returnAuthorization.getState()); } catch (PayPalRESTException e) { req.setAttribute("error", e.getMessage()); } req.getRequestDispatcher("response.jsp").forward(req, resp); }