要使用 Node.js 将支付信息保存到 Google Pay,通常涉及以下几个步骤:
以下是一个基本的示例,展示如何在 Node.js 中集成 Google Pay API 并处理支付请求。
在你的 Node.js 项目中,安装必要的依赖库,例如 express
和 body-parser
。
npm install express body-parser
创建一个简单的 Express 应用来处理支付请求。
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const port = 3000;
app.use(bodyParser.json());
app.post('/create-payment', (req, res) => {
const paymentRequest = {
apiVersion: 2,
apiVersionMinor: 0,
allowedPaymentMethods: [
{
type: 'CARD',
parameters: {
allowedAuthMethods: ['PAN_ONLY', 'CRYPTOGRAM_3DS'],
allowedCardNetworks: ['MASTERCARD', 'VISA']
},
tokenizationSpecification: {
type: 'PAYMENT_GATEWAY',
parameters: {
gateway: 'example',
gatewayMerchantId: 'exampleGatewayMerchantId'
}
}
}
],
merchantInfo: {
merchantId: 'your-merchant-id',
merchantName: 'Example Merchant'
},
transactionInfo: {
totalPriceStatus: 'FINAL',
totalPrice: '12.34',
currencyCode: 'USD',
countryCode: 'US'
}
};
res.json(paymentRequest);
});
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});
在客户端(例如,前端网页)中,使用 Google Pay API 处理支付响应。以下是一个基本的前端示例,展示如何集成 Google Pay 按钮并处理支付响应。
<!DOCTYPE html>
<html>
<head>
<title>Google Pay Example</title>
<script src="https://pay.google.com/gp/p/js/pay.js" async></script>
<script>
function onGooglePayLoaded() {
const paymentsClient = new google.payments.api.PaymentsClient({ environment: 'TEST' });
const paymentDataRequest = {
apiVersion: 2,
apiVersionMinor: 0,
allowedPaymentMethods: [
{
type: 'CARD',
parameters: {
allowedAuthMethods: ['PAN_ONLY', 'CRYPTOGRAM_3DS'],
allowedCardNetworks: ['MASTERCARD', 'VISA']
},
tokenizationSpecification: {
type: 'PAYMENT_GATEWAY',
parameters: {
gateway: 'example',
gatewayMerchantId: 'exampleGatewayMerchantId'
}
}
}
],
merchantInfo: {
merchantId: 'your-merchant-id',
merchantName: 'Example Merchant'
},
transactionInfo: {
totalPriceStatus: 'FINAL',
totalPrice: '12.34',
currencyCode: 'USD',
countryCode: 'US'
}
};
const button = paymentsClient.createButton({
onClick: () => {
paymentsClient.loadPaymentData(paymentDataRequest)
.then(paymentData => {
// Handle the payment data response
console.log(paymentData);
})
.catch(err => {
console.error(err);
});
}
});
document.getElementById('container').appendChild(button);
}
</script>
</head>
<body onload="onGooglePayLoaded()">
<div id="container"></div>
</body>
</html>
领取专属 10元无门槛券
手把手带您无忧上云