Reply
Highlighted
Member
Posts: 5
Registered: ‎09-14-2013

How can I prepopulate the transaction amount with a URL (DPM)?

Hi,

 

I've set my site up basically entirely using the php SDK. I've got it working fine with a live account in test mode. Now what I want to do is set my self up with a billing system. The basic idea is that I'll send an email that contains a link to the payment form (the mysite.com/direct_post.php), and in that link I'll specify the amount of the sale. The URL should look something like

mysite.com/direct_post.php?amount=59.99

 But I don't know how to do that. I don't even know which file of the SDK I need to edit, or whether I need to make the edit in my own direct_post.php page. I've looked and looked tried various things, but nothing has worked yet. One thing I've tried is:

<?php
      require_once 'anet_php_sdk/AuthorizeNet.php'; // The SDK
	  $url = "http://hearthstoneresidentialcleaning.com/direct_post.php";
      $api_login_id = '7L4nZ792at';
      $transaction_key = '5t648U4ZSU4FPfmx';
      $md5_setting = ''; // Your MD5 Setting
      $amount = $_GET['amount'];
      AuthorizeNetDPM::directPostDemo($url, $api_login_id, $transaction_key, $amount, $md5_setting);
      ?>

 

So, I think it's best to leave this as an open ended question, rather than going through everything I've tried, because someone out there probably knows the answer right off.

 

Thanks in advance for the help!

Highlighted
Posts: 1,609
Topics: 15
Kudos: 201
Solutions: 121
Registered: ‎06-23-2011

Re: How can I prepopulate the transaction amount with a URL (DPM)?

That ought to work, if it works when you set $amount to some fixed value (which it appears to). Have you tried printing out $_GET['amount'] to make sure something weird isn't happening before $amount gets set? For instance, like it getting overwritten by something else in your code, or it not being formatted in dd.dd format. Also, you really should be testing with a sandbox account in live mode. Production in test mode does a rather lousy job of properly simulating production in live mode.

Highlighted
Member
Posts: 5
Registered: ‎09-14-2013

Re: How can I prepopulate the transaction amount with a URL (DPM)?

Thank you for the reply. I'll take your advice and switch back to a sandbox setup until it's all sorted out. I figured it would be pretty simple to customize the form and the other functionalities I need once I had run a successful transaction with sandbox, but apparently it's gonna take some more tweaking.

 

But I need to ask, what do you mean by "printing out @_GET['amount']"?

 

Highlighted
Member
Posts: 5
Registered: ‎09-14-2013

Re: How can I prepopulate the transaction amount with a URL (DPM)?

Aha, now I see what you mean about printing. I did that and was able to figure out that the value I'm assigning to $amount is getting accepted, which actually leaves me more stumped than I was before. When I set the value in the URL (to 7.77) and have the script print it, I get the output:

 

7.77 Sorry, an error occurred:

 

with no description of the error. Now, this is happening when I first try to access the payment form, not after I press the "BUY" button, so I'm thinking that this code on the next line

 

AuthorizeNetDPM::directPostDemo($url, $api_login_id, $transaction_key, $amount, $md5_setting);

 isn't able to run for some reason. If I'm not mistaken, this is a call for a method (directPostDemo) that is inside the class "AuthorizeNetDPM". I'm only barely beginning to understand what that means... Any ideas?

 

Highlighted
Member
Posts: 5
Registered: ‎09-14-2013

Re: How can I prepopulate the transaction amount with a URL (DPM)?

Ok, here's a final update on what I've found. I went into the libraries of the SDK and searched for the text "Sorry, an error occurred", and that led me to where I believe the problem is. That text is in:

    /**
     * Implements all 3 steps of the Direct Post Method for demonstration
     * purposes.
     */
    public static function directPostDemo($url, $api_login_id, $transaction_key, $amount = "0.00", $md5_setting = "")
    {
        
        // Step 1: Show checkout form to customer.
        if (!count($_POST) && !count($_GET))
        {
            $fp_sequence = time(); // Any sequential number like an invoice number.
            echo AuthorizeNetDPM::getCreditCardForm($amount, $fp_sequence, $url, $api_login_id, $transaction_key);
        }
        // Step 2: Handle AuthorizeNet Transaction Result & return snippet.
        elseif (count($_POST)) 
        {
            $response = new AuthorizeNetSIM($api_login_id, $md5_setting);
            if ($response->isAuthorizeNet()) 
            {
                if ($response->approved) 
                {
                    // Do your processing here.
                    $redirect_url = $url . '?response_code=1&transaction_id=' . $response->transaction_id; 
                }
                else
                {
                    // Redirect to error page.
                    $redirect_url = $url . '?response_code='.$response->response_code . '&response_reason_text=' . $response->response_reason_text;
                }
                // Send the Javascript back to AuthorizeNet, which will redirect user back to your site.
                echo AuthorizeNetDPM::getRelayResponseSnippet($redirect_url);
            }
            else
            {
                echo "Error -- not AuthorizeNet. Check your MD5 Setting.";
            }
        }
        // Step 3: Show receipt page to customer.
        elseif (!count($_POST) && count($_GET))
        {
            if ($_GET['response_code'] == 1)
            {
                echo "Thank you for your purchase! Transaction id: " . htmlentities($_GET['transaction_id']);
            }
			else
            {
              echo "Sorry, an error occurred: " . htmlentities($_GET['response_reason_text']);
            }
        }
    }

 which is in the file called AuthorizeNetDPM.php. Now, if I understand this correctly, the script uses the conditions  "if (!count($_POST) && !count($_GET))", "elseif (count($_POST))", and "elseif (!count($_POST) && count($_GET))" to determine which stage of the three step payment process you're at. I concluded that because the first cond. statement seems to be what you'd expect if you're starting the process (that is, no data has been passed to or from Authorize.net), the second seems to be picking up on the fact that the payment form will post some data, and the third cond. statement detects that the response code has been sent (which is passed with the GET method).

 

However, I'm also trying to pass data with the GET method. So I believe that the cond. statement "(!count($_POST) && count($_GET))" is instructing the script to run the code after that statement if there is a zero count of POST's and a non-zero number of GET's, and that's why I'm triggering that. The next thing I tried to do is right a condition that would work for my situation:

    /**
     * Implements all 3 steps of the Direct Post Method for demonstration
     * purposes.
     */
    public static function directPostDemo($url, $api_login_id, $transaction_key, $amount = "0.00", $md5_setting = "")
    {
        
        // Step 1: Show checkout form to customer.
        if (!count($_POST) && !count($_GET))
        {
            $fp_sequence = time(); // Any sequential number like an invoice number.
            echo AuthorizeNetDPM::getCreditCardForm($amount, $fp_sequence, $url, $api_login_id, $transaction_key);
        }
        // Step 2: Handle AuthorizeNet Transaction Result & return snippet.
        elseif (count($_POST)) 
        {
            $response = new AuthorizeNetSIM($api_login_id, $md5_setting);
            if ($response->isAuthorizeNet()) 
            {
                if ($response->approved) 
                {
                    // Do your processing here.
                    $redirect_url = $url . '?response_code=1&transaction_id=' . $response->transaction_id; 
                }
                else
                {
                    // Redirect to error page.
                    $redirect_url = $url . '?response_code='.$response->response_code . '&response_reason_text=' . $response->response_reason_text;
                }
                // Send the Javascript back to AuthorizeNet, which will redirect user back to your site.
                echo AuthorizeNetDPM::getRelayResponseSnippet($redirect_url);
            }
            else
            {
                echo "Error -- not AuthorizeNet. Check your MD5 Setting.";
            }
        }
        // Step 3: Show receipt page to customer.
        elseif (!count($_POST) && count($_GET))
        {
            if ($_GET['response_code'] == 1)
            {
                echo "Thank you for your purchase! Transaction id: " . htmlentities($_GET['transaction_id']);
            }
else if (isset($amount))
{
$fp_sequence = time(); // Any sequential number like an invoice number.
            echo AuthorizeNetDPM::getCreditCardForm($amount, $fp_sequence, $url, $api_login_id, $transaction_key);
}
			else
            {
              echo "Sorry, an error occurred: " . htmlentities($_GET['response_reason_text']);
            }
        }
    }

 where you can see that I've taken some of the code from the first step in the payment process (the code that makes the form display) and nested it into a new condition inside the conditional statement that I'm triggering in the third step. However, I get the same error and am officially out of ideas.

 

As a side note, I was able to get the text "Thank you for your purchase! Transaction id:" to display by using this URL:

mysite.com/direct_post.php?response_code=1&amount=7.77

 So I have a lot of confidence that I'm looking in the right place. I just don't seem to be able to write the conditional statement for my situation properly. And again, in that case I was able to print the 7.77, so I know that's being passed.

 

Highlighted
Posts: 1,609
Topics: 15
Kudos: 201
Solutions: 121
Registered: ‎06-23-2011

Re: How can I prepopulate the transaction amount with a URL (DPM)?

Oh, hmm. That is a problem. You could try differentiating your $_GET by having it be something unusual like $_GET['preset_amount'] and then have the Authorize.net code check for count($_GET) && !$_GET['preset_amount']. That would probably solve the issue.