与单页的应用程序集成贝宝 [英] paypal integration with single page app

查看:159
本文介绍了与单页的应用程序集成贝宝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有相同的问题,因为在后<一个描述href=\"https://stackoverflow.com/questions/26550797/implementation-of-paypal-in-single-page-application/27595716#27595716\">Implementation贝宝在单页应用程序的不幸没有人在这个庞大的社会似乎有一个答案:(
随着大量撞头的我能够如下实施肮脏的黑客,我知道这是不是正确的解决方案。可以在任何的大师们亲切地思考这个问题,并回答/提供反馈意见,如果我的肮脏的黑客是正确的方式,也可以得到更好的执行。下面我肮脏的黑客:(
提前非常感谢您的回答/意见。

I have the exact same problem as described in the post Implementation of Paypal in single page application unfortunately nobody in this vast community seems to have an answer :( With lots of head banging I was able to implement a dirty hack as below and I know that this is not the right solution. Could any of the gurus kindly ponder over this question and answer/give feedback if my dirty hack is the right way or it can be better implemented. Below my dirty hack :( Many thanks in advance for your answers/comments.

我有一个按钮,说用PayPal支付和的onClick我打开一个新窗口 - > window.open(/ paypalCreateWIDTH =20像素,高度=20像素);
和我在的node.js服务器捕获这个GET请求/ paypalCreate,并呼吁创建方法看起来liek低于

I have a button to say pay with paypal and onClick I open a new window -> window.open("/paypalCreate", width = "20px", height = "20px"); and I capture this get request "/paypalCreate" in my node.js server and call create method which looks liek below

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});
        }
    });
};

一旦用户关闭其贝宝正在处理的原单SPA窗口将被刷新,从而获得从DB的付款细节,在这里你可以处理你想要的任何方式,SPA打开的窗口。
我知道这是一个肮脏的黑客,而是像你我不能找到更好的方法。请让我还是知道这对你的作品如果你有发现了一个更好的方式做tihs。

Once the user closes the opened window in which paypal was being handled the orginal SPA window will be refreshed and thus getting the payment details from the DB and here you can handle the SPA in whatever way you want. I know that this is a dirty hack, but like you I couldnt find a better way. Please let me know if this works for you or if you have a found a better way to do tihs.

欢呼声,
赤胆

cheers, Chidan

推荐答案

试试这个。这是我用我的应用程序。

Try this. It's what I use for my app.

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模块是这样的。此模块的文档都可以找到这里

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
  },

至于重定向你不需要用户发送到贝宝。
成功只显示交易完成的消息/页。
失败时显示错误,并让他们解决这个问题。

As far as redirection you don't need to send your user to Paypal. On success just show a transaction completed message / page. On failure show the error and let them fix it.

这篇关于与单页的应用程序集成贝宝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