forked from paypal/Payouts-Java-SDK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCreatePayoutsBatch.java
75 lines (63 loc) · 2.67 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
package com.paypal;
import com.paypal.http.HttpResponse;
import com.paypal.http.serializer.Json;
import com.paypal.payouts.*;
import org.apache.commons.lang3.RandomStringUtils;
import org.json.JSONObject;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class CreatePayoutsBatch extends PayPalClient {
/**
* 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();
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;
}
/**
* Builds a Payouts batch create request body with 5 payout items to receivers email
*
* @return Request payload for Payouts create(POST) request
*/
private PayoutsPostRequest buildRequestBody() {
List<PayoutItem> items = IntStream
.range(1, 6)
.mapToObj(index -> new PayoutItem()
.senderItemId("Test_txn_" + index)
.note("Your 5$ Payout!")
.receiver("payout-sdk-" + index + "@paypal.com")
.amount(new Currency()
.currency("USD")
.value("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();
}
}