Reply
Contributor
Posts: 17
Registered: ‎03-11-2019

Accept Hosted FORM ERROR (REDIRECT METHOD)

[ Edited ]

I get the error message below in the console.log using Chrome browser. 

1) The token is received correctly.

2) When the page returns from https://test.authorize.net/payment/payment

     it has nothing but Order Summary on it and the following error message in the console below.

 

Any help you can give is appreciated (been fighting this  for over a week)

 

payment:24 Uncaught SyntaxError: Invalid or unexpected token

 

CODE OVERVIEW:

//-----------------------------------------------------------
    //authenticate your merchant info
    //-----------------------------------------------------------
    $mbrobj = new members(71870);
    $ahaobj = new accepted_host_authorize(null);
    $ahaobj->set_total_transaction('120.00');
    $ahaobj->set_tranid('123456');
    $ahaobj->set_mbrobj($mbrobj);
    //-----------------------------------------------------------
    //This sets the refId  field with the invoice number
    //-----------------------------------------------------------
    $ahaobj->set_invoice($invoice);
    //-----------------------------------------------------------
    //MERCHANT AUTHENTICATE
    //-----------------------------------------------------------
    $maobj  = $ahaobj->merchant_authenticate();
    //-------------------------------------------------
    // NOW GENERATE THE FIELDS FOR THE POST
    //-------------------------------------------------
    $ahaobj->set_mbr_obj($mbrobj);
    //-----------------------------------------------------------
    //SET UP ORDER
    //-----------------------------------------------------------
    $order_obj = $ahaobj->set_order(null,"TGCA ORDER");
    //-----------------------------------------------------------
    //ADD LINE ITEM
    //-----------------------------------------------------------
    $ahaobj->add_line_item('0','TEST','TEST','1','120.00');
    //-----------------------------------------------------------
    //ADD CUSTOMER DATA
    //-----------------------------------------------------------
    $customer_data_obj = $ahaobj->set_customer_data($mbrobj);
    //-----------------------------------------------------------
    //ADD BILL TO
    //-----------------------------------------------------------
    $bill_to_obj = $ahaobj->set_bill_to_info($mbrobj);

    //-----------------------------------------------------------
    //SET HOSTSTED FORM OPTIONS
    //-----------------------------------------------------------
//-----------------------------------------------------------
    //SET HOSTSTED FORM OPTIONS
    //-----------------------------------------------------------
    $hosted_settings_ary = $ahaobj->set_hosted_form_options($mbrobj,
                                        ' http://tgca_dev.jdoherty.net/php/sadispatch.php?event=renew_ccpost&first_time=init',
                                        ' http://tgca_dev.jdoherty.net/php/sadispatch.php?event=renew_ccpost&first_time=cancel');
    //-----------------------------------------------------------
    //CREATE TRANSACTIONS
    //-----------------------------------------------------------
    $trans_type_obj = $ahaobj->create_transaction('120.00',
                                                      $customer_data_obj,
                                                      $bill_to_obj,
                                                      $order_obj);


    //-----------------------------------------------------------
    //get the token from payment server
    //-----------------------------------------------------------
    $resp = $ahaobj->get_an_token($inv,$trans_type_obj,$hosted_settings_ary);


============  GET_AN_TOKEN=============


