Skip to Content
Docs

Add to checkout

Integrate Trustly payments using Pay.com.js or the REST API.

You can accept Trustly payments in three ways:

  • Universal checkout: Pay.com.js displays Trustly in its payment form.
  • Headless checkout: You build your own checkout UI, and Pay.com.js starts the Trustly payment when the customer selects it.
  • Server-to-server: You create the charge from your server and redirect the customer to Trustly.

Before you start, ask your account manager to enable Trustly for your account, then turn it on in the Pay.com dashboard.

Universal checkout

Universal checkout displays Trustly in its payment form when the payment session includes it.

  1. 1

    Create a payment session

    On your server, create a payment session with POST /v1/sessions/payment and include "trustly" in payment_method_types.

    {
      "amount": 5000,
      "currency": "gbp",
      "customer_reference_id": "cust-001",
      "payment_method_types": ["card", "trustly"]
    }

    Amounts are in the currency's minor unit, so 5000 is £50.00. customer_reference_id is your own ID for the customer, and it's required. Return the session's client_secret to your page.

  2. 2

    Display the checkout

    On your page, initialize Pay.com.js with the client_secret and mount Universal checkout, as in Universal checkout. No method-specific configuration is required.

  3. 3

    Handle the result

    When the customer selects Trustly and clicks Pay, Pay.com.js opens Trustly in a window over your page on desktop, and in a new tab on mobile.

    • In a window over your page: if the customer closes it, Pay.com.js calls onFailure.
    • In a new tab: your page asks the customer to finish there. If they close the tab, Pay.com.js keeps waiting for the payment's outcome.

    When the customer finishes, Pay.com.js calls onSuccess if the payment succeeded, onPending if its result isn't final yet, or onFailure if it failed. Fulfill the order when you receive the charge.succeeded webhook event.

Headless checkout

With Headless checkout, you build your own checkout UI, and Pay.com.js starts the Trustly payment when the customer selects it.

  1. 1

    Create a payment session

    Create the payment session on your server as in Universal checkout, with "trustly" in payment_method_types, and initialize Pay.com.js with its client_secret.

  2. 2

    Check that Trustly is available

    Call checkout.getPaymentMethods(), and show your Trustly button only when the list includes it. Don't use checkout.canMakePayments() for this, because it reports only Apple Pay and Google Pay.

    const methods = await checkout.getPaymentMethods()
    const trustly = methods.find((method) => method.paymentMethodType === 'trustly')
  3. 3

    Start the payment

    When the customer selects Trustly, call checkout.headless() with 'trustly'.

    try {
      await checkout.headless('trustly')
    } catch (error) {
      // onFailure receives the error details too.
    }

    Pay.com.js opens Trustly in a window over your page on desktop, and in a new tab on mobile.

    • In a window over your page: if the customer closes it, Pay.com.js calls onFailure.
    • In a new tab: your page asks the customer to finish there. If they close the tab, Pay.com.js keeps waiting for the payment's outcome.

    When the customer finishes, Pay.com.js calls onSuccess if the payment succeeded, onPending if its result isn't final yet, or onFailure if it failed. Fulfill the order when you receive the charge.succeeded webhook event.

Server-to-server

From your server, a Trustly payment takes three steps: create the charge, create a linked authentication session to obtain the redirect URL, and redirect the customer to Trustly.

  1. 1

    Create the charge

    Send POST /v1/charges with source_data.type set to "trustly". Pay.com passes source_data.billing_details to Trustly to prefill its page for the customer.

    {
      "amount": 5000,
      "currency": "gbp",
      "source_data": {
        "type": "trustly",
        "billing_details": {
          "name": "John Smith",
          "address": {
            "country": "GB"
          }
        }
      },
      "customer_reference_id": "cust-001",
      "reference": "order-12345",
      "description": "Order #12345"
    }

    The charge's status is requires_authentication until the customer approves the payment.

  2. 2

    Create a linked authentication session

    Send POST /v1/sessions/authentication/linked with the charge's id as resource and the return_url to return the customer to. confirm defaults to true, so Pay.com completes the charge when the customer approves it. Each charge supports one linked authentication session.

    {
      "resource": "chrg_702172227948251136",
      "return_url": "https://your-site.com/payment/complete"
    }
  3. 3

    Redirect the customer

    Send the customer to the session's url with a full-page redirect. After they approve or cancel the payment, Trustly redirects them back to your return_url. Fulfill the order when you receive the charge.succeeded webhook event, and handle charge.failed as a canceled, declined, or abandoned payment.

    If the customer doesn't finish, the charge stays requires_authentication until the authentication session expires (60 minutes by default). It then moves to failed, and the charge.failed event fires.

Testing

Test against https://api.sandbox.pay.com/v1 with your sk-pay-com-sandbox- API key. Test transactions never move real funds. See Environments. Trustly must be enabled on your sandbox account before you can test. Contact your account manager if it isn't enabled yet.

Trustly test simulator

In the sandbox, all Trustly payments redirect to the Trustly test simulator at checkout.test.trustly.com. The simulator presents three test banks:

BankFlow
BarclaysFull simulated flow: login, one-time code, account selection, and payment confirmation with a second one-time code. Includes Set order options to choose test scenarios.
HSBC UKSame flow as Barclays: login, one-time code, account selection, and payment confirmation with a second one-time code.
Mock BarclaysSimplified flow through a QR code scan on your phone, which opens a mock bank redirect page at mock.public.sbx.trustly.cloud. Enter any credentials to complete. Includes Set order options.

Barclays and HSBC UK use the same simulated banking flow. This is the closest experience to what your customers see in the live environment.

  1. 1

    Select a bank

    Choose Barclays or HSBC UK from the bank list. You're taken to the Trustly test system page. This page explains that all transactions are fictional and banks are simulated.

    To test specific failure scenarios, check Set order options before clicking Continue. See the test scenarios section for the options.

  2. 2

    Log in

    Enter any value in the Login ID field (for example, 1234) and click Continue.

  3. 3

    Enter the first one-time code

    The simulator displays a one-time code. Enter it in the field below and click Continue.

  4. 4

    Select an account

    Choose Checking account or Savings account and click Continue.

  5. 5

    Confirm the payment

    Review the amount and selected account. Click Continue to proceed.

  6. 6

    Enter the second one-time code

    The simulator displays a second one-time code along with the account number and amount. Enter the code and click Confirm payment.

    The customer is redirected to your return_url.

Test scenarios

When you check Set order options on the Trustly test system page, you can pick one of these test scenarios. They're available for Barclays and Mock Barclays.

OptionDescriptionCharge statusFailure code
No test, process as normal (default)Completes the payment normallysucceededNone
D4. Fail payment before it is confirmedSimulator shows "Signing is unavailable"failedauthentication_abandoned
Customer closes window (click X)Customer abandons the flow before completingfailedauthentication_abandoned

For most integration testing, use the default No test, process as normal option to verify the happy path. Use D4 to test your failure handling.

Verifying the charge after authentication

After the customer completes the test simulator and is redirected to your return_url, retrieve the charge to verify its final state:

curl -X GET "https://api.sandbox.pay.com/v1/charges/chrg_770257246822072320" \
  -u ':sk-pay-com-sandbox-…'

After successful authentication, payment_method_details is populated with Trustly-specific fields, including the customer's bank name, account_number, and clearing_house. The payment_method ID uses the format pm_trustly_....

Was this page helpful?