forked from paypal/Payouts-Java-SDK
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCreatePayoutsBatch.java
111 lines (94 loc) · 4.19 KB
/
CreatePayoutsBatch.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package com.paypal;
import com.paypal.http.Encoder;
import com.paypal.http.HttpResponse;
import com.paypal.http.exceptions.HttpException;
import com.paypal.http.serializer.Json;
import com.paypal.payouts.Error;
import com.paypal.payouts.*;
import org.apache.commons.lang3.RandomStringUtils;
import org.json.JSONObject;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class CreatePayoutsBatch extends PayPalClient {
private static final Encoder encoder = new Encoder();
/**
* Creates a Payouts batch request(POST - /v1/payments/payouts) with 5 payout items
* A maximum of 15000 payout items are supported in a single batch request
*
* @return Response for the create payouts call
* @throws IOException when call to the api fails
*/
public HttpResponse<CreatePayoutResponse> createPayout() throws IOException {
PayoutsPostRequest request = buildRequestBody(false);
HttpResponse<CreatePayoutResponse> response = client().execute(request);
System.out.println("Response Body:");
System.out.println(new JSONObject(new Json().serialize(response.result())).toString(4));
return response;
}
/**
* Creates a Payouts batch request(POST - /v1/payments/payouts) with 5 payout items
* All the items are created with invalid amount value to simulate a validation failure
*
* @return Response for the create payouts call
* @throws IOException when call to the api fails
*/
public void createPayoutFailure() throws IOException {
PayoutsPostRequest request = buildRequestBody(true);
try {
client().execute(request);
} catch (HttpException e) {
//Server side API failure
String error = e.getMessage();
Error payoutError = encoder.deserializeResponse(new ByteArrayInputStream(error.getBytes(StandardCharsets.UTF_8)), Error.class, e.headers());
System.out.println("Error Response Body:");
System.out.println(new JSONObject(new Json().serialize(payoutError)).toString(4));
} catch (IOException e) {
//Client side failure
System.out.println(e);
}
}
/**
* Builds a Payouts batch create request body with 5 payout items to receivers email
*
* @param isValidationFailure - Include validation failure in payload
* @return Request payload for Payouts create(POST) request
*/
private PayoutsPostRequest buildRequestBody(boolean isValidationFailure) {
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(isValidationFailure ? "1.0.0" : "1.00")))
.collect(Collectors.toList());
CreatePayoutRequest payoutBatch = 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);
return new PayoutsPostRequest()
.requestBody(payoutBatch);
}
/**
* This is the driver method to execute this sample.
* This creates a Payouts Batch with 5 items and prints the response
*
* @param args
* @throws IOException when call to the api fails
*/
public static void main(String[] args) throws IOException {
new CreatePayoutsBatch().createPayout();
//Simulate a failure scenario to show how to handle failure response
new CreatePayoutsBatch().createPayoutFailure();
}
}