Hosted Checkout Code Examples

Integrate DigetPay Hosted Checkout using your preferred programming language.

Your server creates a payment link, redirects the customer to the DigetPay Checkout page, then confirms the payment with the Status API. Webhooks notify you when a payment changes so you know when to check.

Apple Pay, Mada, 3-D Secure, and PCI compliance are fully handled by the DigetPay Hosted Checkout page. Your application only creates the payment session and redirects the customer.

📘

Prerequisites

Before you begin, make sure you have completed:

  • A merchant account with access to the Merchant Dashboard (pay.digetpay.com)
  • Your Test API Key (x-api-key). See API Modes & Credentials
  • One of the supported languages:
    • PHP 8+
    • Node.js 18+
    • Python 3.10+
    • Java 17+

The API base URL is https://api.digetpay.com/v1 for both Test and Live. Your API key decides the mode.

🚧

Important

The Hosted Checkout endpoint is:

POST /payment/checkout/intiate

The path is intentionally spelled intiate and should be used exactly as documented.

❗️

Never trust the Success URL

A customer arriving at your successUrl does not guarantee payment success, and a webhook is only a notification.

Always verify payment with the Transaction Status API.


Architecture

sequenceDiagram
  autonumber

  participant Browser as Customer Browser
  participant Merchant as Your Server
  participant API as DigetPay API
  participant Checkout as DigetPay Checkout

  Browser->>Merchant: Start checkout
  Merchant->>API: Create Payment Link
  API-->>Merchant: sessionId + redirectUrl

  Merchant->>Browser: HTTP Redirect
  Browser->>Checkout: Complete Payment

  Checkout->>Browser: successUrl / failureUrl
  Browser->>Merchant: Return with sessionId

  Merchant->>API: Query Status using sessionId
  API-->>Merchant: Transaction Status

  opt Webhook configured
    API->>Merchant: Payment notification
  end

  Merchant->>Merchant: Verify payment and update order

Integration Steps

Step 1 — Configuration

Use one API base URL and load the API key from the environment. Use your Test API Key for development and testing, and your Live API Key only in your live deployment.

<?php

return [
    'api_base' => 'https://api.digetpay.com/v1',
    // Test API Key in development/QA; Live API Key in your live deployment.
    'api_key' => getenv('DIGETPAY_API_KEY') ?: '',
];
.env
DIGETPAY_API_KEY=your_test_api_key

Step 2 — API Client

$client = new DigetPayClient($config);

The client should expose:

  • createPaymentLink()
  • getTransactionStatus()
  • processRefund()
🔑

Status API Identifier

For Hosted Checkout, use the stored session ID when querying the transaction status.

The Status API endpoint is:

GET /payment/checkout/status?sessionId=${sessionId}

The response contains the Gateway Transaction ID, which should be stored in your database for future operations such as refunds.


Step 3 — Create Payment Link & Redirect

Create a payment session, save the returned session ID, then redirect the customer to the redirectUrl returned by the API. Use it exactly as returned; do not build the checkout URL yourself.

<?php

require_once __DIR__ . '/../src/DigetPayClient.php';

$config = require __DIR__ . '/../config/digetpay.php';

$client = new DigetPayClient($config);

$orderId = 'ORD-' . time();

$session = $client->createPaymentLink([
    'merchantOrderId' => $orderId,
    'amount' => 10.00,
    'currency' => 'SAR',
    'customerName' => 'Ahmed Ali',
    'customerEmail' => '[email protected]',
    'customerPhone' => '501223324',
    'successUrl' => 'https://yourstore.com/checkout/success',
    'failureUrl' => 'https://yourstore.com/checkout/failure'
]);

// Save session ID before redirecting
// saveSession($orderId, $session['id']);

header('Location: ' . $session['redirectUrl']);
exit;

Apple Pay

No additional code is required.

Apple Pay automatically appears on the DigetPay Hosted Checkout page when:

  • The customer's device supports Apple Pay.
  • Safari is being used.
  • customerPhone is included in the request.

Step 4 — Handle Return URL

After the customer completes payment, DigetPay redirects the browser back to your application.

Important: Returning to the Success URL does not confirm payment. Always verify the transaction with the Transaction Status API.

<?php

$sessionId = $_GET["sessionId"] ?? null;

if (!$sessionId) {

    http_response_code(400);

    exit("Missing sessionId");

}

// Retrieve your order using sessionId

// Call Status API

echo "Processing payment...";
❗️

Never Trust the Redirect

A customer reaching the Success URL only means they returned from the checkout page.

The payment might still be:

  • Pending
  • Failed
  • Cancelled
  • Expired

Only mark the order as paid after a successful Status API response.


Step 5 — Confirm Payment

After the customer returns to your website, verify the payment using the Transaction Status API. Do not rely on the redirect URL or a webhook alone.

<?php

require_once __DIR__ . '/../src/DigetPayClient.php';

$config = require __DIR__ . '/../config/digetpay.php';

$client = new DigetPayClient($config);

// Retrieve the session ID from your database
$sessionId = 'stored-session-id';

$response = $client->getTransactionStatus($sessionId);

$content = $response['data']['content'][0] ?? null;

if ($content && $content['transactionStatus'] === 'SUCCESS') {

    $gatewayTransactionId = $content['id'];

    // Mark order as paid
    // Save gatewayTransactionId for refunds

} elseif ($content && $content['transactionStatus'] === 'PENDING') {

    // Payment is still processing

} else {

    // Payment failed

}

Successful Payment

When the payment status is SUCCESS:

  • Update the order status.
  • Store the Gateway Transaction ID.
  • Send the customer confirmation.
  • Trigger fulfillment.

The Gateway Transaction ID is required later for refunds.


Step 6 — Receive Webhooks

Webhooks are the recommended way to be notified automatically when a payment status changes. A webhook is a notification, not proof of payment: after you receive one, confirm the payment with the Status API before updating the order.

DigetPay sends HTTP POST requests to your configured webhook endpoint whenever a payment status changes. Test Mode and Live Mode use separate webhook configurations.

<?php

$raw = file_get_contents("php://input");

$payload = json_decode($raw,true);

http_response_code(200);

echo "OK";

$transactionId = $payload["transactionId"] ?? null;

$orderId = $payload["orderId"] ?? null;

$status = $payload["status"] ?? null;

// Check idempotency

// Do not trust the payload alone:
// confirm the payment with the Status API

// Update order only after the Status API confirms SUCCESS

Webhook Best Practices

🚧

Implement Idempotency

DigetPay may retry webhook delivery.

Always deduplicate events using:

  • transactionId
  • type

before updating your database.

❗️

Respond Immediately

Return HTTP 200 OK within a few seconds.

Heavy processing such as:

  • Sending emails
  • Updating inventory
  • ERP synchronization
  • Invoice generation

should happen asynchronously.

Recommended Payment Flow

  1. Customer completes payment.
  2. Customer returns to your Success URL, and DigetPay sends a webhook notification.
  3. Verify payment using the Status API.
  4. Update your database (idempotently).
  5. Complete order fulfillment.

This approach provides the highest reliability in Live Mode.


Step 7 — Process Refund

After a successful payment, you can issue either a full or partial refund using the Gateway Transaction ID returned by the Status API.

The Gateway Transaction ID is available in the payment confirmation response (data.content[0].id). Store this value in your database for future refund requests.

<?php

require_once __DIR__ . '/../src/DigetPayClient.php';

$config = require __DIR__ . '/../config/digetpay.php';

$client = new DigetPayClient($config);

$gatewayTransactionId = "2232e99b-0257-47d5-bbfd-022c8951767f";

$amount = 5.00;

$result = $client->processRefund(
    $gatewayTransactionId,
    $amount
);

// Update your order status

Test Mode vs Live Mode

SettingTest ModeLive Mode
API Base URLhttps://api.digetpay.com/v1https://api.digetpay.com/v1
Dashboardhttps://pay.digetpay.com/https://pay.digetpay.com/
API KeyTest API KeyLive API Key
WebhooksTest Webhook ConfigurationLive Webhook Configuration

To go live, switch your API Key and webhook configuration. The API Base URL does not change.


What DigetPay Handles

One of the main advantages of Hosted Checkout is that DigetPay manages all payment UI and PCI-sensitive functionality for you.

FeatureHandled By
Payment FormDigetPay Checkout
Card ValidationDigetPay Checkout
Apple PayDigetPay Checkout
MadaDigetPay Checkout
3-D Secure AuthenticationDigetPay Checkout
PCI DSS ComplianceDigetPay Infrastructure
Card Data StorageDigetPay Infrastructure

Your application only needs to:

  • Create the payment link.
  • Redirect the customer.
  • Verify payment.
  • Handle webhooks.
  • Process refunds.

Go-Live Checklist

Before requesting go-live approval, verify the following:

  • ✅ End-to-end payment tested in Test Mode with your Test API Key and Test Cards.
  • ✅ Success URL configured.
  • ✅ Failure URL configured.
  • ✅ Test and Live webhook URLs configured separately.
  • ✅ Status API implemented.
  • ✅ Webhook processing implemented.
  • ✅ Duplicate webhook protection implemented.
  • ✅ Refund flow tested.
  • ✅ Error handling completed.
  • ✅ Logging enabled.
  • ✅ Live API Key received.

Ready to Go Live

Once every item above has been completed successfully, your Hosted Checkout integration is ready to switch to your Live API Key.


Integration Flow Summary

flowchart LR

A[Create Payment Link]
-->B[Redirect Customer]

B-->C[Customer Pays]

C-->D[Return to Success URL]

D-->E[Query Status API]

C-->F[Receive Webhook]

F-->E

E-->G[Verify Payment]

G-->H[Mark Order Paid]

H-->I[Future Refunds]

Related Documentation


Did this page help you?