Reply
Highlighted
Member
Posts: 3
Registered: ‎12-16-2020

PHP SDK won't process transactions

Trying to place transactions in Sandbox mode while adding Authorize.net to a site. Copied pretty much exactly the example and it throws this:

 

[{"code":"E00027","text":"The transaction was unsuccessful."}]

Here's my actual code (in Laravel):

 

/* Create a merchantAuthenticationType object with authentication details
        retrieved from the constants file */
        $merchantAuthentication = new AnetAPI\MerchantAuthenticationType();
        $merchantAuthentication->setName('secret');
        $merchantAuthentication->setTransactionKey('secretKey');
        
        // Set the transaction's refId
        $refId = 'ref' . time();

        // Create the payment data for a credit card
        $creditCard = new AnetAPI\CreditCardType();
        $creditCard->setCardNumber('4111111111111111');
        $creditCard->setExpirationDate('2021-11');
        $creditCard->setCardCode('123');

        // Add the payment data to a paymentType object
        $paymentOne = new AnetAPI\PaymentType();
        $paymentOne->setCreditCard($creditCard);

        // Create order information
        $order = new AnetAPI\OrderType();

        // Set the customer's Bill To address
        $customerAddress = new AnetAPI\CustomerAddressType();
        $customerAddress->setFirstName("Ellen");
        $customerAddress->setLastName("Johnson");
        $customerAddress->setAddress(request('address'));
        $customerAddress->setCity(request('city'));
        $customerAddress->setState(request('state'));
        $customerAddress->setZip(request('postalCode'));

        if (request('country') == 'US') {
            $country = 'USA';
        } else {
            $country = request('country');
        }

        $customerAddress->setCountry($country);

        // Set the customer's identifying information
        $customerData = new AnetAPI\CustomerDataType();
        $customerData->setType("individual");
        $customerData->setEmail(request('email'));

        // Add values for transaction settings
        $duplicateWindowSetting = new AnetAPI\SettingType();
        $duplicateWindowSetting->setSettingName("duplicateWindow");
        $duplicateWindowSetting->setSettingValue("60");

        // Create a TransactionRequestType object and add the previous objects to it
        $transactionRequestType = new AnetAPI\TransactionRequestType();
        $transactionRequestType->setTransactionType("authCaptureTransaction");
        $transactionRequestType->setAmount(request('amount'));
        $transactionRequestType->setOrder($order);
        $transactionRequestType->setPayment($paymentOne);
        $transactionRequestType->setBillTo($customerAddress);
        $transactionRequestType->setCustomer($customerData);
        $transactionRequestType->addToTransactionSettings($duplicateWindowSetting);

        // Assemble the complete transaction request
        $request = new AnetAPI\CreateTransactionRequest();
        $request->setMerchantAuthentication($merchantAuthentication);
        $request->setRefId($refId);
        $request->setTransactionRequest($transactionRequestType);

        // Create the controller and get the response
        $controller = new AnetController\CreateTransactionController($request);
        $response = $controller->executeWithApiResponse(\net\authorize\api\constants\ANetEnvironment::SANDBOX);
        

        if ($response != null) {
            // Check to see if the API request was successfully received and acted upon
            if ($response->getMessages()->getResultCode() == "Ok") {
                // Since the API request was successful, look for a transaction response
                // and parse it to display the results of authorizing the card
                $tresponse = $response->getTransactionResponse();
            
                if ($tresponse != null && $tresponse->getMessages() != null) {
                    return response()->json([
                        'success' => true,
                        'id' => $refId
                    ]);
                } else {
                    return response()->json([
                        'message' => $response->getMessages()->getResultCode(),
                        'alt_message' => $tresponse->getMessages()
                    ]);
                }
                // Or, print errors if the API request wasn't messageful
            } else {
                $tresponse = $response->getTransactionResponse();

                // this is where its erroring
                return response()->json([
                    'message' => $response->getMessages(),
                    'alt_message' => $tresponse->getMessages(),
                ]);
            }
        } else {
            $tresponse = $response->getTransactionResponse();

            return response()->json([
                'message' => $response->getMessages()->getResultCode(),
                'alt_message' => $tresponse->getMessages(),
            ]);
        }

        $tresponse = $response->getTransactionResponse();

        return response()->json([
            'message' => $response->getMessages()->getResultCode(),
            'alt_message' => $tresponse->getMessages(),
        ]);