Formit & Authorize.net php sdk

I was hoping this would be a straight forward setup but I am getting very lost in the weeds of confusion.

I have downloaded the Authorize.net PHP SDK (2.0.2) and using a sandbox account.

Using Authorize.net / hosted payment redirect.
https://developer.authorize.net/api/reference/features/accept-hosted.html#Integrating_the_Form_using_a_Redirect

Current setup
Page / FormIt with “amount” & “invoice name” put in by user

FormItcall
[[!FormIt?
&hooks=spam,assets/authorize/authorizePaymentForm.php
&validate=amount:required, invoice:required
]]

authorizePaymentForm.php

Issue
// this works if amount is never touched
$transactionRequestType->setAmount(250);

( problem - if I change this to a variable from the form or even just a variable at all)

// code above authorize $amount = $_REQUEST[‘amount’];
$transactionRequestType->setAmount($amount);

// i’ve even tried - just in case - still doesn’t work
// $fixAmount = number_format($amount, 2, ‘.’, ‘’ );

All I get is “Missing or invalid token.”

This is a one off project so I am looking for a quote on getting this working.

Can you post a code snippet? How are you passing your token to the API?

The issue could be related to type, the reason 250 works is because it is an int32, however accessing the $_REQUEST could be returning a string. Try type casting to an int intval($_REQUEST['amount']);.

Also, when using formit, its normally better to access the value through the hook i.e. $hook->getValue('amount')

Thank you for the reply! I tried that and even $amount = floatval($_REQUEST[‘amount’]);. Still no luck.

I can’t use the call below - as this only works in snippets and I am calling a php file directly.
$hook->getValue('amount')

Here is the full code if it helps. Yes that’s some primo duct tape coding (i know)…

<!DOCTYPE html>
<html>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0">
<head>

</head>
<body>
	
<style type="text/css">
label {
display: block;
margin: 5px 0px;
color: #AAA;
}
input {
display: block;
}
input[type=submit] {
margin-top: 20px;
}
.centered {
  position: fixed;
  top: 25%;
  left: 50%;
  /* bring your own prefixes */
  transform: translate(-50%, -50%);
  text-align:center;
}
</style>
<h1 class="centered">Processing Please Wait...</h1>
 <form method="post" action="https://test.authorize.net/payment/payment" id="formAuthorizeNetTestPage" name="formAuthorizeNetTestPage">
		<input type="hidden" name="token" value="<?php
											
  $amount = floatval($_REQUEST['amount']);
												 
  require 'vendor/autoload.php';
  require_once 'constants.php';
  use net\authorize\api\contract\v1 as AnetAPI;
  use net\authorize\api\controller as AnetController;
												 
  define("AUTHORIZENET_LOG_FILE", "phplog");
												  
function getAnAcceptPaymentPage()
{
    /* Create a merchantAuthenticationType object with authentication details
       retrieved from the constants file */
    $merchantAuthentication = new AnetAPI\MerchantAuthenticationType();
    $merchantAuthentication->setName(\SampleCodeConstants::MERCHANT_LOGIN_ID);
    $merchantAuthentication->setTransactionKey(\SampleCodeConstants::MERCHANT_TRANSACTION_KEY);
    
    // Set the transaction's refId
    $refId = 'ref' . time();

    //create a transaction
    $transactionRequestType = new AnetAPI\TransactionRequestType();
    $transactionRequestType->setTransactionType("authCaptureTransaction");
	$transactionRequestType->setAmount($amount);

    // Set Hosted Form options
    $setting1 = new AnetAPI\SettingType();
    $setting1->setSettingName("hostedPaymentButtonOptions");
    $setting1->setSettingValue("{\"text\": \"Pay\"}");

    $setting2 = new AnetAPI\SettingType();
    $setting2->setSettingName("hostedPaymentOrderOptions");
    $setting2->setSettingValue("{\"show\": true, \"merchantName\": \"Merchant Name\"}");

	$setting2a = new AnetAPI\SettingType();
    $setting2a->setSettingName("hostedPaymentPaymentOptions");
    $setting2a->setSettingValue("{\"cardCodeRequired\": true}");
	
	$setting2b = new AnetAPI\SettingType();
    $setting2b->setSettingName("hostedPaymentCustomerOptions");
    $setting2b->setSettingValue("{\"showEmail\": true, \"requiredEmail\": true, \"addPaymentProfile\": true}");
	
	$setting2c = new AnetAPI\SettingType();
    $setting2c->setSettingName("hostedPaymentSecurityOptions");
    $setting2c->setSettingValue("{\"captcha\": true}");
	
    $setting3 = new AnetAPI\SettingType();
    $setting3->setSettingName("hostedPaymentReturnOptions");
    $setting3->setSettingValue(
        "{\"url\": \"https://mysite.com/receipt\", \"cancelUrl\": \"https://mysite.com/cancel\", \"showReceipt\": false}"
    );


    // Build transaction request
    $request = new AnetAPI\GetHostedPaymentPageRequest();
    $request->setMerchantAuthentication($merchantAuthentication);
    $request->setRefId($refId);
    $request->setTransactionRequest($transactionRequestType);

    $request->addToHostedPaymentSettings($setting1);
    $request->addToHostedPaymentSettings($setting2);
	$request->addToHostedPaymentSettings($setting2a);
	$request->addToHostedPaymentSettings($setting2b);
	$request->addToHostedPaymentSettings($setting2c);
    $request->addToHostedPaymentSettings($setting3);
    
    //execute request
    $controller = new AnetController\GetHostedPaymentPageController($request);
    $response = $controller->executeWithApiResponse(\net\authorize\api\constants\ANetEnvironment::SANDBOX);
    
    if (($response != null) && ($response->getMessages()->getResultCode() == "Ok")) {
        echo $response->getToken()."\n";
    } else {
        echo "ERROR :  Failed to get hosted payment page token\n";
        $errorMessages = $response->getMessages()->getMessage();
        echo "RESPONSE : " . $errorMessages[0]->getCode() . "  " .$errorMessages[0]->getText() . "\n";
    }
    return $response;
}
if (!defined('DONT_RUN_SAMPLES')) {
    getAnAcceptPaymentPage();
}
												 
		?>" />
		<button id="btnContinue">Continue to next page</button>
	</form>  

