0

I am trying to follow this example by authorize.net on a simple page for testing purposes

https://developer.authorize.net/api/reference/features/accept_hosted.html#Integrating_the_Form_Using_Iframes_and_Lightboxes

I am using the iFrame/ Lightbox variation, and am creating the Form Token with this PHP SDK snippet:

        use net\authorize\api\contract\v1 as AnetAPI;
        use net\authorize\api\controller as AnetController;    
        $merchantAuthentication = new AnetAPI\MerchantAuthenticationType();
        $merchantAuthentication->setName(\CodeConstants::MERCHANT_LOGIN_ID);
        $merchantAuthentication->setTransactionKey(\CodeConstants::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 now\"}");

        $setting2 = new AnetAPI\SettingType();
        $setting2->setSettingName("hostedPaymentOrderOptions");
        $setting2->setSettingValue("{\"show\": false}");

        $setting3 = new AnetAPI\SettingType();
        $setting3->setSettingName("hostedPaymentReturnOptions");
        $setting3->setSettingValue("{\"showReceipt\": false, \"url\" : \"https://website.com/communicator.html\"}");

        $setting4 = new AnetAPI\SettingType();
        $setting4->setSettingName("hostedPaymentBillingAddressOptions");
        $setting4->setSettingValue("{\"show\": false, \"required\": false}");


        $setting5 = new AnetAPI\SettingType();
        $setting5->setSettingName("hostedPaymentIFrameCommunicatorUrl");
        $setting5->setSettingValue("{\"url\": \"https://website.com/communicator.html\"}");

        $setting6 = new AnetAPI\SettingType();
        $setting6->setSettingName("hostedPaymentPaymentOptions");
        $setting6->setSettingValue("{\"cardCodeRequired\": true, \"showBankAccount\": 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($setting3);
        $request->addToHostedPaymentSettings($setting4);
        $request->addToHostedPaymentSettings($setting5);
        $request->addToHostedPaymentSettings($setting6);

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

        if (($response != null) && ($response->getMessages()->getResultCode() == "Ok")) {
            return $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 FALSE;
        }

which is working fine, I get a token and can proceed with this code to have on the webpage (copied from the authorize.net tutorial)

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0">
<script src="scripts/jquery.min.js"></script>
<script src="scripts/bootstrap.min.js"></script>
<title>HostedPayment Test Page</title>
<style type="text/css">
    body {
        margin: 0px;
        padding: 0px;
        }

    #divAuthorizeNetPopupScreen {
        left: 0px;
        top: 0px;
        width: 100%;
        height: 100%;
        z-index: 1;
        background-color: #808080;
        opacity: 0.5;
        -ms-filter: 'progid:DXImageTransform.Microsoft.Alpha(Opacity=50)';
        filter: alpha(opacity=50);
        }

    #divAuthorizeNetPopup {
        position: absolute;
        left: 50%;
        top: 50%;
        margin-left: -200px;
        margin-top: -200px;
        z-index: 2;
        overflow: visible;
        }

    .AuthorizeNetPopupGrayFrameTheme .AuthorizeNetPopupOuter {
        background-color: #dddddd;
        border-width: 1px;
        border-style: solid;
        border-color: #a0a0a0 #909090 #909090 #a0a0a0;
        padding: 4px;
        }

    .AuthorizeNetPopupGrayFrameTheme .AuthorizeNetPopupTop {
        height: 23px;
        }

    .AuthorizeNetPopupGrayFrameTheme .AuthorizeNetPopupClose {
        position: absolute;
        right: 7px;
        top: 7px;
        }

    .AuthorizeNetPopupGrayFrameTheme .AuthorizeNetPopupClose a {
        background-image: url('content/closeButton1.png');
        background-repeat: no-repeat;
        height: 16px;
        width: 16px;
        display: inline-block;
        }

    .AuthorizeNetPopupGrayFrameTheme .AuthorizeNetPopupClose a:hover {
        background-image: url('content/closeButton1h.png');
        }

    .AuthorizeNetPopupGrayFrameTheme .AuthorizeNetPopupClose a:active {
        background-image: url('content/closeButton1a.png');
        }

    .AuthorizeNetPopupGrayFrameTheme .AuthorizeNetPopupInner {
        background-color: #ffffff;
        border-width: 2px;
        border-style: solid;
        border-color: #cfcfcf #ebebeb #ebebeb #cfcfcf;
        }

    .AuthorizeNetPopupGrayFrameTheme .AuthorizeNetPopupBottom {
        height: 30px;
        }

    .AuthorizeNetPopupGrayFrameTheme .AuthorizeNetPopupLogo {
        position: absolute;
        right: 9px;
        bottom: 4px;
        width: 200px;
        height: 25px;
        background-image: url('content/powered_simple.png');
        }

    .AuthorizeNetPopupSimpleTheme .AuthorizeNetPopupOuter {
        border: 1px solid #585858;
        background-color: #ffffff;
        }
</style>
</head>

