Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

15859: Paypal webhook validation updates #234

Merged
merged 1 commit into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions src/main/java/com/impactupgrade/nucleus/client/PaypalClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,19 @@ public PaypalClient(Environment env) {
}

public boolean isValidWebhookData(String transmissionId, String transmissionTime, String certUrl, String authAlgo, String transmissionSig, String webhookId, String webhookEvent) throws Exception {
JSONObject jsonObject = new JSONObject();
jsonObject.put("transmission_id", transmissionId);
jsonObject.put("transmission_time", transmissionTime);
jsonObject.put("cert_url", certUrl);
jsonObject.put("auth_algo", authAlgo);
jsonObject.put("transmission_sig", transmissionSig);
jsonObject.put("webhook_id", webhookId);
jsonObject.put("webhook_event", new JSONObject(webhookEvent));

WebhookValidationResponse webhookValidationResponse = HttpClient.post(apiUrl + "/v1/notifications/verify-webhook-signature", jsonObject.toString(), MediaType.APPLICATION_JSON, HttpClient.HeaderBuilder.builder().header("Authorization", apiContext.fetchAccessToken()), WebhookValidationResponse.class);
StringBuilder stringBuilder = new StringBuilder();
stringBuilder
.append("{")
.append("\"transmission_id\":\"").append(transmissionId).append("\",")
.append("\"transmission_time\":\"").append(transmissionTime).append("\",")
.append("\"cert_url\":\"").append(certUrl).append("\",")
.append("\"auth_algo\":\"").append(authAlgo).append("\",")
.append("\"transmission_sig\":\"").append(transmissionSig).append("\",")
.append("\"webhook_id\":\"").append(webhookId).append("\",")
.append("\"webhook_event\":").append(webhookEvent)
.append("}");

WebhookValidationResponse webhookValidationResponse = HttpClient.post(apiUrl + "/v1/notifications/verify-webhook-signature", stringBuilder.toString(), MediaType.APPLICATION_JSON, HttpClient.HeaderBuilder.builder().header("Authorization", apiContext.fetchAccessToken()), WebhookValidationResponse.class);
return webhookValidationResponse != null && !"FAILURE".equalsIgnoreCase(webhookValidationResponse.verificationStatus);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
import com.impactupgrade.nucleus.service.segment.EnrichmentService;
import com.impactupgrade.nucleus.util.TestUtil;
import com.paypal.api.payments.Event;
import com.paypal.base.Constants;
import com.paypal.base.rest.APIContext;
import org.json.JSONObject;

import javax.servlet.http.HttpServletRequest;
Expand Down Expand Up @@ -51,20 +49,13 @@ public Response webhook(String json, @Context HttpServletRequest request) throws

String jobName = "Paypal Event";
env.startJobLog(JobType.EVENT, "webhook", jobName, "Paypal");
env.logJobInfo("received event from Paypal", json);
env.logJobInfo("received event from Paypal");
env.logJobInfo("json: {}", json);
StringBuilder headers = new StringBuilder();
Enumeration<String> headerNames = request.getHeaderNames();
while (headerNames.hasMoreElements()) {
String headerName = headerNames.nextElement();
String headerValue = request.getHeader(headerName);
headers.append(headerName).append(": ").append(headerValue).append(", ");
}
env.logJobInfo("headers: {}", headers.toString());
env.logJobInfo("headers: {}", getHeadersInfo(request));

boolean isValid = isValidWebhookRequest(request, json, env);
if (!isValid) {
env.logJobError("Paypal data was invalid");
env.logJobError("Paypal data was invalid!");
env.endJobLog(JobStatus.FAILED);
return Response.status(400).build();
}
Expand Down Expand Up @@ -94,13 +85,6 @@ public Response webhook(String json, @Context HttpServletRequest request) throws
}

private boolean isValidWebhookRequest(HttpServletRequest request, String requestBody, Environment env) throws Exception {
APIContext apiContext = new APIContext(
env.getConfig().paypal.clientId,
env.getConfig().paypal.clientSecret,
env.getConfig().paypal.mode
);
apiContext.addConfiguration(Constants.PAYPAL_WEBHOOK_ID, env.getConfig().paypal.webhookId);

return env.paypalClient().isValidWebhookData(
request.getHeader("Paypal-Transmission-Id"), request.getHeader("Paypal-Transmission-Time"),
request.getHeader("Paypal-Cert-Url"), request.getHeader("Paypal-Auth-Algo"), request.getHeader("Paypal-Transmission-Sig"),
Expand Down
Loading