How to send POST variables to External URL ?

落爺英雄遲暮 提交于 2019-12-24 18:06:09

问题


I'm making a shopping cart. I would like to save the order before going to the payment gateway. My payment gateway requires me to send a POST to external address but not how to do it from a controller action.

public function executeBuy(sfWebRequest $request)
{
  sfProjectConfiguration::getActive()->loadHelpers('Url');

  // save the order
  $this->order = new Order();
  $this->save
  //etc....

  //go to TPV Payment gateway
  $dsAmount       = (float)$order->getPriceWithShipping() * 100;
  $dsOrder        = (int)$order->getId() * 400;
  $dsMerchantCode = (int)sfConfig::get('app_tpv_merchant_code');
  $dsCurrency     = (int)sfConfig::get('app_tpv_merchant_currency');
  $dsMerchantURL  = url_for('cart/ipn', true, array(
    'sf_culture' => $this->getUser()->getCulture(),
  ));
  $options = array(
    'Ds_Merchant_Amount'            => $dsAmount,
    'Ds_Merchant_Currency'          => $dsCurrency,
    'Ds_Merchant_Order'             => $dsOrder,
    'Ds_Merchant_Titular'           => $order->getAddress()->getCustomer()->getNameAndLastName(),
    'Ds_Merchant_MerchantCode'      => $dsMerchantCode,
    'Ds_Merchant_MerchantURL'       => $dsMerchantURL,
    'Ds_Merchant_MerchantSignature' => $digest,
    'Ds_Merchant_Terminal'          => $dsCurrency
  );

  //how to send post $options variables to external url?
}

回答1:


Use cURL to post data:

//set POST variables
$dsMerchantURL = url_for('cart/ipn', true, array(
  'sf_culture' => $this->getUser()->getCulture(),
));

$options = array(
  'Ds_Merchant_Amount' => urlencode($dsAmount),
  'Ds_Merchant_Currency' => urlencode($dsCurrency),
  'Ds_Merchant_Order' => urlencode($dsOrder),
  'Ds_Merchant_Titular' => urlencode($order->getAddress()->getCustomer()->getNameAndLastName()),
  'Ds_Merchant_MerchantCode' => urlencode($dsMerchantCode),
  'Ds_Merchant_MerchantURL' => urlencode($dsMerchantURL),
  'Ds_Merchant_MerchantSignature' => urlencode($digest),
  'Ds_Merchant_Terminal' => urlencode($dsCurrency)
);

//url-ify the data for the POST
foreach($options as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string,'& ');

//open connection
$ch = curl_init();

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

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

//close connection
curl_close($ch);



回答2:


at our website (bpremium.com) we run our payment system over ajax, our website sends commands like "create sale" or "update quantity" over webservices to particular url's and those url's record the current state of your shopping cart and store the sale id in the session.

then when we get to the TPV, we execute a webservice to grab the html for the form, generated, signed and hashed, ready to go with a single button press.

this technique is great for high speed, cause you dont need to keep redirecting and forcing the user to wait, it's a lot less heavy and means you can just open the TPV into a window, fill it in and the merchantURL will catch the POST data from the TPV gateway when it succeeds or fails.



来源:https://stackoverflow.com/questions/10485834/how-to-send-post-variables-to-external-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!