Skip to Content
Docs

Create your first payment

Process a test card payment in the sandbox using a payment session and Pay.com.js.

This quickstart walks through one successful card payment in the sandbox. Your server creates a payment session, your page renders Universal checkout with Pay.com.js, you pay with a test card, and your server confirms the result. No real money moves.

This quickstart covers the happy path. For server-to-server charges, error handling, and 3D Secure, see Create a charge.

Prerequisites

Before you start, make sure you have:

Process a test payment

Follow these five steps to take a card payment in the sandbox.

  1. 1

    Create a payment session (server-side)

    From your backend, create a payment session with POST /v1/sessions/payment. The session holds the amount and currency, and returns a client_secret that your page passes to Pay.com.js.

    • amount is in minor units, the smallest unit of the currency. For example, 1000 is $10.00.
    • currency is a lowercase ISO 4217 code, such as usd.
    • customer_reference_id is required. Pass your own identifier for the customer, and Pay.com creates or reuses a customer with that reference.
    • payment_method_types: ["card"] limits this walkthrough to cards. Omit it to offer every payment method enabled on your account.
    curl -X POST https://api.sandbox.pay.com/v1/sessions/payment \
      -u ':sk-pay-com-sandbox-…' \
      -H "Content-Type: application/json" \
      -H "idempotency-key: $(uuidgen)" \
      -d '{
        "amount": 1000,
        "currency": "usd",
        "customer_reference_id": "cust-quickstart-001",
        "billing_details": {
          "email": "test@example.com"
        },
        "payment_method_types": ["card"],
        "payment_method_options": {
          "card": {
            "duplicate_card_check": false
          }
        }
      }'

    Return client_secret to your page. Keep your API key on the server and never send it to the browser. The client_secret is returned only when you create the session and expires after the session's duration (60 minutes by default). If you lose it, refresh the session to get a new one.

    duplicate_card_check: false stops the sandbox from declining a test card number that was already used (decline_code: duplicate_card). Use it only in the sandbox. In live, keep duplicate card detection enabled so the same card can't be used across multiple customers.

  2. 2

    Load and initialize Pay.com.js (client-side)

    Load Pay.com.js and initialize it with your merchant ID. Both options below work in the sandbox. The npm package loads the script asynchronously (await Pay.com(...)), while the script tag exposes window.Pay.com(...) synchronously once loaded.

    npm install @pay-com/js
    import Pay from '@pay-com/js'
    
    // In live, add `live: true`.
    const pay = await Pay.com({ identifier: 'YOUR_MERCHANT_ID' })

    identifier is your Pay.com merchant ID, not the payment session ID. The session is identified by the client_secret you pass in the next step. See Install and initialize for more installation options.

  3. 3

    Render the checkout

    Add a container to your page:

    <div id="payment-element"></div>

    Create a checkout with the client_secret from step 1, then render the Universal checkout form into the container:

    const checkout = pay.checkout({
      clientSecret: 'CLIENT_SECRET_FROM_STEP_1', // fetched from your server
      onSuccess: (payment) => {
        // payment.transaction is the charge ID, for example chrg_686511195653406720
        console.log('Payment succeeded', payment.transaction, payment.status)
      },
      onPending: (payment) => {
        // The result isn't final yet. Wait for the webhook in step 5.
        console.log('Payment pending', payment.transaction)
      },
      onFailure: (error) => {
        console.log('Payment failed', error.message)
      },
    })
    
    checkout.universal({ container: '#payment-element' })

    The Universal checkout form collects card details and handles 3D Secure when the issuer requires it. When an attempt is declined, onFailure fires so you can display error.message to the customer. To have the form show the message instead, pass toggles: { displayFailureMessages: true } to pay.checkout(). Browser callbacks are for updating your page only. Confirm every payment on your server (step 5) before fulfilling the order.

  4. 4

    Pay with a test card

    Enter the test card 4242 4242 4242 4242 with any future expiration date (for example, 12/28) and a 3-digit CVV such as 123, then submit the form. onSuccess fires with the new charge.

    To try a decline, use 4000 0053 5708 6322 (insufficient funds). onFailure fires, and the customer can try again in the same form, up to the session's max_attempts (5 by default). See test cards for more decline and 3DS scenarios.

  5. 5

    Confirm the payment on your server

    Use either of these to confirm the result server-side:

    • Webhook (recommended): Subscribe to charge.succeeded and charge.failed, or to payment_session.completed and payment_session.failed. See webhooks, and verify the signature of every event before you act on it.
    • API: Retrieve the charge with the charge ID from onSuccess (payment.transaction). You can also retrieve the session. Its charge field holds the charge ID once a payment succeeds.
    curl https://api.sandbox.pay.com/v1/charges/chrg_686511195653406720 \
      -u ':sk-pay-com-sandbox-…'

    status: "succeeded" means the charge was approved and the funds captured. paid stays false until the funds are settled to your bank account later. The charge also appears in your Pay.com dashboard under the sandbox.

Try the API directly (server-to-server)

If you only want to see a charge in the sandbox from the command line, you can create a charge with POST /v1/charges and a test card, with no session or Pay.com.js.

Sending raw card numbers to the API is the server-to-server integration path. In live it requires full PCI DSS compliance (SAQ-D). Use the Pay.com.js flow above unless you're PCI certified. See Find your use case.

curl -X POST https://api.sandbox.pay.com/v1/charges \
  -u ':sk-pay-com-sandbox-…' \
  -H "Content-Type: application/json" \
  -H "idempotency-key: $(uuidgen)" \
  -d '{
    "amount": 1000,
    "currency": "usd",
    "customer_reference_id": "cust-quickstart-001",
    "source_data": {
      "type": "card",
      "card": {
        "number": "4242424242424242",
        "expiry_month": "12",
        "expiry_year": "2028",
        "cvv": "123",
        "name": "Test User"
      }
    },
    "payment_method_options": {
      "card": {
        "duplicate_card_check": false
      }
    }
  }'

A 201 response means the charge was created, not that it succeeded. Always check status, which can also be pending or requires_authentication. A declined charge returns HTTP 400 with error.type: "payment_method_error", error.code: "card_declined", and a decline_code. See decline codes.

Go live

When Pay.com has turned on live for your account, change three things:

  • Call https://api.pay.com/v1 with your sk-pay-com-live- API key.
  • Create your sessions with the live API. Pay.com.js works in the environment of the session you give it. With @pay-com/js, also pass live: true to Pay.com().
  • Remove duplicate_card_check: false.

See Go live for everything that changes in live and a checklist.

Next steps

Extend this payment flow with the full charge guide, webhooks, holds, and more testing.

Was this page helpful?