Skip to content

Commit

Permalink
pbl notification improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
shubhamk67 committed Dec 6, 2024
1 parent 5034023 commit dbe40d9
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 6 deletions.
6 changes: 0 additions & 6 deletions force-app/main/default/classes/AdyenPBLHelper.cls
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,6 @@ public with sharing class AdyenPBLHelper {
dateTimePart = dateTimePart.replace('T', ' ');
Datetime dt = Datetime.valueOf(dateTimePart);

Integer offsetHours = Integer.valueOf(timeZoneOffset.substring(0, 2));
Integer offsetMinutes = Integer.valueOf(timeZoneOffset.substring(3, 5));
// Adjust DateTime by subtracting the offset to store it as UTC
dt = dt.addHours(-offsetHours);
dt = dt.addMinutes(-offsetMinutes);

return dt;
}

Expand Down
19 changes: 19 additions & 0 deletions force-app/main/default/classes/NonPaymentWebhookHandler.cls
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,34 @@ global without sharing class NonPaymentWebhookHandler {
}

private static void processNotification(PaymentAuthorization payAuth, NotificationRequestItem requestItem) {
String paymentLinkId = requestItem.additionalData?.get('paymentLinkId');
if (requestItem.eventCode == AdyenConstants.NOTIFICATION_REQUEST_TYPE_AUTHORISE) {
if (requestItem.success == 'true' && payAuth.Status.equalsIgnoreCase(AdyenOMSConstants.PaymentAuthorizationStatus.PENDING.name())) {
if(String.isNotBlank(paymentLinkId)) {
payAuth.Adyen_Payment_Method_Variant__c = requestItem.paymentMethod;
Payment_Link__c paymentLink = getPaymentLinkByExternalId(paymentLinkId);
paymentLink.Payment_Method_Used__c = requestItem.paymentMethod;
paymentLink.Status__c = AdyenOMSConstants.PaymentLinkStatus.COMPLETED.name();
update paymentLink;
}
payAuth.GatewayRefNumber = requestItem.pspReference;
payAuth.Status = AdyenOMSConstants.PaymentAuthorizationStatus.PROCESSED.name();
update payAuth;
}
}
}

private static Payment_Link__c getPaymentLinkByExternalId(String externalId) {
List<Payment_Link__c> paymentLinks = [
SELECT Id, Name, Payment_Method_Used__c, Status__c
FROM Payment_Link__c
WHERE Name = :externalId
LIMIT 1
];

return paymentLinks.isEmpty() ? null : paymentLinks[0];
}

private static PaymentAuthorization findPaymentAuthorization(NotificationRequestItem requestItem) {
if (requestItem.eventCode == AdyenConstants.NOTIFICATION_REQUEST_TYPE_AUTHORISE) {
return findPaymentAuthByMerchantRef(requestItem.merchantReference);
Expand Down
32 changes: 32 additions & 0 deletions force-app/main/default/classes/NonPaymentWebhookHandlerTest.cls
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
@IsTest
private class NonPaymentWebhookHandlerTest {
private static final String ACTIVE_STATUS = AdyenOMSConstants.PaymentLinkStatus.ACTIVE.name().toLowerCase();
private static final String COMPLETED_STATUS = AdyenOMSConstants.PaymentLinkStatus.COMPLETED.name().toLowerCase();

@IsTest
static void unsupportedNotificationTypeTest() {
Expand Down Expand Up @@ -126,6 +128,36 @@ private class NonPaymentWebhookHandlerTest {
Assert.areEqual(AdyenOMSConstants.PaymentGatewayLogStatus.SUCCESS.name(), payGatewayLog.InteractionStatus.toUpperCase());
}

@IsTest(SeeAllData = true)
static void authWebhookWithPaymentLinkTest() {
// given
PaymentAuthorization payAuth = TestDataFactory.insertCartAndRelatedPA();
PaymentAuthorization payAuthWithOrderSummary = [SELECT Id, OrderPaymentSummary.OrderSummary.OrderNumber FROM PaymentAuthorization WHERE Id =:payAuth.Id LIMIT 1];
String orderNumber = payAuthWithOrderSummary.OrderPaymentSummary.OrderSummary.OrderNumber;
Datetime dateInTheFuture = Datetime.now().addDays(+1);
Payment_Link__c paymentLink = TestDataFactory.createPaymentLink(payAuthWithOrderSummary.OrderPaymentSummary.orderSummaryId, ACTIVE_STATUS, dateInTheFuture);
insert paymentLink;
String successfulPblAuthWebhook = TestDataFactory.mockPblWebhookRequest(AdyenConstants.NOTIFICATION_REQUEST_TYPE_AUTHORISE, TestDataFactory.TEST_PSP_REFERENCE, paymentLink.Name, orderNumber, true);
RestContext.request = createRestRequest(successfulPblAuthWebhook);

// when
Test.startTest();
String response = NonPaymentWebhookHandler.doPost();
Test.stopTest();

// then
payAuth = [SELECT Status, GatewayRefNumber, Adyen_Payment_Method_Variant__c FROM PaymentAuthorization WHERE Id = :payAuth.Id];
PaymentGatewayLog payGatewayLog = [SELECT GatewayRefNumber, InteractionStatus FROM PaymentGatewayLog WHERE ReferencedEntityId = :payAuth.Id];
Payment_Link__c updatedPaymentLink = [SELECT Status__c, Payment_Method_Used__c FROM Payment_Link__c WHERE Id = :paymentLink.Id];
Assert.areEqual(NonPaymentWebhookHandler.ACCEPTED_RESPONSE, response);
Assert.areEqual(AdyenOMSConstants.PaymentAuthorizationStatus.PROCESSED.name(), payAuth.Status.toUpperCase());
Assert.areEqual(COMPLETED_STATUS, updatedPaymentLink.Status__c?.toLowerCase());
Assert.isNotNull(updatedPaymentLink.Payment_Method_Used__c);
Assert.areEqual(TestDataFactory.TEST_PSP_REFERENCE, payAuth.GatewayRefNumber);
Assert.isNotNull(payAuth.Adyen_Payment_Method_Variant__c);
Assert.areEqual(AdyenOMSConstants.PaymentGatewayLogStatus.SUCCESS.name(), payGatewayLog.InteractionStatus.toUpperCase());
}

@IsTest(SeeAllData = true)
static void unsuccessfulAuthorizationWebhookTest() {
// given
Expand Down
5 changes: 5 additions & 0 deletions force-app/main/default/classes/TestDataFactory.cls
Original file line number Diff line number Diff line change
Expand Up @@ -401,4 +401,9 @@ public class TestDataFactory {
String merchantAccountName = [SELECT Merchant_Account__c FROM Adyen_Adapter__mdt LIMIT 1].Merchant_Account__c;
return '{"live": "false", "notificationItems": [{"NotificationRequestItem": {"additionalData": {"hmacSignature": "testsignature"}, "amount": {"currency": "'+ ACTIVE_CURRENCY +'", "value": 1000}, "eventCode": "' + eventCode + '", "eventDate": "2024-01-01T01:00:00+01:00", "merchantAccountCode": "' + merchantAccountName + '", "merchantReference": "' + merchantRef + '", "paymentMethod": "visa", "pspReference": "' + pspReference + '", "originalReference": "' + originalReference + '", "reason": "null", "success": "' + success + '"}}]}';
}

public static String mockPblWebhookRequest(String eventCode, String pspReference, String paymentLinkId, String merchantRef, Boolean success) {
String merchantAccountName = [SELECT Merchant_Account__c FROM Adyen_Adapter__mdt LIMIT 1].Merchant_Account__c;
return '{"live": "false", "notificationItems": [{"NotificationRequestItem": {"additionalData": {"hmacSignature": "testsignature", "paymentLinkId": "'+ paymentLinkId +'"}, "amount": {"currency": "'+ ACTIVE_CURRENCY +'", "value": 1000}, "eventCode": "' + eventCode + '", "eventDate": "2024-01-01T01:00:00+01:00", "merchantAccountCode": "' + merchantAccountName + '", "merchantReference": "' + merchantRef + '", "paymentMethod": "visa", "pspReference": "' + pspReference + '", "reason": "null", "success": "' + success + '"}}]}';
}
}

0 comments on commit dbe40d9

Please sign in to comment.