Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save xlplugins/e7c11102b4ef6bae2e2a98922b8ecde1 to your computer and use it in GitHub Desktop.

Select an option

Save xlplugins/e7c11102b4ef6bae2e2a98922b8ecde1 to your computer and use it in GitHub Desktop.
Funnelkit Checkout Bankful payment plugin comaptibility
function shoptimizer_child_bankful_hosted_direct_redirect( $url, $order ) {
if ( ! function_exists( 'WC' ) || ! WC()->payment_gateways() ) {
return $url;
}
if ( ! $order instanceof WC_Order ) {
return $url;
}
if ( 'bankful_hosted_gateway' !== $order->get_payment_method() ) {
return $url;
}
$wfacp_id = $order->get_meta( '_wfacp_post_id', true );
if ( empty( $wfacp_id ) && ! empty( $_POST['_wfacp_post_id'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
$wfacp_id = absint( wp_unslash( $_POST['_wfacp_post_id'] ) );
}
if ( empty( $wfacp_id ) ) {
return $url;
}
if ( ! function_exists( 'bankful_remote_post' ) || ! function_exists( 'bankful_is_secure_connection' ) ) {
return $url;
}
if ( ! bankful_is_secure_connection() ) {
return $url;
}
$gateways = WC()->payment_gateways()->payment_gateways();
if ( empty( $gateways['bankful_hosted_gateway'] ) ) {
return $url;
}
$gw = $gateways['bankful_hosted_gateway'];
try {
$merchant_info = $gw->merchant_credential_new();
if ( empty( $merchant_info['bf_access_token'] ) ) {
return $url;
}
$response = bankful_remote_post(
$gw->api_endpoint_payment,
array(
'method' => 'POST',
'body' => wp_json_encode( $gw->get_bf_checkout_args( $order ) ),
'headers' => array(
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' . $merchant_info['bf_access_token'],
),
)
);
if ( is_wp_error( $response ) ) {
return $url;
}
$result = json_decode( wp_remote_retrieve_body( $response ) );
if ( empty( $result->redirect_url ) ) {
return $url;
}
$hosted = esc_url_raw( wp_unslash( $result->redirect_url ) );
return ( $hosted && wp_http_validate_url( $hosted ) ) ? $hosted : $url;
} catch ( Throwable $e ) { // phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch
return $url;
}
}
add_filter( 'process_payment_redirect', 'shoptimizer_child_bankful_hosted_direct_redirect', 99, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment