Nodejs PayPal REST API SDK的使用
Nodejs PayPal REST API SDK的使用
Node.js 中使用 PayPal 的 REST API 可以通过 paypal-rest-sdk
这个 npm 包来实现。以下是安装和使用该库的基本步骤:
1. 安装 paypal-rest-sdk
首先,在你的 Node.js 项目中安装 paypal-rest-sdk
:
npm install paypal-rest-sdk
2. 初始化 PayPal SDK
在你的应用中初始化 PayPal SDK,并设置必要的认证信息(客户端 ID 和客户端密钥):
const paypal = require('paypal-rest-sdk');
// 设置 PayPal 配置信息
paypal.configure({
'mode': 'sandbox', // 环境模式:sandbox(测试环境)或 live(生产环境)
'client_id': 'YOUR_CLIENT_ID',
'client_secret': 'YOUR_CLIENT_SECRET'
});
请确保替换 YOUR_CLIENT_ID
和 YOUR_CLIENT_SECRET
为从 PayPal 开发者账户中获取的实际值。
3. 创建支付请求
以下是一个创建支付请求的示例代码:
const createPayment = (req, res) => {
let paymentDetails = {
"intent": "sale",
"payer": {
"payment_method": "paypal"
},
"redirect_urls": {
"return_url": "http://localhost:3000/payment/execute",
"cancel_url": "http://localhost:3000/"
},
"transactions": [{
"amount": {
"total": "5",
"currency": "USD"
},
"description": "This is the payment transaction description."
}]
};
paypal.payment.create(paymentDetails, function(error, payment) {
if (error) {
console.error(error);
return res.status(500).send({ error: 'Failed to create payment.' });
} else {
console.log(JSON.stringify(payment));
for (let i = 0; i < payment.links.length; i++) {
if (payment.links[i].rel === 'approval_url') {
return res.redirect(payment.links[i].href);
}
}
}
});
};
4. 执行支付
当用户通过 PayPal 页面完成支付后,PayPal 会重定向回你指定的 return_url
,此时你需要执行支付:
const executePayment = (req, res) => {
let payerId = req.query.PayerID;
let paymentId = req.query.paymentId;
let execute_payment_json = {
"payer_id": payerId,
"transactions": [{
"amount": {
"total": "5",
"currency": "USD"
}
}]
};
paypal.payment.execute(paymentId, execute_payment_json, function(error, payment) {
if (error) {
console.error(error.response.details);
return res.status(500).send({ error: 'Failed to execute payment.' });
} else {
console.log(JSON.stringify(payment));
return res.send({ success: true, message: 'Payment executed successfully.' });
}
});
};
以上就是使用 Node.js 和 PayPal REST API SDK 的基本流程。你可以根据自己的需求调整金额、货币类型等参数。确保在生产环境中使用正确的客户端 ID 和客户端密钥,并且将环境模式设置为 live
。
嘿,想要用Node.js与PayPal的REST API互动?首先,你需要安装paypal-rest-sdk
这个宝藏包。打开你的终端,输入npm install paypal-rest-sdk
,就像施了魔法一样,它就会出现在你的项目中。
然后,你可以这样初始化SDK:
var paypal = require('paypal-rest-sdk');
paypal.configure({
'mode': 'sandbox', // 开发时用'sandbox',上线后改成'live'
'client_id': '你的客户端ID',
'client_secret': '你的秘密令牌'
});
接着,比如要创建一个支付,你可以这样做:
var create_payment_json = {
"intent": "sale",
"payer": {
"payment_method": "paypal"
},
"redirect_urls": {
"return_url": "http://返回URL",
"cancel_url": "http://取消URL"
},
"transactions": [{
"item_list": {
"items": [{
"name": "测试物品",
"sku": "12345",
"price": "1.00",
"currency": "USD",
"quantity": 1
}]
},
"amount": {
"currency": "USD",
"total": "1.00"
},
"description": "这是个测试支付。"
}]
};
paypal.payment.create(create_payment_json, function (error, payment) {
if (error) {
throw error;
} else {
console.log("创建支付: ", payment);
}
});
是不是感觉像是打开了新世界的大门呢?祝你编程愉快!
在Node.js中使用PayPal REST API可以方便地处理支付、退款等操作。PayPal官方提供了SDK来简化这一过程。下面我将展示如何设置并使用PayPal Node.js SDK来创建一个支付请求。
1. 安装PayPal SDK
首先,你需要安装paypal-rest-sdk
。可以通过npm来安装:
npm install paypal-rest-sdk
2. 设置环境变量
确保你有PayPal的客户端ID和秘密,通常在PayPal开发者账户中的应用程序部分找到。将其设置为环境变量:
export PAYPAL_CLIENT_ID=your_client_id
export PAYPAL_SECRET=your_secret
或者直接在代码中硬编码(不推荐用于生产环境):
const clientId = 'your_client_id';
const clientSecret = 'your_secret';
3. 创建支付请求
以下示例展示了如何使用SDK创建一个支付请求:
const paypal = require('paypal-rest-sdk');
// 配置PayPal SDK
paypal.configure({
'mode': 'sandbox', // 使用'sandbox'进行测试,'live'用于生产环境
'client_id': process.env.PAYPAL_CLIENT_ID,
'client_secret': process.env.PAYPAL_SECRET
});
// 创建支付对象
let payment = {
"intent": "sale",
"payer": {
"payment_method": "paypal"
},
"transactions": [{
"amount": {
"total": "30.11",
"currency": "USD",
"details": {
"subtotal": "30.00",
"tax": "0.07",
"shipping": "0.03"
}
},
"description": "This is the payment transaction description.",
"item_list": {
"items": [{
"name": "Item",
"sku": "item",
"price": "15.00",
"currency": "USD",
"quantity": 2
}]
}
}],
"redirect_urls": {
"return_url": "http://localhost:3000/payment/execute",
"cancel_url": "http://localhost:3000/"
}
};
// 发起支付请求
paypal.payment.create(payment, function (error, payment) {
if (error) {
console.error(error);
} else {
console.log("Payment created successfully:", payment);
for (let i = 0; i < payment.links.length; i++) {
let link = payment.links[i];
if (link.method === 'REDIRECT') {
console.log("Redirecting to:", link.href);
// 这里你可以重定向用户到链接地址
}
}
}
});
这个例子展示了如何创建一个基本的支付请求,并设置了返回和取消URL。当调用paypal.payment.create
时,如果一切顺利,它将返回一个包含支付详情的对象,包括重定向链接,你可以使用这些链接引导用户完成支付流程。
请根据实际需要调整货币类型、金额和其他参数。对于生产环境,记得切换到live
模式。
使用Node.js与PayPal REST API交互,首先需要安装paypal-rest-sdk
。可以通过npm安装此包:
npm install paypal-rest-sdk
初始化SDK:
var paypal = require('paypal-rest-sdk');
paypal.configure({
'mode': 'sandbox', // 环境模式: sandbox(测试) 或 live(生产)
'client_id': '你的客户端ID',
'client_secret': '你的客户端密钥'
});
然后,你可以使用此SDK来创建支付、执行支付、查询支付状态等操作。例如,创建支付:
var create_payment_json = {
"intent": "sale",
"payer": {
"payment_method": "paypal"
},
"redirect_urls": {
"return_url": "http://返回URL",
"cancel_url": "http://取消URL"
},
"transactions": [{
"item_list": {
"items": [{
"name": "物品名称",
"sku": "物品SKU",
"price": "2.50",
"currency": "USD",
"quantity": 1
}]
},
"amount": {
"currency": "USD",
"total": "2.50"
},
"description": "这是描述"
}]
};
paypal.payment.create(create_payment_json, function (error, payment) {
if (error) {
throw error;
} else {
console.log("Create Payment Response");
console.log(payment);
}
});
更多详情请参考官方文档:https://developer.paypal.com/docs/api/overview/