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

Capture reference is always unique #33

Merged
merged 3 commits into from
Jun 10, 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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>51.0</apiVersion>
<apiVersion>60.0</apiVersion>
<status>Active</status>
</ApexClass>
4 changes: 2 additions & 2 deletions force-app/main/default/classes/AdyenCaptureHelper.cls
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ public with sharing class AdyenCaptureHelper {

/**
* invoked by handleFulfillmentOrderStatusChange to capture funds with Adyen
* @param captureRequest
* @param captureRequest with required information
* @return `CommercePayments.GatewayResponse`
*/
public static CommercePayments.GatewayResponse capture(CommercePayments.CaptureRequest captureRequest) {
Expand Down Expand Up @@ -42,7 +42,7 @@ public with sharing class AdyenCaptureHelper {
currencyCode,
captureRequest.amount,
adyenAdapterMdt.Merchant_Account__c,
AdyenPaymentUtility.getReference(pa, captureRequest.amount),
AdyenPaymentUtility.getReference(captureRequest),
adyenAdapterMdt.System_Integrator_Name__c
);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>57.0</apiVersion>
<apiVersion>60.0</apiVersion>
<status>Active</status>
</ApexClass>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>57.0</apiVersion>
<apiVersion>60.0</apiVersion>
<status>Active</status>
</ApexClass>
43 changes: 13 additions & 30 deletions force-app/main/default/classes/AdyenPaymentUtility.cls
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public with sharing class AdyenPaymentUtility {

/**
* Retrieves custom meta data associated with Adyen (Endpoint info) pulls all fields.
*
* @param metaType name of the custom metadata type with Adyen configuration
* @return Adyen_Adapter__mdt for the passed metadata type with all fields.
*/
public static Adyen_Adapter__mdt retrieveGatewayMetadata(String metaType) {
Expand Down Expand Up @@ -220,7 +220,6 @@ public with sharing class AdyenPaymentUtility {
*
* @param pa The PaymentAuthorization sObject
* @return the GatewayRefNumber for the request.
* @see https://ca-test.adyen.com/ca/ca/accounts/showTx.shtml?pspReference=852588546520527A&txType=Payment
*/
public static String getCaptureGatewayRefNumber(PaymentAuthorization pa) {
if (pa == null) {
Expand Down Expand Up @@ -323,36 +322,20 @@ public with sharing class AdyenPaymentUtility {
return String.valueOf(Math.round(Math.random() * MAX)).leftPad(stringLength, '0');
}

public static FulfillmentOrder getFulfillmentOrder(Id orderSummaryId, Decimal amount) {
try {
List<FulfillmentOrder> fulfillmentOrders = [
SELECT FulfillmentOrderNumber, Status, StatusCategory, Type, TypeCategory, GrandTotalAmount
FROM FulfillmentOrder
WHERE OrderSummaryId = :orderSummaryId
ORDER BY CreatedDate DESC
];
for (FulfillmentOrder fulfillmentOrder : fulfillmentOrders) {
if (fulfillmentOrder.GrandTotalAmount == amount) {
return fulfillmentOrder;
}
}
throw new AdyenAsyncAdapter.GatewayException(
'Cannot find any fulfillment order related to order summary Id ' + orderSummaryId + ' with amount ' + amount
);
} catch (Exception ex) {
logException(ex, LoggingLevel.ERROR);
public static String getReference(CommercePayments.CaptureRequest captureRequest) {
Id invoiceId = captureRequest.additionalData?.get('invoiceId');
if (invoiceId == null) {
return getRandomNumber(16);
}
return null;
}

public static String getReference(SObject anyPaymentTypeRecord, Decimal amount) {
String randomNumber = getRandomNumber(16);
OrderPaymentSummary orderPaymentSummary = (OrderPaymentSummary)anyPaymentTypeRecord.getSObject('OrderPaymentSummary');
if (orderPaymentSummary?.OrderSummaryId == null) {
return randomNumber;
List<FulfillmentOrder> fulfillmentOrders = [
SELECT FulfillmentOrderNumber
FROM FulfillmentOrder
WHERE InvoiceId = :invoiceId
];
if (fulfillmentOrders.isEmpty() || fulfillmentOrders.size() > 1) {
return getRandomNumber(16);
}
String reference = getFulfillmentOrder(orderPaymentSummary.OrderSummaryId, amount)?.FulfillmentOrderNumber;
return String.isNotBlank(reference) ? reference : randomNumber;
return fulfillmentOrders[0].FulfillmentOrderNumber;
}

public static void logException(Exception ex, LoggingLevel loggingLevel) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>57.0</apiVersion>
<apiVersion>60.0</apiVersion>
<status>Active</status>
</ApexClass>
65 changes: 25 additions & 40 deletions force-app/main/default/classes/AdyenPaymentUtilityTest.cls
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@IsTest
private class AdyenPaymentUtilityTest {
private static String ASSERT_PRICE_MESSAGE = 'For input price of ';
private static final String ASSERT_PRICE_MESSAGE = 'For input price of ';
@IsTest
static void createAuthorisationRequestTest() {
// given
Expand Down Expand Up @@ -128,6 +128,7 @@ private class AdyenPaymentUtilityTest {
Assert.areEqual(productPrice*100, lineItems[0].amountExcludingTax);
Assert.areEqual((productPrice+taxValue)*100, lineItems[0].amountIncludingTax);
}

@IsTest(SeeAllData=true) // for ConnectApi use only
static void addCreditMemoDataTest() {
Integer productPrice = 100;
Expand Down Expand Up @@ -161,65 +162,49 @@ private class AdyenPaymentUtilityTest {
Assert.areEqual(0, lineItems[0].taxPercentage);
}

@IsTest(SeeAllData=true) // for ConnectApi use only
static void getFulfillmentOrderTest() {
OrderPaymentSummary orderPaySum = createInvoiceAndRelatedRecords(100, 5);
Test.startTest();
FulfillmentOrder fulfillmentOrder = AdyenPaymentUtility.getFulfillmentOrder(orderPaySum.OrderSummaryId, 105);
Test.stopTest();
Assert.isNotNull(fulfillmentOrder);
}

@IsTest
static void getFulfillmentOrderFailTest() {
Test.startTest();
FulfillmentOrder fulfillmentOrder = AdyenPaymentUtility.getFulfillmentOrder(null, 0);
Test.stopTest();
Assert.isNull(fulfillmentOrder);
}

@IsTest(SeeAllData=true) // for ConnectApi use only
static void getReferenceTest() {
// given
OrderPaymentSummary orderPaySum = createInvoiceAndRelatedRecords(100, 5);
PaymentAuthorization payAuth = [
SELECT OrderPaymentSummary.OrderSummaryId
SELECT Id
FROM PaymentAuthorization
WHERE OrderPaymentSummaryId = :orderPaySum.Id
];
Payment payment = [
SELECT OrderPaymentSummary.OrderSummaryId
FROM Payment
WHERE OrderPaymentSummaryId = :orderPaySum.Id
];
FulfillmentOrder fulfillmentOrder = [
SELECT FulfillmentOrderNumber
SELECT FulfillmentOrderNumber, InvoiceId, GrandTotalAmount
FROM FulfillmentOrder
WHERE OrderSummaryId = :orderPaySum.OrderSummaryId
];

CommercePayments.CaptureRequest captureRequest = new CommercePayments.CaptureRequest(Double.valueOf(fulfillmentOrder.GrandTotalAmount), payAuth.Id);
captureRequest.additionalData = new Map<String,String>();
captureRequest.additionalData.put('invoiceId', fulfillmentOrder.InvoiceId);
// when
Test.startTest();
String referencePayAuth = AdyenPaymentUtility.getReference(payAuth, 105);
String referencePayment = AdyenPaymentUtility.getReference(payment, 105);
String referencePayAuth = AdyenPaymentUtility.getReference(captureRequest);
Test.stopTest();

// then
Assert.areEqual(fulfillmentOrder.FulfillmentOrderNumber, referencePayAuth);
Assert.areEqual(fulfillmentOrder.FulfillmentOrderNumber, referencePayment);
}

@IsTest
static void getReferenceRandomTest() {
OrderPaymentSummary orderPaymentSummary = new OrderPaymentSummary(Id = '0bM7Q000000QP0uUAG', OrderSummaryId = '1Os7Q000000Uh77SAC');
PaymentAuthorization payAuth = new PaymentAuthorization(OrderPaymentSummary = orderPaymentSummary);

// given
CommercePayments.CaptureRequest captureRequest1 = new CommercePayments.CaptureRequest(Double.valueOf(99.9), '0Xc7Q000000YFVcSAO');
CommercePayments.CaptureRequest captureRequest2 = new CommercePayments.CaptureRequest(Double.valueOf(99.9), '0Xc7Q000000YFVcSAO');
captureRequest2.additionalData = new Map<String,String>();
captureRequest2.additionalData.put('invoiceId', '3tt7Q000000cJbuQAE');
// when
Test.startTest();
String reference = AdyenPaymentUtility.getReference(payAuth, 23);
String referenceNullOrderPaySum = AdyenPaymentUtility.getReference(new PaymentAuthorization(), 23);
String reference1 = AdyenPaymentUtility.getReference(captureRequest1);
String reference2 = AdyenPaymentUtility.getReference(captureRequest2);
Test.stopTest();

Assert.isNotNull(reference);
Assert.isNotNull(referenceNullOrderPaySum);
Assert.areEqual(16, reference.length());
Assert.areEqual(16, referenceNullOrderPaySum.length());
// then
Assert.isNotNull(reference1);
Assert.isNotNull(reference2);
Assert.areEqual(16, reference1.length());
Assert.areEqual(16, reference2.length());
Assert.areNotEqual(reference1, reference2);
}

private static OrderPaymentSummary createInvoiceAndRelatedRecords(Decimal price, Decimal taxValue) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>57.0</apiVersion>
<apiVersion>60.0</apiVersion>
<status>Active</status>
</ApexClass>