forked from paypal/Payouts-Java-SDK
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGetPayoutItem.java
66 lines (57 loc) · 2.7 KB
/
GetPayoutItem.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
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.json.JSONObject;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
public class GetPayoutItem extends PayPalClient {
private static final Encoder encoder = new Encoder();
/**
* Retrieves a Payouts item details. Payouts item retrieval api(GET - /v1/payments/payouts-items/<item-id>)
*
* @param itemId the Id of a Payout item
* @return the details of the Payouts Item
* @throws IOException when call to the api fails
*/
public HttpResponse<PayoutItemResponse> getPayoutItem(String itemId) throws IOException {
PayoutsItemGetRequest request = new PayoutsItemGetRequest(itemId);
try {
HttpResponse<PayoutItemResponse> response = client().execute(request);
System.out.println("Response Body:");
System.out.println(new JSONObject(new Json().serialize(response.result())).toString(4));
return response;
} catch (HttpException e) {
//Server side API failure
String error = e.getMessage();
com.paypal.payouts.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));
return null;
} catch (IOException e) {
//Client side failure
System.out.println(e);
throw e;
}
}
/**
* This is the driver method to execute this sample.
* This method retrieves a Payout Item detail.
* To retrieve a valid Payout Item, it creates a Payouts Batch with 5 items and use that id
* to retrieve batch status, and use the first item id in response for retrieval
*
* @param args
* @throws IOException when call to the api fails
*/
public static void main(String[] args) throws IOException {
HttpResponse<CreatePayoutResponse> createPayoutResponse = new CreatePayoutsBatch().createPayout();
HttpResponse<PayoutBatch> getBatchResponse = new GetPayoutBatch().getPayoutBatch(createPayoutResponse.result().batchHeader().payoutBatchId());
new GetPayoutItem().getPayoutItem(getBatchResponse.result().items().get(0).payoutItemId());
//Simulate a failure scenario to show how to handle failure response
new GetPayoutItem().getPayoutItem("DUMMY");
}
}