I’ve created the below code in nuxt3 as a serverside api ‘server/api’ and I am calling this using: const { payresponse } = await $fetch( '/api/cybersource/', {}
However I cannot get the return value from const response = await instance.createPayment
When I’ve tried console.log. this is alway fired before the console.log within the instance even though I’ve used await
'use strict';
import cybersourceRestApi from 'cybersource-rest-client';
export default defineEventHandler( async (event) => {
const AuthenticationType = 'http_signature';
const RunEnvironment = 'apitest.cybersource.com';
const MerchantId = 'testrest';
// http_signature parameters
const MerchantKeyId = '08c94330-f618-42a3-b09d-e1e43be5efda';
const MerchantSecretKey = 'yBJxy6LjM2TmcPGu+GaJrHtkke25fPpUX+UY6/L/1tE=';
// jwt parameters
const KeysDirectory = 'Resource';
const KeyFileName = 'testrest';
const KeyAlias = 'testrest';
const KeyPass = 'testrest';
//meta key parameters
const UseMetaKey = false;
const PortfolioID = '';
// logging parameters
const EnableLog = true;
const LogFileName = 'cybs';
const LogDirectory = 'log';
const LogfileMaxSize = '5242880'; //10 MB In Bytes
const EnableMasking = true;
var merchantDetails = {
'authenticationType': AuthenticationType,
'runEnvironment': RunEnvironment,
'merchantID': MerchantId,
'merchantKeyId': MerchantKeyId,
'merchantsecretKey': MerchantSecretKey,
'keyAlias': KeyAlias,
'keyPass': KeyPass,
'keyFileName': KeyFileName,
'keysDirectory': KeysDirectory,
'useMetaKey': UseMetaKey,
'portfolioID': PortfolioID,
//'pemFileDirectory': PemFileDirectory,
'logConfiguration': {
'enableLog': EnableLog,
'logFileName': LogFileName,
'logDirectory': LogDirectory,
'logFileMaxSize': LogfileMaxSize,
'loggingLevel': 'debug',
'enableMasking': EnableMasking
}
}
try {
var configObject = merchantDetails;
var apiClient = new cybersourceRestApi.ApiClient();
var requestObj = new cybersourceRestApi.CreatePaymentRequest();
var clientReferenceInformation = new cybersourceRestApi.Ptsv2paymentsClientReferenceInformation();
clientReferenceInformation.code = 'TC50171_3';
requestObj.clientReferenceInformation = clientReferenceInformation;
var processingInformation = new cybersourceRestApi.Ptsv2paymentsProcessingInformation();
processingInformation.capture = true;
requestObj.processingInformation = processingInformation;
var paymentInformation = new cybersourceRestApi.Ptsv2paymentsPaymentInformation();
var paymentInformationCard = new cybersourceRestApi.Ptsv2paymentsPaymentInformationCard();
paymentInformationCard.number = req.cardnumber;
paymentInformationCard.expirationMonth = req.expirymonth;
paymentInformationCard.expirationYear = req.expiryyear;
paymentInformation.card = paymentInformationCard;
requestObj.paymentInformation = paymentInformation;
var orderInformation = new cybersourceRestApi.Ptsv2paymentsOrderInformation();
var orderInformationAmountDetails = new cybersourceRestApi.Ptsv2paymentsOrderInformationAmountDetails();
orderInformationAmountDetails.totalAmount = req.amount;//'122.00';
orderInformationAmountDetails.currency = req.currency;//'GBP';
orderInformation.amountDetails = orderInformationAmountDetails;
var orderInformationBillTo = new cybersourceRestApi.Ptsv2paymentsOrderInformationBillTo();
orderInformationBillTo.firstName = req.cardholderf;
orderInformationBillTo.lastName = req.cardholderl;//'Doe';
orderInformationBillTo.address1 = req.address1;//'1 Market St';
orderInformationBillTo.locality = req.address2;//'san francisco';
orderInformationBillTo.administrativeArea = req.state;//'CA';
orderInformationBillTo.postalCode = req.postcodde;//'94105';
orderInformationBillTo.country = req.country;//'GB';
orderInformationBillTo.email = req.email;//'[email protected]';
orderInformationBillTo.phoneNumber = req.phone;//'4158880000';
orderInformation.billTo = orderInformationBillTo;
requestObj.orderInformation = orderInformation;
var instance = await new cybersourceRestApi.PaymentsApi(configObject, apiClient);
const response = await instance.createPayment(requestObj, function (error, data, response) {
if (error) {
console.log('nError : ' + JSON.stringify(error));
}
else if (data) {
console.log('nData : ' + JSON.stringify(data));
}
console.log('nResponse : ' + JSON.stringify(response));
console.log('nResponse Code of Process a Payment : ' + JSON.stringify(response['status']));
return {
error: error,
data: data,
response: response
};
});
return response;
}
catch (error) {
console.log('nException on calling the API : ' + error);
return { error };
}
}
else
{
return false;
}
}
);