Skip to main content

Apple Pay

Apple Pay integration allows customers using Apple devices (iPhone, iPad, Mac with Touch ID/Face ID) to pay quickly using cards stored in Apple Wallet.

Flow diagram

Step 1: Register the payment

Register the transaction using the standard method:

curl -X POST https://api-payments.dpay.pl/api/v1_0/payments/register \
-H "Content-Type: application/json" \
-d '{
"transactionType": "transfers",
"service": "abc123",
"value": "79.99",
"url_success": "https://myshop.com/success",
"url_fail": "https://myshop.com/error",
"url_ipn": "https://myshop.com/api/ipn",
"checksum": "..."
}'

Save the transactionId from the response.

Step 2: Initialize the Apple Pay session

Before displaying the Apple Pay button, you need to initialize a session. Send a request to dpay.pl with the parameter xPayType: APPLE_PAY_INIT:

POST https://api-payments.dpay.pl/api/v1_0/cards/payment/{transactionId}/pay/apple-pay
Content-Type: application/json

Session initialization request

{
"xPayType": "APPLE_PAY_INIT",
"validationUrl": "https://apple-pay-gateway.apple.com/paymentservices/paymentSession"
}

Response

{
"error": false,
"session": {
"epochTimestamp": 1700000000,
"expiresAt": 1700003600,
"merchantSessionIdentifier": "SSH...",
"nonce": "abc123",
"merchantIdentifier": "merchant.pl.dpay",
"domainName": "myshop.com",
"displayName": "dpay.pl",
"signature": "..."
}
}

Step 3: Configure the Apple Pay button

<!-- Check Apple Pay availability -->
<div id="apple-pay-button" style="display: none;"></div>

<style>
#apple-pay-button {
-webkit-appearance: -apple-pay-button;
-apple-pay-button-type: pay;
-apple-pay-button-style: black;
width: 100%;
height: 48px;
cursor: pointer;
}
</style>

JavaScript - full integration

// Check if Apple Pay is available
if (window.ApplePaySession && ApplePaySession.canMakePayments()) {
document.getElementById('apple-pay-button').style.display = 'block';
}

document.getElementById('apple-pay-button').addEventListener('click', async () => {
const transactionId = '...'; // From Step 1

// Payment request configuration
const paymentRequest = {
countryCode: 'PL',
currencyCode: 'PLN',
supportedNetworks: ['visa', 'masterCard'],
merchantCapabilities: ['supports3DS'],
total: {
label: 'My Shop',
amount: '79.99',
},
};

const session = new ApplePaySession(3, paymentRequest);

// Session validation - required by Apple
session.onvalidatemerchant = async (event) => {
try {
// Send validationURL to your server
const response = await fetch('/api/pay/apple-pay/session', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
transactionId,
validationUrl: event.validationURL,
}),
});

const merchantSession = await response.json();
session.completeMerchantValidation(merchantSession.session);
} catch (error) {
session.abort();
console.error('Session validation error:', error);
}
};

// Handle payment authorization
session.onpaymentauthorized = async (event) => {
try {
const response = await fetch('/api/pay/apple-pay/process', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
transactionId,
paymentToken: event.payment.token,
}),
});

const result = await response.json();

if (!result.error) {
session.completePayment(ApplePaySession.STATUS_SUCCESS);
window.location.href = '/success';
} else {
session.completePayment(ApplePaySession.STATUS_FAILURE);
}
} catch (error) {
session.completePayment(ApplePaySession.STATUS_FAILURE);
}
};

session.oncancel = () => {
console.log('Customer cancelled Apple Pay payment');
};

// Start the session
session.begin();
});

Server - endpoint handling

Session initialization (APPLE_PAY_INIT)

app.post('/api/pay/apple-pay/session', async (req, res) => {
const { transactionId, validationUrl } = req.body;

const response = await axios.post(
`https://api-payments.dpay.pl/api/v1_0/cards/payment/${transactionId}/pay/apple-pay`,
{
xPayType: 'APPLE_PAY_INIT',
validationUrl,
}
);

res.json(response.data);
});

Payment processing (APPLE_PAY)

app.post('/api/pay/apple-pay/process', async (req, res) => {
const { transactionId, paymentToken } = req.body;

const deviceInfo = {
browserJavaEnabled: false,
browserLanguage: 'pl-PL',
browserColorDepth: '24',
browserScreenHeight: '1080',
browserScreenWidth: '1920',
browserTZ: '-60',
browserUserAgent: req.headers['user-agent'],
browserAcceptHeader: req.headers['accept'] || 'text/html',
browserJavascriptEnabled: true,
};

const response = await axios.post(
`https://api-payments.dpay.pl/api/v1_0/cards/payment/${transactionId}/pay/apple-pay`,
{
xPayType: 'APPLE_PAY',
paymentToken,
deviceInfo,
}
);

res.json(response.data);
});

API responses

Success

{
"error": false,
"status": "paid",
"transactionId": "abc-def-123-456"
}

3D Secure required

{
"error": false,
"status": "3DS_REQUIRED",
"redirectType": "FORM",
"redirectUrl": "https://acs-bank.example.com/3ds",
"redirectParams": { ... }
}

Requirements

Apple Pay configuration
  • Your domain must be verified with Apple - dpay.pl handles domain validation as part of the APPLE_PAY_INIT process
  • Your website must be accessible via HTTPS
  • Apple Pay works exclusively on Apple devices with Safari (or Chrome on iOS 16+)
  • It is not possible to test Apple Pay on Android or Windows devices