diff --git a/.travis.yml b/.travis.yml
new file mode 100644
index 00000000..4be275b4
--- /dev/null
+++ b/.travis.yml
@@ -0,0 +1,9 @@
+language: java
+jdk:
+ - oraclejdk7
+ - openjdk7
+ - openjdk6
+notifications:
+ recipients:
+ - DL-PP-Platform-Java-SDK@ebay.com
+ on_success: change
\ No newline at end of file
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 33d10cd9..c8f7680b 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,8 +1,16 @@
CHANGELOG
=========
+V0.7.0 (May 30, 2013)
+-----------------------
+ * Added support for Auth and Capture APIs
+ * Types Modified to match the API Spec
-V0.5.2 (March 07, 2013)
+V0.6.0 (April 26, 2013)
-----------------------
+ * Added dynamic configuration support for API calls
+V0.5.2 (March 07, 2013)
+-----------------------
* Initial Release
+
diff --git a/README.md b/README.md
index a2b6cb62..18f4313f 100644
--- a/README.md
+++ b/README.md
@@ -1,4 +1,5 @@
-This repository contains java sdk and samples for REST API.
+## PayPal REST API Java SDK [](https://travis-ci.org/paypal/rest-api-sdk-java)
+This repository contains Java SDK and samples for REST API.
Prerequisites:
---------------
@@ -23,7 +24,7 @@ SDK Integration:
com.paypal.sdkrest-api-sdk
- 0.5.2
+ 0.7.0
@@ -31,40 +32,49 @@ To make an API call:
--------------------
* Import stub classes into your code. For example,
- import com.paypal.api.payments.*
+ ```java
+ import com.paypal.api.payments.*
+ ```
* Copy the configuration file `sdk_config.properties` in `rest-api-sample/src/test/resources` folder to your application `src/main/resources`. And load it as a classloader resource,
- InputStream is = PaymentWithCreditCardServlet.class.getResourceAsStream("/sdk_config.properties");
+ ```java
+ InputStream is = PaymentWithCreditCardServlet.class.getResourceAsStream("/sdk_config.properties");
+ ```
* Or load config file from any custom location using absolute path with the below method calls as required,
-
- Payment.initConfig(new File("../sdk_config.properties"));
- Or
- Payment.initConfig(new InputStream(new File("../sdk_config.properties")));
- Or
- Payment.initConfig(new Properties().load(new InputStream(new File("../sdk_config.properties"))));
+ ```java
+ Payment.initConfig(new File("../sdk_config.properties"));
+ Or
+ Payment.initConfig(new InputStream(new File("../sdk_config.properties")));
+ Or
+ Payment.initConfig(new Properties().load(new InputStream(new File("../sdk_config.properties"))));
+ ```
* Create `accesstoken` from `clientID` and `clientSecret` using `OAuthTokenCredential`
- String accessToken = new OAuthTokenCredential(clientID, clientSecret).getAccessToken();
+ ```java
+ String accessToken = new OAuthTokenCredential(clientID, clientSecret).getAccessToken();
+ ```
* Depending on the context of API calls, calling method may be static or non-static (For example, most `GET` http methods are created as `static` methods within the resource). In all API calls, we need to pass `accessToken` created above as argument as shown below,
* If it is static, invoke it as a class method as like
- Payment.get(accessToken, paymentID);
+ ```java
+ Payment.get(accessToken, paymentID);
+ ```
* If it is non-static, invoke it using resource object as like below. The API call takes a APIContext object in the place of AccessToken, APIContext object encapsulates Access Token and Request ID (used for idempotency).
- APIContext apiContext = new APIContext(accessToken);
- (OR)
- APIContext apiContext = new APIContext(accessToken, requestId);
- Payment payment = new Payment();
- payment.setIntent("sale");
- ...
- ...
- ...
- payment.create(apiContext);
+ ```java
+ APIContext apiContext = new APIContext(accessToken);
+ (OR)
+ APIContext apiContext = new APIContext(accessToken, requestId);
+ Payment payment = new Payment();
+ payment.setIntent("sale");
+ ...
+ payment.create(apiContext);
+ ```
SDK Configuration:
diff --git a/pom.xml b/pom.xml
index a3fde597..1e1e08b0 100644
--- a/pom.xml
+++ b/pom.xml
@@ -4,7 +4,7 @@
com.paypal.sdkrest-api
- 0.5.2
+ 0.7.0pomrest-api
diff --git a/rest-api-sample/pom.xml b/rest-api-sample/pom.xml
index 4750366e..86766661 100644
--- a/rest-api-sample/pom.xml
+++ b/rest-api-sample/pom.xml
@@ -3,14 +3,14 @@
4.0.0com.paypal.sdkrest-api-sample
- 0.5.2
+ 0.7.0REST API SAMPLEwarcom.paypal.sdkrest-api-sdk
- 0.5.2
+ 0.7.0log4j
@@ -20,6 +20,15 @@
+
+ org.apache.maven.plugins
+ maven-compiler-plugin
+ 3.0
+
+ 1.5
+ 1.5
+
+ org.mortbay.jettymaven-jetty-plugin
diff --git a/rest-api-sample/src/main/java/com/paypal/api/payments/servlet/AuthorizationCaptureServlet.java b/rest-api-sample/src/main/java/com/paypal/api/payments/servlet/AuthorizationCaptureServlet.java
new file mode 100644
index 00000000..80396f9b
--- /dev/null
+++ b/rest-api-sample/src/main/java/com/paypal/api/payments/servlet/AuthorizationCaptureServlet.java
@@ -0,0 +1,222 @@
+// #AuthorizationCapture Sample
+// This sample code demonstrate how you
+// do a Capture on an Authorization
+// API used: /v1/payments/authorization/{authorization_id}/capture
+package com.paypal.api.payments.servlet;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+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.Address;
+import com.paypal.api.payments.Amount;
+import com.paypal.api.payments.Authorization;
+import com.paypal.api.payments.Capture;
+import com.paypal.api.payments.CreditCard;
+import com.paypal.api.payments.Details;
+import com.paypal.api.payments.FundingInstrument;
+import com.paypal.api.payments.Payer;
+import com.paypal.api.payments.Payment;
+import com.paypal.api.payments.Transaction;
+import com.paypal.api.payments.util.GenerateAccessToken;
+import com.paypal.core.rest.APIContext;
+import com.paypal.core.rest.PayPalRESTException;
+import com.paypal.core.rest.PayPalResource;
+
+public class AuthorizationCaptureServlet extends HttpServlet {
+
+ private static final long serialVersionUID = 1L;
+
+ private static final Logger LOGGER = Logger
+ .getLogger(AuthorizationCaptureServlet.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 = AuthorizationCaptureServlet.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);
+ }
+
+ // ##AuthorizationCapture
+ // Sample showing how to do a Capture using 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);
+
+ // ###Amount
+ // Let's you specify a capture amount.
+ Amount amount = new Amount();
+ amount.setCurrency("USD");
+ amount.setTotal("4.54");
+
+ // ###Capture
+ // A capture transaction
+ Capture capture = new Capture();
+ capture.setAmount(amount);
+
+ // ##IsFinalCapture
+ // If set to true, all remaining
+ // funds held by the authorization
+ // will be released in the funding
+ // instrument. Default is ‘false’.
+ capture.setIsFinalCapture(true);
+
+ // Capture by POSTing to
+ // URI v1/payments/authorization/{authorization_id}/capture
+ Capture responseCapture = authorization.capture(apiContext, capture);
+
+ req.setAttribute("response", Authorization.getLastResponse());
+ LOGGER.info("Capture id = " + responseCapture.getId()
+ + " and status = " + responseCapture.getState());
+ } catch (PayPalRESTException e) {
+ req.setAttribute("error", e.getMessage());
+ }
+ req.setAttribute("request", Authorization.getLastRequest());
+ req.getRequestDispatcher("response.jsp").forward(req, resp);
+ }
+
+ private Authorization getAuthorization(APIContext apiContext)
+ throws PayPalRESTException {
+
+ // ###Details
+ // Let's you specify details of a payment amount.
+ Details details = new Details();
+ details.setShipping("0.03");
+ details.setSubtotal("107.41");
+ details.setTax("0.03");
+
+ // ###Amount
+ // Let's you specify a payment amount.
+ Amount amount = new Amount();
+ amount.setCurrency("USD");
+ amount.setTotal("107.47");
+ amount.setDetails(details);
+
+ // ###Transaction
+ // A transaction defines the contract of a
+ // payment - what is the payment for and who
+ // is fulfilling it. Transaction is created with
+ // a `Payee` and `Amount` types
+ Transaction transaction = new Transaction();
+ transaction.setAmount(amount);
+ transaction
+ .setDescription("This is the payment transaction description.");
+
+ // The Payment creation API requires a list of
+ // Transaction; add the created `Transaction`
+ // to a List
+ List transactions = new ArrayList();
+ transactions.add(transaction);
+
+ // ###Address
+ // Base Address object used as shipping or billing
+ // address in a payment. [Optional]
+ Address billingAddress = new Address();
+ billingAddress.setCity("Johnstown");
+ billingAddress.setCountryCode("US");
+ billingAddress.setLine1("52 N Main ST");
+ billingAddress.setPostalCode("43210");
+ billingAddress.setState("OH");
+
+ // ###CreditCard
+ // A resource representing a credit card that can be
+ // used to fund a payment.
+ CreditCard creditCard = new CreditCard();
+ creditCard.setBillingAddress(billingAddress);
+ creditCard.setCvv2("874");
+ creditCard.setExpireMonth(11);
+ creditCard.setExpireYear(2018);
+ creditCard.setFirstName("Joe");
+ creditCard.setLastName("Shopper");
+ creditCard.setNumber("4417119669820331");
+ creditCard.setType("visa");
+
+ // ###FundingInstrument
+ // A resource representing a Payeer's funding instrument.
+ // Use a Payer ID (A unique identifier of the payer generated
+ // and provided by the facilitator. This is required when
+ // creating or using a tokenized funding instrument)
+ // and the `CreditCardDetails`
+ FundingInstrument fundingInstrument = new FundingInstrument();
+ fundingInstrument.setCreditCard(creditCard);
+
+ // The Payment creation API requires a list of
+ // FundingInstrument; add the created `FundingInstrument`
+ // to a List
+ List fundingInstruments = new ArrayList();
+ fundingInstruments.add(fundingInstrument);
+
+ // ###Payer
+ // A resource representing a Payer that funds a payment
+ // Use the List of `FundingInstrument` and the Payment Method
+ // as 'credit_card'
+ Payer payer = new Payer();
+ payer.setFundingInstruments(fundingInstruments);
+ payer.setPaymentMethod("credit_card");
+
+ // ###Payment
+ // A Payment Resource; create one using
+ // the above types and intent as 'authorize'
+ Payment payment = new Payment();
+ payment.setIntent("authorize");
+ payment.setPayer(payer);
+ payment.setTransactions(transactions);
+
+ Payment responsePayment = payment.create(apiContext);
+ return responsePayment.getTransactions().get(0)
+ .getRelatedResources().get(0).getAuthorization();
+ }
+
+}
diff --git a/rest-api-sample/src/main/java/com/paypal/api/payments/servlet/CreateCreditCardServlet.java b/rest-api-sample/src/main/java/com/paypal/api/payments/servlet/CreateCreditCardServlet.java
index 0da959b3..8027586e 100644
--- a/rest-api-sample/src/main/java/com/paypal/api/payments/servlet/CreateCreditCardServlet.java
+++ b/rest-api-sample/src/main/java/com/paypal/api/payments/servlet/CreateCreditCardServlet.java
@@ -63,8 +63,8 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp)
// 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.setExpireMonth(11);
+ creditCard.setExpireYear(2018);
creditCard.setNumber("4417119669820331");
creditCard.setType("visa");
diff --git a/rest-api-sample/src/main/java/com/paypal/api/payments/servlet/GetAuthorizationServlet.java b/rest-api-sample/src/main/java/com/paypal/api/payments/servlet/GetAuthorizationServlet.java
new file mode 100644
index 00000000..a9a05a5d
--- /dev/null
+++ b/rest-api-sample/src/main/java/com/paypal/api/payments/servlet/GetAuthorizationServlet.java
@@ -0,0 +1,210 @@
+// #GetAuthorization Sample
+// This sample code demonstrate how you
+// can retrieve the details of a Authorization
+// resource
+// API used: /v1/payments/authorization/{id}
+package com.paypal.api.payments.servlet;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.List;
+
+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.Address;
+import com.paypal.api.payments.Amount;
+import com.paypal.api.payments.Authorization;
+import com.paypal.api.payments.CreditCard;
+import com.paypal.api.payments.Details;
+import com.paypal.api.payments.FundingInstrument;
+import com.paypal.api.payments.Payer;
+import com.paypal.api.payments.Payment;
+import com.paypal.api.payments.Transaction;
+import com.paypal.api.payments.util.GenerateAccessToken;
+import com.paypal.core.rest.APIContext;
+import com.paypal.core.rest.PayPalRESTException;
+import com.paypal.core.rest.PayPalResource;
+
+public class GetAuthorizationServlet extends HttpServlet {
+
+ private static final long serialVersionUID = 1L;
+
+ private static final Logger LOGGER = Logger
+ .getLogger(GetAuthorizationServlet.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 = GetAuthorizationServlet.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);
+ }
+
+ // ##GetAuthorization
+ // Sample showing how to do a Get Authorization
+ // using Authorization Id
+ @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 an Authorization Id
+ // by making a Payment with intent
+ // as 'authorize' and parsing through
+ // the Payment object
+ String authorizationId = getAuthorizationID(apiContext);
+
+ // Get Authorization by sending
+ // a GET request with authorization Id
+ // to the
+ // URI v1/payments/authorization/{id}
+ Authorization authorization = Authorization.get(apiContext,
+ authorizationId);
+
+ req.setAttribute("response", Authorization.getLastResponse());
+ LOGGER.info("Authorization id = " + authorization.getId()
+ + " and status = " + authorization.getState());
+ } catch (PayPalRESTException e) {
+ req.setAttribute("error", e.getMessage());
+ }
+ req.getRequestDispatcher("response.jsp").forward(req, resp);
+ }
+
+ private String getAuthorizationID(APIContext apiContext)
+ throws PayPalRESTException {
+ String authorizationID = null;
+
+ // ###Details
+ // Let's you specify details of a payment amount.
+ Details details = new Details();
+ details.setShipping("0.03");
+ details.setSubtotal("107.41");
+ details.setTax("0.03");
+
+ // ###Amount
+ // Let's you specify a payment amount.
+ Amount amount = new Amount();
+ amount.setCurrency("USD");
+ amount.setTotal("107.47");
+ amount.setDetails(details);
+
+ // ###Transaction
+ // A transaction defines the contract of a
+ // payment - what is the payment for and who
+ // is fulfilling it. Transaction is created with
+ // a `Payee` and `Amount` types
+ Transaction transaction = new Transaction();
+ transaction.setAmount(amount);
+ transaction
+ .setDescription("This is the payment transaction description.");
+
+ // The Payment creation API requires a list of
+ // Transaction; add the created `Transaction`
+ // to a List
+ List transactions = new ArrayList();
+ transactions.add(transaction);
+
+ // ###Address
+ // Base Address object used as shipping or billing
+ // address in a payment. [Optional]
+ Address billingAddress = new Address();
+ billingAddress.setCity("Johnstown");
+ billingAddress.setCountryCode("US");
+ billingAddress.setLine1("52 N Main ST");
+ billingAddress.setPostalCode("43210");
+ billingAddress.setState("OH");
+
+ // ###CreditCard
+ // A resource representing a credit card that can be
+ // used to fund a payment.
+ CreditCard creditCard = new CreditCard();
+ creditCard.setBillingAddress(billingAddress);
+ creditCard.setCvv2("874");
+ creditCard.setExpireMonth(11);
+ creditCard.setExpireYear(2018);
+ creditCard.setFirstName("Joe");
+ creditCard.setLastName("Shopper");
+ creditCard.setNumber("4417119669820331");
+ creditCard.setType("visa");
+
+ // ###FundingInstrument
+ // A resource representing a Payeer's funding instrument.
+ // Use a Payer ID (A unique identifier of the payer generated
+ // and provided by the facilitator. This is required when
+ // creating or using a tokenized funding instrument)
+ // and the `CreditCardDetails`
+ FundingInstrument fundingInstrument = new FundingInstrument();
+ fundingInstrument.setCreditCard(creditCard);
+
+ // The Payment creation API requires a list of
+ // FundingInstrument; add the created `FundingInstrument`
+ // to a List
+ List fundingInstruments = new ArrayList();
+ fundingInstruments.add(fundingInstrument);
+
+ // ###Payer
+ // A resource representing a Payer that funds a payment
+ // Use the List of `FundingInstrument` and the Payment Method
+ // as 'credit_card'
+ Payer payer = new Payer();
+ payer.setFundingInstruments(fundingInstruments);
+ payer.setPaymentMethod("credit_card");
+
+ // ###Payment
+ // A Payment Resource; create one using
+ // the above types and intent as 'authorize'
+ Payment payment = new Payment();
+ payment.setIntent("authorize");
+ payment.setPayer(payer);
+ payment.setTransactions(transactions);
+
+ Payment responsePayment = payment.create(apiContext);
+
+ // Retrieve the authorization Id
+ authorizationID = responsePayment.getTransactions().get(0)
+ .getRelatedResources().get(0).getAuthorization().getId();
+ return authorizationID;
+ }
+
+}
diff --git a/rest-api-sample/src/main/java/com/paypal/api/payments/servlet/GetCaptureServlet.java b/rest-api-sample/src/main/java/com/paypal/api/payments/servlet/GetCaptureServlet.java
new file mode 100644
index 00000000..b1b652e8
--- /dev/null
+++ b/rest-api-sample/src/main/java/com/paypal/api/payments/servlet/GetCaptureServlet.java
@@ -0,0 +1,239 @@
+// #GetCapture Sample
+// This sample code demonstrate how you
+// can retrieve the details of a Capture
+// resource
+// API used: /v1/payments/capture/{capture_id}
+package com.paypal.api.payments.servlet;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.List;
+
+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.Address;
+import com.paypal.api.payments.Amount;
+import com.paypal.api.payments.Authorization;
+import com.paypal.api.payments.Capture;
+import com.paypal.api.payments.CreditCard;
+import com.paypal.api.payments.Details;
+import com.paypal.api.payments.FundingInstrument;
+import com.paypal.api.payments.Payer;
+import com.paypal.api.payments.Payment;
+import com.paypal.api.payments.Transaction;
+import com.paypal.api.payments.util.GenerateAccessToken;
+import com.paypal.core.rest.APIContext;
+import com.paypal.core.rest.PayPalRESTException;
+import com.paypal.core.rest.PayPalResource;
+
+public class GetCaptureServlet extends HttpServlet {
+
+ private static final long serialVersionUID = 1L;
+
+ private static final Logger LOGGER = Logger
+ .getLogger(GetAuthorizationServlet.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 = GetCaptureServlet.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);
+ }
+
+ // ##GetCapture
+ // Sample showing how to Get a Capture using
+ // CaptureId
+ @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);
+
+ /// ###Capture
+ // Create a Capture object
+ // by doing a capture on
+ // Authorization object
+ // and retrieve the Id
+ String captureId = getCaptureId(apiContext, authorization);
+
+ // Retrieve the Capture object by
+ // doing a GET call to
+ // URI v1/payments/capture/{capture_id}
+ Capture capture = Capture.get(apiContext, captureId);
+
+ req.setAttribute("response", Capture.getLastResponse());
+ LOGGER.info("Capture id = " + capture.getId()
+ + " and status = " + capture.getState());
+
+ } catch (PayPalRESTException e) {
+ req.setAttribute("error", e.getMessage());
+ }
+ req.getRequestDispatcher("response.jsp").forward(req, resp);
+ }
+
+ private String getCaptureId(APIContext apiContext, Authorization authorization) throws PayPalRESTException{
+ String captureId = null;
+
+ // ###Amount
+ // Let's you specify a capture amount.
+ Amount amount = new Amount();
+ amount.setCurrency("USD");
+ amount.setTotal("4.54");
+
+ // ###Capture
+ Capture capture = new Capture();
+ capture.setAmount(amount);
+
+ // ##IsFinalCapture
+ // If set to true, all remaining
+ // funds held by the authorization
+ // will be released in the funding
+ // instrument. Default is ‘false’.
+ capture.setIsFinalCapture(true);
+
+ // Capture by POSTing to
+ // URI v1/payments/authorization/{authorization_id}/capture
+ Capture responseCapture = authorization.capture(apiContext, capture);
+ captureId = responseCapture.getId();
+
+ return captureId;
+ }
+
+ private Authorization getAuthorization(APIContext apiContext)
+ throws PayPalRESTException {
+
+ // ###Details
+ // Let's you specify details of a payment amount.
+ Details details = new Details();
+ details.setShipping("0.03");
+ details.setSubtotal("107.41");
+ details.setTax("0.03");
+
+ // ###Amount
+ // Let's you specify a payment amount.
+ Amount amount = new Amount();
+ amount.setCurrency("USD");
+ amount.setTotal("107.47");
+ amount.setDetails(details);
+
+ // ###Transaction
+ // A transaction defines the contract of a
+ // payment - what is the payment for and who
+ // is fulfilling it. Transaction is created with
+ // a `Payee` and `Amount` types
+ Transaction transaction = new Transaction();
+ transaction.setAmount(amount);
+ transaction
+ .setDescription("This is the payment transaction description.");
+
+ // The Payment creation API requires a list of
+ // Transaction; add the created `Transaction`
+ // to a List
+ List transactions = new ArrayList();
+ transactions.add(transaction);
+
+ // ###Address
+ // Base Address object used as shipping or billing
+ // address in a payment. [Optional]
+ Address billingAddress = new Address();
+ billingAddress.setCity("Johnstown");
+ billingAddress.setCountryCode("US");
+ billingAddress.setLine1("52 N Main ST");
+ billingAddress.setPostalCode("43210");
+ billingAddress.setState("OH");
+
+ // ###CreditCard
+ // A resource representing a credit card that can be
+ // used to fund a payment.
+ CreditCard creditCard = new CreditCard();
+ creditCard.setBillingAddress(billingAddress);
+ creditCard.setCvv2("874");
+ creditCard.setExpireMonth(11);
+ creditCard.setExpireYear(2018);
+ creditCard.setFirstName("Joe");
+ creditCard.setLastName("Shopper");
+ creditCard.setNumber("4417119669820331");
+ creditCard.setType("visa");
+
+ // ###FundingInstrument
+ // A resource representing a Payeer's funding instrument.
+ // Use a Payer ID (A unique identifier of the payer generated
+ // and provided by the facilitator. This is required when
+ // creating or using a tokenized funding instrument)
+ // and the `CreditCardDetails`
+ FundingInstrument fundingInstrument = new FundingInstrument();
+ fundingInstrument.setCreditCard(creditCard);
+
+ // The Payment creation API requires a list of
+ // FundingInstrument; add the created `FundingInstrument`
+ // to a List
+ List fundingInstruments = new ArrayList();
+ fundingInstruments.add(fundingInstrument);
+
+ // ###Payer
+ // A resource representing a Payer that funds a payment
+ // Use the List of `FundingInstrument` and the Payment Method
+ // as 'credit_card'
+ Payer payer = new Payer();
+ payer.setFundingInstruments(fundingInstruments);
+ payer.setPaymentMethod("credit_card");
+
+ // ###Payment
+ // A Payment Resource; create one using
+ // the above types and intent as 'authorize'
+ Payment payment = new Payment();
+ payment.setIntent("authorize");
+ payment.setPayer(payer);
+ payment.setTransactions(transactions);
+
+ Payment responsePayment = payment.create(apiContext);
+ return responsePayment.getTransactions().get(0).getRelatedResources()
+ .get(0).getAuthorization();
+ }
+}
diff --git a/rest-api-sample/src/main/java/com/paypal/api/payments/servlet/GetPaymentServlet.java b/rest-api-sample/src/main/java/com/paypal/api/payments/servlet/GetPaymentServlet.java
index 982533f7..c274274e 100644
--- a/rest-api-sample/src/main/java/com/paypal/api/payments/servlet/GetPaymentServlet.java
+++ b/rest-api-sample/src/main/java/com/paypal/api/payments/servlet/GetPaymentServlet.java
@@ -53,7 +53,7 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp)
}
- // ##GetPaymentByPaymentId
+ // ##GetPayment
// Call the method with a valid Payment ID
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
diff --git a/rest-api-sample/src/main/java/com/paypal/api/payments/servlet/PaymentWithCreditCardServlet.java b/rest-api-sample/src/main/java/com/paypal/api/payments/servlet/PaymentWithCreditCardServlet.java
index 63d9e1c3..464ad5b2 100644
--- a/rest-api-sample/src/main/java/com/paypal/api/payments/servlet/PaymentWithCreditCardServlet.java
+++ b/rest-api-sample/src/main/java/com/paypal/api/payments/servlet/PaymentWithCreditCardServlet.java
@@ -71,19 +71,19 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp)
CreditCard creditCard = new CreditCard();
creditCard.setBillingAddress(billingAddress);
creditCard.setCvv2("874");
- creditCard.setExpireMonth("11");
- creditCard.setExpireYear("2018");
+ creditCard.setExpireMonth(11);
+ creditCard.setExpireYear(2018);
creditCard.setFirstName("Joe");
creditCard.setLastName("Shopper");
creditCard.setNumber("4417119669820331");
creditCard.setType("visa");
- // ###AmountDetails
+ // ###Details
// Let's you specify details of a payment amount.
- AmountDetails amountDetails = new AmountDetails();
- amountDetails.setShipping("1");
- amountDetails.setSubtotal("5");
- amountDetails.setTax("1");
+ Details details = new Details();
+ details.setShipping("1");
+ details.setSubtotal("5");
+ details.setTax("1");
// ###Amount
// Let's you specify a payment amount.
@@ -91,7 +91,7 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp)
amount.setCurrency("USD");
// Total must be equal to sum of shipping, tax and subtotal.
amount.setTotal("7");
- amount.setDetails(amountDetails);
+ amount.setDetails(details);
// ###Transaction
// A transaction defines the contract of a
diff --git a/rest-api-sample/src/main/java/com/paypal/api/payments/servlet/PaymentWithPayPalServlet.java b/rest-api-sample/src/main/java/com/paypal/api/payments/servlet/PaymentWithPayPalServlet.java
index 839a0abf..5f3ebcc7 100644
--- a/rest-api-sample/src/main/java/com/paypal/api/payments/servlet/PaymentWithPayPalServlet.java
+++ b/rest-api-sample/src/main/java/com/paypal/api/payments/servlet/PaymentWithPayPalServlet.java
@@ -6,16 +6,33 @@
import java.io.IOException;
import java.io.InputStream;
-import java.util.*;
-
-import javax.servlet.*;
-import javax.servlet.http.*;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.UUID;
+
+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.*;
-import com.paypal.api.payments.util.*;
-import com.paypal.core.rest.*;
+import com.paypal.api.payments.Amount;
+import com.paypal.api.payments.Details;
+import com.paypal.api.payments.Links;
+import com.paypal.api.payments.Payer;
+import com.paypal.api.payments.Payment;
+import com.paypal.api.payments.PaymentExecution;
+import com.paypal.api.payments.RedirectUrls;
+import com.paypal.api.payments.Transaction;
+import com.paypal.api.payments.util.GenerateAccessToken;
+import com.paypal.core.rest.APIContext;
+import com.paypal.core.rest.PayPalRESTException;
+import com.paypal.core.rest.PayPalResource;
/**
* @author lvairamani
@@ -65,17 +82,17 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp)
accessToken = GenerateAccessToken.getAccessToken();
// ### Api Context
- // Pass in a `ApiContext` object to authenticate
- // the call and to send a unique request id
+ // 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.
+ // 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
+ // 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 ));
+ /*
+ * String requestId = Long.toString(System.nanoTime(); APIContext
+ * apiContext = new APIContext(accessToken, requestId ));
*/
} catch (PayPalRESTException e) {
req.setAttribute("error", e.getMessage());
@@ -96,12 +113,12 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp)
}
} else {
- // ###AmountDetails
+ // ###Details
// Let's you specify details of a payment amount.
- AmountDetails amountDetails = new AmountDetails();
- amountDetails.setShipping("1");
- amountDetails.setSubtotal("5");
- amountDetails.setTax("1");
+ Details details = new Details();
+ details.setShipping("1");
+ details.setSubtotal("5");
+ details.setTax("1");
// ###Amount
// Let's you specify a payment amount.
@@ -109,7 +126,7 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp)
amount.setCurrency("USD");
// Total must be equal to sum of shipping, tax and subtotal.
amount.setTotal("7");
- amount.setDetails(amountDetails);
+ amount.setDetails(details);
// ###Transaction
// A transaction defines the contract of a
@@ -162,9 +179,9 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp)
+ createdPayment.getId() + " and status = "
+ createdPayment.getState());
// ###Payment Approval Url
- Iterator links = createdPayment.getLinks().iterator();
+ Iterator links = createdPayment.getLinks().iterator();
while (links.hasNext()) {
- Link link = links.next();
+ Links link = links.next();
if (link.getRel().equalsIgnoreCase("approval_url")) {
req.setAttribute("redirectURL", link.getHref());
}
diff --git a/rest-api-sample/src/main/java/com/paypal/api/payments/servlet/PaymentWithSavedCardServlet.java b/rest-api-sample/src/main/java/com/paypal/api/payments/servlet/PaymentWithSavedCardServlet.java
index 10b0f7b6..b066d4cb 100644
--- a/rest-api-sample/src/main/java/com/paypal/api/payments/servlet/PaymentWithSavedCardServlet.java
+++ b/rest-api-sample/src/main/java/com/paypal/api/payments/servlet/PaymentWithSavedCardServlet.java
@@ -63,12 +63,12 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp)
CreditCardToken creditCardToken = new CreditCardToken();
creditCardToken.setCreditCardId("CARD-5BT058015C739554AKE2GCEI");
- // ###AmountDetails
+ // ###Details
// Let's you specify details of a payment amount.
- AmountDetails amountDetails = new AmountDetails();
- amountDetails.setShipping("1");
- amountDetails.setSubtotal("5");
- amountDetails.setTax("1");
+ Details details = new Details();
+ details.setShipping("1");
+ details.setSubtotal("5");
+ details.setTax("1");
// ###Amount
// Let's you specify a payment amount.
@@ -76,7 +76,7 @@ protected void doPost(HttpServletRequest req, HttpServletResponse resp)
amount.setCurrency("USD");
// Total must be equal to the sum of shipping, tax and subtotal.
amount.setTotal("7");
- amount.setDetails(amountDetails);
+ amount.setDetails(details);
// ###Transaction
// A transaction defines the contract of a
diff --git a/rest-api-sample/src/main/java/com/paypal/api/payments/servlet/RefundCaptureServlet.java b/rest-api-sample/src/main/java/com/paypal/api/payments/servlet/RefundCaptureServlet.java
new file mode 100644
index 00000000..23d64c7a
--- /dev/null
+++ b/rest-api-sample/src/main/java/com/paypal/api/payments/servlet/RefundCaptureServlet.java
@@ -0,0 +1,250 @@
+// #RefundCapture Sample
+// This sample code demonstrate how you
+// can do a Refund on a Capture
+// resource
+// API used: /v1/payments/capture/{capture_id}/refund
+package com.paypal.api.payments.servlet;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.List;
+
+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.Address;
+import com.paypal.api.payments.Amount;
+import com.paypal.api.payments.Authorization;
+import com.paypal.api.payments.Capture;
+import com.paypal.api.payments.CreditCard;
+import com.paypal.api.payments.Details;
+import com.paypal.api.payments.FundingInstrument;
+import com.paypal.api.payments.Payer;
+import com.paypal.api.payments.Payment;
+import com.paypal.api.payments.Refund;
+import com.paypal.api.payments.Transaction;
+import com.paypal.api.payments.util.GenerateAccessToken;
+import com.paypal.core.rest.APIContext;
+import com.paypal.core.rest.PayPalRESTException;
+import com.paypal.core.rest.PayPalResource;
+
+public class RefundCaptureServlet extends HttpServlet {
+
+ private static final long serialVersionUID = 1L;
+
+ private static final Logger LOGGER = Logger
+ .getLogger(GetAuthorizationServlet.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 = RefundCaptureServlet.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);
+ }
+
+ // ##RefundCapture
+ // Sample showing to how to do a Refund on
+ // a Capture
+ @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);
+
+ /// ###Capture
+ // Create a Capture object
+ // by doing a capture on
+ // Authorization object
+ Capture capture = getCapture(apiContext, authorization);
+
+ /// ###Refund
+ /// Create a Refund object
+ Refund refund = new Refund();
+
+ // ###Amount
+ // Let's you specify a capture amount.
+ Amount amount = new Amount();
+ amount.setCurrency("USD").setTotal("1");
+
+ refund.setAmount(amount);
+
+ // Create new APIContext for
+ // Refund
+ apiContext = new APIContext(accessToken);
+ // Do a Refund by
+ // POSTing to
+ // URI v1/payments/capture/{capture_id}/refund
+ Refund responseRefund = capture.refund(apiContext, refund);
+
+ req.setAttribute("response", Capture.getLastResponse());
+ LOGGER.info("Refund id = " + responseRefund.getId()
+ + " and status = " + responseRefund.getState());
+
+ } catch (PayPalRESTException e) {
+ req.setAttribute("error", e.getMessage());
+ }
+ req.setAttribute("request", Capture.getLastRequest());
+ req.getRequestDispatcher("response.jsp").forward(req, resp);
+ }
+
+ private Capture getCapture(APIContext apiContext, Authorization authorization) throws PayPalRESTException{
+ // ###Amount
+ // Let's you specify a capture amount.
+ Amount amount = new Amount();
+ amount.setCurrency("USD");
+ amount.setTotal("4.54");
+
+ // ###Capture
+ Capture capture = new Capture();
+ capture.setAmount(amount);
+
+ // ##IsFinalCapture
+ // If set to true, all remaining
+ // funds held by the authorization
+ // will be released in the funding
+ // instrument. Default is ‘false’.
+ capture.setIsFinalCapture(true);
+
+ // Capture by POSTing to
+ // URI v1/payments/authorization/{authorization_id}/capture
+ Capture responseCapture = authorization.capture(apiContext, capture);
+ return responseCapture;
+ }
+
+ private Authorization getAuthorization(APIContext apiContext)
+ throws PayPalRESTException {
+
+ // ###Details
+ // Let's you specify details of a payment amount.
+ Details details = new Details();
+ details.setShipping("0.03");
+ details.setSubtotal("107.41");
+ details.setTax("0.03");
+
+ // ###Amount
+ // Let's you specify a payment amount.
+ Amount amount = new Amount();
+ amount.setCurrency("USD");
+ amount.setTotal("107.47");
+ amount.setDetails(details);
+
+ // ###Transaction
+ // A transaction defines the contract of a
+ // payment - what is the payment for and who
+ // is fulfilling it. Transaction is created with
+ // a `Payee` and `Amount` types
+ Transaction transaction = new Transaction();
+ transaction.setAmount(amount);
+ transaction
+ .setDescription("This is the payment transaction description.");
+
+ // The Payment creation API requires a list of
+ // Transaction; add the created `Transaction`
+ // to a List
+ List transactions = new ArrayList();
+ transactions.add(transaction);
+
+ // ###Address
+ // Base Address object used as shipping or billing
+ // address in a payment. [Optional]
+ Address billingAddress = new Address();
+ billingAddress.setCity("Johnstown");
+ billingAddress.setCountryCode("US");
+ billingAddress.setLine1("52 N Main ST");
+ billingAddress.setPostalCode("43210");
+ billingAddress.setState("OH");
+
+ // ###CreditCard
+ // A resource representing a credit card that can be
+ // used to fund a payment.
+ CreditCard creditCard = new CreditCard();
+ creditCard.setBillingAddress(billingAddress);
+ creditCard.setCvv2("874");
+ creditCard.setExpireMonth(11);
+ creditCard.setExpireYear(2018);
+ creditCard.setFirstName("Joe");
+ creditCard.setLastName("Shopper");
+ creditCard.setNumber("4417119669820331");
+ creditCard.setType("visa");
+
+ // ###FundingInstrument
+ // A resource representing a Payeer's funding instrument.
+ // Use a Payer ID (A unique identifier of the payer generated
+ // and provided by the facilitator. This is required when
+ // creating or using a tokenized funding instrument)
+ // and the `CreditCardDetails`
+ FundingInstrument fundingInstrument = new FundingInstrument();
+ fundingInstrument.setCreditCard(creditCard);
+
+ // The Payment creation API requires a list of
+ // FundingInstrument; add the created `FundingInstrument`
+ // to a List
+ List fundingInstruments = new ArrayList();
+ fundingInstruments.add(fundingInstrument);
+
+ // ###Payer
+ // A resource representing a Payer that funds a payment
+ // Use the List of `FundingInstrument` and the Payment Method
+ // as 'credit_card'
+ Payer payer = new Payer();
+ payer.setFundingInstruments(fundingInstruments);
+ payer.setPaymentMethod("credit_card");
+
+ // ###Payment
+ // A Payment Resource; create one using
+ // the above types and intent as 'authorize'
+ Payment payment = new Payment();
+ payment.setIntent("authorize");
+ payment.setPayer(payer);
+ payment.setTransactions(transactions);
+
+ Payment responsePayment = payment.create(apiContext);
+ return responsePayment.getTransactions().get(0).getRelatedResources()
+ .get(0).getAuthorization();
+ }
+}
diff --git a/rest-api-sample/src/main/java/com/paypal/api/payments/servlet/VoidAuthorizationServlet.java b/rest-api-sample/src/main/java/com/paypal/api/payments/servlet/VoidAuthorizationServlet.java
new file mode 100644
index 00000000..55670b37
--- /dev/null
+++ b/rest-api-sample/src/main/java/com/paypal/api/payments/servlet/VoidAuthorizationServlet.java
@@ -0,0 +1,205 @@
+// #VoidAuthorization Sample
+// This sample code demonstrate how you
+// can do a Void on a Authorization
+// resource
+// API used: /v1/payments/authorization/{authorization_id}/void
+package com.paypal.api.payments.servlet;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+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.Address;
+import com.paypal.api.payments.Amount;
+import com.paypal.api.payments.Authorization;
+import com.paypal.api.payments.CreditCard;
+import com.paypal.api.payments.Details;
+import com.paypal.api.payments.FundingInstrument;
+import com.paypal.api.payments.Payer;
+import com.paypal.api.payments.Payment;
+import com.paypal.api.payments.Transaction;
+import com.paypal.api.payments.util.GenerateAccessToken;
+import com.paypal.core.rest.APIContext;
+import com.paypal.core.rest.PayPalRESTException;
+import com.paypal.core.rest.PayPalResource;
+
+public class VoidAuthorizationServlet extends HttpServlet {
+
+ private static final long serialVersionUID = 1L;
+
+ private static final Logger LOGGER = Logger
+ .getLogger(VoidAuthorizationServlet.class);
+ Map map = new HashMap();
+
+ 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());
+ }
+ }
+
+ @Override
+ protected void doGet(HttpServletRequest req, HttpServletResponse resp)
+ throws ServletException, IOException {
+ doPost(req, resp);
+ }
+
+ // ##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);
+ }
+
+ private Authorization getAuthorization(APIContext apiContext)
+ throws PayPalRESTException {
+
+ // ###Details
+ // Let's you specify details of a payment amount.
+ Details details = new Details();
+ details.setShipping("0.03");
+ details.setSubtotal("107.41");
+ details.setTax("0.03");
+
+ // ###Amount
+ // Let's you specify a payment amount.
+ Amount amount = new Amount();
+ amount.setCurrency("USD");
+ amount.setTotal("107.47");
+ amount.setDetails(details);
+
+ // ###Transaction
+ // A transaction defines the contract of a
+ // payment - what is the payment for and who
+ // is fulfilling it. Transaction is created with
+ // a `Payee` and `Amount` types
+ Transaction transaction = new Transaction();
+ transaction.setAmount(amount);
+ transaction
+ .setDescription("This is the payment transaction description.");
+
+ // The Payment creation API requires a list of
+ // Transaction; add the created `Transaction`
+ // to a List
+ List transactions = new ArrayList();
+ transactions.add(transaction);
+
+ // ###Address
+ // Base Address object used as shipping or billing
+ // address in a payment. [Optional]
+ Address billingAddress = new Address();
+ billingAddress.setCity("Johnstown");
+ billingAddress.setCountryCode("US");
+ billingAddress.setLine1("52 N Main ST");
+ billingAddress.setPostalCode("43210");
+ billingAddress.setState("OH");
+
+ // ###CreditCard
+ // A resource representing a credit card that can be
+ // used to fund a payment.
+ CreditCard creditCard = new CreditCard();
+ creditCard.setBillingAddress(billingAddress);
+ creditCard.setCvv2("874");
+ creditCard.setExpireMonth(11);
+ creditCard.setExpireYear(2018);
+ creditCard.setFirstName("Joe");
+ creditCard.setLastName("Shopper");
+ creditCard.setNumber("4417119669820331");
+ creditCard.setType("visa");
+
+ // ###FundingInstrument
+ // A resource representing a Payeer's funding instrument.
+ // Use a Payer ID (A unique identifier of the payer generated
+ // and provided by the facilitator. This is required when
+ // creating or using a tokenized funding instrument)
+ // and the `CreditCardDetails`
+ FundingInstrument fundingInstrument = new FundingInstrument();
+ fundingInstrument.setCreditCard(creditCard);
+
+ // The Payment creation API requires a list of
+ // FundingInstrument; add the created `FundingInstrument`
+ // to a List
+ List fundingInstruments = new ArrayList();
+ fundingInstruments.add(fundingInstrument);
+
+ // ###Payer
+ // A resource representing a Payer that funds a payment
+ // Use the List of `FundingInstrument` and the Payment Method
+ // as 'credit_card'
+ Payer payer = new Payer();
+ payer.setFundingInstruments(fundingInstruments);
+ payer.setPaymentMethod("credit_card");
+
+ // ###Payment
+ // A Payment Resource; create one using
+ // the above types and intent as 'authorize'
+ Payment payment = new Payment();
+ payment.setIntent("authorize");
+ payment.setPayer(payer);
+ payment.setTransactions(transactions);
+
+ Payment responsePayment = payment.create(apiContext);
+ return responsePayment.getTransactions().get(0)
+ .getRelatedResources().get(0).getAuthorization();
+ }
+
+}
diff --git a/rest-api-sample/src/main/webapp/WEB-INF/web.xml b/rest-api-sample/src/main/webapp/WEB-INF/web.xml
index 9d3900f3..629e9c56 100644
--- a/rest-api-sample/src/main/webapp/WEB-INF/web.xml
+++ b/rest-api-sample/src/main/webapp/WEB-INF/web.xml
@@ -1,85 +1,126 @@
-
- OAuth2TokenGenerator
-
-
- 30
-
-
-
- index.html
-
-
- PaymentWithCreditCardServlet
- com.paypal.api.payments.servlet.PaymentWithCreditCardServlet
-
-
- PaymentWithCreditCardServlet
- /paymentwithcreditcard
-
-
- PaymentWithPayPalServlet
- com.paypal.api.payments.servlet.PaymentWithPayPalServlet
-
-
- PaymentWithPayPalServlet
- /paymentwithpaypal
-
-
- PaymentWithSavedCardServlet
- com.paypal.api.payments.servlet.PaymentWithSavedCardServlet
-
-
- PaymentWithSavedCardServlet
- /paymentwithsavedcard
-
-
- GetPaymentServlet
- com.paypal.api.payments.servlet.GetPaymentServlet
-
-
- GetPaymentServlet
- /getpayment
-
-
- GetPaymentHistoryServlet
- com.paypal.api.payments.servlet.GetPaymentHistoryServlet
-
-
- GetPaymentHistoryServlet
- /getpaymenthistory
-
-
- GetSaleServlet
- com.paypal.api.payments.servlet.GetSaleServlet
-
-
- GetSaleServlet
- /getsale
-
-
- SaleRefundServlet
- com.paypal.api.payments.servlet.SaleRefundServlet
-
-
- SaleRefundServlet
- /salerefund
-
-
- CreateCreditCardServlet
- com.paypal.api.payments.servlet.CreateCreditCardServlet
-
-
- CreateCreditCardServlet
- /createcreditcard
-
-
- GetCreditCardServlet
- com.paypal.api.payments.servlet.GetCreditCardServlet
-
-
- GetCreditCardServlet
- /getcreditcard
-
-
-
+
+ OAuth2TokenGenerator
+
+ 30
+
+
+ index.html
+
+
+ GetAuthorizationServlet
+ com.paypal.api.payments.servlet.GetAuthorizationServlet
+
+
+ AuthorizationCaptureServlet
+ com.paypal.api.payments.servlet.AuthorizationCaptureServlet
+
+
+ VoidAuthorizationServlet
+ com.paypal.api.payments.servlet.VoidAuthorizationServlet
+
+
+ GetCaptureServlet
+ com.paypal.api.payments.servlet.GetCaptureServlet
+
+
+ RefundCaptureServlet
+ com.paypal.api.payments.servlet.RefundCaptureServlet
+
+
+ PaymentWithCreditCardServlet
+ com.paypal.api.payments.servlet.PaymentWithCreditCardServlet
+
+
+ PaymentWithCreditCardServlet
+ /paymentwithcreditcard
+
+
+ PaymentWithPayPalServlet
+ com.paypal.api.payments.servlet.PaymentWithPayPalServlet
+
+
+ PaymentWithPayPalServlet
+ /paymentwithpaypal
+
+
+ PaymentWithSavedCardServlet
+ com.paypal.api.payments.servlet.PaymentWithSavedCardServlet
+
+
+
+ GetAuthorizationServlet
+ /getauthorizationservlet
+
+
+ AuthorizationCaptureServlet
+ /authorizationcaptureservlet
+
+
+ VoidAuthorizationServlet
+ /voidauthorizationservlet
+
+
+ GetCaptureServlet
+ /getcaptureservlet
+
+
+ RefundCaptureServlet
+ /refundcaptureservlet
+
+
+ PaymentWithSavedCardServlet
+ /paymentwithsavedcard
+
+
+ GetPaymentServlet
+ com.paypal.api.payments.servlet.GetPaymentServlet
+
+
+ GetPaymentServlet
+ /getpayment
+
+
+ GetPaymentHistoryServlet
+ com.paypal.api.payments.servlet.GetPaymentHistoryServlet
+
+
+ GetPaymentHistoryServlet
+ /getpaymenthistory
+
+
+ GetSaleServlet
+ com.paypal.api.payments.servlet.GetSaleServlet
+
+
+ GetSaleServlet
+ /getsale
+
+
+ SaleRefundServlet
+ com.paypal.api.payments.servlet.SaleRefundServlet
+
+
+ SaleRefundServlet
+ /salerefund
+
+
+ CreateCreditCardServlet
+ com.paypal.api.payments.servlet.CreateCreditCardServlet
+
+
+ CreateCreditCardServlet
+ /createcreditcard
+
+
+ GetCreditCardServlet
+ com.paypal.api.payments.servlet.GetCreditCardServlet
+
+
+ GetCreditCardServlet
+ /getcreditcard
+
+
+
diff --git a/rest-api-sample/src/main/webapp/index.html b/rest-api-sample/src/main/webapp/index.html
index a6404061..a574677c 100644
--- a/rest-api-sample/src/main/webapp/index.html
+++ b/rest-api-sample/src/main/webapp/index.html
@@ -114,6 +114,58 @@