forked from danashelton/muber
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
54 lines (45 loc) · 1.5 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
const express = require("express");
const braintree = require("braintree");
const bodyParser = require("body-parser")
const PORT = process.env.PORT || 3001;
const app = express();
require('dotenv').config();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use(function (req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
res.setHeader('Access-Control-Allow-Credentials', true);
next();
});
const gateway = braintree.connect({
environment: braintree.Environment.Sandbox,
merchantId: process.env.BRAINTREE_MERCHANTID,
publicKey: process.env.BRAINTREE_PUBLICKEY,
privateKey: process.env.BRAINTREE_PRIVATEKEY
});
app.get("/server/client_token", function (req, res) {
gateway.clientToken.generate({}, function (err, response) {
res.json(response.clientToken);
});
});
app.post("/server/purchase/:nonce", function (req, res) {
var nonceFromTheClient = req.body.payment_method_nonce;
gateway.transaction.sale({
amount: "10.00",
paymentMethodNonce: nonceFromTheClient,
options: {
submitForSettlement: true
}
}, function (err, result) {
});
});
// Serve up static assets (usually on heroku)
if (process.env.NODE_ENV === "production") {
app.use(express.static("client/build"));
}
require("./routes")(app);
app.listen(PORT, function() {
console.log(`🌎 ==> Server now on port ${PORT}!`);
});