forked from paypal/Payouts-Java-SDK
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRunAll.java
71 lines (65 loc) · 3.82 KB
/
RunAll.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
package com.paypal;
import com.paypal.http.HttpResponse;
import com.paypal.payouts.CreatePayoutResponse;
import com.paypal.payouts.PayoutBatch;
import com.paypal.payouts.PayoutItemResponse;
import java.io.IOException;
public class RunAll {
public static void main(String[] args) throws IOException, InterruptedException {
//Create Payout
System.out.println("Creating Payout");
HttpResponse<CreatePayoutResponse> createResponse = new CreatePayoutsBatch().createPayout();
if (createResponse.statusCode() == 201) {
String batchId = createResponse.result().batchHeader().payoutBatchId();
//Retrieve Payout Batch
System.out.println("Successfully created Payout batch with id: " + batchId);
HttpResponse<PayoutBatch> getResponse = new GetPayoutBatch().getPayoutBatch(batchId);
if (getResponse.statusCode() == 200) {
//Retrieve Payout Item
System.out.println("Successfully retrieved Payout batch with id: " + batchId);
String itemId = getResponse.result().items().get(0).payoutItemId();
HttpResponse<PayoutItemResponse> getItemResponse = new GetPayoutItem().getPayoutItem(itemId);
if (getItemResponse.statusCode() == 200) {
System.out.println("Successfully retrieved payout item with id: " + itemId);
int i = 0;
//Check if Payout Batch status is SUCCESS(indicates all Payout items are processed)
//This is for demonstration purpose, defer using this while integrating
//Note: Integrate with Webhooks to get real time updates for Payouts batch and items
do {
Thread.sleep(2000);
getResponse = new GetPayoutBatch().getPayoutBatch(batchId);
if (getResponse.result().batchHeader().batchStatus().equals("SUCCESS")) {
//Cancel 1st unclaimed Payout item
System.out.println("Cancelling unclaimed Payout item with id: " + itemId);
HttpResponse<PayoutItemResponse> cancelResponse = new CancelPayoutItem().cancelPayoutItem(itemId);
if (cancelResponse.statusCode() == 200) {
System.out.println("Successfully cancelled unclaimed Payout item with id: " + itemId);
//Run cancel failure scenario
System.out.println("Simulate failure on cancelling an already cancelled Payout item with id: " + itemId);
new CancelPayoutItem().cancelPayoutItem(itemId);
} else {
System.out.println("Failed to cancel unclaimed Payout item with id: " + itemId);
}
break;
}
i++;
} while (i < 5);
if (i == 5) {
System.out.println("Payouts batch has not processed all payments yet, couldn't cancel unclaimed payment");
}
} else {
System.out.println("Failed to retrieve Payout item with id: " + itemId);
}
} else {
System.out.println("Failed to retrieve Payout batch with id: " + batchId);
}
}
//Execute all failure cases
System.out.println("Create a payout with validation failure");
new CreatePayoutsBatch().createPayoutFailure();
System.out.println("Retrieving an invalid payout");
new GetPayoutBatch().getPayoutBatch("DUMMY");
System.out.println("Retrieving an invalid payout item");
new GetPayoutItem().getPayoutItem("DUMMY");
}
}