function get_an_token($invoice,AnetAPI\TransactionRequestType $trans_type_obj,array $settings_ary)
     {
        $pay_page_request_obj = new AnetAPI\GetHostedPaymentPageRequest();
        $maobj = $this->merchant_authenticate();
        //$this->set_up_accepted_host_options();
        //-----------------------------------------------------------
        //set Merchant Authenticate
        //-----------------------------------------------------------
        $pay_page_request_obj->setMerchantAuthentication($maobj);
        //-----------------------------------------------------------
        //SET REF ID
        //-----------------------------------------------------------
        $pay_page_request_obj->setRefId($invoice);//refId is the invioce
        //-----------------------------------------------------------
        //SET TRANSACTION TYPE
        //-----------------------------------------------------------
        $pay_page_request_obj->setTransactionRequest($trans_type_obj);
        //-----------------------------------------------------------
        //SET HostedPaymentSetting
        //-----------------------------------------------------------
        foreach($settings_ary as $skey => $sval)
        {
            $pay_page_request_obj->addToHostedPaymentSettings($sval);
        }

        //-----------------------------------------------------------
        //EXECUTE REQUEST
        //-----------------------------------------------------------
        $controller = new AnetController\GetHostedPaymentPageController($pay_page_request_obj);
        if($this->mode == '1' or $this->mode == '4')
        {
            $response = $controller->executeWithApiResponse(\net\authorize\api\constants\ANetEnvironment::SANDBOX);
        }
        elseif($this->mode == '3')
        {
            $response = $controller->executeWithApiResponse(\net\authorize\api\constants\ANetEnvironment::PRODUCTION);

        }
        else
        {
            $this->logEvent("ERROR: UNKNOWN MODE[".$this->mode."] ***** SPECIFIED *****");
            return;


}
        //-----------------------------------------------------------
        //SET RESPONSE
        //-----------------------------------------------------------
        if (($response != null) && ($response->getMessages()->getResultCode() == "Ok"))
        {
            $this->token = $response->getToken();
            if($this->DEBUG){$this->logEvent("INFO: TOKEN RECEIVED[".$this->token."]");}
            $message = $response->getMessages()->getMessage();
            foreach($message as $mkey => $mval)
            {
                $this->logEvent("INFO: ".$message[$mkey]->getCode().
                " ".$message[$mkey]->getText());
            }
        }
        else
        {
            $this->logEvent("ERROR: Failed to get hosted payment page token");
            $error_message = $response->getMessages()->getMessage();
            $this->logEvent("ERROR: ".$error_message[0]->getCode()." ".$error_message[0]->getText());
        }
        return($response);

     }

============== set_hosted_form_options ==============
function set_hosted_form_options($mbrobj,$x_realy_url,$x_cancel_url)
     {
        $this->seting_incr = 0;
        $setting1 = new AnetAPI\SettingType();
        $setting1->setSettingName("hostedPaymentButtonOptions");
        $setting1->setSettingValue("{\"text\": \"Submit Payment\"}");

        $setting2 = new AnetAPI\SettingType();
        $setting2->setSettingName("hostedPaymentStyleOptions");
        $setting2->setSettingValue("{\"bgColor\": \"red\"}");

        $setting3 = new AnetAPI\SettingType();
        $setting3->setSettingName("hostedPaymentOrderOptions");
        $setting3->setSettingValue("{\"show\": true,\"merchantName\":\"Texas Girls Coaches Assoc.\"}");

        $setting4 = new AnetAPI\SettingType();
        $setting4->setSettingName("hostedPaymentReturnOptions");
        $x_relay_url = $this->anobj->get_field('x_relay_url');
        $x_relay_url .= "&invid=";
        $x_relay_url .= $this->get_invoice();
        $x_cancel_url = $this->anobj->get_field('x_cancel_url');
        $x_cancel_url .= "&invid=";
        $x_cancel_url .= $this->get_invoice();
       $setting4->setSettingValue("{\"url\": \"".$x_relay_url."\", \"cancelUrl\": \"".$x_cancel_url."\", \"showReceipt\": true}");

        $setting5 = new AnetAPI\SettingType();
        $setting5->setSettingName("hostedPaymentPaymentOptions");
        $setting5->setSettingValue("{\"cardCodeRequired\":true,\"showCreditCard\": true,\"showBankAccount\":true}");

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

        $setting7 = new AnetAPI\SettingType();
        $setting7->setSettingName("hostedPaymentBillingAddressOptions");
        $setting7->setSettingValue("{\"show\":true}");

        $setting8 = new AnetAPI\SettingType();
        $setting8->setSettingName("hostedPaymentCustomerOptions");
        $setting8->setSettingValue("{\"showEmail\":true,\"requiredEmail\":true}");
        //-----------------------------------------------------------
        //Now set the Hosted Payment Options in the page Request
        //-----------------------------------------------------------
        $cnt = 0;
        $hosted_payment_setting[$cnt++] = $setting1;
        $hosted_payment_setting[$cnt++] = $setting2;
        $hosted_payment_setting[$cnt++] = $setting3;
        $hosted_payment_setting[$cnt++] = $setting4;
        $hosted_payment_setting[$cnt++] = $setting5;
        $hosted_payment_setting[$cnt++] = $setting6;
        $hosted_payment_setting[$cnt++] = $setting7;
        $hosted_payment_setting[$cnt++] = $setting8;

        return($hosted_payment_setting);
}

 


main.bundle.js:1 ERROR ReferenceError: g_errorPanelMessage is not defined
   at e.ngOnInit (main.bundle.js:1)
   at main.bundle.js:1
   at main.bundle.js:1
   at el (main.bundle.js:1)
   at Ol (main.bundle.js:1)
   at Object.updateDirectives (main.bundle.js:1)
   at Object.updateDirectives (main.bundle.js:1)
   at Object.Qi [as checkAndUpdateView] (main.bundle.js:1)
   at e.detectChanges (main.bundle.js:1)
   at main.bundle.js:1
Se @ main.bundle.js:1
e.handleError @ main.bundle.js:1
(anonymous) @ main.bundle.js:1
t.invoke @ polyfills.bundle.js:1
n.run @ polyfills.bundle.js:1
e.runOutsideAngular @ main.bundle.js:1
e.tick @ main.bundle.js:1
e._loadComponent @ main.bundle.js:1
e.bootstrap @ main.bundle.js:1
(anonymous) @ main.bundle.js:1
e._moduleDoBootstrap @ main.bundle.js:1
(anonymous) @ main.bundle.js:1
t.invoke @ polyfills.bundle.js:1
onInvoke @ main.bundle.js:1
t.invoke @ polyfills.bundle.js:1
n.run @ polyfills.bundle.js:1
(anonymous) @ polyfills.bundle.js:1
t.invokeTask @ polyfills.bundle.js:1
onInvokeTask @ main.bundle.js:1
t.invokeTask @ polyfills.bundle.js:1
n.runTask @ polyfills.bundle.js:1

All Star
Posts: 577
Registered: ‎11-05-2018

Re: Accept Hosted FORM ERROR (REDIRECT METHOD)

@jmd804500 

 

Not sure what is causing this just glancing at it, but you seem to be making things more complicated than they have to be.  I do not think it is your form being called, as you are calling the form in PHP.  Your console is giving you a js syntax error. Back to your script, is there any reason you put your script together like this as opposed to just using the sample code and modifying it? The sample code works very well and it looks like you're putting together your own thing (or modifying a SIM script). The sample code has been tested and works and there isn't really a reason to start from scratch. 

Contributor
Posts: 17
Registered: ‎03-11-2019

Re: Accept Hosted FORM ERROR (REDIRECT METHOD)

