简体   繁体   中英

Submitting PHP form after http post in AngularJS

I'm trying to integrate a bank payment solution to my angularJS app.

By default the bank solution comes with a PHP document that includes a form. I've managed to make a http.post call from my angularJS app including the total price of the order, but the PHP document returns the form instead of submitting it.

I'm not good at all with PHP, I'm sure I'm missing something super basic but I don't see what.

Here's my http call:

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

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

And the PHP doc:

<?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>

How could I manage to trigger this form and thus redirecting the user to the desired payment solution website?

Try this way to post form data to API, if possible

Create hidden form input elements dynamically in your controller to post data and after submission , it will redirect to desired payment interface automatically.

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();
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM