Skip to content

Commit

Permalink
Update FuturePay interactions with config options
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesmcroft committed Oct 4, 2023
1 parent 3ded9fb commit e90abba
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 11 deletions.
6 changes: 5 additions & 1 deletion samples/vuejs/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ const initiateRegularAgreement = () => {
option: RegularAgreementOption.Default,
cartId: 'MerchantReference',
testMode: 100
} as RegularAgreement);
} as RegularAgreement, {
openInNewTab: true,
callbackUrl: null,
additionalProperties: null
});
};
const cancelAgreement = () => {
Expand Down
21 changes: 21 additions & 0 deletions src/FuturePayAgreementConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { AgreementMap } from "./AgreementMap";

/**
* Defines the configuration options used when interacting with FuturePay.
*/
export default interface FuturePayAgreementConfig {
/**
* The optional callback URL to be used by FuturePay to notify of the interaction status.
*/
callbackUrl: string | null;

/**
* The optional additional properties to be used to construct the form inputs used when interacting with FuturePay.
*/
additionalProperties: AgreementMap | null;

/**
* The optional flag to indicate whether the FuturePay interaction should be initiated in a new tab.
*/
openInNewTab: boolean;
}
29 changes: 19 additions & 10 deletions src/FuturePayService.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Agreement } from "./Agreement";
import { AgreementMap } from "./AgreementMap";
import { CancelAgreement } from "./CancelAgreement";
import FuturePayAgreementConfig from "./FuturePayAgreementConfig";

/**
* Defines a class used to initiate recurring payments in FuturePay.
Expand All @@ -21,11 +22,14 @@ export class FuturePayService {
/**
* Initiates a payment in FuturePay.
* @param agreement The agreement object to construct the form inputs used to initiate the payment in FuturePay.
* @param callbackUrl The optional callback URL to be used by FuturePay to notify the payment status.
* @param additionalProperties The optional additional properties to be used to construct the form inputs used to initiate the payment in FuturePay.
* @param options The configuration options for constructing the agreement cancellation interaction.
* @returns A promise that resolves to the HTML form inputs used to initiate the payment in FuturePay.
*/
initiateAgreement(agreement: Agreement, callbackUrl: string | null = null, additionalProperties: AgreementMap | null = null): Promise<string> {
initiateAgreement(agreement: Agreement, options: FuturePayAgreementConfig = {
callbackUrl: null,
additionalProperties: null,
openInNewTab: false
}): Promise<string> {
var formInputsHtml = "";

for (var key in agreement) {
Expand All @@ -34,13 +38,14 @@ export class FuturePayService {
}
}

formInputsHtml += this.generateAdditionalPropertiesInputs(additionalProperties);
formInputsHtml += this.generateCallbackUrlInput(callbackUrl);
formInputsHtml += this.generateAdditionalPropertiesInputs(options.additionalProperties);
formInputsHtml += this.generateCallbackUrlInput(options.callbackUrl);

if (document) {
var form = document.createElement("form");
form.setAttribute("method", "post");
form.setAttribute("action", this.purchaseUrl);
form.setAttribute("target", options.openInNewTab ? "_blank" : "_self");
form.innerHTML = formInputsHtml;
document.body.appendChild(form);

Expand All @@ -57,11 +62,14 @@ export class FuturePayService {
/**
* Cancels an agreement in FuturePay.
* @param cancelAgreement The cancel agreement object to construct the form inputs used to cancel the agreement in FuturePay.
* @param callbackUrl The optional callback URL to be used by FuturePay to notify the payment status.
* @param additionalProperties The optional additional properties to be used to construct the form inputs used to cancel the agreement in FuturePay.
* @param options The configuration options for constructing the agreement cancellation interaction.
* @returns A promise that resolves to the HTML form inputs used to cancel the agreement in FuturePay.
*/
cancelAgreement(cancelAgreement: CancelAgreement, callbackUrl: string | null = null, additionalProperties: AgreementMap | null = null): Promise<string> {
cancelAgreement(cancelAgreement: CancelAgreement, options: FuturePayAgreementConfig = {
callbackUrl: null,
additionalProperties: null,
openInNewTab: false
}): Promise<string> {
var formInputsHtml = "";

for (var key in cancelAgreement) {
Expand All @@ -72,13 +80,14 @@ export class FuturePayService {

formInputsHtml += `<input type='hidden' name='op-cancelFP'></input>`

formInputsHtml += this.generateAdditionalPropertiesInputs(additionalProperties);
formInputsHtml += this.generateCallbackUrlInput(callbackUrl);
formInputsHtml += this.generateAdditionalPropertiesInputs(options.additionalProperties);
formInputsHtml += this.generateCallbackUrlInput(options.callbackUrl);

if (document) {
var form = document.createElement("form");
form.setAttribute("method", "post");
form.setAttribute("action", this.adminUrl);
form.setAttribute("target", options.openInNewTab ? "_blank" : "_self");
form.innerHTML = formInputsHtml;
document.body.appendChild(form);

Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export * from './AgreementMap';
export * from './CancelAgreement';
export * from './CurrencyCode';
export * from './DelayUnit';
export * from './FuturePayAgreementConfig';
export * from './FuturePayService';
export * from './FuturePayType';
export * from './LimitedAgreement';
Expand Down

0 comments on commit e90abba

Please sign in to comment.