Question

Using the default Magento Recurring profiles, is it possible, either through Event/Observer or a cron job to determine which profiles have been cancelled or suspended and take some subsequent action, such as updating a customer group, on those expired/changed profiles?

Was it helpful?

Solution

Using the recurring payments collection, iterate over them and issue requests to the Paypal API to get information about their status:

<?php

$collection = Mage::getModel('sales/recurring_profile')->getCollection();

foreach($collection as $profile){
    $profile->fetchUpdate();
    if($profile->hasDataChanges()){
        //display changes, including status
        print_r($profile);
    }
}

This eventually makes its way to the paypal/api_nvp model which calls callGetRecurringPaymentsProfileDetails - getting details from the API about the state of the profile.

If you wanted to update Magento with these details, in the hasDataChanges if block, call save on the profile:

$profile->save();

Source: https://www.x.com/developers/paypal/documentation-tools/api/getrecurringpaymentsprofiledetails-api-operation-nvp

OTHER TIPS

As of Magento v1.9.1.0

Copy this file:

magento/app/code/core/Mage/Payment/Model/Recurring/Profile.php

To here:

magento/app/code/local/Mage/Payment/Model/Recurring/Profile.php

Then edit the new copy and add the following after the constant declarations (around line 52):

/**
 * Prefix of model events names
 *
 * @var string
 */
protected $_eventPrefix = 'recurring_profile';

Now you can observe the recurring_profile_save_commit_after event, and add some observer code like this:

/**
 * Listen to recurring payment profile changes
 * @param   Varien_Event_Observer $observer
 * @return  Custom_Model_Observer
 */
public function processSubscriptionChange($observer)
{

  $data = $observer->getEvent()->getObject()->getData();
  $customer_id = $data['customer_id'];
  $status = $data['state'];
  $sku = $data['order_item_info']['sku'];
  Mage::log('UPDATED RECURRING PAYMENT PROFILE: '.$customer_id. " - ". $sku ." = ".$status, null, 'events.log', true);

  return $this;
} 

Magento does not implement updating of status of recurring profiles on magento side after updating of status (cancel, reactivate or suspend) on Paypal side. Magento team could did implement it via IPN requests but didn't make it - they implemented only automatic order creation. But status of profile can be updated on magento side manually with "Get Update" button on Profile page in backend or frontend.

When magento is updating status of profile on magento-side it save profile model with new data and dispatch the following event model_save_after because Mage_Sales_Model_Recurring_Profile extends from Mage_Core_Model_Abstract.

You can compare $this->origData('status') with $this->getData('status') from object in observer for knowledge about changing of status after saving of model and compare $this->getData('status') with Mage_Sales_Model_Recurring_Profile::STATE_SUSPENDED for knowledge that current status is 'suspended'.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top