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

PBL process notification improvements #75

Merged
merged 1 commit into from
Dec 13, 2024
Merged
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
pbl notification improvements
shubhamk67 committed Dec 6, 2024
commit dbe40d932e51eaf38f20259738a5281541da6fa5
6 changes: 0 additions & 6 deletions force-app/main/default/classes/AdyenPBLHelper.cls
Original file line number Diff line number Diff line change
@@ -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;
}

19 changes: 19 additions & 0 deletions force-app/main/default/classes/NonPaymentWebhookHandler.cls
Original file line number Diff line number Diff line change
@@ -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);
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() {
@@ -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
5 changes: 5 additions & 0 deletions force-app/main/default/classes/TestDataFactory.cls
Original file line number Diff line number Diff line change
@@ -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 + '"}}]}';
}
}