Created
October 30, 2019 15:58
-
-
Save sadam1807/bc1912d14a3a1f7fc891d9a6d5d8fff2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express'); | |
const bodyParser = require('body-parser'); | |
const crypto = require('crypto'); | |
const squareConnect = require('square-connect'); | |
const dotenv = require('dotenv'); | |
dotenv.config(); | |
const app = express(); | |
const port = 4000; | |
var cors = require('cors'); | |
app.use(cors()) | |
// Set the Access Token | |
const accessToken = process.env.ACCESS_TOKEN | |
app.use(bodyParser.json()); | |
app.use(bodyParser.urlencoded({ extended: false })); | |
app.use(express.static(__dirname)); | |
// Set Square Connect credentials and environment | |
const defaultClient = squareConnect.ApiClient.instance; | |
// Configure OAuth2 access token for authorization: oauth2 | |
const oauth2 = defaultClient.authentications['oauth2']; | |
oauth2.accessToken = accessToken; | |
// Set 'basePath' to switch between sandbox env and production env | |
// sandbox: https://connect.squareupsandbox.com | |
// production: https://connect.squareup.com | |
defaultClient.basePath = 'https://connect.squareupsandbox.com'; | |
app.post('/process-payment', async (req, res) => { | |
const request_params = req.body; | |
// length of idempotency_key should be less than 45 | |
const idempotency_key = crypto.randomBytes(22).toString('hex'); | |
// Charge the customer's card | |
const payments_api = new squareConnect.PaymentsApi(); | |
const request_body = { | |
source_id: request_params.nonce, | |
amount_money: { | |
amount: 100, // £1.00 charge | |
currency: 'GBP' | |
}, | |
idempotency_key: idempotency_key | |
}; | |
try { | |
const response = await payments_api.createPayment(request_body); | |
res.status(200).json({ | |
'title': 'Payment Successful', | |
'result': response | |
}); | |
} catch(error) { | |
res.status(500).json({ | |
'title': 'Payment Failure', | |
'result': error.response.text | |
}); | |
} | |
}); | |
app.listen( | |
port, | |
() => console.log(`listening on - http://localhost:${port}`) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment