paypal与单页应用程序集成

时间:2014-12-22 02:13:44

标签: angularjs node.js backbone.js paypal single-page-application

我遇到与帖子Implementation of Paypal in single page application中描述的完全相同的问题,不幸的是,在这个庞大的社区中没有人似乎有答案:( 有很多头撞击我能够实现如下的脏黑客,我知道这不是正确的解决方案。任何大师都可以仔细思考这个问题并回答/提供反馈,如果我的肮脏黑客是正确的方法,或者它可以更好地实施。在我的脏黑客下面:( 非常感谢您的回答/评论。

我有一个按钮,用paypal支付费用,然后点击我打开一个新窗口 - > window.open("/paypalCreate", width = "20px", height = "20px"); 我抓住了这个获取请求" / paypalCreate"在我的node.js服务器中,调用看起来低于

的创建方法
exports.create = function (req, res) {
    //Payment object
    var payment = {
       //fill details from DB
    };   

    //Passing the payment over to PayPal
    paypal.payment.create(payment, function (error, payment) {
        if (error) {
            console.log(error);
        } else {
            if (payment.payer.payment_method === 'paypal') {
                req.session.paymentId = payment.id;
                var redirectUrl;
                for (var i = 0; i < payment.links.length; i++) {
                    var link = payment.links[i];
                    if (link.method === 'REDIRECT') {
                        redirectUrl = link.href;
                    }
                }
                res.redirect(redirectUrl);
            }
        }
    });
};

This redirects user to paypal and once user confirms or cancels payment, the redirect urls are called. And in the success redirect url I capture the payment details into the databse and render a html in this opened window with the confirmation.

exports.execute = function (req, res) {
    var paymentId = req.session.paymentId;
    var payerId = req.param('PayerID');

    // 1. if this is executed, then that means the payment was successful, now store the paymentId, payerId and token into the database
    // 2. At the close of the popup window open a confirmation for the reserved listing
    var details = {"payer_id": payerId};
    paypal.payment.execute(paymentId, details, function (error, payment) {
        if (error) {
            console.log(error);
        } else {
            //res.send("Hell yeah!");
            res.render('paypalSuccess', {payerId: payerId, paymentId: paymentId});
        }
    });
};

用户关闭正在处理PayPal的已打开窗口后,将刷新原始SPA窗口,从而从DB获取付款详细信息,您可以在此处以任何方式处理SPA。 我知道这是一个肮脏的黑客,但像你一样,我找不到更好的方法。如果这对您有用,或者您找到了更好的方法,请告诉我。

欢呼声, 赤丹

1 个答案:

答案 0 :(得分:12)

试试这个。这是我用于我的应用程序的内容。

var config = require("config3");
var paypal_api = require("paypal-rest-sdk");
paypal_api.configure(config.paypal);
var log = require("app/log");

function pay(creditCard, amount, description, callback) {
    var paypalOptions = {
        intent: "sale",
        payer: {
            payment_method: "credit_card",
            funding_instruments: [{credit_card: creditCard}]
        },
        transactions: [{
            amount: {
                total: amount,
                currency: "USD"
            },
            description: description
        }]
    };
    if (config.paypal.enabled) {
        paypal_api.payment.create(paypalOptions, function (error, response) {
            log.debug({
              err: error,
              response: response || (error && error.response)
            }, "paypal payment response");
            callback(error, response);
        });
    } else {
        setImmediate(function () {
            callback(null, {"fakePaypal": "is fake"});
        });
    }
}

module.exports = pay;

编辑:config3模块看起来像这样。可以找到此模块的文档here

module.exports = {
  paypal: {
    client_id: "Secret API key",
    client_secret: "Secret API key",
    host: "api.sandbox.paypal.com",
    enabled: true
  },
  mysql: {
    host: 'localhost',
    user: 'root',
    password: '',
    database: ''
  },
  redis: {
    host: "localhost",
    port: 6379
  },

就重定向而言,您不需要将用户发送到Paypal。 成功时只显示一个事务已完成的消息/页面。 失败时显示错误并让他们修复它。