出于某种原因,PayPal快速结账对我不起作用,它显示我的空白页面说:
没有令牌传递
以下是我从url获取服务器响应的代码,因为包含代码的文件是全局的,任何页面都可用于处理成功响应
setTimeout(function () {
var paypalToken = Router.current().params.query.token;
var paypalPayerId = Router.current().params.query.PayerID;
var price = '';
var itemId = '';
if (paypalToken != undefined && paypalPayerId != undefined) {
check(paypalToken, String);
check(paypalPayerId, String);
check(price, String);
check(itemId, String);
Meteor.call('fetchItemId', paypalToken, function (err, data) {
if (err) {
alert(err);
}
else {
itemId = data;
Meteor.call('fetchPriceById', itemId, function (err, data) {
if (err) {
alert(err);
}
else {
price = data.toString();
Meteor.call('processPayment', paypalToken, paypalPayerId, price, function (error, result) {
Meteor.call('fetchLatestToken', function (err, data) {
if (err) {
alert(err);
}
else {
//console.log(data.itemId);
pathPaypal = "https://domain/item/" + data.itemId;
window.location = pathPaypal;
}
});
});
}
});
}
});
}
}
服务器端方法
fetchItemId: function(paypalToken){
check(paypalToken, String);
purchasesData = Purchases.findOne( { paypalToken: paypalToken });
return purchasesData.itemId;
},
fetchPriceById: function(itemId){
check(itemId, String);
itemData = Products.findOne( { _id: itemId });
return itemData.price;
},
checkOut:function(itemId,price){
check(itemId, String);
check(price, String);
Future = Npm.require('fibers/future');
var accessToken = new Future();
Meteor.http.call("POST", "https://api-3t.paypal.com/nvp", {
headers: {
"Accept": "application/json",
"Accept-Language": "en_US",
"Content-Type": "application/x-www-form-urlencoded"
},
params: {
"USER": "user"
"PWD": "pass"
"SIGNATURE": "signature",
"METHOD": "SetExpressCheckout",
"VERSION": "93",
"PAYMENTREQUEST_0_AMT": price,
"PAYMENTREQUEST_0_PAYMENTACTION": "SALE",
"PAYMENTREQUEST_0_CURRENCYCODE": "USD",
"RETURNURL": "http://domain/item/"+itemId,
"CANCELURL": "http://domain/item/"+itemId,
}
},function(error,result){
if (result.statusCode === 200) {
paypalToken = decodeURIComponent(result.content.split("&")[0].split("=")[1]);
/* database operations */
try {
Purchases.insert({
itemId: itemId,
clientId: Meteor.userId(),
paypalToken: paypalToken,
status: '0',
createdAt: new Date()
});
}
catch(e){
console.log('transaction in progress');
}
data = Purchases.find( { itemId: itemId }).fetch();
//console.log(data);
accessToken.return('');
return '';
}
});
accessToken.wait();
},
processPayment:function(token, PayerID, price){
var accessToken = new Future();
check(token, String);
check(PayerID, String);
check(price, String);
Meteor.http.call("POST", "https://api-3t.paypal.com/nvp", {
headers: {
"Accept": "application/json",
"Accept-Language": "en_US",
"Content-Type": "application/x-www-form-urlencoded"
},
params: {
"USER": "user",
"PWD": "pwd",
"SIGNATURE": "signature",
"METHOD": "DoExpressCheckoutPayment",
"VERSION": "93",
"TOKEN": token,
"PAYERID": PayerID,
"PAYMENTREQUEST_0_PAYMENTACTION": "SALE",
"PAYMENTREQUEST_0_AMT": price,
"PAYMENTREQUEST_0_CURRENCYCODE": "USD",
}
},function(error,result){
if (result.statusCode === 200) {
try {
var purchase;
var purchase2;
purchase = Purchases.findOne( { paypalToken: token }, {sort:{$natural:-1}});
Purchases.update(purchase._id, {$set: {status: '1'}});
}
catch(e){
console.log('transaction in progress');
}
accessToken.return(result);
}
});
accessToken.wait();
}