Welcome to PayPal Java SDK. This repository contains PayPal's Java SDK for Payouts and samples for v1/payments/payouts 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 Payouts APIs which includes Payouts.
Please refer to the PayPal Payouts 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 necessary to fork this repository for using the PayPal SDK. Please take a look at PayPal Payouts SDK for configuring and working with SDK without forking this code.
For contributing to this repository or using the samples you can fork 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 a Payout and print batch_id for the created Payout.
package com.paypal;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.paypal.http.HttpResponse;
import com.paypal.http.exceptions.HttpException;
import com.paypal.payouts.*;
public class CreatePayoutExample {
public static void main(String[] args) {
// Construct a request object and set desired parameters
// Here, CreatePayoutRequest() creates a POST request to /v1/payments/payouts
List<PayoutItem> items = IntStream
.range(1, 6)
.mapToObj(index -> new PayoutItem()
.senderItemId("Test_txn_" + index)
.note("Your 1$ Payout!")
.receiver("payout-sdk-" + index + "@paypal.com")
.amount(new Currency()
.currency("USD")
.value("1.00")))
.collect(Collectors.toList());
CreatePayoutRequest request = new CreatePayoutRequest()
.senderBatchHeader(new SenderBatchHeader()
.senderBatchId("Test_sdk_" + RandomStringUtils.randomAlphanumeric(7))
.emailMessage("SDK payouts test txn")
.emailSubject("This is a test transaction from SDK")
.note("Enjoy your Payout!!")
.recipientType("EMAIL"))
.items(items);
try {
// Call API with your client and get a response for your call
PayoutsPostRequest httpRequest = new PayoutsPostRequest().requestBody(request);
HttpResponse<CreatePayoutResponse> response = Credentials.client.execute(httpRequest);
// If call returns body in response, you can get the de-serialized version by
// calling result() on the response
CreatePayoutResponse payouts = response.result();
System.out.println("Payouts Batch ID: " + payouts.batchHeader().payoutBatchId());
payouts.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 create a Payout with validation failure to showcase how to parse the failed response entity. Refer samples for more scenarios
package com.paypal;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.paypal.http.HttpResponse;
import com.paypal.http.exceptions.HttpException;
import com.paypal.payouts.*;
public class CreatePayoutExample {
public static void main(String[] args) {
// Construct a request object and set desired parameters
// Here, CreatePayoutRequest() creates a POST request to /v1/payments/payouts
List<PayoutItem> items = IntStream
.range(1, 6)
.mapToObj(index -> new PayoutItem()
.senderItemId("Test_txn_" + index)
.note("Your 1$ Payout!")
.receiver("payout-sdk-" + index + "@paypal.com")
.amount(new Currency()
.currency("USD")
.value("1.0.0")))
.collect(Collectors.toList());
CreatePayoutRequest request = new CreatePayoutRequest()
.senderBatchHeader(new SenderBatchHeader()
.senderBatchId("Test_sdk_" + RandomStringUtils.randomAlphanumeric(7))
.emailMessage("SDK payouts test txn")
.emailSubject("This is a test transaction from SDK")
.note("Enjoy your Payout!!")
.recipientType("EMAIL"))
.items(items);
try {
// Call API with your client and get a response for your call
PayoutsPostRequest httpRequest = new PayoutsPostRequest().requestBody(request);
Credentials.client.execute(httpRequest);
} catch (HttpException he) {
// Something went wrong server-side
// Parse failure response to get the reason for failure
Encoder encoder = new Encoder();
String error = he.getMessage();
Error payoutError = encoder.deserializeResponse(new ByteArrayInputStream(error.getBytes(StandardCharsets.UTF_8)), Error.class, e.headers());
System.out.println(payoutError.name());
System.out.println(payoutError.details().get(0).field());
System.out.println(payoutError.details().get(0).issue());
he.headers().forEach(x -> System.out.println(x + " :" + he.headers().header(x)));
} catch (IOException ioe) {
// Something went wrong client-side
System.out.println(ioe);
}
}
}
This will retrieve a payouts batch
package com.paypal;
import java.io.IOException;
import com.paypal.http.HttpResponse;
import com.paypal.http.exceptions.HttpException;
import com.paypal.payouts.*;
public class GetPayoutsExample {
public static void main(String[] args) {
//Retrieve payouts batch details, a maximum of 1000 items can be fetched in a single call.
//Follow pagination to get all the items(Payouts supports a maximum of 15,000 items in a batch.
PayoutsGetRequest request = new PayoutsGetRequest(batchId)
.page(1)
.pageSize(10)
.totalRequired(true);
try {
// Call API with your client and get a response for your call
HttpResponse<PayoutBatch> response = Credentials.client.execute(request);
// If call returns body in response, you can get the de-serialized version by
// calling result() on the response
PayoutBatch payout = response.result();
System.out.println("Payouts Batch ID: " + payouts.batchHeader().payoutBatchId());
System.out.println("Payouts Batch Status: " + payouts.batchHeader().batchStatus());
payout.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 Payouts samples.
To try out different samples head to this link.
Note: Update the PayPalClient.java
with your sandbox client credentials or pass your client credentials as environment variable while executing the samples.
Code released under SDK LICENSE