I'm not using Commerce or any e-commerce modules, as the needs for this site are very light. What I'm trying to do is integrate the Webform module with the SIM API. The API specifies that you should post a form to authorize.net containing certain variables in hidden fields. Upon submitting, the user gets redirected to the same authorize.net url that the form went to, where they are shown an invoice reflecting the variables you sent, and they can enter their payment info and click a "buy" button. I have a static HTML page containing a form that works as intended; however, I'm trying to hook all this into Webform module so that the site admin can easily manage the site's "products" (form elements).

This tiny module takes values from the Webform (hooking into hook_webform_submission_insert() ) and uses them to populate an array of values as specified in the SIM API. Then I'm calling drupal_http_request thus:

$query = http_build_query($my_data_array, '', '&');
$options = array(
      'headers' => array('Content-Type' => 'application/x-www-form-urlencoded'),
      'method' => 'POST',
      'data' => $query,
      'timeout' => 15,
);
$response = drupal_http_request($url, $options);

However, the user never gets redirected, as they do upon manual submission of the plain HTML form. When I inspect $response using:

drupal_set_message("Response:<pre>" . print_r($response, TRUE) . '</pre>');

The output I get is:

stdClass Object
(
    [request] => POST /gateway/transact.dll HTTP/1.0
Content-Type: application/x-www-form-urlencoded
User-Agent: Drupal (+http://drupal.org/)
Host: test.authorize.net
Content-Length: 1503

name='x_line_item'&value='1<|>Photo+Workshop+Tickets<|><|>2<|>150<|>F'&name='Line1'&value='Photo Workshop Tickets Qty=2 Amount=300'&x_login=xxxxxxxx&x_amount=300&x_description=My+Organization+Donation&\
x_invoice_num=1234567&x_fp_sequence=555&x_fp_timestamp=13879865&\
x_fp_hash=xxxxxxxxxxxxxxx&x_test_request=true&x_show_form=PAYMENT_FORM&\
x_receipt_link_method=LINK&x_receipt_link_text=Click+here+to+return+to+our+web+site&\
x_receipt_link_url=http%3A%2F%2Fwww.example.com%2Fstoreack
    [data] => 

(Some variables stripped out of the request for privacy, and linebreaks added with "\" for readability.)

So. [data] is empty, and the user never gets redirected. Is this how drupal_http_request is supposed to work? Or am I just implementing something wrong? When I use the same array to build a query string on the target $url and then do drupal_goto($url), it works great. But then the address bar is full of query vars...ugly, and less than optimal from a security standpoint (I know POST isn't much more secure, but at least people can't simply edit the address bar to change the variables).

I've peeked into the Commerce SIM integration module and some others but haven't found anything that helps. I don't know how to troubleshoot further. Thanks.

Comments

stefan lehmann’s picture

Doesn't the empty [data] just mean that the actual response is empty?

Can't you redirect the request to your own script and have a look if everything comes in as expected. The apostrophes in your request look a bit dodgy to me.

skyesong’s picture

I'm trying to accomplish exactly the same thing. The AuthorizeNetWebform module comes close, but it doesn't (yet) support SIM.

ecclescake’s picture

Caveat: I'm very new to Drupal and there is almost surely a more elegant way to do this.

Basically, I wrote a small module that implements hook_webform_submission_insert(). Within that function, I gather the relevant data from the webform fields (price, quantity, item number, etc) and add it to an HTML form string structured per the Auth.net SIM API. Then I store that HTML string in the $_SESSION variable and execute drupal_goto('store-confirm'). Also in my module, I implement hook_menu() to create a store-confirm page with a custom page callback to show the HTML form to the user, then when they click the Confirm button on that page the form gets posted to Auth.net where they complete the checkout process. Make sense?

The trickiest parts were figuring out how to designate on which webforms my module should get called (i.e. not the contact form), and parsing the webform contents into an HTML form string. For the first, I check nid, which is not really flexible but it works for me. All of my relevant webforms are in the same nodequeue but I don't know how to test for that in a module. For the second, I am indebted to an article at Drupal Coder. I used the component mapping code snippet from the linked article.

It's been working without a hitch for several months now, so at least it has that going for it! If anyone has solved this in a different way I'd be interested to hear about it.