8

Ho lo stesso identico problema descritto nel post Implementation of Paypal in single page application sfortunatamente nessuno in questa vasta comunità sembra avere una risposta :( Con un sacco di colpi di testa sono riuscito a implementare un hack sporco come sotto e so che questa non è la soluzione giusta. Qualcuno dei guru potrebbe riflettere su questa domanda e rispondere/dare un feedback se il mio trucco sporco è il modo giusto o può essere implementato meglio Sotto il mio sporco trucco :( Molte grazie . in anticipo per le risposte/commentiintegrazione paypal con app a singola pagina

ho un pulsante per inserire la paga con paypal e onClick apro una nuova finestra ->window.open("/paypalCreate", width = "20px", height = "20px"); e catturare questo ottenere richiesta "/ paypalCreate" nei miei node.js server e chiamare creare metodo che sembra liek sotto

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

Una volta che l'utente chiude la finestra aperta in cui PayPal è stata gestita la finestra originale SPA sarà rinfrescata e quindi ottenere i dati di pagamento dal DB e qui si può gestire la SPA nel modo che preferisci. So che questo è un trucco sporco, ma come te non riuscivo a trovare un modo migliore. Per favore fatemi sapere se questo funziona per voi o se avete trovato un modo migliore per farlo.

applausi, Chidan

risposta

11

Prova questo. È quello che uso per la mia 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; 

Modifica: il modulo config3 sarebbe simile a questo. La documentazione per questo modulo può essere trovato 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 
    }, 

Per quanto riguarda il reindirizzamento non è necessario inviare l'utente a Paypal. In caso di successo, mostra solo un messaggio/pagina di transazione completata. In caso di errore, mostra l'errore e lascia che lo risolvano.

+0

Ciao Ben, grazie. Ho 2 domande però, come appare il tuo file di configurazione? e questo funzionerebbe per il pagamento con carte di credito dove non c'è reindirizzamento al sito paypal, ma come posso gestire il reindirizzamento al sito paypal, e tornare al successo o cancellare l'url? –

+0

Ho aggiornato la mia risposta alle tue due domande. Se questo ha risolto il problema, accettalo come risposta accettata. Grazie – BenH