Elements

Learn how to customize and stylize elements to seamlessly integrate them into your user experience or purchase flow.‌

An Element is a UI component that DigitalRiver.js creates to collect sensitive customer information without having the data touch your servers. You can customize and stylize these components to seamlessly integrate into your user experience or purchase flow. The library collects and tokenizes the data contained in these elements without exposing you to PCI-compliance liability.‌

element.mount();

Use this function to place the created elements on your page. The function accepts an identifier of a container div. The library will load the created element within the specified container.

<form id="payment-form">
    <div id="card-number"></div>
    <div id="card-expiration"></div>
    <div id="card-cvv"></div>
    <button type="submit">Submit</button>
</form>
 
 
cardNumber.mount('card-number');
cardExpiration.mount('card-expiration');
cardCVV.mount('card-cvv');

element.on();

Use this function to listen to events that you can use to build and enhance your purchase flow.

Blur

A Blur event triggers when an element loses focus.

cardNumber.on('blur', function(event) {
    console.log('card number blur', event);
});
{
    elementType: "cardnumber"
}
{
    elementType: "cardnumber"
}

Cancel

A Cancel event occurs when the customer closes the Apple Pay Element Payment Request interface.

applepay.on('cancel', function(event) {
    //do stuff
}
{
    elementType: "applepay"
}

Change

A Change event triggers when an element changes state.‌

If an error is detected, Digital River will return a Change Event Error object with the event payload.

cardNumber.on('change', function(event) {
    console.log('card number change', event);
});
{
    brand: "unknown",
    complete: false,
    empty: true,
    elementType: "cardnumber",
    error: null
}

Invalid element change event example

{
    brand: "unknown",
    complete: true,
    empty: false,
    elementType: "cardnumber",
    "error": {
        "type": "validation_error",
        "code": "invalid_card_number",
        "message": "Your card number is invalid."
    }
}

Incomplete element change event example

{
    brand: "unknown",
    complete: false,
    empty: false,
    elementType: "cardnumber",
    error: {
        "type": "validation_error",
        "code": "incomplete_card_number",
        "message": "Your card number is incomplete."
    }
}

Error types, codes, messages

Card brands

The following table lists the supported credit card brands.

Click

A Click event occurs when the customer clicks an Apple Pay Element.

applepay.on('click', function(event) {
    //do stuff
}
{
    elementType: "applepay"
}

Focus

A Focus event triggers when an element gains focus.

cardNumber.on('ready', function(event) {
    console.log('card number ready', event);
});
{    
    elementType: "cardnumber"
}

Ready

A Ready event triggers when an element is ready and able to receive blur(), focus(), or update() calls.

cardNumber.on('focus', function(event) {
    console.log('card number focus', event);
});
{
    elementType: "cardnumber"
}

Return

A Return event triggers when a customer presses the Return key while the input field has focus.

cardNumber.on('return', function(event) {
    console.log('card number return', event);
});
{
    elementType: "cardnumber"
}

Shipping address change

A Shipping Address Change event occurs when the Customer has selected a different Shipping Address within the Payment Request interface. The event will create the following object structure.

applepay.on('shippingaddresschange', function(event) {
    var shippingAddress = event.shippingAddress;
  
  
    //create a Payment Request Details Update Object
    var newDetails = createPaymentRequestDetailsUpdateObject();
      
    event.updateWith(newDetails);
});

Apple Pay shipping address change object

{
    shippingAddress: {
        "name": "John Smith",
        "firstName": "John",
        "lastName": "Smith",
        "phone": "952-111-1111",
        "email": "jsmith@digitalriver.com",
        "address": {
            "line1": "10380 Bren Rd W",
            "line2": "string",
            "city": "Minnetonka",
            "postalCode": "55129",
            "state": "MN",
            "country": "US"
        }
    },
    "updateWith": function(data) {
        callback(data);
    }
}

Shipping option change

A Shipping Option Change event occurs when the Customer has selected a different Shipping Option within the Payment Request interface. The event will emit the following object structure.

applepay.on('shippingaddresschange', function(event) {
    var shippingAddress = event.shippingAddress;
  
  
    //create a Payment Request Details Update Object
    var newDetails = createPaymentRequestDetailsUpdateObject();
      
    event.updateWith(newDetails);
});

Apple Pay shipping option change object

{
    "shippingOption": {
        "id": "overnight-shipping",
        "label": "Overnight Shipping",
        "amount": 100,
        "detail": "Get this in 1 business day."
    },
    "updateWith": function(data) {
        callback(data);
    }
}

Source

A Source event occurs when the Customer completes their interaction with the Payment Request interface and creates a Payment Source. The emitted object will be a Payment Request Response object.

googlepay.on('source', function(result) {
    var source = result.source;
     
    //pass the source to your back end for further processing
}

Element functions

Use these functions to trigger functionality within the specified Element to further enhance your purchase flow experience.‌

element.blur();

This function triggers the blur() event. This will remove the focus from the element.

cardNumber.blur();

element.clear();

This function clears the contents of the element.

cardNumber.clear();

element.focus();

This function triggers the focus() event and places the focus on the element.

cardNumber.destroy();

element.destroy();

This function destroys the element. Removes the element and all of its associated data so you cannot use it again. You must create a new element if you want to restore the associated data.

cardNumber.destroy();

element.unmount();

This function removes the element from the Document Object Module (DOM). The element and its associated data still exists. You can place it on the page again by calling its mount() function.

cardNumber.unmount();

element.update(options);

This function updates the element with any included options. This can include custom styles and classes.

var options = {
    classes: {
        base: "DRElement",
        complete: "complete",
        empty: "empty",
        focus: "focus",
        invalid: "invalid",
        webkitAutofill: "autofill"
    },
    style: {
        base: {
            color: "#fff",
            fontFamily: "Arial, Helvetica, sans-serif",
            fontSize: "20px",
            fontSmoothing: "auto",
            fontStyle: "italic",
            fontVariant: "normal",
            letterSpacing: "3px"
        },
        empty: {
            color: "#fff"
        },
        complete: {
            color: "green"
        },
        invalid: {
            color: "red"
        }
    }
};
 
cardNumber.update(options);

Styling an element container

Custom classes

You can specify custom classes as part of a Class object included within the Options object when you create or update an element. If you do not provide custom classes, the system uses the default options.

var options = {
    classes: {
        base: "DRElement",
        complete: "complete",
        empty: "empty",
        focus: "focus",
        invalid: "invalid",
        webkitAutofill: "autofill"
    }
}
 
var cardNumber = elements.create("cardnumber", options);

Available custom classes

Custom styles

You can specify custom styles as part of a Style object included within the Options object when creating or updating an Element. If you don't provide custom styles, the system uses the browser defaults.

var options = {
    style: {
        base: {
            color: "#fff",
            fontFamily: "Arial, Helvetica, sans-serif",
            fontSize: "20px",
            fontSmoothing: "auto",
            fontStyle: "italic",
            fontVariant: "normal",
            letterSpacing: "3px"
        },
        empty: {
            color: "#fff"
        },
        complete: {
            color: "green"
        },
        invalid: {
            color: "red",
        }
    }
}
 
 
var cardNumber = elements.create('cardnumber', options);

Available custom style classes

Available custom styles

Pseudo-classes

Other customizable attributes

Last updated