×

Error

Please login first

Auto Actions Trigger Code on CBSubs Donation Transaction

7 years 5 months ago #288028 by jamesmoeller
I’d like to verify that I’m attempting to do this the correct way and also get additional clarification on how to access the donation amount in my php auto action code.

I have a CBSubs donation plan setup that allows the user to specify a donation amount. Essentially what I’d like to do is have an Auto Action setup to execute php code that will write that donation amount out to an external database after the purchase is complete. I’ve used this post (www.joomlapolis.com/forum/255-developer-members-support/233410-solved-oncpayafterpaymentstatusupdateevent-and-var3) as an example, and have successfully utilized the onCPayUserStateChange trigger exactly as described in krileon’s initial reply to trigger the code when the donation payment is complete. This works even on repeat donations. It looks like the donation transaction must flip the [var2] User State to “A” / Active, and then sometime later revert back to a different state…and then back to “A” when the same user makes an additional donation. Nonetheless, combining that with the condition of a specific CBSubs plan ID # (as noted in the post) seems to work and trigger the code when the donation plan is purchased. What I’d like to do next is get the donation amount into my php code so that I can write it out to an external database. Is there a way to access the donation amount in my php code? The purchase of the donation plan can potentially be combined with the purchase of other subscription or merchandise plans, so the basket amount may not represent the donation amount. Of course there will always only be 1 donation purchase in any basket, but even when combined with other purchases I need the value of that specific donation.

Thanks,
Jim

Please Log in to join the conversation.

7 years 5 months ago #288072 by krileon
You won't be able to with that trigger. Typically you'd use onCPayAfterPaymentStatusUpdateEvent then loop the basket items, but that won't work unless you use a Code action with Method set to PHP and write custom PHP to do that. You might be able to use onCPayPaymentItemEvent with the below conditions.

[var1] Equal To afterUpdatePaymentItem
[var2_plan_id] Equal To PLAN_ID_HERE
[var2_status] Equal To A

var1 is the event fired. var2 is the paid something object. var3 is the basket object. var4 is the payment item object. var4 being the most important as you should be able to use [var4_rate] to grab the amount they donated. Below is the specific class files for the variables.

var2
components/com_comprofiler/plugin/user/plug_cbpaidsubscriptions/models/something/cbpaidSomething.php

var4
components/com_comprofiler/plugin/user/plug_cbpaidsubscriptions/models/order/cbpaidPaymentItem.php


Kyle (Krileon)
Community Builder Team Member
Before posting on forums: Read FAQ thoroughly + Read our Documentation + Search the forums
CB links: Documentation - Localization - CB Quickstart - CB Paid Subscriptions - Add-Ons - Forge
--
If you are a Professional, Developer, or CB Paid Subscriptions subscriber and have a support issue please always post in your respective support forums for best results!
--
If I've missed your support post with a delay of 3 days or greater and are a Professional, Developer, or CBSubs subscriber please send me a private message with your thread and will reply when possible!
--
Please note I am available Monday - Friday from 8:00 AM CST to 4:00 PM CST. I am away on weekends (Saturday and Sunday) and if I've missed your post on or before a weekend after business hours please wait for the next following business day (Monday) and will get to your issue as soon as possible, thank you.
--
My role here is to provide guidance and assistance. I cannot provide custom code for each custom requirement. Please do not inquire me about custom development.

Please Log in to join the conversation.

7 years 5 months ago #288126 by jamesmoeller
I was not able to get the onCPayPaymentItemEvent to trigger given the conditions you suggested. But I am already doing a ‘code’ action with ‘php’ method. I need to do it this way in order to write the donation value out to my cloud external database. So I experimented with your first suggestion of using the onCPayAfterPaymentStatusUpdateEvent trigger. If I’m understanding this correctly, onCPayAfterPaymentStatusUpdateEvent will trigger whenever there is a payment status change…and as long as there are no further conditions associated with the auto action, it will trigger regardless of what’s in the basket. It will even trigger if there is an invalid payment transaction status change, such as a cancelled payment. But for a valid paid transaction, the $unifiedStatus (i.e.[var4]) should return “Completed”. So as long as I check for a completed transaction and then look at the basket items I should be able to pull in, and process, the donation value. Is that basically correct?

But I’m have having problems accessing the basket items and many of the return values from the event trigger in my php code . From this link (www.joomlapolis.com/documentation/18358-using-cb-triggers) and the definition of the function as
$_PLUGINS->trigger( 'onCPayAfterPaymentStatusUpdateEvent', array( &$user, &$paymentBasket, &$subscriptions, $unifiedStatus, $previousUnifiedStatus, $eventType, &$notification ))
It looks like when I specify [var2] in my php code it is replaced with &$paymentBasket, which is a pointer to an array. How do I index into those arrays?

$FirstBasketItem = [var2][0];
Doesn’t seem to work.

$NewPaymentStatus = [var4];
As a non-array value, this does return “Completed”, which is the correct payment status after a valid payment transaction.

Finally, are there examples or documentation of any of this on joomlapolis.com?

Thanks,
Jim

Please Log in to join the conversation.

7 years 5 months ago - 7 years 5 months ago #288144 by krileon
For onCPayAfterPaymentStatusUpdateEvent you need to check that [var4] is either "Completed" or "Processed" in your conditions (e.g. use a regexp condition for /Completed|Processed/). To properly loop the basket items take a look at the below integration files as they both loop the basket items.

components/com_comprofiler/plugin/user/plug_cbpaidsubscriptions/plugin/cbsubsgoogleanalytics/cbsubs.googleanalytics.php

components/com_comprofiler/plugin/user/plug_cbpaidsubscriptions/plugin/cbsubsidevaf/cbsubs.idevaf.php

To access variable objects from a Code action you need to use the below usage and it'll be the object, string, etc.. whatever the variable is.

$vars['VARIABLE']

Example as follows.

$paymentBasket = $vars['var2'];


Kyle (Krileon)
Community Builder Team Member
Before posting on forums: Read FAQ thoroughly + Read our Documentation + Search the forums
CB links: Documentation - Localization - CB Quickstart - CB Paid Subscriptions - Add-Ons - Forge
--
If you are a Professional, Developer, or CB Paid Subscriptions subscriber and have a support issue please always post in your respective support forums for best results!
--
If I've missed your support post with a delay of 3 days or greater and are a Professional, Developer, or CBSubs subscriber please send me a private message with your thread and will reply when possible!
--
Please note I am available Monday - Friday from 8:00 AM CST to 4:00 PM CST. I am away on weekends (Saturday and Sunday) and if I've missed your post on or before a weekend after business hours please wait for the next following business day (Monday) and will get to your issue as soon as possible, thank you.
--
My role here is to provide guidance and assistance. I cannot provide custom code for each custom requirement. Please do not inquire me about custom development.

Please Log in to join the conversation.

7 years 4 weeks ago - 7 years 4 weeks ago #293344 by jamesmoeller
I have a follow-up on this thread.

I’m trying to understand conceptually how the PayPal communication takes place and how / when the onCPayAfterPaymentStatusUpdateEvent event gets triggered, as I’m getting some odd results under certain situations.

My situation: I have implemented this Auto Action Trigger on three different websites…one is a non-public website behind on my local network, the other two are public sites use for testing. The auto action is relatively simple (summary code included below), where the php code is triggered onCPayAfterPaymentStatusUpdateEvent. The code looks for “Completed” PayPal transactions and then looks for a specific plan ID (the ID of my donation plan), and then gets the donation amount and writes that to cloud storage.

When testing this on all three systems (all in different tabs of the same browser) I received odd results in that it would work on one site at one moment, but then not a bit later. Just very inconsistent. Also, up until today, I was able to get this to work on the non-public website as well, but today I’m receiving an “incorrect PayPal address” error from PayPal on this site.

Questions:

How does the communication flow between the website and PayPal? I’ve set the IPN and PDT (Payment Data Transfer) correctly back at PayPal, but I noticed that the URLs set within PayPal don’t seem to matter in the communication flow or return of payment data with CBSubs.

In addition, with the first site not being public, how does the PayPal information get received by this website? Should the PayPal transaction even work correctly with a non-public site? I don’t fully understand how it worked in the past and returned correctly to my private website from PayPal.

When exactly does the onCPayAfterPaymentStatusUpdateEvent get triggered…when the customer returns to my site from PayPal in their browser?

How does the browser session factor into this functionality? I seem to get inconsistent results if I run transactions on my websites from different tabs within the same browser, and seemingly consistent results if I run transactions on my sites from completely different browsers (Chrome, Firefox, etc.)

Thanks,
Jim

======
$TransactionStatus = "[var4]";
//Process the basket items only if the transaction status is "Complete"
if ($TransactionStatus == "Completed") {
   $paymentBasket = $vars['var2'];
   $paymentItems = $paymentBasket->loadPaymentItems();
   //Loop through all the payment items.
   foreach ( $paymentItems as $item ) {

        $itemPlanID = $item->plan_id;

        //Process only the $specificplanid
        $specificplanid = 3;
        if ($itemPlanID == $specificplanid) {
            // Get the subscription amount, i.e. the amount of the donation.
            $subscription = $item->loadSubscription();
            $DonationAmount = $subscription->amount;

            < my code to write donation amount to cloud storage >

        }
   }
}

Please Log in to join the conversation.

7 years 4 weeks ago #293346 by krileon
You're probably having fatal errors are the below.

$TransactionStatus = [var4];

All substitutions should be treated as strings. This means you need to include that in quotes as follows.

$TransactionStatus = '[var4]';

Most likely through you can just move that condition check to the auto action it self as follows.

[var4] Equal To Completed


Additionally the below isn't valid.

$paymentBasket = $vars;

$vars is just the variables available to the action so it's just an array of variables. You need to specify what variable you want to use (e.g. $vars). See my reply from earlier below where I've explained what object you'll get from what variable based off the needs you specified.

www.joomlapolis.com/forum/255-developer-members-support/235267-auto-actions-trigger-code-on-cbsubs-donation-transaction#288144


Kyle (Krileon)
Community Builder Team Member
Before posting on forums: Read FAQ thoroughly + Read our Documentation + Search the forums
CB links: Documentation - Localization - CB Quickstart - CB Paid Subscriptions - Add-Ons - Forge
--
If you are a Professional, Developer, or CB Paid Subscriptions subscriber and have a support issue please always post in your respective support forums for best results!
--
If I've missed your support post with a delay of 3 days or greater and are a Professional, Developer, or CBSubs subscriber please send me a private message with your thread and will reply when possible!
--
Please note I am available Monday - Friday from 8:00 AM CST to 4:00 PM CST. I am away on weekends (Saturday and Sunday) and if I've missed your post on or before a weekend after business hours please wait for the next following business day (Monday) and will get to your issue as soon as possible, thank you.
--
My role here is to provide guidance and assistance. I cannot provide custom code for each custom requirement. Please do not inquire me about custom development.

Please Log in to join the conversation.

Moderators: beatnantkrileon
Time to create page: 0.189 seconds

Facebook Twitter LinkedIn