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 using the Status API or Webhooks.

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:

  • Sandbox Setup
  • Staging x-api-key
  • Merchant account
  • One of the supported languages:
    • PHP 8+
    • Node.js 18+
    • Python 3.10+
    • Java 17+
🚧

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.

Always verify payment using:

  • Transaction Status API
  • Webhooks

Architecture

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

  Browser->>Merchant: Checkout
  Merchant->>API: Create Payment Link
  API-->>Merchant: redirectUrl
  Merchant->>Browser: HTTP Redirect
  Browser->>Checkout: Complete Payment
  Checkout->>Browser: successUrl / failureUrl
  Browser->>Merchant: Return
  Merchant->>API: Query Status
  API-->>Merchant: Transaction Status

Integration Steps

Step 1 — Configuration

<?php

return [
    'environment' => getenv('DIGETPAY_ENV') ?: 'staging',
    'api_key' => getenv('DIGETPAY_API_KEY') ?: '',
    'environments' => [
        'staging' => [
            'api_base' => 'https://fin-api.digetpay.com/v1',
        ],
        'production' => [
            'api_base' => 'https://api.digetpay.com/v1',
        ],
    ],
];
.env
DIGETPAY_ENV=staging
DIGETPAY_API_KEY=your_fin_staging_key

Step 2 — API Client

$client = new DigetPayClient($config);

The client should expose:

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

Step 3 — Create Payment Link & Redirect

Create a payment session, save the returned session ID, then redirect the customer to the DigetPay Hosted Checkout page.

<?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 using either:

  • Transaction Status API
  • Webhooks
<?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, or
  • A valid Webhook notification.

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 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

Although the Status API confirms payments, Webhooks are the recommended way to receive payment events automatically.

DigetPay sends HTTP POST requests to your configured webhook endpoint whenever a payment status changes.

<?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;

// Verify signature

// Check idempotency

// Update order

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.
  3. Verify payment using the Status API.
  4. Receive the Webhook.
  5. Update your database.
  6. Complete order fulfillment.

This approach provides the highest reliability in production.


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

Staging vs Production

SettingStagingProduction
Environmentstagingproduction
API Base URLhttps://fin-api.digetpay.com/v1https://api.digetpay.com/v1
Dashboardhttps://fin-admin.digetpay.comhttps://admin.digetpay.com
API KeyFin Staging KeyProduction API Key

Before switching to production, replace both the API Base URL and your API Key.


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.

Production Checklist

Before requesting Production access, verify the following:

  • ✅ Production API Key received.
  • ✅ Success URL configured.
  • ✅ Failure URL configured.
  • ✅ Webhook URL configured.
  • ✅ Status API implemented.
  • ✅ Webhook processing implemented.
  • ✅ Duplicate webhook protection implemented.
  • ✅ Refund flow tested.
  • ✅ End-to-end payment tested.
  • ✅ Error handling completed.
  • ✅ Logging enabled.

Ready for Production

Once every item above has been completed successfully, your Hosted Checkout integration is ready for Production.


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]

E-->G[Verify Payment]

F-->G

G-->H[Mark Order Paid]

H-->I[Future Refunds]

Related Documentation



Did this page help you?