We use classes all I have done is to encapsulate the regular SDK in a class to allow it fit into our system ("Which is much larger than the authorize.net part).

 

Renaissance,

 

does the order and such look correct? 

 

Jim

Contributor
Posts: 17
Registered: ‎03-11-2019

Re: Accept Hosted FORM ERROR (REDIRECT METHOD)

my read of the documentation is that if I don't specify a payment method then the API will put a credit card in there. Is there some API call in hostedPayment Options which needs to be specified?

Contributor
Posts: 17
Registered: ‎03-11-2019

Re: Accept Hosted FORM ERROR (REDIRECT METHOD)

EXAMPLE: merchant_authenticate

function merchant_authenticate()
     {
        if($this->DEBUG)
        {
            $this->logEvent("INFO: URL  [".$this->anobj->get_field('url')."]");
            $this->logEvent("INFO: LOGIN[".$this->anobj->get_field('login')."]");
            $this->logEvent("INFO: XTKEY[".$this->anobj->get_field('x_trans_key')."]");
        }
        $this->maobj = new AnetAPI\MerchantAuthenticationType();
        $this->maobj->setName($this->anobj->get_field('login'));
        $this->maobj->setTransactionKey($this->anobj->get_field('x_trans_key'));
        return($this->maobj);
     }

EXAMPLE: create_transaction

function create_transaction($amount,$customer_obj,$bill_to_obj,$order_obj)
     {
            $transactionRequestType = new AnetAPI\TransactionRequestType();
            $transactionRequestType->setTransactionType( "authCaptureTransaction");
            $transactionRequestType->setAmount($amount);
            $transactionRequestType->setOrder($order_obj);
            $transactionRequestType->setCustomer($customer_obj);
            $transactionRequestType->setBillTo($bill_to_obj);
            //$transactionRequestType->setPayment($paymentOne);
            //-----------------------------------------------------------
            //get the line items
            //-----------------------------------------------------------
            $lineItems = $this->get_line_items();
            $transactionRequestType->setLineItems($lineItems);

        return($transactionRequestType);
     }

EXAMPLE add_line_Item:

 

function add_line_item($item_id,$name,$desc,$qty,$unit_price,$taxable=0)
     {
        if($this->DEBUG)
        {
            $this->logEvent("INFO: ITEM ID [".$item_id."]");
            $this->logEvent("INFO: NAMe    [".$name."]");
            $this->logEvent("INFO: DESC    [".$desc."]");
            $this->logEvent("INFO: QTY     [".$qty."]");
            $this->logEvent("INFO: UNIT PR [".$unit_price."]");
            $this->logEvent("INFO: TAXABLE [".$taxable."]");
        }
            $lineItem1 = new AnetAPI\LineItemType();
            $lineItem1->setItemId($item_id);
            $lineItem1->setName($name);
            $lineItem1->setDescription($desc);
            $lineItem1->setQuantity($qty);
            $lineItem1->setUnitPrice($unit_price);
            $lineItem1->setTaxable(0); // 1 Yes 0 for no
            $this->lineItems_ary[$this->linc++] = $lineItem1;
      }

EXAMPLE: set_customer_data

 

function set_customer_data(members $mbrobj) 
   { 
       // Customer info 
       $customer = new AnetAPI\CustomerDataType(); 
       $customer->setId($mbrobj->get_field('id')); 
       $customer->setEmail($mbrobj->get_field('email_three')); 
       $customer->setType("individual"); 
       return($customer); 
   }

EXAMPLE: set_bill_to_info(mbrobj) <<-- mbrobj is an object with members name address etc.

 

function set_bill_to_info($mbrobj)
    {
            // Bill To
            $billto = new AnetAPI\CustomerAddressType();
            $billto->setFirstName($mbrobj->get_field('fname'));
            $billto->setLastName($mbrobj->get_field('lname'));
            $billto->setAddress($mbrobj->get_field('address_one'));
            $billto->setCity($mbrobj->get_field('city'));
            $billto->setZip($mbrobj->get_field('zip'));
            $billto->setCountry("USA");
            return($billto);

    }
Highlighted
All Star
Posts: 577
Registered: ‎11-05-2018

Re: Accept Hosted FORM ERROR (REDIRECT METHOD)

@jmd804500 

 

I can't figure out what you are doing without looking at this for much longer than I can budget, unfortunately. You are making things overly complicated. There is no need to make a custom function for each part of the hosted form API call. Your problem will be solved almost instantly if you copy and paste the sample PHP code from the reference. You will just have to add your line items and the other fields as necessary. The sample code is missing several properties and is just something basic that works and that you can start with.  It will get you there much faster than this.