1.0.1 • Published 3 years ago

@paytrail/web-component-e2 v1.0.1

Weekly downloads
95
License
MIT
Repository
github
Last release
3 years ago

:exclamation: 3rd party developed Web Component library for creating payments with Paytrail E2 Interface.

Table of Contents

Table of contents generated with markdown-toc

Easy to use plug-and-play Web Component for Paytrail integrations.

Makes use of the E2 Payments API and eases the process of creating a Paytrail Payment gateway.

Extremely easy to set up. Uses the testing credentials, if none are probided, so you can easily get into demoing.

Installation

paytrail-web-component-e2 will be available as a NPM package and through a CDN.

NPM

npm install @paytrail/web-component-e2

CDN

<script src="https://unpkg.com/@paytrail/web-component-e2"></script>

Usage

Setup examples can be found in the setup region of the README.

Example use cases for the generation of auth codes and handling products can be found in the examples region of the README.

For the latest information and instructions, see the Official Paytrail Documentation.

Attributes

AttributeDescriptionValidationRequired
merchant_authentication_hashMerchant Secret. Used to generate the AUTHCODE if using calculateAuthCodeString and calculating the code in the frontend. 
amountPaytrail DocumentationFloat between 0.65–499999.99 (10) X
authcodePaytrail Documentation0-9, A-Z. (64) X
expiration_for_payment_creationPaytrail DocumentationISO-8601 notation datetime with time zone
localePaytrail Documentationfi*FI, sv_SE, and en_US
merchant_idPaytrail DocumentationNumeric string. (11)X
msg_settlement_payerPaytrail DocumentationUnicode alphabets and ()[]{}*+-_,.\"'
msg_ui_merchant_panelPaytrail DocumentationUnicode alphabets and ()[]{}*+-_,.\"'
msg_ui_payment_methodPaytrail DocumentationUnicode alphabets and ()[]{}*+-_,.\"'
order_numberPaytrail Documentation If no order number is provided, one will be generated.0-9, a-z, A-Z and ()[]{}*+-_,X
params_inPaytrail Documentation0-9, A-Z, [],_. (4096)X
params_outPaytrail Documentation0-9, A-Z, [],_. (255)X
payer_company_namePaytrail DocumentationUnicode alphabets and ()[]{}+-_,:&!?@#\$£=;~/\"'.
payer_person_addr_countryPaytrail Documentationa-z, A-Z. (2)
payer_person_addr_postal_codePaytrail Documentation0-9, a-z, A-Z. (16)
payer_person_addr_streetPaytrail DocumentationUnicode alphabets
payer_person_addr_townPaytrail DocumentationUnicode alphabets and ()[]{}+-_,:&!?@#\$£=;~/\"'
payer_person_emailPaytrail Documentationexample@domain.org, max length for example is 64. (255)
payer_person_firstnamePaytrail DocumentationUnicode alphabets and ()[]{}+-_,:&!?@#\$£=;~/\"'
payer_person_lastnamePaytrail DocumentationUnicode alphabets and ()[]{}+-_,:&!?@#\$£=;~/\"'
payer_person_phonePaytrail Documentation0-9, +-. (64)
payment_methodsPaytrail Documentation0-9 and , (64)
reference_numberPaytrail DocumentationAlphanumeric, either numeric value complying Finnish reference number standard or numeric Finnish reference number in international RF format (e.g. 1232 or RF111232). (20)
url_cancelPaytrail DocumentationValid URL including http(s). URLs that do not include any dots (e.g. http://localhost) are currently not supported. (2048)X
url_notifyPaytrail DocumentationValid URL including http(s). URLs that do not include any dots (e.g. http://localhost) are currently not supported. (2048)
url_successPaytrail DocumentationValid URL including http(s). URLs that do not include any dots (e.g. http://localhost) are currently not supported. (2048)X
vat_is_includedPaytrail Documentation0, 1. (1)

Methods

MethodTypeDescription
addProducts(products: Product or Array<Product>): voidAdd product(s) to the transaction. Product fields must match the fields in the documentation
calculateAuthCodeString(): voidCalculates the AUTHCODE string, encrypting it and appending it into the AUTHCODE input field. Assumes that a merchant_authentication_hash is provided.
getAuthCodeString(): stringGenerates the AUTHCODE string needed for calculating the AUTHCODE hash. Doesn't append a merchant_authentication_hash, instead assumes that the given string is passed to another API and that said API appends the merchant_authentication_hash before encrypting the data
setAuthCode(authcode): stringSets the authcode into the corresponding input field. Use this if you calculate the authcode in the backend and want to append it as a reaction to e.g. a fetch request.
getProducts(): Array<Product>Returns a Array of Products set into the form
removeProduct(product: Product): voidRemove a product from the form
removeProductAtIndex(index: any): voidRemove a product from the form given a array index
setProducts(products: any): voidSet the products in the form, overwriting the current products
submit(): voidSubmit the Paytrail form manually

Product attributes

Attribute DescriptionValidation Required
item_title[N] Paytrail DocumentationUnicode alphabets and ()[]{}+-_,:&!?@#\$£=;~/\"'. X
item_id[N] Paytrail Documentation0-9. (16) 
item_quantity[N] Paytrail DocumentationFloating point number. (10) 
item_unit_price[N] Paytrail DocumentationFloating point number. between –499 999.99 – 499 999.99. (10) X
item_vat_percent[N] Paytrail DocumentationFloating point number. between 0-100. (10) X
item_discount_percent[N] Paytrail DocumentationFloating point number. (10) 
item_type[N] Paytrail Documentation1, 2, and 3. (1) 

Examples

Setting products

Products are not set through HTML attributes, like everything else is within this component. Instead products are added using the API of <paytrail-web-component-e2>.

  • addProducts() adds the given product(s) into the existing product array, and created the input fields for it.
  • getProducts() returns an array with all of the products set in the component
  • removeProduct() expect the exact same product, that was added as a parameter, and then removes it from the array
  • removeProductAtIndex() removes a product with given index

The API is simply called by selecting the component from the DOM and calling them:

document.querySelector('paytrail-web-component-e2').addProducts(productList);

Authcode generation

The authcode can be generated in two ways:

Generating the authcode in the front-end

If you want to generate the authcode in the frontend, you can call calculateAuthCodeString(), which will use the merchant_authentication_hash provided to generate a authentication code. The function will return a promise, you can await for the generation to finish, and then submit the form manually.

async handleSubmit() {
    await this.paytrailField.calculateAuthCodeString();
    this.paytrailField.submit();
}

Generating the authcode in the frontend is not advisable, since you will be exposing your merchant_authentication_hash to the public.

Generating the authcode in the back-end

Another way of generating the authcode is by getting the authcode string (a string of values, seperated by pipes (|), and passing it to the backend.

const paytrailComponent = document.querySelector("paytrail-web-component-e2");

function handleSubmit() {
    const authCodeString = paytrailComponent.getAuthCodeString();
    const formData = new FormData();
    formData.append('authCodeString', authCodeString);
    fetch('http://localhost:3000/authcode', {
        method: 'POST',
        body: formData,
    })
        .then(res => res.text())
        .then(res => {
            paytrailComponent.setAuthCode(res);
            paytrailComponent.submit();
        });
}

paytrailComponent.addEventListener('paytrail-submit', handleSubmit);

In the back end, the operation of generating the code would look something like this:

const sha256 = require("js-sha256");

calculateAuthCode(request) {
    const authCodeString = 'MY_MERCHANT_HASH' + req.body.authCodeString;
    return {authcode: sha256.hex(authCodeString).toUpperCase()};
}

Form Submission

If a authcode has been set for the component, the form submission will launch on the click of the submit button. If however not authcode has been set (for example in cases where you generate it in the backend), a paytrail-submit -event is triggered and the default of the form submit is prevented.

Minimal Setup

<paytrail-web-component-e2>
    <label>Pay Here</label>
</paytrail-web-component-e2>

Using the required fields

<paytrail-web-component-e2
    MERCHANT_ID="13466"
    URL_SUCCESS="http://www.example.com/success"
    URL_CANCEL="http://www.example.com/cancel"
    ORDER_NUMBER="123456"
    PARAMS_IN="MERCHANT_ID,URL_SUCCESS,URL_CANCEL,ORDER_NUMBER,PARAMS_IN,PARAMS_OUT,AMOUNT"
    PARAMS_OUT="PAYMENT_ID,TIMESTAMP,STATUS"
    AMOUNT="350.00"
    AUTHCODE="BBDF8997A56F97DC0A46C99C88C2EEF9D541AAD59CFF2695D0DD9AF474086D71"
>
    <label>Pay Here</label>
</paytrail-web-component-e2>

Using all fields

<paytrail-web-component-e2
    MERCHANT_ID="13466"
    URL_SUCCESS="http://www.example.com/success"
    URL_CANCEL="http://www.example.com/cancel"
    ORDER_NUMBER="123456"
    PARAMS_IN="MERCHANT_ID,URL_SUCCESS,URL_CANCEL,ORDER_NUMBER,PARAMS_IN,PARAMS_OUT,MSG_UI_MERCHANT_PANEL,URL_NOTIFY,LOCALE,REFERENCE_NUMBER,PAYMENT_METHODS,PAYER_PERSON_PHONE,PAYER_PERSON_EMAIL,PAYER_PERSON_FIRSTNAME,PAYER_PERSON_LASTNAME,PAYER_COMPANY_NAME,PAYER_PERSON_ADDR_STREET,PAYER_PERSON_ADDR_POSTAL_CODE,PAYER_PERSON_ADDR_TOWN,PAYER_PERSON_ADDR_COUNTRY,AMOUNT"
    PARAMS_OUT="ORDER_NUMBER,PAYMENT_ID,AMOUNT,CURRENCY,PAYMENT_METHOD,TIMESTAMP,STATUS"
    MSG_UI_MERCHANT_PANEL="Order 123456"
    URL_NOTIFY="http://www.example.com/notify"
    LOCALE="en_US"
    REFERENCE_NUMBER="RF111232"
    PAYMENT_METHODS="1"
    PAYER_PERSON_PHONE="01234567890"
    PAYER_PERSON_EMAIL="john.doe@example.com"
    PAYER_PERSON_FIRSTNAME="John"
    PAYER_PERSON_LASTNAME="Doe"
    PAYER_COMPANY_NAME="Test Company"
    PAYER_PERSON_ADDR_STREET="Test Street 1"
    PAYER_PERSON_ADDR_POSTAL_CODE="608009"
    PAYER_PERSON_ADDR_TOWN="Test Town"
    PAYER_PERSON_ADDR_COUNTRY="AA"
>
    <label>Pay Here</label>
</paytrail-web-component-e2>

Directing to a certain payment provider

Do direct to a payment provider, you just need to supply a single entry into the PAYMENT_METHODS -field.

By providing a background_image -property, we can make the button the selected bank's logo.

<paytrail-web-component-e2
    MERCHANT_ID="13466"
    URL_SUCCESS="http://www.example.com/success"
    URL_CANCEL="http://www.example.com/cancel"
    ORDER_NUMBER="123456"
    PARAMS_IN="MERCHANT_ID,URL_SUCCESS,URL_CANCEL,ORDER_NUMBER,PARAMS_IN,PARAMS_OUT,AMOUNT"
    PARAMS_OUT="PAYMENT_ID,TIMESTAMP,STATUS"
    AMOUNT="350.00"
    AUTHCODE="BBDF8997A56F97DC0A46C99C88C2EEF9D541AAD59CFF2695D0DD9AF474086D71"
    PAYMENT_METHODS="52"
>
    <img src="my_bank_icon_url" />
</paytrail-web-component-e2>

Payment Methods

By specifying a PAYMENT_METHODS -field, you can create buttons that go straight to the vendor's site.

The payment methods and their codes are listed below

For the most up to date list of all of the payment methods, see the official documentation

Name Code
Nordea1
OsuusPankki2
Danske Bank
Ålandsbanken5
Handelsbanken6
 Paypal9
S-Pankki 10
 Jousto18
Aktia 50
POP Pankki 51
Säästöpankki52
Visa (Nets)53
MasterCard (Nets)54
Diners Club (Nets)55
American Express (Nets)56
MobilePay58
Collector Bank60
Oma Säästöpankki61