【发布时间】:2016-06-28 15:55:55
【问题描述】:
我确定这是一个愚蠢的问题,但我对后端非常陌生,所以请原谅我。
我正在使用 express/node 构建一个 angularjs 应用程序,并尝试集成 PayPal(作为Node.js SDK),我想要的是从角度控制器调用 SDK 上的支付方法,我正在执行以下操作:
点击按钮:
// controller
$scope.pay = function(amount) {
PaymentFactory.doPayment(amount);
}
支付工厂:
// PaymentFactory
return {
doPayment: function(amount) {
$http.get("../../../server/payments/paypal.js")
.then(function(response) {
console.log( response );
})
}
}
那么服务器端文件如下:
require('paypal-adaptive');
var app = require('../../server.js');
var PayPal = require('paypal-adaptive');
var paypalSdk = new PayPal({
userId: 'userid',
password: 'password',
signature: 'signature',
sandbox: true //defaults to false
});
var payload = {
requestEnvelope: {
errorLanguage: 'en_US'
},
actionType: 'PAY_PRIMARY',
currencyCode: 'GBP',
feesPayer: 'EACHRECEIVER',
memo: 'Chained payment example',
cancelUrl: 'returnUrl,
returnUrl: 'cancelUrl',
receiverList: {
receiver: [
{
email: 'email1',
amount: '3.40',
primary:'true'
},
{
email: 'email2',
amount: '1.20',
primary:'false'
}
]
}
};
paypalSdk.pay(payload, function (err, response) {
if (err) {
console.log(err);
} else {
// Response will have the original Paypal API response
// But also a paymentApprovalUrl, so you can redirect the sender to checkout easily
console.log('Redirect to %s', response.paymentApprovalUrl);
return response;
}
});
当然,get 请求只是返回一个服务器端文件内容的字符串,我理解为什么上面的方法不起作用,但不确定如何使它起作用。我的目标是从角度工厂调用 PayPal SDK 并取回响应,以便我可以将用户重定向到 URL。直接的解决方案会有所帮助,但更重要的是,我需要指出我在这里不理解的原则,即应该如何根据用户操作调用函数以从服务器端获取这些数据。我已经尝试过搜索,但我并没有真正在搜索中使用的语言。
【问题讨论】:
标签: angularjs node.js http express asynchronous