I am trying to develop a custom gateway where I need to get order information such as value, credit card number, card owners name, address etc. After getting this, I will send a request using the API provided by Centinel 3D Secure. If successful they will return a url for the credit card bank along with some other information which I am to POST to the bank URL.
The bank will then post data using a form to my callback URL. I can't seem to get it to work however. This is a sample of my code:
function process_payment( $order_id ) {
global $woocommerce;
//code to get data using API
if( (strcasecmp('Y', $_SESSION['Centinel_Enrolled']) == 0){
echo '<form action="'.$_SESSION["ACSUrl"].'"" method="post">
<input type=hidden name="PaReq" value="'.$_SESSION["RandomValue"].'"/>
<input type=hidden name="TermUrl" value="'$myWPCallbackUrl.'?>"/>
<input type=hidden name="MD" value="Data"/>
<input type="submit" value="Submit" id="submit_centinel_payment_form"/>
<script type="text/javascript">
jQuery(function(){
jQuery("#submit_centinel_payment_form").click();});
</script>
</form>
}
This doesn't do a redirect to the server. Does anyone have any idea why this doesn't work?
using pur javascript you can achieve this goal. Instead of using this code
<script type="text/javascript">
jQuery(function(){
jQuery("#submit_centinel_payment_form").click();});
</script>
You can use this code
<script type="text/javascript">
document.getElementById('paymentForm').submit(); // SUBMIT FORM
</script>
provided you add an id to your form. You also have syntax error in your form and I would suggest to take out the javascript code outside of form element. You can finally have this code
echo '<form action="'.$_SESSION["ACSUrl"].'" method="post" id="paymentForm">
<input type=hidden name="PaReq" value="'.$_SESSION["RandomValue"].'"/>
<input type=hidden name="TermUrl" value="'.$myWPCallbackUrl.'"/>
<input type=hidden name="MD" value="Data"/>
<input type="submit" value="Submit" id="submit_centinel_payment_form"/>
</form>
<script type="text/javascript">
document.getElementById("paymentForm").submit(); // SUBMIT FORM
</script>
';
This is an old question, but for all of you struggling with the same problem now, I hope my answer will help. I had the exact same problem and this is how I solved it.
I don't know much what is happening in the background of process_payment( $ order_id )
function that is causing this kind of behavior, but you can not redirect from the function via php echo, nor via javascript, but only via function return statement, like in example below:
function process_payment( $order_id ) {
global $woocommerce;
$order = new WC_Order( $order_id );
// Mark as on-hold (we're awaiting the cheque)
$order->update_status('on-hold', __( 'Awaiting cheque payment', 'woocommerce' ));
// Reduce stock levels
$order->reduce_order_stock();
// Remove cart
$woocommerce->cart->empty_cart();
// Return thank you redirect
return array(
'result' => 'success',
'redirect' => $this->get_return_url( $order )
);
}
(example pasted from Wocommerce Payment Gateway API)
So, for your specific problem I would put your form in separate file, secure_form.php. All needed data will be accessible via get parameters:
<!DOCTYPE html>
<html>
<head>
<title>3D Secure Verification</title>
<script language="Javascript">
function OnLoadEvent() { document.form.submit(); }
</script>
</head>
<body OnLoad="OnLoadEvent();">
Invoking 3-D secure form, please wait ...
<form name="form" action="<?php echo rawurldecode( $_GET[ 'acs_url' ] ); ?>" method="post">
<input type="hidden" name="PaReq" value="<?php echo rawurldecode( $_GET[ 'pareq' ] ); ?>">
<input type="hidden" name="TermUrl" value="<?php echo rawurldecode( $_GET[ 'term_url' ] ); ?>">
<input type="hidden" name="MD" value="<?php echo rawurldecode( $_GET[ 'authencity_token' ] ); ?>">
<noscript>
<p>Please click</p><input id="to-asc-button" type="submit">
</noscript>
</form>
</body>
</html>
And then, in your process_payment( $ order_id )
function you set the variables, and call your form (don't forget to pass get parameters):
function process_payment( $order_id ) {
global $woocommerce;
//code to get data using API
if( (strcasecmp('Y', $_SESSION['Centinel_Enrolled']) == 0){
// Set variables and redirect to 3DS check form
$acs_url = rawurlencode( $_SESSION["ACSUrl"] );
$pareq = rawurlencode( $_SESSION["RandomValue"] );
$authencity_token = rawurlencode( 'Data' );
$term_url = rawurlencode( $myWPCallbackUrl );
$url = path_to_secure_form.php . "?acs_url=$acs_url&pareq=$pareq&authencity_token=$authencity_token&term_url=$term_url";
return [
'result' => 'success',
'redirect' => $url
];
}
}