Skip to content

Instantly share code, notes, and snippets.

@wizardnet972
Created July 12, 2019 14:19
Show Gist options
  • Select an option

  • Save wizardnet972/654bd6dcf132e87a0d58d52b8a70911f to your computer and use it in GitHub Desktop.

Select an option

Save wizardnet972/654bd6dcf132e87a0d58d52b8a70911f to your computer and use it in GitHub Desktop.
const express = require("express");
const bodyParser = require("body-parser");
const engines = require("consolidate");
const paypal = require("paypal-rest-sdk");
const app = express();
app.engine("ejs", engines.ejs);
app.set("views", "./views");
app.set("view engine", "ejs");
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
paypal.configure({
mode: "sandbox", //sandbox or live
client_id:
"AarUi7DfcZaVwTyuHOE2qMPGP1Gy65T8KNHicseSgSXB-gm_2upRM74fU-MKmslNaHKqNOUsxMrNv9I-",
client_secret:
"EI0ea3hVzYFwuts33i0RUVhxF48woSUSg7lwNbkImLrHpcEYpUmJPRXdf4CXn4kFacsRoZ-62cn9Xe6h"
});
app.get("/", (req, res) => {
res.render("index");
});
app.get("/paypal", (req, res) => {
var create_payment_json = {
intent: "sale",
payer: {
payment_method: "paypal"
},
redirect_urls: {
return_url: "http://localhost:3000/success",
cancel_url: "http://localhost:3000/cancel"
},
transactions: [
{
item_list: {
items: [
{
name: "item",
sku: "item",
price: "1.00",
currency: "USD",
quantity: 1
}
]
},
amount: {
currency: "USD",
total: "1.00"
},
description: "This is the payment description."
}
]
};
paypal.payment.create(create_payment_json, function(error, payment) {
if (error) {
throw error;
} else {
console.log("Create Payment Response");
console.log(payment);
res.redirect(payment.links[1].href);
}
});
});
app.get("/success", (req, res) => {
// res.send("Success");
var PayerID = req.query.PayerID;
var paymentId = req.query.paymentId;
var execute_payment_json = {
payer_id: PayerID,
transactions: [
{
amount: {
currency: "USD",
total: "1.00"
}
}
]
};
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));
res.render("success");
}
});
});
app.get("cancel", (req, res) => {
res.render("cancel");
});
app.listen(3000, () => {
console.log("Server is running");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment