我正在尝试使用paypal sdk进行付款。 我的前端是angularjs,我的后端是nodejs。
在我的前端,我只是在我的节点服务器中调用路由:
$http.post('/paypal/pay', cart)
我在节点服务器中配置了CORS。
CORS问题发生在" res.redirect"线。
我还将CORS配置为来自paypal配置的头文件以尝试解决问题,但没有成功。
我正在使用localhost中的nodejs测试paypal付款。
当我尝试付款时,我收到了下一个错误:
我做错了什么?
//错误
https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-YYYYYY: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access.
//节点服务器
const express = require('express');
const app = express();
const PaypalAPI=require('./routes/paypal-checkout');
app.all('*', function(req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
next();
});
app.use('/paypal',PaypalAPI)
var server = app.listen(app.get('port'), function () {
console.log('Servidor rodando em ' +app.get('host') + ' Porta: '+app.get('port'));
});
// node router - paypal-checkout.js
const express = require('express');
const paypal = require('paypal-rest-sdk');
const router = express.Router();
paypal.configure({
'mode': 'sandbox', //sandbox or live
'client_id': 'XXXX',
'client_secret': 'YYYYYY',
'headers' : {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'PUT, GET, POST, DELETE, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type, Authorization'
}
});
router.post('/pay', (req, res) => {
const create_payment_json = {
"intent": "sale",
"payer": {
"payment_method": "paypal"
},
"redirect_urls": {
"return_url": "http://localhost/paypal/success",
"cancel_url": "http://localhost/paypal/cancel"
},
"transactions": [{
"item_list": {
"items": [{
"name": "Red Sox Hat",
"sku": "001",
"price": "25.00",
"currency": "BRL",
"quantity": 1
}]
},
"amount": {
"currency": "BRL",
"total": "25.00"
},
"description": "Hat for the best team ever"
}]
};
paypal.payment.create(create_payment_json, function (error, payment) {
if (error) {
throw error;
} else {
for(let i = 0;i < payment.links.length;i++){
if(payment.links[i].rel === 'approval_url'){
res.redirect(payment.links[i].href);
}
}
}
});
});
答案 0 :(得分:0)
我也遇到了这个问题。虽然OP可能会迟到但我想指出这是浏览器中的一个问题。答案已发布before,但以下是选项: