Skip to content

Commit

Permalink
Merge pull request #7142 from SalesforceFoundation/feature/242__pause…
Browse files Browse the repository at this point in the history
…E2EFix

Pause API E2E fixes
  • Loading branch information
balsamhindi authored Nov 14, 2022
2 parents 8632f24 + 92699c3 commit 753f434
Show file tree
Hide file tree
Showing 8 changed files with 182 additions and 87 deletions.
17 changes: 15 additions & 2 deletions force-app/main/default/classes/GE_PaymentServices.cls
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,21 @@ public with sharing class GE_PaymentServices {
* @return String JSON of visualforce URL
*/
@AuraEnabled(cacheable=true)
public static String getVfURL(String namespace) {
return DomainCreator.getVisualforceHostname(namespace);
public static OriginUrls getOriginUrls(String namespace) {
return new OriginUrls(
DomainCreator.getVisualforceHostname(namespace),
DomainCreator.getLightningHostname()
);
}

public class OriginUrls {
@AuraEnabled public String visualForceOriginUrl;
@AuraEnabled public String lightningOriginUrl;

public OriginUrls(String visualForceOriginUrl, String lightningOriginUrl) {
this.visualForceOriginUrl = 'https://'+visualForceOriginUrl;
this.lightningOriginUrl = 'https://'+lightningOriginUrl;
}
}

/**
Expand Down
93 changes: 57 additions & 36 deletions force-app/main/default/classes/PS_CommitmentRequest.cls
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,17 @@ public inherited sharing class PS_CommitmentRequest {
.withBody(jsonRequestBody)
.build();
}
public static HttpRequest buildRequest(String commitmentId, String jsonRequestBody,
PS_Request.ElevateEndpoint endpoint, UTIL_Http.Method method) {

return new PS_Request.Builder()
.withMethod(method)
.withEndpoint(endpoint)
.withCommitmentId(commitmentId)
.withRecommendedTimeout()
.withBody(jsonRequestBody)
.build();
}

/**
* @description Builds a HTTP request for the provided commitment Id, Http method and endpoint
Expand All @@ -205,6 +216,7 @@ public inherited sharing class PS_CommitmentRequest {
return new PS_Request.Builder()
.withCommitmentId(commitmentId)
.withEndpoint(endpoint)
.withRecommendedTimeout()
.withMethod(method)
.build();
}
Expand All @@ -224,11 +236,14 @@ public inherited sharing class PS_CommitmentRequest {
: getUpdateRequestBody(rd, oldRd, token);
}

public RequestBody getPauseRequestBody(RecurringDonationSchedule__c schedule) {
return new RequestBody()
.withStartTimestamp(schedule.StartDate__c)
.withEndTimestamp(schedule.EndDate__c)
.withReason(ELEVATE_PAUSE_REASONS.get(schedule.StatusReason__c));
public PauseRequestBody getPauseRequestBody(RD2_ScheduleService.ElevatePauseSchedule schedule) {


return new PauseRequestBody()
.withStartTimestamp(schedule.startDate)
.withEndTimestamp(schedule.endDate)
.withReasonComment(ELEVATE_PAUSE_REASONS.get(schedule.statusReason))
.withReason(ELEVATE_PAUSE_REASONS.get(schedule.statusReason));
}

/***
Expand Down Expand Up @@ -472,6 +487,41 @@ public inherited sharing class PS_CommitmentRequest {
public String consentType;
}

public class PauseRequestBody {
public PauseRequestBody() {}

public String startTimestamp;
public String endTimestamp;
public String reason;
public String reasonComment;


public PauseRequestBody withStartTimestamp(Datetime startTimestamp) {
this.startTimestamp = getISOFormatDateTimeFor(startTimestamp);
return this;
}

public PauseRequestBody withEndTimestamp(Datetime endTimestamp) {
this.endTimestamp = getISOFormatDateTimeFor(endTimestamp);
return this;
}

public PauseRequestBody withReason(String reason) {
this.reason = reason;
return this;
}

public PauseRequestBody withReasonComment(String reasonComment) {
this.reasonComment = reasonComment;
return this;
}

public String getISOFormatDateTimeFor(Datetime specifiedDate) {
return specifiedDate.formatGmt('yyyy-MM-dd\'T\'HH:mm:ss\'Z\'');
}

}

/***
* @description Assists in constructing the Commitment HttpRequest body.
*/
Expand All @@ -498,10 +548,7 @@ public inherited sharing class PS_CommitmentRequest {
*/
public Map<String, Object> productMetadata;

public String startTimestamp;
public String endTimestamp;
public String reason;
public String reasonComment;


/***
* @description Constructor
Expand Down Expand Up @@ -689,32 +736,6 @@ public inherited sharing class PS_CommitmentRequest {
public Boolean isCommitmentUpdate() {
return this.id != null;
}

public RequestBody withStartTimestamp(Date startTimestamp) {
this.startTimestamp = getISOFormatDateTimeFor(startTimestamp);
return this;
}

public RequestBody withEndTimestamp(Date endTimestamp) {
this.endTimestamp = getISOFormatDateTimeFor(endTimestamp);
return this;
}

public RequestBody withReason(String reason) {
this.reason = reason;
return this;
}

public RequestBody withReasonComment(String reasonComment) {
this.reasonComment = reasonComment;
return this;
}

private String getISOFormatDateTimeFor(Date specifiedDate) {
Datetime specifiedDateTime = Datetime.newInstance(
specifiedDate.year(), specifiedDate.month(), specifiedDate.day());
return JSON.serialize(specifiedDateTime);
}
}

/***
Expand Down Expand Up @@ -775,7 +796,7 @@ public inherited sharing class PS_CommitmentRequest {
Datetime donationDatetime = donationDate == Datetime.now().date()
? Datetime.now()
: Datetime.newInstance(donationDate.year(), donationDate.month(), donationDate.day());

return donationDatetime.formatGMT('yyyy-MM-dd\'T\'HH:mm:ss\'Z\'');
}
}
Expand Down
21 changes: 12 additions & 9 deletions force-app/main/default/classes/RD2_CommitmentService.cls
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,28 @@ public without sharing class RD2_CommitmentService {
if (shouldSendToElevate(rd, oldRd, paymentMethodToken)) {
PS_CommitmentRequest.RequestBody requestBody = new PS_CommitmentRequest().getRequestBody(rd, oldRd, paymentMethodToken);

UTIL_Http.Method method = String.isBlank(rd.CommitmentId__c)
? UTIL_Http.Method.POST
: UTIL_Http.Method.PATCH;

response = sendRequest(rd.CommitmentId__c, JSON.serialize(requestBody),
PS_Request.ElevateEndpoint.COMMITMENT);
PS_Request.ElevateEndpoint.COMMITMENT, method);

processResponse(rd, response);
}

return response;
}

public UTIL_Http.Response handleCommitmentPause(RecurringDonationSchedule__c schedule, RD2_RecurringDonation rd) {
public UTIL_Http.Response handleCommitmentPause(RD2_ScheduleService.ElevatePauseSchedule schedule, RD2_RecurringDonation rd) {
UTIL_Http.Response response;

PS_CommitmentRequest.RequestBody requestBody = new PS_CommitmentRequest().getPauseRequestBody(schedule);
PS_CommitmentRequest.PauseRequestBody requestBody = new PS_CommitmentRequest().getPauseRequestBody(schedule);

UTIL_Http.Method method = schedule.shouldEditPause ? UTIL_Http.Method.PATCH : UTIL_Http.Method.POST;

response = sendRequest(rd.getSObject()?.CommitmentId__c, JSON.serialize(requestBody),
PS_Request.ElevateEndpoint.COMMITMENT_PAUSE);
PS_Request.ElevateEndpoint.COMMITMENT_PAUSE, method);

processResponse(rd, response);

Expand Down Expand Up @@ -144,13 +150,12 @@ public without sharing class RD2_CommitmentService {
* @return response Payments API response
*/
private UTIL_Http.Response sendRequest(String commitmentId, String jsonRequestBody,
PS_Request.ElevateEndpoint endpoint) {
PS_Request.ElevateEndpoint endpoint, UTIL_Http.Method method) {
UTIL_Http.Response response;

try {
HttpRequest request = PS_CommitmentRequest.buildRequest(commitmentId, jsonRequestBody,
endpoint);

endpoint, method);
response = requestService.sendRequest(request);

} catch (Exception ex) {
Expand All @@ -166,9 +171,7 @@ public without sharing class RD2_CommitmentService {
try {
HttpRequest request = PS_CommitmentRequest.buildRequest(commitmentId,
UTIL_Http.Method.DEL, endpoint);

response = requestService.sendRequest(request);

} catch (Exception ex) {
response = requestService.buildErrorResponse(ex);
}
Expand Down
4 changes: 3 additions & 1 deletion force-app/main/default/classes/RD2_PauseForm_CTRL.cls
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ public with sharing class RD2_PauseForm_CTRL {
Boolean hasScheduleChanged = false;
try {
PauseData pause = (PauseData) JSON.deserialize(jsonPauseData, PauseData.class);

RD2_RecurringDonation rd = getRecurringDonation(pause.rdId);

if (isOnlineRecurringDonationRecord(rd)) {
Expand Down Expand Up @@ -302,7 +303,8 @@ public with sharing class RD2_PauseForm_CTRL {
if (isPauseRemoved(pause)) {
response = commitmentService.handleRemoveCommitmentPause(rd);
} else {
response = commitmentService.handleCommitmentPause(buildPauseSchedule(pause), rd);
response = commitmentService.handleCommitmentPause(
pauseHandler.createElevatePauseSchedule(pause, rd), rd);
}

return response;
Expand Down
43 changes: 42 additions & 1 deletion force-app/main/default/classes/RD2_ScheduleService.cls
Original file line number Diff line number Diff line change
Expand Up @@ -1027,6 +1027,42 @@ public without sharing class RD2_ScheduleService {
}
}

/**
* Inner class to build Elevate specific pause schedules
*
*/
public class ElevatePauseSchedule {
public Datetime startDate;
public Datetime endDate;
public String statusReason;
public Boolean shouldEditPause;
public ElevatePauseSchedule(List<RecurringDonationSchedule__c> donationSchedules,
RD2_PauseForm_CTRL.PauseData pause) {
this.startDate = pause.startDate;
this.endDate = calculateElevatePauseEndDate(pause.resumeAfterDate, donationSchedules);
this.statusReason = pause.pausedReason?.value;
this.shouldEditPause = shouldEditPause(donationSchedules);
}
private Datetime calculateElevatePauseEndDate (Date endDate,
List<RecurringDonationSchedule__c> donationSchedules) {
RD2_ScheduleService rd2ScheduleService = new RD2_ScheduleService();
Date nextDonationDate = rd2ScheduleService.getNextInstallment(
endDate, donationSchedules
).nextDonationDate;

return Datetime.newInstance(
nextDonationDate.year(), nextDonationDate.month(), nextDonationDate.day()).addSeconds(-1);

}

private Boolean shouldEditPause (List<RecurringDonationSchedule__c> donationSchedules) {
for (RecurringDonationSchedule__c schedule : donationSchedules) {
return schedule.IsPause__c;
}
return false;
}
}

/***
* @description Handles Pause Recurring Donation functionality
*/
Expand All @@ -1040,8 +1076,13 @@ public without sharing class RD2_ScheduleService {
StartDate__c = startDate,
EndDate__c = endDate
);

}

public ElevatePauseSchedule createElevatePauseSchedule (RD2_PauseForm_CTRL.PauseData pause, RD2_RecurringDonation rd) {
return new ElevatePauseSchedule(rd.getSObject().RecurringDonationSchedules__r, pause);
}


/***
* @description Indicates if the Recurring Donation has an active pause schedule
* at some point in the future (not necessarily in a current pause state).
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import psElevateTokenHandler from '../psElevateTokenHandler';
import getOriginUrls from '@salesforce/apex/GE_PaymentServices.getOriginUrls';

const createPostMessageEvent = (type) => {
switch (type) {
Expand Down Expand Up @@ -43,6 +44,16 @@ const createPostMessageEvent = (type) => {
default:
}
}
jest.mock(
'@salesforce/apex/GE_PaymentServices.getOriginUrls',
() => {
return {
default: jest.fn()
};
},
{ virtual: true }
);


const buildVFUrls = async(domainInfo, namespace) => {
return await psElevateTokenHandler.getVisualForceOriginURLs(domainInfo, namespace);
Expand Down Expand Up @@ -71,6 +82,10 @@ describe('c-ps-Elevate-Token-Handler', () => {
});

it('should discard an invalid message', async() => {
getOriginUrls.mockResolvedValue({
visualForceOriginUrl: 'https://flow-connect-2738-dev-ed--c.vf.force.com',
lightningOriginUrl: 'https://flow-connect-2738-dev-ed--c.lightning.force.com'
});
psElevateTokenHandler.setVisualforceOriginURLs(mockDomainInfo());
await flushPromises();
const isMessageHandled =
Expand Down Expand Up @@ -100,14 +115,14 @@ describe('c-ps-Elevate-Token-Handler', () => {
it('should create one non-namespaced visualforce origin urls', async () => {
const vfURLS = buildVFUrls(mockDomainInfo(), 'c');
return vfURLS.then(data => {
expect(data.length).toEqual(1);
expect(data.length).toEqual(2);
});
});

it('should create one namespaced visualforce origin urls', () => {
const vfURLS = buildVFUrls(mockDomainInfo(), 'npsp');
return vfURLS.then(data => {
expect(data.length).toEqual(1);
expect(data.length).toEqual(2);
});
});

Expand All @@ -132,7 +147,7 @@ describe('c-ps-Elevate-Token-Handler', () => {
it('should create two non-namespaced visualforce origin urls on Experience Sites', () => {
const vfURLS = buildVFUrls(mockDomainInfoExperienceSite(), 'c');
return vfURLS.then(data => {
expect(data.length).toEqual(2);
expect(data.length).toEqual(3);
expect(data[1].value.includes('c')).toBe(true);
});
});
Expand Down
Loading

0 comments on commit 753f434

Please sign in to comment.