package org.openedit.store.modules; import java.io.IOException; import java.util.Enumeration; import java.util.HashMap; import java.util.Map; import java.util.stream.Collectors; import javax.servlet.http.HttpServletRequest; import org.entermediadb.asset.MediaArchive; import org.ijsolutions.testbench.TestBench; import org.ijsolutions.testbench.modules.BaseTestingModule; import org.openedit.WebPageRequest; import org.openedit.cart.paypal.PaypalUtil; import com.paypal.api.payments.Event; import com.paypal.base.Constants; import com.paypal.base.rest.APIContext; public class PaypalModule extends BaseTestingModule{ protected PaypalUtil fieldPaypalUtil; public void handleIPN(WebPageRequest inReq) throws Exception{ String result = getPaypalUtil().handleIPN(inReq); inReq.putPageValue("paymentResult", result); System.out.print(result); } public void handlePDT(WebPageRequest inReq) throws Exception{ String result = getPaypalUtil().handlePDT(inReq); inReq.putPageValue("paymentResult", result); MediaArchive archive = getMediaArchive(inReq); //the result should be matched to an order.... } public PaypalUtil getPaypalUtil() { if (fieldPaypalUtil == null) { fieldPaypalUtil = new PaypalUtil(); } return fieldPaypalUtil; } public void setPaypalUtil(PaypalUtil paypalUtil) { this.fieldPaypalUtil = paypalUtil; } public void handleWebHook(WebPageRequest inReq) throws Exception{ TestBench bench = getTestBench(inReq); String clientID = bench.get("paypalkey"); String clientSecret = bench.get("paypalsecret"); String webhookid = bench.get("paypalwebhook"); String mode = bench.get("paypalmode"); // ### Api Context APIContext apiContext = new APIContext(clientID, clientSecret, mode); // Set the webhookId that you received when you created this webhook. apiContext.addConfiguration(Constants.PAYPAL_WEBHOOK_ID, webhookid); Boolean result = Event.validateReceivedEvent(apiContext, getHeadersInfo(inReq.getRequest()), getBody(inReq.getRequest())); if(result) { System.out.print(result); } } // Simple helper method to help you extract the headers from HttpServletRequest object. private static Map getHeadersInfo(HttpServletRequest request) { Map map = new HashMap(); @SuppressWarnings("rawtypes") Enumeration headerNames = request.getHeaderNames(); while (headerNames.hasMoreElements()) { String key = (String) headerNames.nextElement(); String value = request.getHeader(key); map.put(key, value); } return map; } // Simple helper method to fetch request data as a string from HttpServletRequest object. private static String getBody(HttpServletRequest request) throws IOException { String body = request.getReader().lines().collect(Collectors.joining(System.lineSeparator())); return body; } }