payments/servlet/CreateCreditCardServlet.java

CreateCreditCard Sample

Using the 'vault' API, you can store a Credit Card securely on PayPal. You can use a saved Credit Card to process a payment in the future. The following code demonstrates how can save a Credit Card on PayPal using the Vault API. API used: POST /v1/vault/credit-card

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.CreditCard; import com.paypal.api.payments.util.GenerateAccessToken; import com.paypal.api.payments.util.ResultPrinter; import com.paypal.base.rest.APIContext; import com.paypal.base.rest.PayPalRESTException; import com.paypal.base.rest.PayPalResource; public class CreateCreditCardServlet extends HttpServlet { private static final long serialVersionUID = 1L; private static final Logger LOGGER = Logger .getLogger(CreateCreditCardServlet.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 = CreateCreditCardServlet.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); }

Create

Sample showing how to create a CreditCard.

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

CreditCard

A resource representing a credit card that can be used to fund a payment.

CreditCard creditCard = new CreditCard(); creditCard.setExpireMonth(11); creditCard.setExpireYear(2018); creditCard.setNumber("4417119669820331"); creditCard.setType("visa"); 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();

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 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 ));

Save

Creates the credit card as a resource in the PayPal vault. The response contains an 'id' that you can use to refer to it in the future payments.

CreditCard createdCreditCard = creditCard.create(apiContext); LOGGER.info("Credit Card Created With ID: " + createdCreditCard.getId()); ResultPrinter.addResult(req, resp, "Created Credit Card", CreditCard.getLastRequest(), CreditCard.getLastResponse(), null); } catch (PayPalRESTException e) { ResultPrinter.addResult(req, resp, "Created Credit Card", CreditCard.getLastRequest(), null, e.getMessage()); } req.getRequestDispatcher("response.jsp").forward(req, resp); } }