首页 > 解决方案 > Node.js E00007 Authorize.net 错误 - 由于身份验证值无效,用户身份验证失败

问题描述

我已经被这个问题困扰了一段时间,看起来生成令牌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));
            })
       })

标签: node.jspayment-gatewaypaymentauthorize.net

解决方案


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

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


推荐阅读