package it.acxent.bank.paypalcheckout; import com.paypal.core.PayPalEnvironment; import com.paypal.core.PayPalHttpClient; import com.paypal.http.HttpRequest; import com.paypal.http.HttpResponse; import com.paypal.http.exceptions.HttpException; import com.paypal.http.serializer.Json; import com.paypal.orders.AmountWithBreakdown; import com.paypal.orders.ApplicationContext; import com.paypal.orders.LinkDescription; import com.paypal.orders.Order; import com.paypal.orders.OrderRequest; import com.paypal.orders.OrdersCreateRequest; import com.paypal.orders.OrdersGetRequest; import com.paypal.orders.PurchaseUnitRequest; import java.io.IOException; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.apache.commons.lang3.StringUtils; import org.json.JSONObject; public class PayPalOrder { private PayPalHttpClient client; private PayPalReq payPalReq; private boolean useSandbox = true; public PayPalOrder(PayPalReq payPalReq) { setPayPalReq(payPalReq); this.useSandbox = getPayPalReq().isUseSandbox(); } private OrderRequest buildMinimumRequestBody() { OrderRequest orderRequest = new OrderRequest(); orderRequest.checkoutPaymentIntent("CAPTURE"); ApplicationContext applicationContext = new ApplicationContext().cancelUrl(getPayPalReq().getCancelURL()) .returnUrl(getPayPalReq().getReturnUrl()); orderRequest.applicationContext(applicationContext); List purchaseUnitRequests = new ArrayList<>(); PurchaseUnitRequest purchaseUnitRequest = new PurchaseUnitRequest() .amountWithBreakdown(new AmountWithBreakdown() .currencyCode(getPayPalReq().getCurrency()).value(String.valueOf(getPayPalReq().getAmt()))) .description(getPayPalReq().getDESC()); purchaseUnitRequests.add(purchaseUnitRequest); orderRequest.purchaseUnits(purchaseUnitRequests); return orderRequest; } public HttpResponse createOrder(boolean debug) throws IOException { OrdersCreateRequest request = new OrdersCreateRequest(); request.header("prefer", "return=representation"); request.requestBody(buildMinimumRequestBody()); HttpResponse response = getClient().execute((HttpRequest)request); if (debug && response.statusCode() == 201) { System.out.println("Order with Minimum Payload: "); System.out.println("Status Code: " + response.statusCode()); System.out.println("Status: " + ((Order)response.result()).status()); 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)((Order)response.result()).links()) System.out.println("\t" + link.rel() + ": " + link.href() + "\tCall Type: " + link.method()); System.out.println("Total 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)); } return response; } public static void main(String[] args) { try { PayPalReq ppr = new PayPalReq(); new PayPalOrder(ppr).createOrder(true); } catch (HttpException e) { System.out.println(e.getLocalizedMessage()); } catch (Exception e) { e.printStackTrace(); } } public PayPalReq getPayPalReq() { return this.payPalReq; } public void setPayPalReq(PayPalReq payPalReq) { this.payPalReq = payPalReq; } public PayPalHttpClient getClient() { if (this.client == null) { System.out.println("clientid: " + getPayPalReq().getPaypalClientId()); System.out.println("clientSc: " + getPayPalReq().getPaypalClientSecret()); if (this.useSandbox) { PayPalEnvironment.Sandbox sandbox = new PayPalEnvironment.Sandbox(getPayPalReq().getPaypalClientId(), getPayPalReq().getPaypalClientSecret()); this.client = new PayPalHttpClient((PayPalEnvironment)sandbox); } else { PayPalEnvironment.Live live = new PayPalEnvironment.Live(getPayPalReq().getPaypalClientId(), getPayPalReq().getPaypalClientSecret()); this.client = new PayPalHttpClient((PayPalEnvironment)live); } } return this.client; } public String prettyPrint(JSONObject jo, String pre) { Iterator keys = jo.keys(); StringBuilder pretty = new StringBuilder(); while (keys.hasNext()) { String key = (String)keys.next(); pretty.append(String.format("%s%s: ", pre, StringUtils.capitalize(key))); if (jo.get(key) instanceof JSONObject) { pretty.append(prettyPrint(jo.getJSONObject(key), pre + "\t")); continue; } if (jo.get(key) instanceof org.json.JSONArray) { int sno = 1; for (Object jsonObject : (Iterable)jo.getJSONArray(key)) { pretty.append(String.format("\n%s\t%d:\n", pre, sno++)); pretty.append(prettyPrint((JSONObject)jsonObject, pre + "\t\t")); } continue; } pretty.append(String.format("%s\n", jo.getString(key))); } return pretty.toString(); } public HttpResponse getOrder(String orderId) throws IOException { OrdersGetRequest request = new OrdersGetRequest(orderId); HttpResponse response = getClient().execute((HttpRequest)request); return response; } }