Donation matching custom code issue #6151
-
Hello, I work for a nonprofit lifewater.org and we are trying to solve a matching donation issue on our site. When someone makes a donation on a matched project they receive a WordPress error where the screen is blank except for the words "cheatin' huh?". We have determined it is not related to the matching user's permissions but related to a piece of custom code in this plugin “mu-plugins/give/_matching-donations.php”. That file is running a custom matching donation function on line 43 that is in conflict with line 216 of the “plugins/give/includes/payments/functions.php” on line 216 class GiveDonationMatching
{
public $form_id;
public $amount;
// public $user;
public function __construct()
{
$this->add_actions();
}
public function add_actions()
{
//Location: give/includes/payments/functions.php:210
add_action( 'give_insert_payment', [ $this, 'handle_submission' ], 20, 2 );
}
/**
* @param $payment_id
* @param $payment_data
*/
public function handle_submission( $payment_id, $payment_data )
{
//We should do a bail if $payment_data['status'] = pending
//this could cause some issues where a donation starts in pending, then later moves to approved
//perhaps we should tie into give_setup_payment to cover all bases
//I think this is fired when a donation is created or updated
//in there we can check for status and should also check that the matching donation hasn't already been made
$this->form_id = isset( $payment_data[ 'give_form_id' ] ) ? $payment_data[ 'give_form_id' ] : 0;
$this->amount = $payment_data[ 'price' ];
//Bail if there is no match.
if ( !get_field( 'has_match', $this->form_id ) ) return;
$this->create_matching_donation();
}
public function create_matching_donation()
{
$payment = new Give_Payment();
$payment->user_info = array(
'first_name' => get_field( 'match_donor_name', $this->form_id ),
'last_name' => get_field( 'match_donor_last_name', $this->form_id ),
'email' => get_field( 'match_donor_email', $this->form_id ),
'address' => array(
'line1' => get_field( 'match_donor_address', $this->form_id ),
'city' => get_field( 'match_donor_city', $this->form_id ),
'country' => 'United States',
'state' => get_field( 'match_donor_state', $this->form_id ),
'zip' => get_field( 'match_donor_zip', $this->form_id ),
),
);
$payment->first_name = get_field( 'match_donor_name', $this->form_id );
$payment->last_name = get_field( 'match_donor_last_name', $this->form_id );
$payment->email = get_field( 'match_donor_email', $this->form_id );
$payment->mode = give_is_test_mode() ? 'test' : 'live';
$payment->form_title = get_the_title( $this->form_id );
$payment->form_id = $this->form_id;
$payment->total = $this->amount;
$payment->date = date( 'Y-m-d H:i:s', current_time( 'timestamp' ) );
$payment->status = 'publish';
$payment->currency = give_get_currency();
$payment->save();
$payment->update_meta( '_give_payment_is_match', true );
}
} $give_donation_matching = new GiveDonationMatching(); Any ideas on how to fix this but also allow a matched donation to process on our site? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hey @awatts200! Hmm, I see what you're going for here. You know what you might try doing is to use Since this code would fire inside of a remove_action('give_insert_payment, [$this, 'handle_submission'], 20);
give_insert_payment($args);
add_action( 'give_insert_payment', [ $this, 'handle_submission' ], 20, 2 ); Let us know if that helps! |
Beta Was this translation helpful? Give feedback.
Hey @awatts200!
Hmm, I see what you're going for here. You know what you might try doing is to use
give_insert_payment
instead of just instantiating a newGive_Payment
and attempting to save that. The reason is because that functions does more than just create the payment.Since this code would fire inside of a
give_insert_payment
hook, you'd need to, inside thecreate_matching_donation()
function, remove the hook before calling the function and adding it back. Otherwise you'll get stuck in a recursive infinite loop. So something like: