Skip to content

Commit 1dad14e

Browse files
committed
first draft still lots to do
1 parent cee1517 commit 1dad14e

File tree

12 files changed

+1826
-0
lines changed

12 files changed

+1826
-0
lines changed

express-oauth/.DS_Store

6 KB
Binary file not shown.

express-oauth/app.js

+213
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
var createError = require('http-errors');
2+
var express = require('express');
3+
var path = require('path');
4+
var cookieParser = require('cookie-parser');
5+
var logger = require('morgan');
6+
7+
var indexRouter = require('./routes/index');
8+
var usersRouter = require('./routes/users');
9+
10+
var cybersourceRestApi = require('cybersource-rest-client');
11+
12+
// common parameters
13+
const AuthenticationType = 'http_signature';
14+
const RunEnvironment = 'cybersource.environment.SANDBOX';
15+
const MerchantId = 'testrest';
16+
17+
// http_signature parameters
18+
const MerchantKeyId = '08c94330-f618-42a3-b09d-e1e43be5efda';
19+
const MerchantSecretKey = 'yBJxy6LjM2TmcPGu+GaJrHtkke25fPpUX+UY6/L/1tE=';
20+
21+
// jwt parameters
22+
const KeysDirectory = 'Resource';
23+
const KeyFileName = 'testrest';
24+
const KeyAlias = 'testrest';
25+
const KeyPass = 'testrest';
26+
27+
// logging parameters
28+
const EnableLog = true;
29+
const LogFileName = 'cybs';
30+
const LogDirectory = '../log';
31+
const LogfileMaxSize = '5242880'; //10 MB In Bytes
32+
33+
34+
var configObj = {
35+
'authenticationType': AuthenticationType,
36+
'runEnvironment': RunEnvironment,
37+
38+
'merchantID': MerchantId,
39+
'merchantKeyId': MerchantKeyId,
40+
'merchantsecretKey': MerchantSecretKey,
41+
42+
'keyAlias': KeyAlias,
43+
'keyPass': KeyPass,
44+
'keyFileName': KeyFileName,
45+
'keysDirectory': KeysDirectory,
46+
47+
'enableLog': EnableLog,
48+
'logFilename': LogFileName,
49+
'logDirectory': LogDirectory,
50+
'logFileMaxSize': LogfileMaxSize
51+
};
52+
53+
54+
var app = express();
55+
56+
// view engine setup
57+
app.set('views', path.join(__dirname, 'views'));
58+
app.set('view engine', 'ejs');
59+
60+
app.use(logger('dev'));
61+
app.use(express.json());
62+
app.use(express.urlencoded({ extended: false }));
63+
app.use(cookieParser());
64+
app.use(express.static(path.join(__dirname, 'public')));
65+
66+
app.use('/', indexRouter);
67+
app.use('/users', usersRouter);
68+
69+
// THIS IS THE SERVER-SIDE REQUEST TO GENERATE THE DYNAMIC KEY
70+
// REQUIRED FOR THE MICROFORM TO TOKENIZE
71+
app.get('/checkout', function (req, res) {
72+
73+
try {
74+
var instance = new cybersourceRestApi.KeyGenerationApi(configObj);
75+
76+
var request = new cybersourceRestApi.GeneratePublicKeyRequest();
77+
request.encryptionType = 'RsaOaep256';
78+
request.targetOrigin = 'http://localhost:3000';
79+
80+
var opts = [];
81+
opts['format'] = 'JWT';
82+
83+
console.log('\n*************** Generate Key ********************* ');
84+
85+
instance.generatePublicKey(request, opts, function (error, data, response) {
86+
if (error) {
87+
console.log('Error : ' + error);
88+
console.log('Error status code : ' + error.statusCode);
89+
}
90+
else if (data) {
91+
console.log('Data : ' + JSON.stringify(data));
92+
console.log('CaptureContext: '+data.keyId);
93+
res.render('index', { keyInfo: JSON.stringify(data.keyId)});
94+
}
95+
console.log('Response : ' + JSON.stringify(response));
96+
console.log('Response Code Of GenerateKey : ' + response['status']);
97+
callback(error, data);
98+
});
99+
100+
} catch (error) {
101+
console.log(error);
102+
}
103+
104+
});
105+
106+
// THIS ROUTE SIMPLY POWERS THE TOKEN PAGE TO DISPLAY THE TOKEN
107+
// NOTE THIS IS AN INTERIM STEP FOR THE SAMPLE AND WOULD OBVIOUSLY
108+
// NOT BE PART OR A REAL CHECKOUT FLOW
109+
app.post('/token', function (req, res) {
110+
111+
try {
112+
113+
console.log('Response : ' + req.body.flexresponse);
114+
var tokenResponse = JSON.parse(req.body.flexresponse)
115+
116+
res.render('token', { flexresponse: req.body.flexresponse} );
117+
118+
} catch (error) {
119+
res.send('Error : ' + error + ' Error status code : ' + error.statusCode);
120+
}
121+
122+
123+
});
124+
125+
// THIS REPRESENTS THE SERVER-SIDE REQUEST TO MAKE A PAYMENT WITH THE TRANSIENT
126+
// TOKEN
127+
app.post('/receipt', function (req, res) {
128+
129+
var tokenResponse = JSON.parse(req.body.flexresponse)
130+
console.log('Transient token for payment is: ' + JSON.stringify(tokenResponse));
131+
132+
try {
133+
134+
var instance = new cybersourceRestApi.PaymentsApi(configObj);
135+
136+
var clientReferenceInformation = new cybersourceRestApi.Ptsv2paymentsClientReferenceInformation();
137+
clientReferenceInformation.code = 'test_flex_payment';
138+
139+
var processingInformation = new cybersourceRestApi.Ptsv2paymentsProcessingInformation();
140+
processingInformation.commerceIndicator = 'internet';
141+
142+
var amountDetails = new cybersourceRestApi.Ptsv2paymentsOrderInformationAmountDetails();
143+
amountDetails.totalAmount = '102.21';
144+
amountDetails.currency = 'USD';
145+
146+
var billTo = new cybersourceRestApi.Ptsv2paymentsOrderInformationBillTo();
147+
billTo.country = 'US';
148+
billTo.firstName = 'John';
149+
billTo.lastName = 'Deo';
150+
billTo.phoneNumber = '4158880000';
151+
billTo.address1 = 'test';
152+
billTo.postalCode = '94105';
153+
billTo.locality = 'San Francisco';
154+
billTo.administrativeArea = 'MI';
155+
billTo.email = '[email protected]';
156+
billTo.address2 = 'Address 2';
157+
billTo.district = 'MI';
158+
billTo.buildingNumber = '123';
159+
160+
var orderInformation = new cybersourceRestApi.Ptsv2paymentsOrderInformation();
161+
orderInformation.amountDetails = amountDetails;
162+
orderInformation.billTo = billTo;
163+
164+
// EVERYTHING ABOVE IS JUST NORMAL PAYMENT INFORMATION
165+
// THIS IS WHERE YOU PLUG IN THE MICROFORM TRANSIENT TOKEN
166+
var tokenInformation = new cybersourceRestApi.Ptsv2paymentsTokenInformation();
167+
tokenInformation.transientTokenJwt = tokenResponse;
168+
169+
var request = new cybersourceRestApi.CreatePaymentRequest();
170+
request.clientReferenceInformation = clientReferenceInformation;
171+
request.processingInformation = processingInformation;
172+
request.orderInformation = orderInformation;
173+
request.tokenInformation = tokenInformation;
174+
175+
console.log('\n*************** Process Payment ********************* ');
176+
177+
instance.createPayment(request, function (error, data, response) {
178+
if (error) {
179+
console.log('\nError in process a payment : ' + JSON.stringify(error));
180+
}
181+
else if (data) {
182+
console.log('\nData of process a payment : ' + JSON.stringify(data));
183+
res.render('receipt', { paymentResponse: JSON.stringify(data)} );
184+
185+
}
186+
console.log('\nResponse of process a payment : ' + JSON.stringify(response));
187+
console.log('\nResponse Code of process a payment : ' + JSON.stringify(response['status']));
188+
callback(error, data);
189+
});
190+
191+
} catch (error) {
192+
console.log(error);
193+
}
194+
195+
});
196+
197+
// catch 404 and forward to error handler
198+
app.use(function(req, res, next) {
199+
next(createError(404));
200+
});
201+
202+
// error handler
203+
app.use(function(err, req, res, next) {
204+
// set locals, only providing error in development
205+
res.locals.message = err.message;
206+
res.locals.error = req.app.get('env') === 'development' ? err : {};
207+
208+
// render the error page
209+
res.status(err.status || 500);
210+
res.render('error');
211+
});
212+
213+
module.exports = app;

express-oauth/bin/www

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#!/usr/bin/env node
2+
3+
/**
4+
* Module dependencies.
5+
*/
6+
7+
var app = require('../app');
8+
var debug = require('debug')('express-microform:server');
9+
var http = require('http');
10+
11+
/**
12+
* Get port from environment and store in Express.
13+
*/
14+
15+
var port = normalizePort(process.env.PORT || '3000');
16+
app.set('port', port);
17+
18+
/**
19+
* Create HTTP server.
20+
*/
21+
22+
var server = http.createServer(app);
23+
24+
/**
25+
* Listen on provided port, on all network interfaces.
26+
*/
27+
28+
server.listen(port);
29+
server.on('error', onError);
30+
server.on('listening', onListening);
31+
32+
/**
33+
* Normalize a port into a number, string, or false.
34+
*/
35+
36+
function normalizePort(val) {
37+
var port = parseInt(val, 10);
38+
39+
if (isNaN(port)) {
40+
// named pipe
41+
return val;
42+
}
43+
44+
if (port >= 0) {
45+
// port number
46+
return port;
47+
}
48+
49+
return false;
50+
}
51+
52+
/**
53+
* Event listener for HTTP server "error" event.
54+
*/
55+
56+
function onError(error) {
57+
if (error.syscall !== 'listen') {
58+
throw error;
59+
}
60+
61+
var bind = typeof port === 'string'
62+
? 'Pipe ' + port
63+
: 'Port ' + port;
64+
65+
// handle specific listen errors with friendly messages
66+
switch (error.code) {
67+
case 'EACCES':
68+
console.error(bind + ' requires elevated privileges');
69+
process.exit(1);
70+
break;
71+
case 'EADDRINUSE':
72+
console.error(bind + ' is already in use');
73+
process.exit(1);
74+
break;
75+
default:
76+
throw error;
77+
}
78+
}
79+
80+
/**
81+
* Event listener for HTTP server "listening" event.
82+
*/
83+
84+
function onListening() {
85+
var addr = server.address();
86+
var bind = typeof addr === 'string'
87+
? 'pipe ' + addr
88+
: 'port ' + addr.port;
89+
debug('Listening on ' + bind);
90+
}

0 commit comments

Comments
 (0)