【问题标题】:paypal integration with single page app贝宝与单页应用程序的集成
【发布时间】:2015-02-20 03:29:27
【问题描述】:

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

我有一个按钮可以用 paypal 和 onClick 我打开一个新窗口 -> window.open("/paypalCreate", width = "20px", height = "20px"); 我在我的 node.js 服务器中捕获了这个获取请求“/paypalCreate”并调用了如下所示的 create 方法

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 窗口将被刷新,从而从数据库中获取付款详细信息,您可以在此处以任何您想要的方式处理 SPA。 我知道这是一个肮脏的黑客,但像你一样我找不到更好的方法。如果这对您有用,或者您是否找到了更好的方法,请告诉我。

干杯, 赤胆

【问题讨论】:

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


    【解决方案1】:

    试试这个。这是我用于我的应用程序的东西。

    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。 成功时只显示交易完成的消息/页面。 失败时显示错误并让他们修复它。

    【讨论】:

    • 嗨,本,谢谢。不过我有两个问题,你的配置文件是什么样的?这适用于没有重定向到贝宝网站的信用卡付款,但是我如何处理重定向到贝宝网站,并返回成功或取消网址?
    • 我更新了对您两个问题的回答。如果这解决了您的问题,请接受它作为已接受的答案。谢谢
    猜你喜欢
    • 2017-03-22
    • 1970-01-01
    • 2015-03-22
    • 2011-06-14
    • 2012-07-15
    • 2015-12-19
    • 2016-09-12
    • 2016-07-18
    • 2014-07-03
    相关资源
    最近更新 更多