在AngularJS中的http post之后提交PHP表单

时间:2018-03-29 17:16:10

标签: php angularjs

我正在尝试将银行支付解决方案集成到我的angularJS应用中。

默认情况下,银行解决方案附带一个包含表单的PHP文档。我设法从我的angularJS应用程序进行http.post调用,包括订单的总价格,但PHP文档返回表单而不是提交。

我对PHP一点也不擅长,我确定我错过了一些超级基本但我不知道的东西。

这是我的http电话:

$scope.sabadell = function(){
  $scope.pago = {
    'total':$scope.total
  }
  var $promise=$http.post('sabadell/pago.php',$scope.pago); 

  $promise.then(function (data) {
    console.log(data);
  });
};

PHP文档:

<?php

    $contentType = explode(';', $_SERVER['CONTENT_TYPE']); 
    $rawBody = file_get_contents("php://input");

    $data = array(); 

    if(in_array('application/json', $contentType)) { 
      $data = json_decode($rawBody);
      $total = $data->total;
    } else {
      parse_str($data, $data); 
    }

    include 'apiRedsys.php';

    $miObj = new RedsysAPI;

    // Valores de entrada
    $merchantCode   ="xxx";
    $terminal       ="1";
    $amount         =$total;
    $currency       ="978";
    $transactionType ="0";
    $merchantURL    =""; 
    $urlOK          =""; 
    $urlKO          =""; 
    $order          =time();

    $urlPago = "https://sis-t.redsys.es:25443/sis/realizarPago";

    $miObj->setParameter("DS_MERCHANT_AMOUNT",$amount);
    $miObj->setParameter("DS_MERCHANT_ORDER",strval($order));
    $miObj->setParameter("DS_MERCHANT_MERCHANTCODE",$merchantCode);
    (...)

    $version="HMAC_SHA256_V1";
    $key = 'xxx';

    $request = "";
    $params = $miObj->createMerchantParameters();
    $signature = $miObj->createMerchantSignature($key);

?>
<html lang="es">
<head>
</head>
<body>

<form name="frm" action=" <?php echo $urlPago ?>" method="POST" target="_blank" id="sabadell">
Ds_Merchant_SignatureVersion <input type="text" name="Ds_SignatureVersion" value="<?php echo $version; ?>"/></br>
Ds_Merchant_MerchantParameters <input type="text" name="Ds_MerchantParameters" value="<?php echo $params; ?>"/></br>
Ds_Merchant_Signature <input type="text" name="Ds_Signature" value="<?php echo $signature; ?>" /></br>
<input type="submit" value="Enviar" >
</form>

<script type="text/javascript">
    document.getElementById('sabadell').submit(); // SUBMIT FORM
</script>
</body>
</html>

我如何设法触发此表单,从而将用户重定向到所需的付款解决方案网站?

1 个答案:

答案 0 :(得分:0)

尝试这种方式将表单数据发布到API(如果可能)

在控制器中动态创建隐藏的表单输入元素以发布数据,提交后,它将自动重定向到所需的支付界面。

var path = $urlPago // post URL (action url)

var params = [{ 'Ds_SignatureVersion' : $version },
              { 'Ds_MerchantParameters' : $params },
              { 'Ds_Signature' : $signature },]  //<========= this contains list of key value of form

function _redirect(path, params, method) {
            method = method || "POST"; // Set method to post by default if not specified.

            // The rest of this code assumes you are not using a library.
            // It can be made less wordy if you use one.
            var form = document.createElement("form");
            form.setAttribute("method", method);
            form.setAttribute("action", path);

            for(var key in params) {
                if(params.hasOwnProperty(key)) {
                    var hiddenField = document.createElement("input");
                    hiddenField.setAttribute("type", "hidden");
                    hiddenField.setAttribute("name", key);
                    hiddenField.setAttribute("value", params[key]);
                    form.appendChild(hiddenField);
                }
            }
            console.error(form);
            document.body.appendChild(form);
            form.submit();
        }