Welcome to PayPal Checkout. This repository contains PayPal's Java SDK for Checkout and samples for v2/checkout/orders and v2/payments APIs.
This is a part of the next major PayPal SDK. It includes a simplified interface to only provide simple model objects and blueprints for HTTP calls. This repo currently contains functionality for PayPal Checkout APIs which includes Orders V2 and Payments V2.
Please refer to the PayPal Checkout Integration Guide for more information. Also refer to Setup your SDK for additional information about setting up the SDK's.
Java JDK 8 or higher
An environment which supports TLS 1.2 (see the TLS-update site for more information)
It is not mandatory to fork this repository for using the PayPal SDK. You can refer PayPal Checkout Server SDK for configuring and working with SDK without forking this code.
For contirbuting or referrring the samples, You can fork/refer this repository.
Get client ID and client secret by going to https://developer.paypal.com/developer/applications and generating a REST API app. Get Client ID and Secret from there.
package com.paypal;
import com.paypal.core.PayPalEnvironment;
import com.paypal.core.PayPalHttpClient;
public class Credentials {
static String clientId = "CLIENT-ID";
static String secret = "CLIENT-SECRET";
// Creating a sandbox environment
private static PayPalEnvironment environment = new PayPalEnvironment.Sandbox(clientId, secret);
// Creating a client for the environment
static PayPalHttpClient client = new PayPalHttpClient(environment);
}
This will create an order and print order id for the created order
package com.paypal;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.braintreepayments.http.HttpResponse;
import com.braintreepayments.http.exceptions.HttpException;
import com.paypal.orders.*;
public class CreateOrderExample {
public static void main(String[] args) {
Order order = null;
// Construct a request object and set desired parameters
// Here, OrdersCreateRequest() creates a POST request to /v2/checkout/orders
OrderRequest orderRequest = new OrderRequest();
orderRequest.intent("CAPTURE");
List<PurchaseUnitRequest> purchaseUnits = new ArrayList<>();
purchaseUnits
.add(new PurchaseUnitRequest().amount(new AmountWithBreakdown().currencyCode("USD").value("100.00")));
orderRequest.purchaseUnits(purchaseUnits);
OrdersCreateRequest request = new OrdersCreateRequest().requestBody(orderRequest);
try {
// Call API with your client and get a response for your call
HttpResponse<Order> response = Credentials.client.execute(request);
// If call returns body in response, you can get the de-serialized version by
// calling result() on the response
order = response.result();
System.out.println("Order ID: " + order.id());
order.links().forEach(link -> System.out.println(link.rel() + " => " + link.method() + ":" + link.href()));
} catch (IOException ioe) {
if (ioe instanceof HttpException) {
// Something went wrong server-side
HttpException he = (HttpException) ioe;
System.out.println(he.getMessage());
he.headers().forEach(x -> System.out.println(x + " :" + he.headers().header(x)));
} else {
// Something went wrong client-side
}
}
}
}
This will capture an order
package com.paypal;
import java.io.IOException;
import com.braintreepayments.http.HttpResponse;
import com.braintreepayments.http.exceptions.HttpException;
import com.paypal.orders.*;
public class CaptureOrderExample {
public static void main(String[] args) {
Order order = null;
OrdersCaptureRequest request = new OrdersCaptureRequest("APPROVED-ORDER-ID");
try {
// Call API with your client and get a response for your call
HttpResponse<Order> response = Credentials.client.execute(request);
// If call returns body in response, you can get the de-serialized version by
// calling result() on the response
order = response.result();
System.out.println("Capture ID: " + order.purchaseUnits().get(0).payments().captures().get(0).id());
order.purchaseUnits().get(0).payments().captures().get(0).links()
.forEach(link -> System.out.println(link.rel() + " => " + link.method() + ":" + link.href()));
} catch (IOException ioe) {
if (ioe instanceof HttpException) {
// Something went wrong server-side
HttpException he = (HttpException) ioe;
System.out.println(he.getMessage());
he.headers().forEach(x -> System.out.println(x + " :" + he.headers().header(x)));
} else {
// Something went wrong client-side
}
}
}
}
To run integration tests using your client id and secret, run the test
gradle command with the -Pintegration
flag
$ PAYPAL_CLIENT_ID=your_client_id PAYPAL_CLIENT_SECRET=your_client_secret ./gradlew clean test -Pintegration
You may use the client id and secret above for demonstration purposes.
You can start off by trying out creating and capturing an order.
To try out different samples for both create and authorize intent head to this link.
Note: Update the PayPalClient.java
with your sandbox client credentials or pass your client credentials as environment variable whie executing the samples.
Code released under SDK LICENSE