HomePaymentsNo-codeDevelopersContribute
Safepay Home PageLive DashboardTest Dashboard

Express Checkout integration guide

Start accepting payments on your website or app.


How it works

When the shopper goes to checkout on your website, you redirect them to a Hosted Checkout page. After they make the payment, the shopper gets redirected to your page, and you show the shopper the outcome of the payment session.

You make two API requests to our server:

  1. When the shopper goes to your checkout and clicks on the "Payment" button, make a request to create a payment tracker.
  2. After you have created the tracker, make a request to create an authentication token.
  3. After the shopper pays and are redirected back to your store, poll an endpoint on your server until you receive confirmation from Safepay through a webhook that the payment has been completed.

Before you begin

Before you begin to integrate, make sure you have followed the Get started with Safepay guide to:

  • Get an overview of the steps needed to accept live payments.
  • Create your test account.

After you have created your test account:

  • Get your API key.
  • Get your client key.
  • Set up webhooks to know the payment outcome.

1

Install an API library

Requirements

  • Node.js version 18 or later.

Installation

You can use npm or yarn

Terminal
# Install the API librarynpm install --save @sfpy/node-core

2

Create a payment session

When the shopper goes to checkout, for example by selecting a Checkout button, make a POST /order/payments/v3/ request from your server, including:

ParameterRequiredDescription
merchant_api_keyYesThe public API key for your account. Your test and live accounts will have different API Keys.
intentYesThe payment channel you wish your shopper to make the payment through. Acceptable values are CYBERSOURCE and MPGS.
modeYesThis defines the payment flow you want your customer to go through. Refer to the section on modes for more details on what each mode can do.
currencyYesThis is the currency to charge your customer in. Refer to the section on supported currencies to understand what currencies Safepay currently supports.
amountYesThis is the amount, in lowest denomination, to charge your customer. For instance, if you wish to charge $100, the currency will be USD and the amount will be 10000.

For example:

Create a payment session in NodeJS
const safepay = require('@sfpy/node-core')('SAFEPAY_SECRET_KEY', {
  authType: 'secret',
  host: 'https://sandbox.api.getsafepay.com' // for live payments use https://api.getsafepay.com
});

try {
  const response = safepay.payments.session.setup({
    "merchant_api_key": "SAFEPAY_API_KEY",
    "intent": "CYBERSOURCE",
    "mode": "payment",
    "currency": "USD",
    "amount": 10000
  })
} catch (err) {
  console.log(error);
}

The /order/payments/v3/ response will give you the payment session you have just created. The response will have the token for the tracker object which is the payment session. This will be required when generating the Checkout URL.

For example:

Payment session response
{
  "data": {
    "tracker": {
      "token": "track_4f7d7e2d-ee05-44e3-81b7-6f6f2cca9727",
      "client": "sec_a8e0a889-b987-4b7e-9757-a6bf28e4d4e2",
      "environment": "sandbox",
      "state": "TRACKER_STARTED",
      "intent": "CYBERSOURCE",
      "mode": "payment",
      "next_actions": {
        "CYBERSOURCE": {
          "kind": "GENERATE_CAPTURE_CONTEXT"
        }
      },
      "purchase_totals": {
        "quote_amount": {
          "currency": "USD",
          "amount": 10000
        },
        "base_amount": {
          "currency": "PKR",
          "amount": 27800
        },
        "conversion_rate": {
          "base_currency": "PKR",
          "quote_currency": "USD",
          "rate": 278
        }
      }
    },
    "capabilities": {
      "CYBERSOURCE": true
    }
  },
  "status": {
    "errors": [],
    "message": "success"
  }
}

3

Create an authentication token

After creating the payment session, make a POST request to /client/passport/v1/token to generate a short lived authentication token.

For example:

Create an authentication token in NodeJS
const safepay = require('@sfpy/node-core')('SAFEPAY_SECRET_KEY', {
  authType: 'secret',
  host: 'https://sandbox.api.getsafepay.com' // for live payments use https://api.getsafepay.com
});

try {
  const response = safepay.auth.passport.create()
} catch (err) {
  console.log(error);
}

The /client/passport/v1/token response will give you the authentication token you will need to generate the Checkout URL.

For example:

Authentication token response
{
  "data": "xnTyRgITVcHlyeKT2cf59_e836PouieQ6xPpuQiwFXD8M6HoJ283EP_zta2SKkm6B_IFNGEBmg=="
}

4

Generate the Checkout URL

Now that the payment session and authentication token have been generated, your server will have to generate the URL for the Checkout page in order for you to redirect your shopper to complete their payment.

You can use our SDKs to generate the URL for you. For example:

Generate a Safepay Checkout URL in NodeJS
const safepay = require('@sfpy/node-core')('SAFEPAY_SECRET_KEY', {
  authType: 'secret',
  host: 'https://sandbox.api.getsafepay.com' // for live payments use https://api.getsafepay.com
});

