Skip to content

Commit

Permalink
task: changing names to avoid conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
daniloc-adyen committed Jul 30, 2024
1 parent 6831105 commit 4b7b34b
Show file tree
Hide file tree
Showing 15 changed files with 178 additions and 42 deletions.
30 changes: 3 additions & 27 deletions force-app/main/default/classes/AdyenAsyncAdapter.cls
Original file line number Diff line number Diff line change
@@ -1,30 +1,12 @@
/**
* This adapter is called by the Payment Gateway.
* The http calls are delegated to the AdyenPaymentHelper Class.
*
* This will process a CAPTURE and a REFUND Request as well as the corresponding Async callbacks.
*
* @see AdyenPaymentHelper
* @see AdyenClient
* This class is being deprecated after v2 due to the introduction of the payment gateway provider metadata.
* This way when upgrading the package conflicts will be avoided.
* The new one should be AdyenGatewayAdapter.
*/
global with sharing class AdyenAsyncAdapter implements CommercePayments.PaymentGatewayAdapter, CommercePayments.PaymentGatewayAsyncAdapter {

global AdyenAsyncAdapter() {}

/**
* The entry point for processing payment requests. Returns the response from the payment gateway.
* Accepts the gateway context request and handover the operation to AdyenPaymentHelper to call the appropriate capture or refund operation.
*
* @param paymentGatewayContext from SF Commerce Payments
* @return CommercePayments.GatewayResponse
*
* @implNotes
* [CAPTURE] is called after setting Fulfillment.Status to 'Fulfilled' which in turns fires processes
* and flows to create invoices which ultimately fires this.
*
* [REFUND] is called by using the action on the order summary page (left hand side).
*
*/
global CommercePayments.GatewayResponse processRequest(CommercePayments.PaymentGatewayContext paymentGatewayContext) {
try {
return AdyenPaymentHelper.handleFulfillmentOrderStatusChange(paymentGatewayContext);
Expand All @@ -33,12 +15,6 @@ global with sharing class AdyenAsyncAdapter implements CommercePayments.PaymentG
}
}

/**
* Listens to the incoming async notification callback from Adyen and handover to AdyenPaymentHelper for processing
*
* @param gatewayNotificationContext from SF Commerce Payments
* @return CommercePayments.GatewayNotificationResponse
*/
global CommercePayments.GatewayNotificationResponse processNotification(CommercePayments.PaymentGatewayNotificationContext gatewayNotificationContext) {
return AdyenPaymentHelper.handleAsyncNotificationCallback(gatewayNotificationContext);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ private class AdyenAuthorisationHelperTest {
AdyenAuthorisationHelper.authorise(TestDataFactory.createAuthorisationRequest(null));
Assert.fail();
} catch (Exception ex) {
Assert.isInstanceOfType(ex, AdyenAsyncAdapter.GatewayException.class);
Assert.isInstanceOfType(ex, AdyenGatewayAdapter.GatewayException.class);
Assert.isTrue(ex.getMessage().containsIgnoreCase('400'));
}
Test.stopTest();
Expand Down
2 changes: 1 addition & 1 deletion force-app/main/default/classes/AdyenCaptureHelper.cls
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public with sharing class AdyenCaptureHelper {
errorMessage = 'Payment Amount Missing';
}
if (String.isNotBlank(errorMessage)) {
throw new AdyenAsyncAdapter.GatewayException(errorMessage);
throw new AdyenGatewayAdapter.GatewayException(errorMessage);
}

String pspReference = paymentAuth.GatewayRefNumber;
Expand Down
18 changes: 18 additions & 0 deletions force-app/main/default/classes/AdyenGatewayAdapter.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
global with sharing class AdyenGatewayAdapter implements CommercePayments.PaymentGatewayAdapter, CommercePayments.PaymentGatewayAsyncAdapter {

global AdyenGatewayAdapter() {}

global CommercePayments.GatewayResponse processRequest(CommercePayments.PaymentGatewayContext paymentGatewayContext) {
try {
return AdyenPaymentHelper.handleFulfillmentOrderStatusChange(paymentGatewayContext);
} catch (Exception ex) {
return new CommercePayments.GatewayErrorResponse('500', ex.getMessage());
}
}

global CommercePayments.GatewayNotificationResponse processNotification(CommercePayments.PaymentGatewayNotificationContext gatewayNotificationContext) {
return AdyenPaymentHelper.handleAsyncNotificationCallback(gatewayNotificationContext);
}

public class GatewayException extends Exception {}
}
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>60.0</apiVersion>
<status>Active</status>
</ApexClass>
131 changes: 131 additions & 0 deletions force-app/main/default/classes/AdyenGatewayAdapterTest.cls
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
@IsTest
private class AdyenGatewayAdapterTest {
@TestSetup
static void makeData() {
Account acct = TestDataFactory.createAccount();
insert acct;
TestDataFactory.insertBasicPaymentRecords(acct.Id, null);
}

@IsTest
static void testCaptureOutboundSuccess() {
Test.setMock(HttpCalloutMock.class, new TestDataFactory.EchoHttpMock());

Test.startTest();
Id authId = [SELECT Id FROM PaymentAuthorization ORDER BY CreatedDate DESC LIMIT 1].Id;
CommercePayments.CaptureRequest captureRequest = new CommercePayments.CaptureRequest(TestDataFactory.TEST_AMOUNT, authId);
CommercePayments.PaymentGatewayContext context = new CommercePayments.PaymentGatewayContext(captureRequest, CommercePayments.RequestType.Capture);
CommercePayments.GatewayResponse captureResponse = TestDataFactory.adyenAdapter.processRequest(context);
Test.stopTest();

Assert.isTrue(captureResponse.toString().contains('[capture-received]'));
Assert.isTrue(captureResponse.toString().contains(TestDataFactory.TEST_PSP_REFERENCE));
}

@IsTest
static void testCaptureOutboundFailure() {
Test.setMock(HttpCalloutMock.class, new TestDataFactory.FailureResponse());

Test.startTest();
Id authId = [SELECT Id FROM PaymentAuthorization ORDER BY CreatedDate DESC LIMIT 1].Id;
CommercePayments.CaptureRequest captureRequest = new CommercePayments.CaptureRequest(TestDataFactory.TEST_AMOUNT, authId);
CommercePayments.PaymentGatewayContext context = new CommercePayments.PaymentGatewayContext(captureRequest, CommercePayments.RequestType.Capture);
CommercePayments.GatewayResponse gatewayResponse = TestDataFactory.adyenAdapter.processRequest(context);
Test.stopTest();

Assert.isInstanceOfType(gatewayResponse, CommercePayments.GatewayErrorResponse.class);
Assert.isTrue(gatewayResponse.toString().containsIgnoreCase('400'));
}

@IsTest
static void testCaptureOutboundMissingPaymentAuthorization() {
Test.setMock(HttpCalloutMock.class, new TestDataFactory.EchoHttpMock());

Test.startTest();
CommercePayments.CaptureRequest captureRequest = new CommercePayments.CaptureRequest(TestDataFactory.TEST_AMOUNT, null);
CommercePayments.PaymentGatewayContext context = new CommercePayments.PaymentGatewayContext(captureRequest, CommercePayments.RequestType.Capture);
CommercePayments.GatewayResponse gatewayResponse = TestDataFactory.adyenAdapter.processRequest(context);
Test.stopTest();

Assert.isInstanceOfType(gatewayResponse, CommercePayments.GatewayErrorResponse.class);
Assert.isTrue(gatewayResponse.toString().containsIgnoreCase(AdyenPaymentUtility.NO_PAYMENT_AUTH_FOUND_BY_ID));
}

@IsTest
static void testCaptureOutboundMissingAmount() {
Test.setMock(HttpCalloutMock.class, new TestDataFactory.EchoHttpMock());
Id authId = [SELECT Id FROM PaymentAuthorization ORDER BY CreatedDate DESC LIMIT 1].Id;
Test.startTest();
CommercePayments.CaptureRequest captureRequest = new CommercePayments.CaptureRequest(null, authId);
CommercePayments.PaymentGatewayContext context = new CommercePayments.PaymentGatewayContext(captureRequest, CommercePayments.RequestType.Capture);
CommercePayments.GatewayResponse gatewayResponse = TestDataFactory.adyenAdapter.processRequest(context);
Test.stopTest();
Assert.isInstanceOfType(gatewayResponse, CommercePayments.GatewayErrorResponse.class);
Assert.isTrue(gatewayResponse.toString().containsIgnoreCase('Payment Amount Missing'));
}

@IsTest
static void testCaptureInboundSuccess() {
AdyenPaymentHelper.TEST_NOTIFICATION_REQUEST_BODY = TestDataFactory.createNotificationRequestBody('CAPTURE', TestDataFactory.TEST_PSP_REFERENCE);

Test.startTest();
CommercePayments.GatewayNotificationResponse captureResponse = TestDataFactory.adyenAdapter.processNotification(null);
Test.stopTest();

Assert.isFalse(captureResponse.toString().containsIgnoreCase('error'));
}

@IsTest
static void testRefundOutboundSuccess() {
Test.setMock(HttpCalloutMock.class, new TestDataFactory.EchoHttpMock());

Test.startTest();
Id paymentId = [SELECT Id FROM Payment ORDER BY CreatedDate DESC LIMIT 1].Id;
CommercePayments.ReferencedRefundRequest refundRequest = new CommercePayments.ReferencedRefundRequest(TestDataFactory.TEST_AMOUNT, paymentId);
CommercePayments.PaymentGatewayContext context = new CommercePayments.PaymentGatewayContext(refundRequest, CommercePayments.RequestType.ReferencedRefund);
CommercePayments.GatewayResponse refundResponse = TestDataFactory.adyenAdapter.processRequest(context);
Test.stopTest();

Assert.isTrue(refundResponse.toString().contains('received'));
}

@IsTest
static void testRefundOutboundFailure() {
Test.setMock(HttpCalloutMock.class, new TestDataFactory.FailureResponse());

Test.startTest();
Id paymentId = [SELECT Id FROM Payment ORDER BY CreatedDate DESC LIMIT 1].Id;
CommercePayments.ReferencedRefundRequest refundRequest = new CommercePayments.ReferencedRefundRequest(TestDataFactory.TEST_AMOUNT, paymentId);
CommercePayments.PaymentGatewayContext context = new CommercePayments.PaymentGatewayContext(refundRequest, CommercePayments.RequestType.ReferencedRefund);
CommercePayments.GatewayResponse refundResponse = TestDataFactory.adyenAdapter.processRequest(context);
Test.stopTest();

Assert.isInstanceOfType(refundResponse, CommercePayments.GatewayErrorResponse.class);
Assert.isTrue(refundResponse.toString().containsIgnoreCase('400'));
}

@IsTest
static void testRefundOutboundMissingPayment() {
Test.setMock(HttpCalloutMock.class, new TestDataFactory.EchoHttpMock());
Test.startTest();
CommercePayments.ReferencedRefundRequest refundRequest = new CommercePayments.ReferencedRefundRequest(TestDataFactory.TEST_AMOUNT, null);
CommercePayments.PaymentGatewayContext context = new CommercePayments.PaymentGatewayContext(refundRequest, CommercePayments.RequestType.ReferencedRefund);
CommercePayments.GatewayResponse gatewayResponse = TestDataFactory.adyenAdapter.processRequest(context);
Test.stopTest();
Assert.isInstanceOfType(gatewayResponse, CommercePayments.GatewayErrorResponse.class);
Assert.isTrue(gatewayResponse.toString().containsIgnoreCase(AdyenPaymentUtility.NO_PAYMENT_FOUND_BY_ID));
}

@IsTest
static void testRefundOutboundMissingAmount() {
Test.setMock(HttpCalloutMock.class, new TestDataFactory.EchoHttpMock());
Id paymentId = [SELECT Id FROM Payment ORDER BY CreatedDate DESC LIMIT 1].Id;
Test.startTest();
CommercePayments.ReferencedRefundRequest refundRequest = new CommercePayments.ReferencedRefundRequest(null, paymentId);
CommercePayments.PaymentGatewayContext context = new CommercePayments.PaymentGatewayContext(refundRequest, CommercePayments.RequestType.ReferencedRefund);
CommercePayments.GatewayResponse gatewayResponse = TestDataFactory.adyenAdapter.processRequest(context);
Test.stopTest();
Assert.isInstanceOfType(gatewayResponse, CommercePayments.GatewayErrorResponse.class);
Assert.isTrue(gatewayResponse.toString().containsIgnoreCase('Payment Amount Missing'));
}
}
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>60.0</apiVersion>
<status>Active</status>
</ApexClass>
2 changes: 1 addition & 1 deletion force-app/main/default/classes/AdyenPaymentHelper.cls
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public with sharing class AdyenPaymentHelper {
notification = new CommercePayments.ReferencedRefundNotification();
gatewayMessageTemplate = '[refund-{0}] {1}';
} else {
throw new AdyenAsyncAdapter.GatewayException('Notification of type ' + notificationRequestItem.eventCode + ' does not match criteria');
throw new AdyenGatewayAdapter.GatewayException('Notification of type ' + notificationRequestItem.eventCode + ' does not match criteria');
}

String result;
Expand Down
2 changes: 1 addition & 1 deletion force-app/main/default/classes/AdyenPaymentHelperTest.cls
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ private class AdyenPaymentHelperTest {
AdyenPaymentHelper.createNotificationSaveResult(nri);
Assert.fail();
} catch (Exception ex) { // then
Assert.isInstanceOfType(ex, AdyenAsyncAdapter.GatewayException.class);
Assert.isInstanceOfType(ex, AdyenGatewayAdapter.GatewayException.class);
}
}
}
10 changes: 5 additions & 5 deletions force-app/main/default/classes/AdyenPaymentUtility.cls
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public with sharing class AdyenPaymentUtility {
Id = :paymentId
];
if (payments.isEmpty()) {
throw new AdyenAsyncAdapter.GatewayException(NO_PAYMENT_FOUND_BY_ID + paymentId);
throw new AdyenGatewayAdapter.GatewayException(NO_PAYMENT_FOUND_BY_ID + paymentId);
}
return payments[0];
}
Expand All @@ -48,7 +48,7 @@ public with sharing class AdyenPaymentUtility {
WHERE DeveloperName = :developerName
];
if (adyenAdapters.isEmpty()) {
throw new AdyenAsyncAdapter.GatewayException(NO_ADYEN_ADAPTER_BY_NAME + developerName);
throw new AdyenGatewayAdapter.GatewayException(NO_ADYEN_ADAPTER_BY_NAME + developerName);
}
return adyenAdapters[0];
}
Expand All @@ -63,7 +63,7 @@ public with sharing class AdyenPaymentUtility {
WHERE Merchant_Account__c = :merchantAccountName
];
if (adyenAdapters.isEmpty()) {
throw new AdyenAsyncAdapter.GatewayException(NO_ADYEN_ADAPTER_BY_MERCHANT + merchantAccountName);
throw new AdyenGatewayAdapter.GatewayException(NO_ADYEN_ADAPTER_BY_MERCHANT + merchantAccountName);
}
return adyenAdapters[0];
}
Expand Down Expand Up @@ -109,7 +109,7 @@ public with sharing class AdyenPaymentUtility {
Id = :paymentAuthId
];
if (paymentAuthorizations.isEmpty()) {
throw new AdyenAsyncAdapter.GatewayException(NO_PAYMENT_AUTH_FOUND_BY_ID + paymentAuthId);
throw new AdyenGatewayAdapter.GatewayException(NO_PAYMENT_AUTH_FOUND_BY_ID + paymentAuthId);
}
return paymentAuthorizations[0];
}
Expand Down Expand Up @@ -383,7 +383,7 @@ public with sharing class AdyenPaymentUtility {
CommercePayments.PaymentsHttp paymentsHttp = new CommercePayments.PaymentsHttp();
HttpResponse response = paymentsHttp.send(request);
if (response.getStatusCode() != 200 && response.getStatusCode() != 201) {
throw new AdyenAsyncAdapter.GatewayException('Adyen Checkout API returned: ' + response.getStatusCode() + ', body: ' + response.getBody());
throw new AdyenGatewayAdapter.GatewayException('Adyen Checkout API returned: ' + response.getStatusCode() + ', body: ' + response.getBody());
} else {
return response;
}
Expand Down
3 changes: 1 addition & 2 deletions force-app/main/default/classes/AdyenRefundHelper.cls
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ public with sharing class AdyenRefundHelper {
* @param refundRequest The CommercePayments.ReferencedRefundRequest Object.
* @return refundResponse The CommercePayments.ReferencedRefundResponse Object.
*
* @see AdyenClient
*/
public static CommercePayments.GatewayResponse refund(CommercePayments.ReferencedRefundRequest refundRequest) {
Payment payment = AdyenPaymentUtility.retrievePayment(refundRequest.paymentId);
Expand All @@ -22,7 +21,7 @@ public with sharing class AdyenRefundHelper {
errorMessage = 'Payment Amount Missing';
}
if (errorMessage != null) {
throw new AdyenAsyncAdapter.GatewayException(errorMessage);
throw new AdyenGatewayAdapter.GatewayException(errorMessage);
}

String pspReference = payment.PaymentAuthorization?.GatewayRefNumber != null ? payment.PaymentAuthorization.GatewayRefNumber : payment.GatewayRefNumber;
Expand Down
2 changes: 1 addition & 1 deletion force-app/main/default/classes/TestDataFactory.cls
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class TestDataFactory {
public static final String ACTIVE_CURRENCY = [SELECT IsoCode FROM CurrencyType WHERE IsActive = TRUE LIMIT 1].IsoCode;
public static final String ASSERT_PRICE_MESSAGE = 'For input price of ';

public static AdyenAsyncAdapter adyenAdapter = new AdyenAsyncAdapter();
public static AdyenGatewayAdapter adyenAdapter = new AdyenGatewayAdapter();

public static Account createAccount() {
return new Account(Name = 'Test Account');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<GatewayProviderPaymentMethodType xmlns="http://soap.sforce.com/2006/04/metadata">
<gtwyProviderPaymentMethodType>AdyenComponent</gtwyProviderPaymentMethodType>
<masterLabel>Alternative Payment Method</masterLabel>
<paymentGatewayProvider>Adyen</paymentGatewayProvider>
<paymentGatewayProvider>Adyen_OMS_Provider</paymentGatewayProvider>
<paymentMethodType>AlternativePaymentMethod</paymentMethodType>
<recordType>AlternativePaymentMethod.Alternative_Payment_Method</recordType>
</GatewayProviderPaymentMethodType>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<PaymentGatewayProvider xmlns="http://soap.sforce.com/2006/04/metadata">
<apexAdapter>AdyenAsyncAdapter</apexAdapter>
<apexAdapter>AdyenGatewayAdapter</apexAdapter>
<idempotencySupported>Yes</idempotencySupported>
<masterLabel>SalesforceOrderManagement-Adyen</masterLabel>
<masterLabel>Adyen OMS Provider</masterLabel>
</PaymentGatewayProvider>
2 changes: 2 additions & 0 deletions manifest/package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
<members>AdyenPaymentUtilityTest</members>
<members>AdyenRefundHelper</members>
<members>TestDataFactory</members>
<members>AdyenGatewayAdapter</members>
<members>AdyenGatewayAdapterTest</members>
<name>ApexClass</name>
</types>
<types>
Expand Down

0 comments on commit 4b7b34b

Please sign in to comment.