Form does not redirect to payment page
-
I am trying to add a payment gateway to the plugin. For this, I followed all the instructions here.
In the last snippet where we have to write our own code to call gateway API, I have added a hidden form inside the snippet which auto submits and redirected to payment page using wp_redirect().
After clicking the ‘Donate Now’ button this snippet runs fine and Chrome Dev tools show the page redirected successfully with required form data but the GiveWP form page does not change and all I see is an empty form with a spinning wheel.
Can someone please help me with this?
I will post the snippet which I have used to redirect here:<?php /** * Process CCAvenue checkout submission. * * @param array $posted_data List of posted data. * * @since 1.0.0 * @access public * * @return void */ function give_process_ccavenue_donation($posted_data) { // Make sure we don't have any left over errors present. give_clear_errors(); // Any errors? $errors = give_get_errors(); // No errors, proceed. if ( ! $errors ) { $form_id = intval( $posted_data['post_data']['give-form-id'] ); $price_id = ! empty( $posted_data['post_data']['give-price-id'] ) ? $posted_data['post_data']['give-price-id'] : 0; $donation_amount = ! empty( $posted_data['price'] ) ? $posted_data['price'] : 0; // Setup the payment details. $donation_data = array( 'price' => $donation_amount, 'give_form_title' => $posted_data['post_data']['give-form-title'], 'give_form_id' => $form_id, 'give_price_id' => $price_id, 'date' => $posted_data['date'], 'user_email' => $posted_data['user_email'], 'purchase_key' => $posted_data['purchase_key'], 'currency' => 'INR', 'user_info' => $posted_data['user_info'], 'status' => 'pending', 'gateway' => 'ccavenue', ); // Record the pending donation. $donation_id = give_insert_payment($donation_data); if (!$donation_id) { // Record Gateway Error as Pending Donation in Give is not created. give_record_gateway_error( __( 'CCAvenue Error', 'ccavenue-for-give' ), sprintf( /* translators: %s Exception error message. */ __( 'Unable to create a pending donation with Give.', 'ccavenue-for-give' ) ) ); // Send user back to checkout. give_send_back_to_checkout( '?payment-mode=ccavenue' ); return; } } else { // Send user back to checkout. give_send_back_to_checkout( '?payment-mode=ccavenue' ); } // End if(). // Do the actual payment processing using the custom payment gateway API. To access the GiveWP // settings, use give_get_option() as a reference, this pulls the API key entered above: give_get_option('insta_for_give_instamojo_api_key') $merchant_id = give_get_option('ccavenue_merchant_id'); $access_code = give_get_option('ccavenue_access_code'); $working_key = give_get_option('ccavenue_working_key'); $order_id = rand (100000,999999); $redirect_url = 'http://localhost/wordpress/donation-confirmation'; $cancel_url = 'http://localhost/wordpress/donation-failed/'; $tx_id = rand (1000000,9999999); $url = "https://test.ccavenue.com/transaction/transaction.do?command=initiateTransaction"; $merchant_data_arr = array( 'merchant_id' => $merchant_id, 'order_id' => $order_id, 'language' => 'EN', 'amount' => $donation_amount, 'currency' => 'INR', 'redirect_url' => $redirect_url, 'cancel_url' => $cancel_url, 'tid' => $tx_id, ); foreach ($merchant_data_arr as $key => $value){ $merchant_data.=$key.'='.$value.'&'; } // Method for encrypting the data. $encrypted_data = encrypt($merchant_data, $working_key); redirect_to_gateway($url, $encrypted_data, $access_code); } /* * @param1 : Plain String * @param2 : Working key provided by CCAvenue * @return : Decrypted String */ function encrypt($plainText,$key) { $key = hextobin(md5($key)); $initVector = pack("C*", 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f); $openMode = openssl_encrypt($plainText, 'AES-128-CBC', $key, OPENSSL_RAW_DATA, $initVector); $encryptedText = bin2hex($openMode); return $encryptedText; } /* * @param1 : Encrypted String * @param2 : Working key provided by CCAvenue * @return : Plain String */ function decrypt($encryptedText,$key) { $key = hextobin(md5($key)); $initVector = pack("C*", 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f); $encryptedText = hextobin($encryptedText); $decryptedText = openssl_decrypt($encryptedText, 'AES-128-CBC', $key, OPENSSL_RAW_DATA, $initVector); return $decryptedText; } function hextobin($hexString) { $length = strlen($hexString); $binString=""; $count=0; while($count<$length) { $subString =substr($hexString,$count,2); $packedString = pack("H*",$subString); if ($count==0) { $binString=$packedString; } else { $binString.=$packedString; } $count+=2; } return $binString; } function redirect_to_gateway($url, $encrypted_data, $access_code) { echo '<form method="post" name="redirect" action="'.$url.'">'; echo '<input type="hidden" name="encRequest" value="' . $encrypted_data . '">'; echo '<input type="hidden" name="access_code" value="' . $access_code . '">'; echo '</form>'; echo '<script type="text/javascript">document.redirect.submit();</script>'; wp_redirect($url); } add_action( 'give_gateway_ccavenue', 'give_process_ccavenue_donation' );
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- The topic ‘Form does not redirect to payment page’ is closed to new replies.