Can't add new card for existing customer in authorize.net- Node JS

57 views Asked by At

I'm new to authorize net. Created an API to create a customer in authorize.net using this doc link]

I have added my code below. In this case, the customer is getting created fine and the card details are also getting saved. But now I want the same customer to be able to add another card to their account. I want it to be done in node js. But I can't find a code for that in node.js in their doc. Can anyone help me with this? Thanks in advance.

This is the code I used

    async createCustomerProfile(callback) {
    console.log('id =====', process.env.AUTHORIZE_LOGIN_ID)

    var merchantAuthenticationType = new ApiContracts.MerchantAuthenticationType();
    merchantAuthenticationType.setName(process.env.AUTHORIZE_LOGIN_ID);
    merchantAuthenticationType.setTransactionKey(process.env.AUTHORIZE_TRANSACTION_KEY);

    var creditCard = new ApiContracts.CreditCardType();
    creditCard.setCardNumber('4111111111111111');
    creditCard.setExpirationDate('0823');

    var paymentType = new ApiContracts.PaymentType();
    paymentType.setCreditCard(creditCard);

    var customerAddress = new ApiContracts.CustomerAddressType();
    customerAddress.setFirstName('test');
    customerAddress.setLastName('scenario');
    customerAddress.setAddress('123 Main Street');
    customerAddress.setCity('Bellevue');
    customerAddress.setState('WA');
    customerAddress.setZip('98004');
    customerAddress.setCountry('USA');
    customerAddress.setPhoneNumber('000-000-0000');

    var customerPaymentProfileType = new ApiContracts.CustomerPaymentProfileType();
    customerPaymentProfileType.setCustomerType(ApiContracts.CustomerTypeEnum.INDIVIDUAL);
    customerPaymentProfileType.setPayment(paymentType);
    customerPaymentProfileType.setBillTo(customerAddress);

    var paymentProfilesList = [];
    paymentProfilesList.push(customerPaymentProfileType);

    var customerProfileType = new ApiContracts.CustomerProfileType();
    customerProfileType.setMerchantCustomerId('MP_test');
    customerProfileType.setDescription('Profile description here');
    customerProfileType.setEmail('[email protected]');
    customerProfileType.setPaymentProfiles(paymentProfilesList);

    var createRequest = new ApiContracts.CreateCustomerProfileRequest();
    createRequest.setProfile(customerProfileType);
    createRequest.setValidationMode(ApiContracts.ValidationModeEnum.TESTMODE);
    createRequest.setMerchantAuthentication(merchantAuthenticationType);

    //pretty print request
    //console.log(JSON.stringify(createRequest.getJSON(), null, 2));


    var ctrl = new ApiControllers.CreateCustomerProfileController(createRequest.getJSON());

    ctrl.execute(function () {

        var apiResponse = ctrl.getResponse();

        var response = new ApiContracts.CreateCustomerProfileResponse(apiResponse);

        //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());
            }
        }
        else {
            console.log('Null response received');
        }
        console.log('response ===============', response)
        callback(response);
    });
},
0

There are 0 answers