Datatrans Payment Button

Use this JavaScript library to render an Apple Pay or Google Pay button. It uses the Payment Request API specified by W3C when available. If not, it falls back to the corresponding vendor implementation. This library gives you a single integration for rendering payment buttons. It can automatically detect which button should be displayed depending on the browser and device combination that is supported and used. In the demo below, depending on your device you will see either an Apple Pay or Google Pay button. Please note that for Apple Pay this library only works with a Datatrans merchantId which has been enabled for Apple Pay processing via Datatrans.

Configure and test the example

Events output

How it works

1) The payment request

The library expects a payment request when a button is created. The payment request is an object which conforms with the W3C payment request interface description. The object is extended with a transaction property, which contains Datatrans-specific values such as "countryCode", "refno", etc.

const payment = {
  // W3C spec
  details: {
    total: {
      label: 'My Merchant',
      amount: { value: '4', currency: 'USD' },
    },
    displayItems: [
      {
        label: 'Tax',
        amount: { value: '2.50', currency: 'USD' },
      },
    ],
  },
  // W3C spec
  options: {
    requestPayerEmail: false,
    requestPayerName: false,
    requestPayerPhone: false,
  },
  // Datatrans specific
  transaction: {
    countryCode: 'CH',
    refno: '3e23dasdasd1123',
  },
};

2) Initialize the library

<script src="https://pay.sandbox.datatrans.com/upp/payment/js/payment-button-3.0.0.js" type="text/javascript"></script>

const paymentButton = new PaymentButton();

paymentButton.init({
  merchantId: '1000011011',
  merchantName: 'Test',
  tokenOnly: false,
  autoSettle: false,
  allowedCardNetworks: ['AMEX', 'DISCOVER', 'MASTERCARD', 'VISA'],
  googlePayConfiguration: {
    buttonType: 'long',
    buttonStyle: 'black',
    merchantId: '01234567890123456789'
  },
  applePayConfiguration: {
    buttonType: 'plain',
    buttonStyle: 'black'
  },
});

3) Listen for the init event and create the button

paymentButton.on("init", function () {
  paymentButton.create(document.getElementById('paybutton'), payment)
});

4) Listen for the authorization event and continue

paymentButton.on("authorization", function (response) {
  // handle transaction result
});

Reference

Events

The following events are emitted by the library:
  • init - emitted when the library is initialized and ready to use
  • create - emitted when payment button is rendered
  • authorization - emitted when an authorization response has been received - payload: authorization response
  • abort - emitted when the payment request has been cancelled by the user
  • unsupported - emitted when no payment method is supported
  • error - emitted when an error occurs - <optional>payload: error message

Init parameters

Option Description Data type
merchantId
required
Your Datatrans merchantId String
merchantName
required
Your merchant name String
allowedCardNetworks
required
Which card networks to accept String array
useGooglePay Use this property to disable Google Pay. Note: By default Google Pay is enabled if supported by the browser Boolean
useApplePay Use this property to disable Apple Pay. Note: Apple Pay can only be displayed in Safari browsers. Note: By default Apple Pay is enabled if supported by the browser Boolean
autoSettle Whether the transaction will be settled automatically. Default is false. Boolean
googlePayConfiguration Object
buttonType The type of button, described here String
buttonStyle The style of button, described here String
merchantId Your Google Pay merchantId. Can be found in the Google Pay merchant console String
applePayConfiguration Object
buttonType The type of button, described here String
buttonStyle The style of the button, described here String

Transaction parameters

Option Description Data type
refno
required
Your reference number. Find out more here String
countryCode
required
ISO 3166-1 alpha-2 country code where the transaction is processed.
This is required for merchants based in European Economic Area (EEA) countries.
Mandatory for payment-button v2 and v3 libraries.
String
createAlias
Whether to create an alias.
The alias will be sent on the webhook and is also returned through a status call.
Boolean
authenticationOnly Only authenticate the transaction (used for deferred authorization) Boolean
mcp Configuration of Multi-Currency Pricing. Find out more here. Object
amount Object
value
Amount the customer will pay in their currency.
If you want to include fractional monetary units (pennies, cents, Rappen), you will have to separate the amount using a decimal point (e.g. 5.10 instead of 510).
String
currency ISO 4217 currency code of the amount the customer will pay. String
provider Identify the MCP provider used. String
reasonIndicator If received from acquirer the reason indicator can be set String
conversionRate
Rate used to calculate MCP amount.
Note: Only used in dynamic MCP
String | Number
transactionDate
Date when the rate was fetched from the MCP provider.
Note: Only used in dynamic MCP
String
retrievalReferenceNumber
Reference number of the MCP provider response.
Note: Only used in dynamic MCP
String