payments/servlet/GetPaymentServlet.java

GetPayment Sample

This sample code demonstrates how you can retrieve the details of a payment resource. API used: /v1/payments/payment/{payment-id}

package com.paypal.api.payments.servlet; import java.io.IOException; import java.io.InputStream; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.log4j.Logger; import com.paypal.api.payments.Payment; import com.paypal.api.payments.util.GenerateAccessToken; import com.paypal.api.payments.util.ResultPrinter; import com.paypal.base.rest.PayPalRESTException; import com.paypal.base.rest.PayPalResource;

author lvairamani

public class GetPaymentServlet extends HttpServlet { private static final long serialVersionUID = 1L; private static final Logger LOGGER = Logger .getLogger(GetPaymentServlet.class); 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 = GetPaymentServlet.class .getResourceAsStream("/sdk_config.properties"); try { PayPalResource.initConfig(is); } catch (PayPalRESTException e) { LOGGER.fatal(e.getMessage()); } } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); }

GetPayment

Call the method with a valid Payment ID

@Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { try {

AccessToken

Retrieve the access token from OAuthTokenCredential by passing in ClientID and ClientSecret It is not mandatory to generate Access Token on a per call basis. Typically the access token can be generated once and reused within the expiry window

String accessToken = GenerateAccessToken.getAccessToken();

Retrieve the payment object by calling the static get method on the Payment class by passing a valid AccessToken and Payment ID

Payment payment = Payment.get(accessToken, "PAY-0XL713371A312273YKE2GCNI"); LOGGER.info("Payment retrieved ID = " + payment.getId() + ", status = " + payment.getState()); ResultPrinter.addResult(req, resp, "Get Payment", Payment.getLastRequest(), Payment.getLastResponse(), null); } catch (PayPalRESTException e) { ResultPrinter.addResult(req, resp, "Get Payment", Payment.getLastRequest(), null, e.getMessage()); } req.getRequestDispatcher("response.jsp").forward(req, resp); } }