问题
I am using a javascript file that runs transactions via node. (node create-transaction.js in the terminal returns 200 :) Right now I have a post request that posts something called opaquedata from my client side to my api in my routes file and it works like charm! My only questions is how do i replace the static opaque data inside the boilerplate create-transaction.js file as seen below, with the dynamic opaqudata I am receiving in my api post request. Then how do I get it to run the file automatically or does it just do it by itself when the api is hit. Any help is appreciated
routes.js
router.post('/', function(req, res) {
res.json({response: "You sent me a POST request",
opaqueData:req.body.createTransactionRequest.transactionRequest.payment.opaqueData,
body: req.body});//Do whatever...
});
create-transaction.js
var ApiContracts = require('authorizenet').APIContracts;
var ApiControllers = require('authorizenet').APIControllers;
var SDKConstants = require('authorizenet').Constants;
var utils = require('./utils.js');
var constants = require('./constants.js');
function createAnAcceptPaymentTransaction(callback) {
var merchantAuthenticationType = new ApiContracts.MerchantAuthenticationType();
merchantAuthenticationType.setName(constants.apiLoginKey);
merchantAuthenticationType.setTransactionKey(constants.transactionKey);
var opaqueData = new ApiContracts.OpaqueDataType();
opaqueData.setDataDescriptor('COMMON.ACCEPT.INAPP.PAYMENT');
//needs to change into dynamic value from my post request in routes.js
opaqueData.setDataValue('eyJjb2RlIjoiNTBfMl8wNjAwMDUzRDdFMDU4NzVBMkYxQjNBM0NBQUU2N0RFNEYxMTgwQzQzRDk2NTZEREQ5MjdEODU2NTExQ0YyQzBBMDY0QUM2MUZEMUM3RTczNDI1QTQyNzA0M0FDNEM2MDk3MEI4RDZGIiwidG9rZW4iOiI5NTk0MTQwMTE2NDE4MzM1NzAzNjAxIiwidiI6IjEuMSJ9');
var createRequest = new ApiContracts.CreateTransactionRequest();
createRequest.setMerchantAuthentication(merchantAuthenticationType);
createRequest.setTransactionRequest(transactionRequestType);
//pretty print request
console.log(JSON.stringify(createRequest.getJSON(), null, 2));
var ctrl = new ApiControllers.CreateTransactionController(createRequest.getJSON());
//Defaults to sandbox
//ctrl.setEnvironment(SDKConstants.endpoint.production);
ctrl.execute(function(){
var apiResponse = ctrl.getResponse();
var response = new ApiContracts.CreateTransactionResponse(apiResponse);
//pretty print response
console.log(JSON.stringify(response, null, 2));
if(response != null){
if(response.getMessages().getResultCode() == ApiContracts.MessageTypeEnum.OK){
if(response.getTransactionResponse().getMessages() != null){
console.log('Successfully created transaction with Transaction ID: ' + response.getTransactionResponse().getTransId());
console.log('Response Code: ' + response.getTransactionResponse().getResponseCode());
console.log('Message Code: ' + response.getTransactionResponse().getMessages().getMessage()[0].getCode());
console.log('Description: ' + response.getTransactionResponse().getMessages().getMessage()[0].getDescription());
}
else {
console.log('Failed Transaction.');
if(response.getTransactionResponse().getErrors() != null){
console.log('Error Code: ' + response.getTransactionResponse().getErrors().getError()[0].getErrorCode());
console.log('Error message: ' + response.getTransactionResponse().getErrors().getError()[0].getErrorText());
}
}
}
else {
console.log('Failed Transaction. ');
if(response.getTransactionResponse() != null && response.getTransactionResponse().getErrors() != null){
console.log('Error Code: ' + response.getTransactionResponse().getErrors().getError()[0].getErrorCode());
console.log('Error message: ' + response.getTransactionResponse().getErrors().getError()[0].getErrorText());
}
else {
console.log('Error Code: ' + response.getMessages().getMessage()[0].getCode());
console.log('Error message: ' + response.getMessages().getMessage()[0].getText());
}
}
}
else {
console.log('Null Response.');
}
callback(response);
});
}
if (require.main === module) {
createAnAcceptPaymentTransaction(function(){
console.log('createAnAcceptPaymentTransaction call complete.');
});
}
module.exports.createAnAcceptPaymentTransaction = createAnAcceptPaymentTransaction;
来源:https://stackoverflow.com/questions/62780956/i-would-like-to-automatically-hit-an-api-with-dynamic-values-from-my-client-side