// Google Pay API configuration
const baseRequest = {
apiVersion: 2,
apiVersionMinor: 0
};
const allowedCardNetworks = ["AMEX", "DISCOVER", "JCB", "MASTERCARD", "VISA"];
const allowedCardAuthMethods = ["PAN_ONLY", "CRYPTOGRAM_3DS"];
const tokenizationSpecification = {
type: 'PAYMENT_GATEWAY',
parameters: {
'gateway': 'cyrexa',
'gatewayMerchantId': process.env.YONOBI_MERCHANT_ID
}
};
const baseCardPaymentMethod = {
type: 'CARD',
parameters: {
allowedAuthMethods: allowedCardAuthMethods,
allowedCardNetworks: allowedCardNetworks
}
};
const cardPaymentMethod = Object.assign(
{},
baseCardPaymentMethod,
{
tokenizationSpecification: tokenizationSpecification
}
);
const paymentDataRequest = Object.assign({}, baseRequest);
paymentDataRequest.allowedPaymentMethods = [cardPaymentMethod];
paymentDataRequest.transactionInfo = {
totalPriceStatus: 'FINAL',
totalPriceLabel: 'Total',
totalPrice: '99.99',
currencyCode: 'USD',
countryCode: 'US'
};
paymentDataRequest.merchantInfo = {
merchantId: process.env.GOOGLE_PAY_MERCHANT_ID,
merchantName: 'Example Store'
};
// Initialize Google Pay
function initializeGooglePay() {
const paymentsClient = new google.payments.api.PaymentsClient({
environment: 'TEST' // Change to 'PRODUCTION' for live
});
return paymentsClient;
}
// Process Google Pay payment
async function processGooglePayPayment(paymentData, customerInfo) {
const apiEndpoint = process.env.YONOBI_H2H_ENDPOINT;
const apiKey = process.env.YONOBI_API_KEY;
try {
const response = await fetch(apiEndpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-API-Key': apiKey
},
body: JSON.stringify({
// Google Pay specific
name: customerInfo.name,
paymentToken: paymentData.paymentMethodData.tokenizationData.token,
// Customer info
email: customerInfo.email,
phoneNumber: customerInfo.phoneNumber,
address: customerInfo.address.street,
city: customerInfo.address.city,
state: customerInfo.address.state,
postalCode: customerInfo.address.postalCode,
country: customerInfo.address.country,
// Transaction
amount: paymentData.transactionInfo.totalPrice,
unit: paymentData.transactionInfo.currencyCode,
originDomain: window.location.hostname,
referenceId: customerInfo.orderId,
// URLs
notifyUrl: customerInfo.webhookUrl,
successUrl: customerInfo.successUrl,
failureUrl: customerInfo.failureUrl,
// Browser info
browserInfo: collectBrowserInfo()
})
});
const result = await response.json();
if (response.ok) {
return {
success: true,
paymentRequestId: result.paymentRequestId,
status: result.status,
transactionId: result.transactionId
};
} else {
return {
success: false,
error: result.error,
message: result.message
};
}
} catch (error) {
return {
success: false,
error: 'NETWORK_ERROR',
message: error.message
};
}
}
// Complete Google Pay flow
async function handleGooglePayPayment() {
const paymentsClient = initializeGooglePay();
try {
// Request payment data from Google Pay
const paymentData = await paymentsClient.loadPaymentData(paymentDataRequest);
// Process payment with Cyrexa
const result = await processGooglePayPayment(paymentData, {
name: 'John Smith',
email: 'john@example.com',
phoneNumber: '+1234567890',
address: {
street: '123 Main Street',
city: 'New York',
state: 'NY',
postalCode: '10001',
country: 'US'
},
orderId: 'GPY-54321',
webhookUrl: 'https://api.example.com/webhook',
successUrl: 'https://shop.example.com/success',
failureUrl: 'https://shop.example.com/failure'
});
if (result.success) {
// Payment successful
window.location.href = '/success?id=' + result.paymentRequestId;
} else {
// Handle payment error
displayError(result.message);
}
} catch (error) {
console.error('Google Pay error:', error);
displayError('Google Pay payment failed');
}
}