<body>
<form method="post" action="https://test.authorize.net/payment/payment" id="formAuthorizeNetPopup" name="formAuthorizeNetPopup" target="iframeAuthorizeNet" style="display:none;">
    <input type="hidden" id="popupToken" name="token" value="Replace with form token from getHostedPaymentPageResponse" />
</form>        
<input type="text" id="inputtoken" value="" />
<br />
<div>
    Trigger Accept Transaction
    <button id="btnOpenAuthorizeNetPopup" onclick="AuthorizeNetPopup.openPopup()">Open AuthorizeNetPopup</button>
</div>
<div id="divAuthorizeNetPopup" style="display:none;" class="AuthorizeNetPopupGrayFrameTheme">
    <div class="AuthorizeNetPopupOuter">
        <div class="AuthorizeNetPopupTop">
            <div class="AuthorizeNetPopupClose">
                <a href="javascript:;" onclick="AuthorizeNetPopup.closePopup();" title="Close"> </a>
            </div>
        </div>
        <div class="AuthorizeNetPopupInner">
            <iframe name="iframeAuthorizeNet" id="iframeAuthorizeNet" src="empty.html" frameborder="0" scrolling="no"></iframe>
        </div>
        <div class="AuthorizeNetPopupBottom">
            <div class="AuthorizeNetPopupLogo" title="Powered by Authorize.Net"></div>
        </div>
        <div id="divAuthorizeNetPopupScreen" style="display:none;"></div>
    </div>
</div>

<script type="text/javascript">
    (function () {
        if (!window.AuthorizeNetPopup) window.AuthorizeNetPopup = {};
        if (!AuthorizeNetPopup.options) AuthorizeNetPopup.options = {
            onPopupClosed: null
            };

        AuthorizeNetPopup.closePopup = function () {
            document.getElementById("divAuthorizeNetPopupScreen").style.display = "none";
            document.getElementById("divAuthorizeNetPopup").style.display = "none";
            document.getElementById("iframeAuthorizeNet").src = "empty.html";
            document.getElementById("btnOpenAuthorizeNetPopup").disabled = false;
            if (AuthorizeNetPopup.options.onPopupClosed) AuthorizeNetPopup.options.onPopupClosed();
            };


        AuthorizeNetPopup.openPopup = function () {
            var popup = document.getElementById("divAuthorizeNetPopup");
            var popupScreen = document.getElementById("divAuthorizeNetPopupScreen");
            var ifrm = document.getElementById("iframeAuthorizeNet");
            var form = document.forms["formAuthorizeNetPopup"];
            $("#popupToken").val($("#inputtoken").val());
            form.action = "https://test.authorize.net/payment/payment";
            ifrm.style.width = "442px";
            ifrm.style.height = "1000px";

            form.submit();

            popup.style.display = "";
            popupScreen.style.display = "";
            centerPopup();
            };

        AuthorizeNetPopup.onReceiveCommunication = function (querystr) {
                var params = parseQueryString(querystr);
                switch (params["action"]) {
                    case "successfulSave":
                        AuthorizeNetPopup.closePopup();
                        break;
                    case "cancel":
                        AuthorizeNetPopup.closePopup();
                        break;
                    case "transactResponse":
                        var response = params["response"];
                        document.getElementById("token").value = response;
                        AuthorizeNetPopup.closePopup();
                        break;
                    case "resizeWindow":
                        var w = parseInt(params["width"]);
                        var h = parseInt(params["height"]);
                        var ifrm = document.getElementById("iframeAuthorizeNet");
                        ifrm.style.width = w.toString() + "px";
                        ifrm.style.height = h.toString() + "px";
                        centerPopup();
                        break;
                    }
            };


        function centerPopup() {
            var d = document.getElementById("divAuthorizeNetPopup");
            d.style.left = "50%";
            d.style.top = "50%";
            var left = -Math.floor(d.clientWidth / 2);
            var top = -Math.floor(d.clientHeight / 2);
            d.style.marginLeft = left.toString() + "px";
            d.style.marginTop = top.toString() + "px";
            d.style.zIndex = "2";
            if (d.offsetLeft < 16) {
                d.style.left = "16px";
                d.style.marginLeft = "0px";
                }
            if (d.offsetTop < 16) {
                d.style.top = "16px";
                d.style.marginTop = "0px";
                }
            }

        function parseQueryString(str) {
                var vars = [];
                var arr = str.split('&');
                var pair;
                for (var i = 0; i < arr.length; i++) {
                    pair = arr[i].split('=');
                    vars.push(pair[0]);
                    vars[pair[0]] = unescape(pair[1]);
                }
                return vars;
            }
    }());

</script>
</body>
</html>    

Basically I just followed the tutorial... but when i click on "pay", the form gets stuck at "processing" and I do not seem to get any response via the communicator.

I even added:

 AuthorizeNetPopup.onReceiveCommunication = function (querystr) {

                            console.log(querystr);

To log any communication, but there is never anything logged...

What am I doing wrong here, I feel like I just copy pasted but i do not get it to work...

Thanks for any help!

Tom
  • 906
  • 1
  • 8
  • 30

0 Answers0