---
url: https://simplepay.webmania.cc/en/recipes/express.md
description: >-
  SimplePay integration in an Express app: start a payment, handle the back URL,
  receive the IPN with the express.text() raw body. Full example code.
---

# Express

The package ships as an ES module only, so the project needs `"type": "module"` (or `.mjs` files).

## Full example

```ts [server.ts]
import express from 'express'
import { startPayment, getPaymentResponse, handleIpnRequest } from 'simplepay-js-sdk'

const app = express()

// Start a payment – the JSON parser is fine here
app.post('/api/simplepay/start', express.json(), async (req, res) => {
  const order = await getOrder(req.body.orderId)
  try {
    const { paymentUrl } = await startPayment({
      orderRef: order.ref,
      total: order.total,
      customerEmail: order.email,
    })
    res.json({ paymentUrl })
  } catch (error) {
    console.error('SimplePay start', error)
    res.status(502).json({ error: 'Could not start the payment' })
  }
})

// Back URL (SIMPLEPAY_REDIRECT_URL = https://example.com/payment/back)
app.get('/payment/back', (req, res) => {
  try {
    const result = getPaymentResponse(String(req.query.r ?? ''), String(req.query.s ?? ''))
    res.render('payment-result', result) // event: SUCCESS | FAIL | TIMEOUT | CANCEL
  } catch {
    res.status(400).send('Invalid redirect')
  }
})

// IPN – needs RAW text, NOT express.json()
app.post('/api/simplepay/ipn', express.text({ type: 'application/json' }), async (req, res) => {
  const ipnBody = typeof req.body === 'string' ? req.body : ''
  const incomingSignature = req.get('Signature') ?? ''

  let result
  try {
    result = handleIpnRequest(ipnBody, incomingSignature, process.env.SIMPLEPAY_MERCHANT_KEY_HUF!)
  } catch (error) {
    console.error('SimplePay IPN', error)
    res.status(400).send('Invalid signature')
    return
  }

  const ipn = JSON.parse(ipnBody) as { orderRef: string, transactionId: number, status: string }
  if (ipn.status === 'FINISHED') {
    await markOrderPaid(ipn.orderRef, ipn.transactionId) // make it idempotent
  }

  res
    .status(200)
    .set('Signature', result.signature)
    .type('application/json')
    .send(result.responseBody) // as a string, unchanged
})

app.listen(3000)
```

## IPN

::: danger A global `app.use(express.json())`
If the app has a global `app.use(express.json())` at the top, it **consumes** the IPN body before `express.text()` sees it, and `req.body` is already an object. Either register the IPN route **before** the global parser, or keep it out of the parser:

```ts
app.post('/api/simplepay/ipn', express.text({ type: 'application/json' }), ipnHandler)
app.use(express.json()) // now only runs for the other routes
```

:::

* `res.json(result.responseBody)` is **wrong**: it wraps the text in quotes.
* `res.send(JSON.parse(...))` is **wrong**: it serializes again.
* Correct: `res.type('application/json').send(result.responseBody)`.

Several currencies: [IPN – Multiple currencies](../guide/ipn#multiple-currencies).
