Mollie.js

Mollie JavaScript SDK v1
https://js.mollie.com/v1/mollie.js

Mollie’s browser-side JavaScript library is called Mollie.js. You can use it to offer a more deeply embedded checkout experience. See the guides on Mollie Components and on building your own checkout for more information.

The Mollie object is used to initialize Mollie Components. Component objects each reference an embeddable checkout component, such as a credit card number field or a credit card expiry date field.

Mollie(profileId[, options])

profileIdstringrequired
Your profile ID, for example pfl_3RkSN1zuPE.
optionsoptions objectoptional

Any options you want to set. E.g. { locale: "nl_NL"}

Show child parameters

localestringoptional

Allows you to preset the language to be used. When this parameter is not provided, the browser language will be used instead. If we also not support the browser language then it will be shown in English. We recommend you provide the language tag, since this is usually more accurate.

Possible values: en_US nl_NL nl_BE fr_FR fr_BE de_DE de_AT de_CH es_ES ca_ES pt_PT it_IT nb_NO sv_SE fi_FI da_DK is_IS hu_HU pl_PL lv_LV lt_LT

testmodebooleanoptional
Set to true to enable test mode. Test tokens will be recognizable by the test suffix, e.g. tkn_123abctest.

mollie.createToken()

Calling the createToken method will receive a token if successful. This token must then be sent to your back end where it can be passed as the cardToken parameter to the Create Payment API.

This method has to be invoked from the submit event of your checkout form:

Javascript

1
2
3
4
5
6
7
 form.addEventListener('submit', e => {
   e.preventDefault();

   mollie.createToken().then(function(result) {
     // Handle the result this can be either result.token or result.error.
   });
 });

ES6

1
2
3
4
5
form.addEventListener('submit', async e => {
  e.preventDefault();

  const { token, error } = await mollie.createToken();
});

mollie.createComponent(type[, options])

This will create the Components which the shopper uses to enter the card holder data. After creating, the components should be mounted in your checkout.

You need to create four components, one for each card holder data field.

typestringrequired

The createComponent method will create an component ready to be mounted.

Possible values: "cardHolder" "cardNumber" "verificationCode" "expiryDate"

optionsoptions objectoptional

The options you want to give to Mollie Components. E.g. { styles: fontSize: "10px"}

Show child parameters

stylesstyles objectoptional

Javascript

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
 var options = {
                 styles : {
                   base: {
                     color: '#eee',
                     fontSize: '10px',
                     '::placeholder' : {
                       color: 'rgba(68, 68, 68, 0.2)',
                     }
                   }
                 }
               }

 var cardNumberEl = mollie.createComponent('cardNumber', options)

ES6

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
 const options = {
                   styles : {
                     base: {
                       color: '#eee',
                       fontSize: '10px',
                       '::placeholder' : {
                         color: 'rgba(68, 68, 68, 0.2)',
                       }
                     }
                   }
                 }

 const cardNumberEl = mollie.createComponent('cardNumber', options)

Component methods

component.mount(targetElement)

Adds the component to the DOM, meaning it will become visible for the user from this point onwards.

1
2
 <label for="card" >Card label</label>
 <div id="card"></div>
targetElementHTMLElement|stringrequired
An HTMLElement or a valid CSS Selector such as #id and .class.

Javascript

1
 cardNumberEl.mount('#card');

ES6

1
 cardNumberEl.mount('#card');

component.addEventListener(event, callback)

Components can listen to several events. The callback receives an object with all the related information.

eventstringrequired

Subscribe to the event that are emitted by Mollie js.

Possible values: "blur" "focus" "change"

callbackfunctionrequired
A function that will be called whenever the event is been emitted.

Javascript

1
2
 var callback = function(event) { console.log('We need a real world example here', event.type) }
 cardNumberEl.addEventListener('change', callback);

ES6

1
2
 const callback = (event)=> { console.log('We need a real world example here', event.type) }
 cardNumberEl.addEventListener('change', callback);

component.unmount()

Removes the component from the DOM. Note that state — such as input values — is not preserved when re-mounting.

Javascript

1
 cardNumberEl.unmount();

ES6

1
 cardNumberEl.unmount();