1

I'm writing a custom module which needs to send data string like this: "CODES[]=xxx&NAMES[]=xxx" via POST to 3rdparty payment gateway AND redirect user there.

I've written a form to gather data from customer, but I can't use [] in form fields names, Drupal reports an error (an array instead of string) and quits.

I can post all data manually via drupal_http_request, but it doesn't redirects browser to needed URL. If I use drupal_goto after drupal_http_request, the data is lost and payment service doesn't recognize the user. If I include $request(my drupal_http_request)->result in a page, I get redirected to 404 page of my site with a non-existent URL (/order/checkout.php which has to be appended to payment server root and instead glues to my server root URL).

If I use $form['#redirect'] or $form['#action'] it posts fields names without "[]".

Question is, how can I append these square brackets after request is formed, but before it is sent?

Or how can I send data along with redirecting?

Will be very grateful for help, this problem is driving me nuts for a week now.

UPD My code:

    //a form
function mymodule_ask(&$form_state) {

 $form = array();

$form['#action'] = 'https://3rdpartysite.url/script.php';
$form['#method'] = 'post';

 $form['ORDER_PRICE[]'] = array(
   '#type' => 'textfield',
   '#title' => t('Amount'),
   '#size' => 10,
   '#required' => TRUE
 );

 $form['ORDER_PNAME[]'] = array (
   '#type' => 'radios',
   '#title' => t('Name'),
   '#options' => array('Avia' => 'Avia tickets', 'Other' => 'Other'),
   '#required' => TRUE
 );

 $form['submit'] = array (
   '#type' => 'submit',
   '#value' => t('PAY')
 );

return $form;
}

function mymodule_ask_submit {
    //in form submit function retrieving data from form values
    $requestar['ORDER_PNAME[]'] = $form_state['values']['ORDER_PNAME'];
    $requestar['ORDER_PRICE[]'] = $form_state['values']['ORDER_PRICE'] * 1.02;
    //forming an array to post
    $onestr = '';
    foreach ($requestar as $key => $value) {
    $onestr .= $key."=".$value."&";
    }
    $resstr = chop($onestr, '&');
    // and then I tried different ways

    //either creating and auto-submitting a form with needed values
    create_form($requestar); //separate function creating this form
    drupal_execute('payu_addform', $form_state);
    // but it doesn't pass drupal evaluation because of "ORDER_PRICE[]" element name

     //or via http_request
    $result = drupal_http_request('https://PAYMENT_SERVER_URL/script.php',  array('Content-Type' => 'application/x-www-form-urlencoded'), 'POST', $resstr);
    drupal_set_message($result->request);

    // but it takes me to MY_SERVER_URL/order/checkout.php?CART_ID=442bc42170616cd36dfd2246434d4210, while I need it to make to PAYMENT_SERVER_URL/order/checkout.php?bla-bla-bla
    //may be I can use $result->request in some other way? Just printing within this submit function gives nothing... But how can I pass it to make drupal take me out?

}
  • 1
    You can't, that's not how HTTP works. Only browser can send POST data to a server. Your server-side code cannot. Only thing you can do is to set form's target to have values sent directly to 3rd party server. – Mołot Jan 23 '14 at 12:39
  • Can you post your code? – berliner Jan 23 '14 at 12:45
  • 1
    If you want the redirect to go to the result page on the remote site, as if the form had been submitted on that site originally, the $form['#action'] method is the only possible solution. – Clive Jan 23 '14 at 12:45
  • 1
    @Clive well, he could also let action stay as it was, and on a result page on his site create hidden form that would auto-POST to the target server with javascript, or offered button for graceful degradation. Not simple, not reliable, but can be done. – Mołot Jan 23 '14 at 12:48
  • @Clive - Yes, that I understand perfectly, but how can I edit form-element names after evaluation and before submitting? This payment service accepts data strictly in given format, while Drupal can't allow it. – Puhache Jan 23 '14 at 17:38
1

Try setting the name directly:

$form['order_price'] = array(
  '#type' => 'textfield',
  '#title' => t('Amount'),
  '#size' => 10,
  '#required' => TRUE,
  '#name' => 'ORDER_PRICE[]',
);
  • I get exactly the same result with pre-populated hidden fields and text fields with default values. It just does what you'd expect. Do you have any modules installed that might interfere with form submissions? – Clive Jan 23 '14 at 18:29
  • I see that you succeeded. I can't. It just says that the form value is required. Syntax is the same. – Puhache Jan 23 '14 at 18:30
  • Hmm, I'll think about modules. BTW, I'm on 6. What version of Drupal do you use? – Puhache Jan 23 '14 at 18:31
  • Ah, that would be 7 :) I don't have a D6 install on this machine but I'm intrigued as to why this might not work so I'll get one set up and add a form. I think I know why from a high level but I'm just guessing until I can see it break. Could you edit the full form code into the question so I haven't got to write that out myself? – Clive Jan 23 '14 at 18:33
  • added form code. Thanks for giving me so much of your time! – Puhache Jan 23 '14 at 18:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.