Your conditions are probably is what is failing. The database state is considered in transit. It can change based off webhooks, what state they're moving into/from, etc..
Are you needing to act on the payment status change or the subscription status change? onCPayAfterPaymentStatusChange is the correct trigger for payment status changes and onCPayUserStateChange is the correct trigger for subscription status changes. With both you should be using the variables already provided to them instead of querying for information. After selecting either trigger you should see what variables are available to them just below the Triggers parameter.
Since you mentioned payment being finalized and subscription being active I'd act on subscription state change with onCPayUserStateChange. The below for example should work fine.
Global
Triggers: onCPayUserStateChange
User: Automatic
Access: Everybody
Conditions
Condition 1
Field: Custom > Value
Custom Value: [var2]
Operator: Equal To
Value: A
Condition 2
Field: Custom > Code
Code:
Code:
return ( $variables['var9']->getCurrentBasket()->gateway_account ?? 0 );
Operator: Equal To
Value: YOUR_PAYPAL_GATEWAY_ID_HERE
This will work on renewals as well. If you only want it on first payment you'll need to add a condition against the $reason. Example as follows.
Field: Custom > Value
Custom Value: [var6]
Operator: Not Equal To
Value: R