0

hi guys i'm working on food delivery app with firebase cloud firestore using flutter and i want make paypal payment method in my app to checkout orders, so i have paypal sdk with node.js is working fine in my app when i want checkout orders, the problem i have right now is url of paypal checkout he have http://10.0.2.2:8000/pay and this url is working for emulator only, the solution i want is to host this url with firebase hosting to work for real devices, any help for this problem or if you have any ideas I will be grateful to tell me, btw i'm newbie in mobile app developer

this is my code in 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"
  }
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Adnantjee.xx
  • 1
  • 1
  • 2
  • 1
    You can't host an arbitrary node app on Firebase Hosting. If you need to stay with Firebase, you should look into Cloud Functions or other backend services. – Doug Stevenson Oct 01 '20 at 21:52
  • If this is an Express.js based app, it may be possible to run it on Cloud Functions and expose that through Firebase Hosting. See https://stackoverflow.com/questions/42415677/can-i-use-firebase-hosting-to-write-a-restful-api-in-node-js/42416254#42416254 – Frank van Puffelen Oct 01 '20 at 22:20

0 Answers0