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

Node.js E00007 Authorize.net error - User authentication failed due to invalid authentication values

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

E00007 User authentication failed due to invalid authentication values.

我看过很多指南,但它们通常都在 Java 或 PHP 中。许多指南都指出需要设置 ctrl.setEnvironment(SDKConstants.endpoint.production);

当 运行 本地节点 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));
            })
       })

您的沙盒帐户和生产帐户是独立的,没有任何关联。 https://sandbox.authorize.net/ URL 是沙盒环境,您必须在此处使用您的沙盒凭据。如果您的代码在沙箱中工作,它将无法在生产中工作,直到您将您的凭据更新为您的生产凭据 并且 将您的代码设置为指向生产 URLs。

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