加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – 在单页面应用程序中实现Paypal

发布时间:2020-12-17 16:57:39 所属栏目:安全 来源:网络整理
导读:我目前正在开发一款游戏,它将包含一个基于API的后端,以及一个Web前端(AngularJS中的单页应用程序)和几个移动设备(使用Cordova).我计划通过主域名提供SPA以及CDN. SPA(和主页)都是静态 HTML / Javascript / CSS文件,因此动态的唯一部分是api.托管静态站点的“
我目前正在开发一款游戏,它将包含一个基于API的后端,以及一个Web前端(AngularJS中的单页应用程序)和几个移动设备(使用Cordova).我计划通过主域名提供SPA以及CDN. SPA(和主页)都是静态 HTML / Javascript / CSS文件,因此动态的唯一部分是api.托管静态站点的“主服务器”的域名将采用example.com的样式,api的域名将为api.example.com

我想知道如何将Paypal集成到这个场景中.互联网似乎没有提供太多关于如何将它整合到S.P.A中的建议……或者我的google-fu可能会关闭.谢谢你的回复.

解决方法

以下是我处理这种情况的方法,

我有一个按钮,用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,payment) {
        if (error) {
            console.log(error);
        } else {
            //res.send("Hell yeah!");
            res.render('paypalSuccess',{payerId: payerId,paymentId: paymentId});
        }
    });
};

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

干杯,赤胆

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读