Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not redirect users after navigating back to order page. #187

Merged
merged 4 commits into from
Mar 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions includes/class-wc-gateway-payfast.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,29 @@ public function __construct() {

// Add support for WooPayments multi-currency.
add_filter( 'woocommerce_currency', array( $this, 'filter_currency' ) );

add_filter( 'nocache_headers', array( $this, 'no_store_cache_headers' ) );
}

/**
* Use the no-store, private cache directive on the order-pay endpoint.
*
* This prevents the browser caching the page even when the visitor has clicked
* the back button. This is required to determine if a user has pressed back while
* in the payfast gateway.
*
* @since x.x.x
*
* @param string[] $headers Array of caching headers.
* @return string[] Modified caching headers.
*/
public function no_store_cache_headers( $headers ) {
if ( ! is_wc_endpoint_url( 'order-pay' ) ) {
return $headers;
}

$headers['Cache-Control'] = 'no-cache, must-revalidate, max-age=0, no-store, private';
return $headers;
}

/**
Expand Down Expand Up @@ -486,6 +509,29 @@ class="button cancel"
'</a>
<script type="text/javascript">
jQuery(function(){
// Feature detect.
if (
typeof PerformanceNavigationTiming !== "undefined" &&
typeof window.performance !== "undefined" &&
typeof performance.getEntriesByType === "function"
) {
var isBackForward = false;
var entries = performance.getEntriesByType("navigation");
entries.forEach((entry) => {
if (entry.type === "back_forward") {
isBackForward = true;
}
});
if (isBackForward) {
/*
* Do not submit form on back or forward.
* Ensure that the body is unblocked/not showing the redirect message.
*/
jQuery("body").unblock();
return;
}
}

jQuery("body").block(
{
message: "' . esc_html__( 'Thank you for your order. We are now redirecting you to Payfast to make payment.', 'woocommerce-gateway-payfast' ) . '",
Expand Down
Loading