簡體   English   中英

在AngularJS中的http發布后提交PHP表單

[英]Submitting PHP form after http post in 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>

我如何設法觸發此表單,從而將用戶重定向到所需的支付解決方案網站?

嘗試通過這種方式將表單數據發布到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();
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM