I am trying to create a customer payment profile with the Node SDK using the sample code. While I am able to create a customer profile successfully and receive a successful response from the API call
const httpStatus = require("http-status"); const ApiContracts = require("authorizenet").APIContracts; const ApiControllers = require("authorizenet").APIControllers; const SDKConstants = require("authorizenet").Constants; const User = require("../models/user.model"); const RefreshToken = require("../models/refreshToken.model"); const moment = require("moment-timezone"); const { jwtExpirationInterval } = require("../../config/vars"); const sgMail = require("@sendgrid/mail"); const bcrypt = require("bcryptjs"); const CustomerProfilesModule = require("../utils/authorizeNet/CustomerProfiles"); sgMail.setApiKey(process.env.SENDGRID_API_KEY.replace(/\r?\n|\r/g, "")); exports.register = async (req, res, next) => { try { const userData = req.body; let customerProfileResult = await CustomerProfilesModule.createCustomerProfile(userData); console.log( "❌ ❌ ❌ ❌ ❌ customerProfile Result ", customerProfileResult ); if (!userData || userData.error) { return next(error); } else { const { isTrial } = userData; const user = await new User(userData).save(); const token = generateTokenResponse(user, user.token()); res.status(httpStatus.CREATED); return res.json({ token, user }); } } catch (error) { console.log(error.message); return next(User.checkDuplicateEmail(error)); } };create-customer-profile.js
"use strict"; var ApiContracts = require("authorizenet").APIContracts; var ApiControllers = require("authorizenet").APIControllers; var utils = require("../utils.js"); async function createCustomerProfile(user) { console.log(" user parameter", user); var merchantAuthenticationType = new ApiContracts.MerchantAuthenticationType(); merchantAuthenticationType.setName(process.env.AUTHORIZE_NET_API_LOGIN_KEY); merchantAuthenticationType.setTransactionKey( process.env.AUTHORIZE_NET_TRANSACTION_KEY ); var creditCard = new ApiContracts.CreditCardType(); creditCard.setCardNumber(user.cardNumber); if (user.cardExpiry.length > 4) { creditCard.setExpirationDate( `${user.cardExpiry.slice(0, 1)}${user.cardExpiry.slice(3, 4)}` ); } else { creditCard.setExpirationDate(user.cardExpiry); } console.log("creditCard", creditCard); var paymentType = new ApiContracts.PaymentType(); paymentType.setCreditCard(creditCard); var customerAddress = new ApiContracts.CustomerAddressType(); customerAddress.setFirstName(user.firstName); customerAddress.setLastName(user.lastName); customerAddress.setAddress(user.mailingAddress); customerAddress.setCity(user.mailingCity); customerAddress.setState(user.mailingState); customerAddress.setZip(user.mailingZip); customerAddress.setCountry("USA"); customerAddress.setPhoneNumber(user.userPhone); var customerPaymentProfileType = new ApiContracts.CustomerPaymentProfileType(); customerPaymentProfileType.setCustomerType( ApiContracts.CustomerTypeEnum.INDIVIDUAL ); customerPaymentProfileType.setPayment(paymentType); customerPaymentProfileType.setBillTo(customerAddress); var paymentProfilesList = []; paymentProfilesList.push(customerPaymentProfileType); console.log( "paymentProfilesList", paymentProfilesList ); var customerProfileType = new ApiContracts.CustomerProfileType(); customerProfileType.setMerchantCustomerId( "M_" + utils.getRandomString("cust") ); customerProfileType.setDescription( `${user.firstName} ${user.lastName}'s Account'` ); customerProfileType.setEmail(user.userEmail); customerProfileType.setPaymentProfiles(paymentProfilesList); var createRequest = new ApiContracts.CreateCustomerProfileRequest(); createRequest.setProfile(customerProfileType); createRequest.setValidationMode(ApiContracts.ValidationModeEnum.TESTMODE); createRequest.setMerchantAuthentication(merchantAuthenticationType); var ctrl = new ApiControllers.CreateCustomerProfileController( createRequest.getJSON() ); // All above code is ran when CustomerProfilesModule.createCustomerProfile(userData) is executed in auth.controller.js // However the following line (line 130 in auth.controller.js) is ran before the below ctrl.execute() code is completed // // console.log("❌ ❌ ❌ ❌ ❌ customerProfile Result ", customerProfileResult); // // All the above code is executed before that console.log("❌ ❌ ❌") statement above, however the below code doesn't run before that console.log // I'd like the below code to execute before the remaining register route is finished, but just don't know what is going on! ctrl.execute(async function () { var apiResponse = await ctrl.getResponse(); console.log("apiResponse", apiResponse); var response = new ApiContracts.CreateCustomerProfileResponse(apiResponse); console.log("response", response); //pretty print response //console.log(JSON.stringify(response, null, 2)); if (response != null) { if ( response.getMessages().getResultCode() == ApiContracts.MessageTypeEnum.OK ) { console.log( "Successfully created a customer profile with id: " + response.getCustomerProfileId() ); } else { console.log("Result Code: " + response.getMessages().getResultCode()); console.log( "Error Code: " + response.getMessages().getMessage()[0].getCode() ); console.log( "Error message: " + response.getMessages().getMessage()[0].getText() ); return { error: "Error message: " + response.getMessages().getMessage()[0].getText(), }; } } else { console.log("Null response received"); return { error: "Null response received" }; } }); } module.exports.createCustomerProfile = createCustomerProfile;
12-29-2021 03:00 PM