<script type='text/javascript'>document.formAuthorizeNetTestPage.submit();</script>
	
</body>
</html>

@rcarnrick Thanks for sending the example. I’ve moved it out of a function and to the top of the page to make it easier to read and debug.

Run that and paste the results of the debug, might help narrow down the error

<?php

require 'vendor/autoload.php';
require_once 'constants.php';


$amount = floatval($_REQUEST['amount']);
$token = null;

$debug = [
    'amount' => $amount,
    'MERCHANT_LOGIN_ID' => \SampleCodeConstants::MERCHANT_LOGIN_ID,
    'MERCHANT_TRANSACTION_KEY' => \SampleCodeConstants::MERCHANT_TRANSACTION_KEY
];

use net\authorize\api\contract\v1 as AnetAPI;
use net\authorize\api\controller as AnetController;

define("AUTHORIZENET_LOG_FILE", "phplog");

/* Create a merchantAuthenticationType object with authentication details
   retrieved from the constants file */
$merchantAuthentication = new AnetAPI\MerchantAuthenticationType();
$merchantAuthentication->setName(\SampleCodeConstants::MERCHANT_LOGIN_ID);
$merchantAuthentication->setTransactionKey(\SampleCodeConstants::MERCHANT_TRANSACTION_KEY);

// Set the transaction's refId
$refId = 'ref' . time();

//create a transaction
$transactionRequestType = new AnetAPI\TransactionRequestType();
$transactionRequestType->setTransactionType("authCaptureTransaction");
$transactionRequestType->setAmount($amount);

// Set Hosted Form options
$setting1 = new AnetAPI\SettingType();
$setting1->setSettingName("hostedPaymentButtonOptions");
$setting1->setSettingValue('{"text": "Pay"}');

$setting2 = new AnetAPI\SettingType();
$setting2->setSettingName("hostedPaymentOrderOptions");
$setting2->setSettingValue('{"show": true, "merchantName": "Merchant Name"}');

$setting2a = new AnetAPI\SettingType();
$setting2a->setSettingName("hostedPaymentPaymentOptions");
$setting2a->setSettingValue('{"cardCodeRequired": true}');

$setting2b = new AnetAPI\SettingType();
$setting2b->setSettingName("hostedPaymentCustomerOptions");
$setting2b->setSettingValue('{"showEmail": true, "requiredEmail": true, "addPaymentProfile": true}');

$setting2c = new AnetAPI\SettingType();
$setting2c->setSettingName("hostedPaymentSecurityOptions");
$setting2c->setSettingValue('{"captcha": true}');

$setting3 = new AnetAPI\SettingType();
$setting3->setSettingName("hostedPaymentReturnOptions");
$setting3->setSettingValue(
    '{"url": "https://mysite.com/receipt", "cancelUrl": "https://mysite.com/cancel", "showReceipt": false}'
);


// Build transaction request
$request = new AnetAPI\GetHostedPaymentPageRequest();
$request->setMerchantAuthentication($merchantAuthentication);
$request->setRefId($refId);
$request->setTransactionRequest($transactionRequestType);

$request->addToHostedPaymentSettings($setting1);
$request->addToHostedPaymentSettings($setting2);
$request->addToHostedPaymentSettings($setting2a);
$request->addToHostedPaymentSettings($setting2b);
$request->addToHostedPaymentSettings($setting2c);
$request->addToHostedPaymentSettings($setting3);

//execute request
$controller = new AnetController\GetHostedPaymentPageController($request);
$response = $controller->executeWithApiResponse(\net\authorize\api\constants\ANetEnvironment::SANDBOX);
$debug["response"] = $response->getMessages();

if (($response != null) && ($response->getMessages()->getResultCode() == "Ok")) {
    $token = $response->getToken();
} else {
    $debug["ERROR"] = "Failed to get hosted payment page token";
    $debug["errorMessages"] = $response->getMessages()->getMessage();
    $debug["RESPONSE"] = $errorMessages[0]->getCode() . "  " . $errorMessages[0]->getText();
}

?>
<!DOCTYPE html>
<html>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0">

<head>

</head>

<body>

    <style type="text/css">
        label {
            display: block;
            margin: 5px 0px;
            color: #AAA;
        }

        input {
            display: block;
        }

        input[type=submit] {
            margin-top: 20px;
        }

        .centered {
            position: fixed;
            top: 25%;
            left: 50%;
            /* bring your own prefixes */
            transform: translate(-50%, -50%);
            text-align: center;
        }
    </style>
    <h1 class="centered">Processing Please Wait...</h1>
    <form method="post" action="https://test.authorize.net/payment/payment" id="formAuthorizeNetTestPage" name="formAuthorizeNetTestPage">
        <input type="hidden" name="token" value="<?= $token ?>" />
        <button id="btnContinue">Continue to next page</button>
    </form>

    <script type='text/javascript'>
        document.formAuthorizeNetTestPage.submit();
    </script>

    <!-- Debug helpers-->
    <pre>
        <?php
            echo print_r($debug, true);
        ?>
    </pre>

</body>

</html>