try {
  const checkoutURL = safepay.checkouts.payment.create({
    // this the tracker.token from the payment session response
    tracker: "TRACKER_TOKEN",
    // the authentication token
    tbt: "AUTHENTICATION_TOKEN",
    // either production or sandbox
    environment: "sandbox",
    // attach a source, like your website domain or mobile app bundle identifier (or "mobile" for mobile webview)
    source: "mywebsite.com",
    // the success URL to redirect your customer to after payment complete
    redirect_url: "https://mywebsite.com/order/success",
    // the cancel URL to redirect your customer to after the customer cancels the checkout session
    cancel_url: "https://mywebsite.com/order/cancel"
  })
} catch (err) {
  console.log(error);
}

Redirect the shopper to the URL (url) generated above. The shopper pays on the Hosted Checkout page.

5

Get the outcome

1. Show the result of the payment session

  1. After the shopper makes the payment, they are redirected back to your website.

  2. Get the tracker that is appended to the return URL from the Hosted Checkout page. Use it to get the outcome of the payment session.

    Redirect URL for a successful
    https://your-company.com/order/complete?tracker=track_4f7d7e2d-ee05-44e3-81b7-6f6f2cca9727
    
  3. Make a GET /reporter/api/v1/payments/{tracker} request including the tracker token. For example:

Fetch a tracker to get the status in NodeJS
const safepay = require('@sfpy/node-core')('SAFEPAY_SECRET_KEY', {
  authType: 'secret',
  host: 'https://sandbox.api.getsafepay.com' // for live payments use https://api.getsafepay.com
});

    try {
      const response = safepay.reporter.payments.fetch(trackerToken)
    } catch (err) {
      console.log(error);
    }

The response includes the current status of the payment. Search for the tracker.state property. If it shows TRACKER_ENDED, the payment has been successfully completed. For example:

Fetch payment session response
{
  "data": {
    "tracker": {
      "token": "track_1a46dc70-91f9-4892-8e25-0bb5205b69c7",
      "client": "sec_edeade52-46aa-483b-b87d-009d3ce41554",
      "environment": "sandbox",
      "state": "TRACKER_ENDED",
      "intent": "CYBERSOURCE",
      "mode": "payment",
      "next_actions": {
        "CYBERSOURCE": {
          "kind": "NOOP",
          "request_id": "req_9ea53be5-828f-4730-b187-9d3d350ab72a"
        }
      },
      "purchase_totals": {
        "quote_amount": {
          "currency": "PKR",
          "amount": 600000
        },
        "base_amount": {
          "currency": "PKR",
          "amount": 600000
        },
        "conversion_rate": {
          "base_currency": "PKR",
          "quote_currency": "PKR",
          "rate": 1
        }
      }
    },
    "action": {
      "token": "req_9ea53be5-828f-4730-b187-9d3d350ab72a",
      "payment_method": {
        "token": "card_353d4bd9-1d72-4f4d-a358-b277833795a4",
        "expiration_month": "12",
        "expiration_year": "2023",
        "card_type_code": "001",
        "card_type": "Visa",
        "bin_number": "445653",
        "last_four": "1096"
      }
    }
  },
  "status": {
    "errors": [],
    "message": "success"
  }
}

2. Update your order management system

You get the outcome of each payment asynchronously, in a payment.succeeded webhook. Use the tracker from the webhook to match it to the tracker you generated in the payment session step. For a successful payment, the event contains success: true.

Refer to Webhooks for more information on how to get set up to receive events from Safepay.

Mobile

For mobile, you may render the checkout session in a WebView. Note that when generating the checkout URL using the backend SDKs, you will need to set the source as mobile.

To mimic redirection, you can observe changes to the checkout URL and close the Webview on success or cancellation. The paths to observe are as follows:

ScenarioURL pathExample
Success/external/completehttps://sandbox.api.getsafepay.com/embedded/external/complete?environment=sandbox&...
Cancellation/external/errorhttps://sandbox.api.getsafepay.com/embedded/external/error?environment=sandbox&...

Payment errors and retries

If the payment encounters an error, the shopper can retry the payment on the Hosted Checkout page. You receive a webhook for each payment attempt. So, you can receive more than one webhook with the same tracker.

Expiration

The Hosted Checkout page expires after the authentication token expires. A single authentication token lasts for 1 hour. In case the authentication token expires, we prompt the shopper to return back to your store and restart the checkout process.

Test and go live

Before going live, use our list of test cards and other payment methods to test your integration. We recommend testing each payment method that you intend to offer to your shoppers.

You can check the test payments in your Dashboard, under the Payments > Payments 2.0 tab.

When you are ready to go live, you need to:

  1. Apply for a live account.
  2. Submit a request to add payment methods on your live account.
  3. Switch from test to our live endpoints.