forked from rajarampadmanathan/Checkout-Java-SDK-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPatchOrder.java
More file actions
executable file
·68 lines (62 loc) · 2.66 KB
/
PatchOrder.java
File metadata and controls
executable file
·68 lines (62 loc) · 2.66 KB
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
package com.paypal;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONObject;
import com.paypal.http.HttpResponse;
import com.paypal.http.serializer.Json;
import com.paypal.AuthorizeIntentExamples.CreateOrder;
import com.paypal.orders.AmountBreakdown;
import com.paypal.orders.AmountWithBreakdown;
import com.paypal.orders.LinkDescription;
import com.paypal.orders.Money;
import com.paypal.orders.Order;
import com.paypal.orders.OrdersGetRequest;
import com.paypal.orders.OrdersPatchRequest;
import com.paypal.orders.Patch;
public class PatchOrder extends PayPalClient {
/**
* Method to created body for patch order
*
* @return List<Patch> list of patches to be made
* @throws IOException
*/
private List<Patch> buildRequestBody() throws IOException {
List<Patch> patches = new ArrayList<>();
patches.add(new Patch().op("replace").path("/intent").value("CAPTURE"));
patches.add(new Patch().op("replace").path("/purchase_units/@reference_id=='PUHF'/amount")
.value(new AmountWithBreakdown().currencyCode("USD").value("200.00")
.amountBreakdown(new AmountBreakdown().itemTotal(new Money().currencyCode("USD").value("180.00"))
.taxTotal(new Money().currencyCode("USD").value("20.00")))));
return patches;
}
/**
* Method to patch order
*
* @throws IOException Exceptions from API if any
*/
public void patchOrder(String orderId) throws IOException {
OrdersPatchRequest request = new OrdersPatchRequest(orderId);
request.requestBody(buildRequestBody());
client().execute(request);
OrdersGetRequest getRequest = new OrdersGetRequest(orderId);
HttpResponse<Order> response = client.execute(getRequest);
System.out.println("After Patch:");
System.out.println("Order ID: " + response.result().id());
System.out.println("Intent: " + response.result().checkoutPaymentIntent());
System.out.println("Links: ");
for (LinkDescription link : response.result().links()) {
System.out.println("\t" + link.rel() + ": " + link.href() + "\tCall Type: " + link.method());
}
System.out.println("Gross Amount: " + response.result().purchaseUnits().get(0).amountWithBreakdown().currencyCode() + " "
+ response.result().purchaseUnits().get(0).amountWithBreakdown().value());
System.out.println("Full response body:");
System.out.println(new JSONObject(new Json().serialize(response.result())).toString(4));
}
public static void main(String[] args) throws IOException {
System.out.println("Before PATCH:");
HttpResponse<Order> response = new CreateOrder().createOrder(true);
System.out.println("\nAfter PATCH (Changed Intent and Amount):");
new PatchOrder().patchOrder(response.result().id());
}
}