如何将Node.js应用程序托管到Firebase托管和Paypal SDK和Flutter

时间:2020-10-01 21:22:32

标签: node.js firebase flutter paypal-sandbox firebase-hosting

嗨,大家好,我正在使用扑扑的Firebase Cloud Firestore开发食品交付应用程序,我想在我的应用程序中使用Paypal付款方式来结帐订单,所以当我使用我的应用程序时,我的node.js的Paypal SDK运行良好要结帐单, 我现在遇到的问题是他拥有http://10.0.2.2:8000/pay的贝宝结帐的网址,该网址仅适用于模拟器,我想要的解决方案是使用Firebase托管此网址,以便为实际设备工作,此问题的任何帮助或者,如果您有任何想法,我将不胜感激地告诉我, 顺便说一句,我是移动应用程序开发人员的新手

这是我在app.js中的代码

var paypal = require("paypal-rest-sdk"); 
var express = require("express"); 
var amount = 0; var app = express();

var bodyParser = require("body-parser");

app.use(
    bodyParser.urlencoded({
        extended:false
    }) ); app.use(bodyParser.json())

paypal.configure({
    'mode': 'sandbox', //sandbox or live
    'client_id': 'client id here',
    'client_secret': 'secret key here' });

    app.post("/pay", (req,res)=>{

    console.log(req.body);
    amount = req.body.price;
    var create_payment_json = {
        "intent": "sale",
        "payer": {
            "payment_method": "paypal"
        },
        "redirect_urls": {
            "return_url": "http://10.0.2.2:8000/success",
            "cancel_url": "http://cancel.url"
        },
        "transactions": [{
            "item_list": {
                "items": [{
                    "name": "item",
                    "sku": "item",
                    "price": amount,
                    "currency": "USD",
                    "quantity": 1
                }]
            },
            "amount": {
                "currency": "USD",
                "total": amount
            },
            "description": "This is the payment description."
        }]
    };

    paypal.payment.create(create_payment_json, (error, payment)=> {
        if (error) {
            throw error;
        } else {
            console.log("Create Payment Response");
            console.log(payment);
            for(let i = 0 ; i < payment.links.length ; i++){

                if(payment.links[i].rel == 'approval_url'){

                    res.redirect(payment.links[i].href);
                }
            }
        }
    }); })

app.get("/success", (req, res)=>{

    var execute_payment_json = {
        "payer_id": req.query.PayerID,
        "transactions": [{
            "amount": {
                "currency": "USD",
                "total": amount
            }
        }]
    };
    
    var paymentId = req.query.paymentId;
    
    paypal.payment.execute(paymentId, execute_payment_json, function (error, payment) {
        if (error) {
            console.log(error.response);
            throw error;
        } else {
            console.log("Get Payment Response");
            console.log(JSON.stringify(payment));
        }
    }); })


app.listen(8000,"127.0.0.1", (req,res)=>{

    console.log("server started") });

packages.js

{
  "name": "server",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "adnantjee.xx",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.19.0",
    "express": "^4.17.1",
    "paypal-rest-sdk": "^1.8.1"
  }
}

0 个答案:

没有答案
相关问题