-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: non-payment webhook initial code
- Loading branch information
Showing
6 changed files
with
63 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
force-app/main/default/classes/NonPaymentWebhookHandler.cls
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
@RestResource(UrlMapping='/nonPaymentWebhook/v1/*') | ||
global with sharing class NonPaymentWebhookHandler { | ||
@HttpPost | ||
global static String doPost() { | ||
NotificationRequestItem notificationRequestItem; | ||
RestRequest req = RestContext.request; | ||
String acceptedResponse = '[accepted]'; | ||
try { | ||
notificationRequestItem = WebhookUtils.parseAdyenNotificationRequest(req.requestBody.toString()); | ||
Adyen_Adapter__mdt adyenAdapter = AdyenPaymentUtility.retrieveAdapterByMerchantAcct(notificationRequestItem.merchantAccountCode); | ||
HMACValidator validator; | ||
validator = new HMACValidator(notificationRequestItem, adyenAdapter.HMAC_Key__c); | ||
if (!Test.isRunningTest() && !validator.validateHMAC()) { | ||
return acceptedResponse + ', but not a valid notification request'; | ||
} | ||
// TODO: CAPTURE/REFUND and its failed counterparts should not be valid here | ||
if (!AdyenPaymentUtility.isValidNotification(notificationRequestItem)) { | ||
return acceptedResponse + ', but no valid psp reference found or webhook type was ignored'; | ||
} | ||
if (!AdyenPaymentUtility.relatedPaymentFound(notificationRequestItem.originalReference)) { | ||
return acceptedResponse + ', but no related payment record found'; | ||
} | ||
// TODO: create gateway log | ||
return acceptedResponse; | ||
} catch (Exception ex) { | ||
System.debug(LoggingLevel.ERROR, ex.getMessage()); | ||
if (ex instanceof HMACValidator.HmacValidationException) { | ||
return '[accepted] but not valid notification request'; | ||
} else { | ||
return '[accepted] but an exception happened: ' + ex.getMessage(); | ||
} | ||
} | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
force-app/main/default/classes/NonPaymentWebhookHandler.cls-meta.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata"> | ||
<apiVersion>61.0</apiVersion> | ||
<status>Active</status> | ||
</ApexClass> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
public with sharing class WebhookUtils { | ||
public static NotificationRequestItem parseAdyenNotificationRequest(String notificationRequestBody) { | ||
String notificationRequestCompatibleBody = AdyenPaymentUtility.makeSalesforceCompatible(notificationRequestBody); | ||
AdyenNotification adyenNotification = (AdyenNotification) JSON.deserialize(notificationRequestCompatibleBody, AdyenNotification.class); | ||
NotificationRequestItem notificationRequestItem = new NotificationRequestItem(); | ||
if (adyenNotification != null) { | ||
for (NotificationItems notificationItem : adyenNotification.notificationItems) { | ||
if (notificationItem.NotificationRequestItem != null) { | ||
notificationRequestItem = notificationItem.NotificationRequestItem; | ||
} | ||
} | ||
} | ||
return notificationRequestItem; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata"> | ||
<apiVersion>61.0</apiVersion> | ||
<status>Active</status> | ||
</ApexClass> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters