|
| 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; |
0 commit comments