Skip to content

MemberPress does not display gateway exceptions/errors on payment formย #3

@remcotolsma

Description

@remcotolsma

The exceptions/errors from gateways are stored in $_POST['errors']:

public function process_payment_form() {
if(isset($_POST['mepr_process_payment_form']) && isset($_POST['mepr_transaction_id']) && is_numeric($_POST['mepr_transaction_id'])) {
$txn = new MeprTransaction($_POST['mepr_transaction_id']);
if($txn->rec != false) {
$mepr_options = MeprOptions::fetch();
if(($pm = $mepr_options->payment_method($txn->gateway)) && $pm instanceof MeprBaseRealGateway) {
$errors = $pm->validate_payment_form(array());
if(empty($errors)) {
// process_payment_form either returns true
// for success or an array of $errors on failure
try {
$pm->process_payment_form($txn);
}
catch(Exception $e) {
MeprHooks::do_action('mepr_payment_failure', $txn);
$errors = array($e->getMessage());
}
}
if(empty($errors)) {
//Reload the txn now that it should have a proper trans_num set
$txn = new MeprTransaction($txn->id);
$product = new MeprProduct($txn->product_id);
$sanitized_title = sanitize_title($product->post_title);
$query_params = array('membership' => $sanitized_title, 'trans_num' => $txn->trans_num, 'membership_id' => $product->ID);
if($txn->subscription_id > 0) {
$sub = $txn->subscription();
$query_params = array_merge($query_params, array('subscr_id' => $sub->subscr_id));
}
MeprUtils::wp_redirect($mepr_options->thankyou_page_url(build_query($query_params)));
}
else {
// Artificially set the payment method params so we can use them downstream
// when display_payment_form is called in the 'the_content' action.
$_REQUEST['payment_method_params'] = array(
'method' => $pm->id,
'amount' => $txn->amount,
'user' => new MeprUser($txn->user_id),
'product_id' => $txn->product_id,
'transaction_id' => $txn->id
);
$_REQUEST['mepr_payment_method'] = $pm->id;
$_POST['errors'] = $errors;
return;
}
}
}
}
$_POST['errors'] = array(__('Sorry, an unknown error occurred.', 'memberpress'));
}

Only errors in $_REQUEST['errors'] are displayed:

// Called in the 'the_content' hook ... used to display a signup form
public function display_payment_form() {
$mepr_options = MeprOptions::fetch();
if(isset($_REQUEST['payment_method_params'])) {
extract($_REQUEST['payment_method_params'], EXTR_SKIP);
if(isset($_REQUEST['errors']) && !empty($_REQUEST['errors'])) {
$errors = $_REQUEST['errors'];
MeprView::render('/shared/errors', get_defined_vars());
}
if(($pm = $mepr_options->payment_method($method)) &&
($pm instanceof MeprBaseRealGateway)) {
$pm->display_payment_form($amount, $user, $product_id, $transaction_id);
}
}
}

I did notice some 'Deprecated?' comments:

$_POST['errors'] = $errors; //Deprecated?
$_REQUEST['errors'] = $errors;

catch(MeprCreateException $e) {
$_POST['errors'] = array(__( 'The user was unable to be saved.', 'memberpress')); //Deprecated?
$_REQUEST['errors'] = array(__( 'The user was unable to be saved.', 'memberpress'));
return;
}

Sigh, MemberPress... ๐Ÿ˜ž

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Status

    External

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions