Support

Home Forums Event Espresso Premium Getting payment and transaction objects from hook after payment is applied

Getting payment and transaction objects from hook after payment is applied

Posted: May 2, 2018 at 7:20 am


deon@dieselbrook.co.za

May 2, 2018 at 7:20 am

I am using the AHEE__EE_Payment_Processor__update_txn_based_on_payment__successful hook in order to do some custom coding with regards to events.

What I ultimately need to do is to get the event id that the transaction/registration was done for; as I want to update the access list for the specific event page when a registration is done and paid for (there is already a page associated with the event, which is already coded and working fine).

I can see in the source code for EE_Payment_Processor.core.php that it passes the payment and transaction objects to the hook.

What I have so far:

add_action('AHEE__EE_Payment_Processor__update_txn_based_on_payment__successful', 'evt_add_reg', 10, 2); // I first tried without the extra parameters, but also didn't work
function evt_add_reg($transaction, $payment)
{
global $wpdb;
wp_mail('john-henry@dieselbrook.co.za', 'test', 'test'); //this does fire off, meaning that the hook does work
$payment_object = $payment instanceof EE_Payment ? $payment : null;
$event = $payment_object->get_first_event(); //this will get the event?
$event_id = $event->ID();
wp_mail('john-henry@dieselbrook.co.za', 'test object', $payment_object); //this doesn't work, meaning that it can't find the data
}

I have also tried using the passed parameters directly, which didn’t work:

function evt_add_reg($transaction, $payment)
{
global $wpdb;
wp_mail('john-henry@dieselbrook.co.za', 'test', 'test'); // the same applies as above
$payment_object = $payment instanceof EE_Payment ? $payment : null;
$event = $payment_object->get_first_event();
$event_id = $event->ID();
wp_mail('john-henry@dieselbrook.co.za', 'test object', $payment); // the same applies as above
}

Please let me know if the data is structured differently, and if I should adapt my code in order to get the ID (or if there is an easier way to get the id).


Tony

  • Support Staff

May 2, 2018 at 10:21 am

wp_mail('john-henry@dieselbrook.co.za', 'test object', $payment_object); //this doesn't work, meaning that it can't find the data

You’re passing the payment_object to wp_mail, not the event_id.

So:

wp_mail('john-henry@dieselbrook.co.za', 'test object', $event_id);

Would be an email containing the event id.

Also:

$payment_object = $payment instanceof EE_Payment ? $payment : null;

If for some reason $payment wasn’t an instance of EE_Payment, your going to get fatal errors as your function then runs methods on null below…

$event = $payment_object->get_first_event();

If $payment_object is null, fatal error.

So rather than setting the var as null, return instead:

if( ! $payment_object instanceof EE_Payment ) {
    //get out, you don't have an EE_Payment object to work with.
    return;
}


deon@dieselbrook.co.za

May 3, 2018 at 1:31 am

Thanks, but I only added that so that I can debug as I can’t use logs or the console to generate output. There is a lot of code after this section, but if it isn’t finding the data from the event, then the other code can’t work, so no point in including it really (it is custom code that doesn’t really have anything to do with the event espresso code, don’t expect you guys to help out with that ๐Ÿ™‚ ) I just need the event id in order to proceed.

I opened a ticket earlier, in which someone told me that the format of the object data has changed from an example that I was working from (custom shortcode in the messages).

The example below is a snippet from a different function and a different hook, but perhaps the same happened here, that the format has changed and I thus cannot access the data.

is_array( $extra_data ) && !empty( $extra_data['data'] ) && $extra_data['data'] instanceof EE_Messages_Addressee ? $extra_data['data'] : null;


Tony

  • Support Staff

May 3, 2018 at 2:02 am

Thanks, but I only added that so that I can debug as I canโ€™t use logs or the console to generate output.

Sure you can, you can use a little function like this: http://www.stumiller.me/sending-output-to-the-wordpress-debug-log/

Add this snippet to your wp-config.php file:

https://eventespresso.com/wiki/troubleshooting-checklist/#wpdebug

The write_log function then writes whatever you pass it to /wp-content/debug.log

I opened a ticket earlier, in which someone told me that the format of the object data has changed from an example that I was working from (custom shortcode in the messages).

That’s was me and it only applied to the messages system and my example code, to provide more context, the $extra_data var was just a single object, and it was usually the EE_Attendee object, now $extra_data is an array in which one of the elements will usually be an EE_Attendee object, but there could be more elements in the array to be used as… well, extra data ๐Ÿ™‚

I’m using your code in WP console and replicating it by pulling in an EE Transaction, then pulling a payment from that transaction so I have the same objects you should have – http://take.ms/ljJNB

That works and emails me the object and event_id – http://take.ms/ZXTzV

I then hooked into AHEE__EE_Payment_Processor__update_txn_based_on_payment__successful with your function and ran a registration using Stripe, the result was another set of emails containing the data – http://take.ms/T6dWO (Note the ID’s are different as it’s a new transaction/payment)

So the code you are using above seems to be working fine?

Which payment method are you using to complete the payment?

When you say it doesn’t work, what is happening? What emails do you receive?


deon@dieselbrook.co.za

May 3, 2018 at 2:32 am

Ah okay, you learn something new every day, thanks ๐Ÿ™‚

I’ve now inspected the $payment and $transaction objects that is passed to the hook (AHEE__EE_Payment_Processor__update_txn_based_on_payment__successful), and I can see all the data that I need there. But it is protected, So I’ve tried using get_first_event public method located in the EE_Payment class in order to get the data:


if ($payment instanceof EE_Payment)
{
$event = $payment->get_first_event();
$event_id = $event->ID();
wp_mail('john-henry@dieselbrook.co.za', 'Event Id', $event_id );
}
else
{
return;
}

But this still isn’t working, even though it is a public method.

The thing is I want this to happen every time there is a successful payment applied to the transaction, either VIA bank transfer (applied from the backend) or from a payment gateway. So I wouldn’t necessarily know the ID of the transaction.


Tony

  • Support Staff

May 3, 2018 at 2:41 am

But this still isnโ€™t working, even though it is a public method.

What isn’t working? Are you getting a blank email or nothing at all?

Are you applying a payment from within the admin now or using an online payment method?

So I wouldnโ€™t necessarily know the ID of the transaction.

I’m not sure why you would need to know the transaction ID, but if you have the transaction object you have the transaction ID.


deon@dieselbrook.co.za

May 3, 2018 at 2:56 am

Not getting anything at all in the first point, it isn’t sending a mail.

I am currently using a payment gateway, not from the admin itself (but the hook still gets called, as tested from my previous code).

I saw in your code that you called the transaction via its ID in line 6.

Thank you very much for your input and patience with this (as well as the previous ticket), I’m just struggling to understand why I can’t use the public method as designated by the class if I have the object.


Tony

  • Support Staff

May 3, 2018 at 3:27 am

I saw in your code that you called the transaction via its ID in line 6.

I’m pulling a Transaction by ID so that I have a Transaction in the console, I’m replicating the object you have available on that hook, this was my previous reply:

Iโ€™m using your code in WP console and replicating it by pulling in an EE Transaction, then pulling a payment from that transaction so I have the same objects you should have

You don’t need to know the ID, you already have the transaction object passed to the hook.

I am currently using a payment gateway, not from the admin itself (but the hook still gets called, as tested from my previous code).

Which payment gateway?

Add the write_log function I gave you above to either, enable WP_Debug using the snippet I gave you and then add:

write_log($payment); (just above your EE_Payment conditional)

write_log($event); (just after you’ve create $event)

write_log($event_id); (just after you’ve created $event_id)

before the email is sent, retest and check the debug.log file to see if you have the objects written to the log.

There are too many unknowns in your code right now, you don’t know if if ($payment instanceof EE_Payment) is failing, if the wp_mail() call is failing or even if if ($payment instanceof EE_Payment) is passing and then there’s a fatal after that.

In short, break down your code and check each variable as you run through to find what is actually being run and what they contain before you use them, right now you don’t know if the email is sent or not and just that it doesn’t work, you need to narrow that down to the specific area that fails because the above is working for me.


deon@dieselbrook.co.za

May 3, 2018 at 4:24 am

Okay, got it.

The get_first_event method failed.

After I saw the debug messages I realised that I am not on the latest version of Event Espresso, and that this method didn’t exist in my code ( I was looking at the EE_Payment class on GitHub).

Thank you very much for your assistance, especially with teaching me how to debug properly! Never working with mails again, thank goodness.

You can close the ticket now.


Tony

  • Support Staff

May 3, 2018 at 4:40 am

Great! I’m glad it’s working for you ๐Ÿ™‚

I’d recommend always using the latest version of EE, we update with new features and bug fixes etc little and often.

The support post ‘Getting payment and transaction objects from hook after payment is applied’ is closed to new replies.

Have a question about this support post? Create a new support post in our support forums and include a link to this existing support post so we can help you.

Support forum for Event Espresso 3 and Event Espresso 4.
Documentation for EE3 and EE4
Documentation for Event Espresso 3

Documentation for Event Espresso 4

Status: closed

Updated by  Tony 5 months, 2 weeks ago ago

Topic Tags

Notifications

This topic is: not resolved
Do NOT follow this link or you will be banned from the site!