4 - Payment Processing Example
Frontend Implementation
Processing Apple Pay Payments
Here's how to handle Apple Pay payments in your React application:
const processApplePayPayment = async (paymentResponse) => {
try {
console.log("Processing Apple Pay payment...");
// Extract payment information
const paymentData = paymentResponse.details;
const paymentMethod = paymentResponse.methodName;
// Generate unique order ID
const orderId = `order_${Date.now()}_${Math.random()
.toString(36)
.substr(2, 9)}`;
// Prepare payment request
const paymentRequest = {
orderId: orderId,
amount: amount.toString(),
currency: currency,
token: paymentData,
paymentMethod: paymentMethod,
product: {
name: "Your Product Name",
category: "Your Category",
price: amount.toString(),
quantity: 1,
},
shippingAddress: paymentResponse.shippingAddress,
payerName: paymentResponse.payerName,
payerEmail: paymentResponse.payerEmail,
payerPhone: paymentResponse.payerPhone,
};
// Send to your backend
const baseUrl = process.env.REACT_APP_API_BASE_URL || "";
const url = `${baseUrl}/api/payments/applepay`;
const response = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(paymentRequest),
credentials: "include",
});
if (!response.ok) {
const errorText = await response.text();
throw new Error(`Payment failed: ${response.status} - ${errorText}`);
}
const paymentResult = await response.json();
console.log("Payment completed successfully:", paymentResult);
return paymentResult;
} catch (error) {
console.error("Apple Pay payment failed:", error);
throw error;
}
};Frontend Request Format
Your frontend should send the following payload to your backend:
{
"orderId": "order_1640995200000_abc123",
"amount": "29.99",
"currency": "USD",
"token": {
"paymentData": {
"version": "RSA_v1",
"data": "encrypted_payment_data_base64",
"signature": "payment_signature",
"header": {
"publicKeyHash": "public_key_hash",
"transactionId": "transaction_id",
"wrappedKey": "wrapped_key"
}
},
"paymentMethod": {
"displayName": "Visa 1234",
"network": "Visa",
"type": "debit"
},
"transactionIdentifier": "ABC123XYZ456"
},
"paymentMethod": "https://apple.com/apple-pay",
"product": {
"name": "Your Product Name",
"category": "Your Category",
"price": "29.99",
"quantity": 1
},
"shippingAddress": {
"addressLines": ["123 Main St"],
"locality": "San Francisco",
"administrativeArea": "CA",
"postalCode": "94105",
"country": "US"
},
"payerName": "John Doe",
"payerEmail": "john@example.com",
"payerPhone": "+1234567890"
}Expected Backend Response
Your backend will return a response in this format:
{
"success": true,
"transactionId": "txn_1640995200000_xyz789",
"authCode": "AUTH_ABC123",
"orderId": "order_1640995200000_abc123",
"amount": "29.99",
"currency": "USD",
"status": "authorized",
"timestamp": "2021-12-31T12:00:00.000Z",
"message": "Payment processed successfully",
"processor": "novatti"
}Backend Implementation
Request to Novatti
Your backend should format requests to Novatti as follows:
{
"Header": {
"TransactionType": "OneStepPayment",
"MerchantID": "YOUR_MERCHANT_ID",
"UserID": "YOUR_USER_ID",
"Version": "2"
},
"Transaction": {
"MerchantTxnID": "order_1640995200000_abc123",
"Currency": "USD",
"Amount": "29.99",
"ChannelType": "07",
"Method": "APPLEPAY",
"SurchargeAction": "APPLY"
},
"SecureTokenHolder": {
"ApplePayToken": "encoded_apple_pay_token_from_frontend"
}
}Response from Novatti
Novatti will respond with:
{
"Result": {
"ResponseCode": "00",
"ResponseMessage": "Success"
},
"Transaction": {
"TransactionID": "txn_1640995200000_xyz789",
"AuthCode": "AUTH_ABC123",
"MerchantTxnID": "order_1640995200000_abc123",
"Amount": "29.99",
"Currency": "USD",
"Status": "AUTHORIZED",
"Timestamp": "2021-12-31T12:00:00.000Z"
}
}Key Points to Remember
- Security: The Apple Pay token contains encrypted payment data that only Novatti can decrypt
- Order IDs: Generate unique order IDs for each transaction to avoid duplicates
- Error Handling: Always implement proper error handling for failed payments
- Logging: Keep detailed logs for troubleshooting and compliance
- Testing: Test thoroughly in Apple's sandbox environment before going live
Next Steps
- Set up your merchant credentials with Novatti
- Configure your Apple Pay merchant identifier
- Implement the frontend payment flow
- Set up your backend API endpoint
- Test the complete integration
- Go live with confidence!
Need help with implementation? Contact our support team for assistance.