Node.js E00007 Authorize.net 错误 - 由于身份验证值无效,用户身份验证失败

Posted

技术标签:

【中文标题】Node.js E00007 Authorize.net 错误 - 由于身份验证值无效,用户身份验证失败【英文标题】:Node.js E00007 Authorize.net error - User authentication failed due to invalid authentication values 【发布时间】:2021-02-28 04:06:30 【问题描述】:

我已经被这个问题困扰了一段时间,看起来生成令牌 getMerchantDetails 正在工作。但是,第二个功能createAnAcceptPaymentTransaction在付款时不起作用

E00007 由于身份验证值无效,用户身份验证失败。

我查看了许多指南,但它们通常都是用 Java 或 php 编写的。很多指南都指向ctrl.setEnvironment(SDKConstants.endpoint.production);需要设置。

在本地运行 Node API 时付款有效,但在生产站点上运行时,它只会抛出错误 E00007。

我正在使用https://sandbox.authorize.net/ - 我是否必须使用生产帐户才能在生产环境中进行测试?即使我的沙盒帐户设置为实时模式?

欢迎提出建议

const ApiContracts = require('authorizenet').APIContracts;
const ApiControllers = require('authorizenet').APIControllers;
const SDKConstants = require('authorizenet').Constants;

    async getMerchantDetails() 
        const merchantAuthenticationType = new ApiContracts.MerchantAuthenticationType();
        merchantAuthenticationType.setName(config.authorizeNet.apiLoginKey);
        merchantAuthenticationType.setTransactionKey(config.authorizeNet.transactionKey);

        const getRequest = new ApiContracts.GetMerchantDetailsRequest();
        getRequest.setMerchantAuthentication(merchantAuthenticationType);

        console.log(JSON.stringify(getRequest.getJSON(), null, 2));

        const ctrl = new ApiControllers.GetMerchantDetailsController(getRequest.getJSON());
        ctrl.setEnvironment(SDKConstants.endpoint.production);

        return new Promise(function (resolve, reject) 
            ctrl.execute(async () => 
                const apiResponse = ctrl.getResponse();
                const response = new ApiContracts.GetMerchantDetailsResponse(apiResponse);

                console.log(JSON.stringify(response, null, 2));
           )
       )
   
    createAnAcceptPaymentTransaction(setDataValue, checkout) 
        const merchantAuthenticationType = new ApiContracts.MerchantAuthenticationType();
        merchantAuthenticationType.setName(config.authorizeNet.apiLoginKey);
        merchantAuthenticationType.setTransactionKey(config.authorizeNet.transactionKey);

        const opaqueData = new ApiContracts.OpaqueDataType();
        opaqueData.setDataDescriptor('COMMON.ACCEPT.INAPP.PAYMENT');
        opaqueData.setDataValue(setDataValue);

        const paymentType = new ApiContracts.PaymentType();
        paymentType.setOpaqueData(opaqueData);

        const orderDetails = new ApiContracts.OrderType();
        orderDetails.setInvoiceNumber(`$checkout?.id?.slice(checkout?.id?.length - 16)`);
        orderDetails.setDescription('Generated by API');

        const tax = new ApiContracts.ExtendedAmountType();
        tax.setAmount('0');
        tax.setName('Base tax');
        tax.setDescription('Base tax is set to 0');

        const duty = new ApiContracts.ExtendedAmountType();
        duty.setAmount('0');
        duty.setName('Duty');
        duty.setDescription('Duty is set to 0');

        let shipping;
        if (checkout?.meta?.shippingPrice) 
            shipping = new ApiContracts.ExtendedAmountType();
            shipping.setAmount(checkout?.meta?.shippingPrice / 100);
            shipping.setName(checkout?.meta?.shippingName);
            shipping.setDescription(`ShipStation - $checkout?.meta?.shippingCode`);
        

        const billTo = new ApiContracts.CustomerAddressType();

        const name = checkout.billing.fullName.split(' ');
        billTo.setFirstName(name.slice(0, name.length - 1).join(' '));
        billTo.setLastName(name[name.length]);
        // billTo.setCompany()
        billTo.setAddress(`$checkout.shipping.streetOne $checkout.shipping.streetTwo`);
        billTo.setCity(checkout.shipping.city);
        billTo.setState(checkout.shipping.county);
        billTo.setZip(checkout.shipping.postcode);
        billTo.setCountry(checkout.shipping.country);

        const shipTo = new ApiContracts.CustomerAddressType();
        const billName = checkout.shipping.fullName.split(' ');
        shipTo.setFirstName(billName.slice(0, billName.length - 1).join(' '));
        shipTo.setLastName(billName[billName.length]);
        shipTo.setAddress(`$checkout.shipping.streetOne $checkout.shipping.streetTwo`);
        shipTo.setCity(checkout.shipping.city);
        shipTo.setState(checkout.shipping.county);
        shipTo.setZip(checkout.shipping.postcode);
        shipTo.setCountry(checkout.shipping.country);

        const lineItemList = [];

        checkout.products.map(product => 
            const productLine = new ApiContracts.LineItemType();

            productLine.setItemId(product._id);
            productLine.setName(AuthorizeNetClass.cutString(product.data.name));
            productLine.setDescription(AuthorizeNetClass.cutString(product.data.description));
            productLine.setQuantity(product.quantity);
            productLine.setUnitPrice(product.data.price / 100);
            lineItemList.push(productLine);
        );

        const lineItems = new ApiContracts.ArrayOfLineItem();
        lineItems.setLineItem(lineItemList);

        const transactionSetting1 = new ApiContracts.SettingType();
        transactionSetting1.setSettingName('duplicateWindow');
        transactionSetting1.setSettingValue('120');

        const transactionSetting2 = new ApiContracts.SettingType();
        transactionSetting2.setSettingName('recurringBilling');
        transactionSetting2.setSettingValue('false');

        const transactionSettingList = [];
        transactionSettingList.push(transactionSetting1);
        transactionSettingList.push(transactionSetting2);

        const transactionSettings = new ApiContracts.ArrayOfSetting();
        transactionSettings.setSetting(transactionSettingList);

        const transactionRequestType = new ApiContracts.TransactionRequestType();
        transactionRequestType.setTransactionType(
            ApiContracts.TransactionTypeEnum.AUTHCAPTURETRANSACTION
        );
        transactionRequestType.setPayment(paymentType);
        transactionRequestType.setAmount(checkout.meta.total / 100);
        transactionRequestType.setLineItems(lineItems);
        // transactionRequestType.setUserFields(userFields);
        transactionRequestType.setOrder(orderDetails);
        transactionRequestType.setTax(tax);
        transactionRequestType.setDuty(duty);
        if (checkout?.meta?.shippingPrice) 
            transactionRequestType.setShipping(shipping);
        
        transactionRequestType.setBillTo(billTo);
        transactionRequestType.setShipTo(shipTo);
        transactionRequestType.setTransactionSettings(transactionSettings);

        const createRequest = new ApiContracts.CreateTransactionRequest();
        createRequest.setMerchantAuthentication(merchantAuthenticationType);
        createRequest.setTransactionRequest(transactionRequestType);

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

        const ctrl = new ApiControllers.CreateTransactionController(createRequest.getJSON());
        //Defaults to sandbox
        ctrl.setEnvironment(SDKConstants.endpoint.production);

        return new Promise(function (resolve, reject) 
            ctrl.execute(async () => 
                const apiResponse = ctrl.getResponse();

                const response = new ApiContracts.CreateTransactionResponse(apiResponse);
                console.log(JSON.stringify(response, null, 2));
            )
       )

【问题讨论】:

【参考方案1】:

您的沙盒帐户和生产帐户是分开的,没有任何关系。 https://sandbox.authorize.net/ URL 是沙盒环境,您必须在此处使用您的沙盒凭据。如果您的代码在沙盒中运行,则在您将凭据更新为生产凭据将代码设置为指向生产 URL 之前,它不会在生产中运行。

您可以在生产环境中进行测试,但您需要确保您未处于实时模式,否则您会为需要连接到您的商家帐户的任何交易(即付款、作废、退款)产生费用。一般来说,在沙盒环境中进行测试更简单,只需更新您的配置以使用生产环境,并在您准备好接受交易时使用正确的凭据。

【讨论】:

以上是关于Node.js E00007 Authorize.net 错误 - 由于身份验证值无效,用户身份验证失败的主要内容,如果未能解决你的问题,请参考以下文章

为 Java、Python、Ruby、Node.JS 和 PHP 开放支付网关库

无法在 Node JS 中使用 JWT 访问受保护的路由

验证 spotify web api 不起作用

Authorize.Net Paypal Express PHP API:交易未在 Authorize.Net 中更新,也未捕获正确金额的问题

Authorize.net : 底层连接已关闭

重力表格和 Authorize.net 提要