55 lines
2.6 KiB
Java
55 lines
2.6 KiB
Java
package com.paypal;
|
|
|
|
import com.paypal.AuthorizeIntentExamples.CreateOrder;
|
|
import com.paypal.http.HttpRequest;
|
|
import com.paypal.http.HttpResponse;
|
|
import com.paypal.http.serializer.Json;
|
|
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;
|
|
import java.io.IOException;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import org.json.JSONObject;
|
|
|
|
public class PatchOrder extends PayPalClient {
|
|
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;
|
|
}
|
|
|
|
public void patchOrder(String orderId) throws IOException {
|
|
OrdersPatchRequest request = new OrdersPatchRequest(orderId);
|
|
request.requestBody(buildRequestBody());
|
|
client().execute((HttpRequest)request);
|
|
OrdersGetRequest getRequest = new OrdersGetRequest(orderId);
|
|
HttpResponse<Order> response = this.client.execute((HttpRequest)getRequest);
|
|
System.out.println("After Patch:");
|
|
System.out.println("Order ID: " + ((Order)response.result()).id());
|
|
System.out.println("Intent: " + ((Order)response.result()).checkoutPaymentIntent());
|
|
System.out.println("Links: ");
|
|
for (LinkDescription link : (Iterable<LinkDescription>)((Order)response.result()).links())
|
|
System.out.println("\t" + link.rel() + ": " + link.href() + "\tCall Type: " + link.method());
|
|
System.out.println("Gross Amount: " + ((Order)response.result()).purchaseUnits().get(0).amountWithBreakdown().currencyCode() + " " + ((Order)
|
|
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(((Order)response.result()).id());
|
|
}
|
|
}
|