要创建一个类似PayPal的结账页面,您需要了解以下几个基础概念:
以下是一个简单的示例,展示如何使用PayPal的API集成一个自定义结账页面:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Custom Checkout</title>
</head>
<body>
<form id="paypalForm" action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="business" value="your_paypal_email@example.com">
<input type="hidden" name="item_name" value="Sample Product">
<input type="hidden" name="amount" value="10.00">
<input type="hidden" name="currency_code" value="USD">
<input type="hidden" name="return" value="http://yourwebsite.com/return">
<input type="hidden" name="cancel_return" value="http://yourwebsite.com/cancel">
<input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_buynow_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
<img alt="" border="0" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1">
</form>
</body>
</html>
const express = require('express');
const axios = require('axios');
const app = express();
app.post('/process-payment', async (req, res) => {
const { token, payerId } = req.body;
const amount = '10.00';
const currency = 'USD';
try {
const response = await axios.post('https://api-3t.paypal.com/nvp', querystring.stringify({
METHOD: 'DoExpressCheckoutPayment',
TOKEN: token,
PAYERID: payerId,
PAYMENTACTION: 'Sale',
AMT: amount,
CURRENCYCODE: currency,
RETURNURL: 'http://yourwebsite.com/return',
CANCELURL: 'http://yourwebsite.com/cancel'
}));
if (response.data.ACK === 'Success') {
res.send('Payment successful!');
} else {
res.status(500).send('Payment failed.');
}
} catch (error) {
console.error(error);
res.status(500).send('Payment failed.');
}
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
通过以上步骤,您可以创建一个类似PayPal的结账页面,并处理支付流程。
领取专属 10元无门槛券
手把手带您无忧上云