0

Problem

I am successfully posting to a remote payment website, but at the same time I need to save details of the order into the database

Form

This is the form that submits the data, most of which is stored in the configuration.

echo $this->Form->create(null, array('url' => Configure::read('Payment.strPurchaseURL'))); ?>

echo $this->Form->hidden('navigate', array(
    'name'  => 'navigate',
    'value' => ''
));
echo $this->Form->hidden('VPSProtocol', array(
    'name'  => 'VPSProtocol',
    'value' => Configure::read('Payment.strProtocol')
));
echo $this->Form->hidden('TxType', array(
    'name'  => 'TxType',
    'value' => Configure::read('Payment.strTransactionType')
));
echo $this->Form->hidden('Vendor', array(
    'name'  => 'Vendor',
    'value' => Configure::read('Payment.strVendorName')
));
echo $this->Form->hidden('Crypt', array(
    'name'  => 'Crypt',
    'value' => $encrypted
));
echo $this->Form->end(__('Proceed to payment'));

Controller

The rest of the controller for the view does its job correctly, but this if statement never gets called, because the action of the post takes it away from the controller. $post_data is data from the session, generated by a previous form.

    if ($this->request->is('post')) {
        $this->Order->create();
        if ($this->Order->save($post_data)) {
        ...

Logic is flawed

I know my logic is flawed, but from my research you can't post from the controller, so I either end up with a working save button or a working remote post.

I need to do both, but I haven't stumbled upon a good way yet, hopefully one of you fine people can set me straight.

Many thanks.

Community
  • 1
  • 1
jzahedieh
  • 1,570
  • 1
  • 15
  • 27
  • 1
    possible duplicate of [PHP Redirect with POST data](http://stackoverflow.com/questions/5576619/php-redirect-with-post-data) – tereško Dec 07 '12 at 21:51
  • @tereško I did report it but they don't to be flagging it, I am fairly new there is anything I can to defer the answer to that question? – jzahedieh Dec 08 '12 at 10:46

1 Answers1

1

I would think that you should be able to use PHP cURL to achieve what you want. After saving the order, you can do a POST request to the secondary URL. Here's an example, untested:

if ($this->request->is('post')) {
    $this->Order->create();
    if ($this->Order->save($post_data) {
        $url = 'http://domain.com/post.php';

        //url-ify the data for the POST
        $fields_string = http_build_query($post_data);

        //open connection
        $ch = curl_init();

        //set the url, number of POST vars, POST data
        curl_setopt($ch,CURLOPT_URL, $url);
        curl_setopt($ch,CURLOPT_POST, count($post_data));
        curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);

        //execute post
        $result = curl_exec($ch);

        //close connection
        curl_close($ch);
    }
}

Your best bet is to do some research on cURL :)

Hoff
  • 1,762
  • 14
  • 27
  • Many thanks, will check it out tomorrow, looks like the right path – jzahedieh Dec 06 '12 at 17:32
  • Looks like cURL wasn't the thing I was looking for since you can't redirect clients using it. I found the answer was to add an extra step, as http://stackoverflow.com/questions/5576619/php-redirect-with-post-data?lq=1 Many thanks for your time though. – jzahedieh Dec 07 '12 at 17:26