In the client component of the settings field you get the values from the input fields and serialize them as a string. This string must then be returned as the value of the custom field control.
/// <reference name="MicrosoftAjax.js"/>
/// <reference name="Telerik.Sitefinity.Resources.Scripts.jquery-1.6.3-vsdoc.js" assembly="Telerik.Sitefinity.Resources"/>
Type.registerNamespace("SitefinityWebApp.CustomPaymentProcessor");
SitefinityWebApp.CustomPaymentProcessor.CustomPaymentSettingsField = function (element) {
SitefinityWebApp.CustomPaymentProcessor.CustomPaymentSettingsField.initializeBase(this, [element]);
this._element = element;
this._testPasswordControl = null;
this._testUsernameControl = null;
this._livePasswordControl = null;
this._liveUsernameControl = null;
this._timeoutControl = null;
this._paymentTypeControl = null;
this._processingModeControl = null;
this._processorCreditCardsControl = null;
this._processingModeControlDelegate = null;
this._state = null;
this._appLoadedDelegate = null;
this._appLoaded = false;
};
SitefinityWebApp.CustomPaymentProcessor.CustomPaymentSettingsField.prototype =
{
initialize: function () {
SitefinityWebApp.CustomPaymentProcessor.CustomPaymentSettingsField.callBaseMethod(this, "initialize");
if (this._appLoadedDelegate === null) {
this._appLoadedDelegate = Function.createDelegate(this, this._applicationLoaded);
}
if (this._processingModeControl) {
this._processingModeControlDelegate = Function.createDelegate(this, this._processingModeControlHandler);
this._processingModeControl.add_valueChanged(this._processingModeControlDelegate);
}
Sys.Application.add_load(this._appLoadedDelegate);
},
dispose: function () {
SitefinityWebApp.CustomPaymentProcessor.CustomPaymentSettingsField.callBaseMethod(this, "dispose");
if (this._appLoadedDelegate) {
Sys.Application.remove_load(this._appLoadedDelegate);
delete this._appLoadedDelegate;
}
},
/* -------------------- public methods ----------- */
// this method is called by definitions engine just before saving. It should return true
// if value is valid; otherwise false. As our field control is composed of several other
// field controls, we will validate them (remember, both this control and child controls
// inherit base FieldControl JS component.
validate: function () {
if (!$(this.get_element()).is(':visible')) {
return true;
}
// if only one of the child field controls fails, this (parent/container) field control
// should fail as well
var isValid = true;
if (!this.get_timeoutControl().validate()) {
isValid = false;
}
if (!this.get_processorCreditCardsControl().validate()) {
isValid = false;
}
// here you can also perform other validations that could depend
// on the values of multiple fields, so you have a very extensible
// validation meschanism
if (this.get_processingModeControl().get_value() == "live") {
// validate live fields
if (!this.get_livePasswordControl().validate()) {
isValid = false;
}
if (!this.get_liveUsernameControl().validate()) {
isValid = false;
}
}
else {
// validate test fields
if (!this.get_testPasswordControl().validate()) {
isValid = false;
}
if (!this.get_testUsernameControl().validate()) {
isValid = false;
}
}
// we return the aggregate result of all our validations
return isValid;
},
reset: function () {
// Setup default values
// ProcessorCreditCards is set to undefined so updateUI handles both the 'New' and 'Edit' cases correctly.
this._state = { 'TestPassword': '', 'TestUsername': '', 'LivePassword': '', 'LiveUsername': '', 'Timeout': '30000', 'ProcessingMode': '', 'ProcessorCreditCards': undefined, 'PaymentType': '' };
},
// Gets the value of the field control.
get_value: function () {
return Sys.Serialization.JavaScriptSerializer.serialize(this._getSettingsObject());
},
// Sets the value of the text field control depending on DisplayMode.
set_value: function (value) {
if (value !== null && value.length > 0) {
var settingsObject = Sys.Serialization.JavaScriptSerializer.deserialize(value);
this._state = settingsObject;
}
if (this._appLoaded) {
this._updateUI();
}
},
// Returns true if the value of the field is changed
isChanged: function () {
// TODO: implement
},
/* -------------------- events -------------------- */
/* -------------------- event handlers ------------ */
_applicationLoaded: function (sender, agrs) {
this._appLoaded = true;
this._updateUI();
},
_processingModeControlHandler: function (e) {
$('.sf_payFlowProPaymentMode').hide();
if (this.get_processingModeControl().get_value() == 'test') {
$('.sf_payFlowProTestMode').show();
}
else {
$('.sf_payFlowProLiveMode').show();
}
},
/* -------------------- private methods -------------------- */
// this method updates the user interface with the values from the state
_updateUI: function () {
this.get_testPasswordControl().set_value(this._state.TestPassword);
this.get_testUsernameControl().set_value(this._state.TestUsername);
this.get_livePasswordControl().set_value(this._state.LivePassword);
this.get_liveUsernameControl().set_value(this._state.LiveUsername);
this.get_timeoutControl().set_value(this._state.Timeout);
this.get_processingModeControl().set_value(this._state.ProcessingMode);
// By default we want all credit cards to be checked. We made the reset method set the state's ProcessorCreditCards to undefined.
var creditCardValues = this._state.ProcessorCreditCards;
if (creditCardValues === undefined) {
creditCardValues = $.map(this.get_processorCreditCardsControl()._choices, function (choice) { return choice.Value; });
}
this.get_processorCreditCardsControl().set_value(creditCardValues);
this.get_paymentTypeControl().set_value(this._state.PaymentType);
$('.sf_payFlowProPaymentMode').hide();
if (this.get_processingModeControl().get_value() == 'test') {
$('.sf_payFlowProTestMode').show();
}
else {
$('.sf_payFlowProLiveMode').show();
}
},
_getSettingsObject: function () {
var creditCards = this.get_processorCreditCardsControl().get_value();
if (!this._isArray(creditCards)) {
creditCards = [creditCards];
}
var settings = {
'TestPassword': this.get_testPasswordControl().get_value(),
'TestUsername': this.get_testUsernameControl().get_value(),
'LivePassword': this.get_livePasswordControl().get_value(),
'LiveUsername': this.get_liveUsernameControl().get_value(),
'Timeout': this.get_timeoutControl().get_value(),
'ProcessingMode': this.get_processingModeControl().get_value(),
"ProcessorCreditCards": creditCards,
'PaymentType': this.get_paymentTypeControl().get_value()
};
return settings;
},
// a more compact version
_isArray: function (obj) {
return (obj.constructor.toString().indexOf('Array') != -1);
},
/* -------------------- properties ---------------- */
// gets the reference to the choice field which holds credit cards to be processed
get_processorCreditCardsControl: function () {
return this._processorCreditCardsControl;
},
// gets the reference to the choice field which holds credit cards to be processed
set_processorCreditCardsControl: function (value) {
this._processorCreditCardsControl = value;
},
// gets the reference to the choice field which holds available processing modes
get_processingModeControl: function () {
return this._processingModeControl;
},
// sets the reference to the choice field which holds available processing modes
set_processingModeControl: function (value) {
this._processingModeControl = value;
},
// gets the reference to the control that holds the test password of the payment processor
get_testPasswordControl: function () {
return this._testPasswordControl;
},
// sets the reference to the control that holds the test password of the payment processor
set_testPasswordControl: function (value) {
this._testPasswordControl = value;
},
// gets the reference to the control that holds the test username of the payment processor
get_testUsernameControl: function () {
return this._testUsernameControl;
},
// sets the reference to the control that holds the test username of the payment processor
set_testUsernameControl: function (value) {
this._testUsernameControl = value;
},
// gets the reference to the control that holds the live password of the payment processor
get_livePasswordControl: function () {
return this._livePasswordControl;
},
// sets the reference to the control that holds the live password of the payment processor
set_livePasswordControl: function (value) {
this._livePasswordControl = value;
},
// gets the reference to the control that holds the live username of the payment processor
get_liveUsernameControl: function () {
return this._liveUsernameControl;
},
// sets the reference to the control that holds the live username of the payment processor
set_liveUsernameControl: function (value) {
this._liveUsernameControl = value;
},
// gets the reference to the control that holds the timeout of the payment processor
get_timeoutControl: function () {
return this._timeoutControl;
},
// sets the reference to the control that holds the timeout of the payment processor
set_timeoutControl: function (value) {
this._timeoutControl = value;
},
// gets the reference to the control that holds the payment type of the payment processor
get_paymentTypeControl: function () {
return this._paymentTypeControl;
},
// sets the reference to the control that holds the payment type of the payment processor
set_paymentTypeControl: function (value) {
this._paymentTypeControl = value;
}
};
SitefinityWebApp.CustomPaymentProcessor.CustomPaymentSettingsField.registerClass("SitefinityWebApp.CustomPaymentProcessor.CustomPaymentSettingsField", Telerik.Sitefinity.Web.UI.Fields.FieldControl);