Jump to content

Community

Developer Documentation

Custom actions for Commerce packages

IP.Nexus for IP.Board allowed each Package to specify a "Custom module" - a PHP script which could specify custom code to run when purchases of that package were made, expired, cancelled, etc.

In IPS Community Suite 4, it is possible to overload any class in the suite, so this is no longer a specific option for packages. But it is easy to recreate.

 

Step 1: Create a Plugin

You will need to create a plugin, which requires you to have developer mode enabled. We strongly advise against installing developer mode on a live installation so do this, and your development on a test installation.

Once developer mode is enabled, a "Create Plugin" button will appear in the Admin CP under System > Site Features -> Plugin. Use this tool to create your plugin, after which you'll be taken into the Plugin Developer Center.

Once in the Plugin Developer Center you will create a Code Hook on the \IPS\nexus\Package class (for more information about Code Hooks and other Plugin features see this guide).

 

Step 2: Write the code

The content of the plugin will be very similar to the custom module from before, however there are some key differences:

  • The parameters for the methods are different. Rather than arrays, you will be passed objects for purchase and other data. Since your code is running on an instance of a package, you will not be passed $package and should use $this instead. A full list of the available methods and their signatures are below.
  • Because your code will run for every package, you must first check the ID number ($this->id) to see if it is a package you want to run the code for.
  • You are overloading a method which may be doing things itself, so you need to call the parent:: method within your code.
  • If you were using the IP.Board 3 API, you will of course need to update your calls to use the IPS Community Suite 4 API.

For example, if this was your custom module on IP.Nexus for IP.Board 3:

<?php

class custom_actions_FOO
{
    /**
     * Purchase record generated (run after onPaid)
     *
     * @param	array	The member purchasing
     * @param	array   Package data (combined array with row from nexus_packages and nexus_packages_*, depending on the package type)
     * @param	invoice	Invoice Model
     * @param	array	Row from nexus_purchases [since Nexus 1.5]
     * @return	void
     */
    public function onPurchaseGenerated( $member, $package, $invoice, $purchase )
    {    
		ipsRegistry::DB()->insert( 'purchase_log', array(
			'member'	=> $member['member_id'],
			'package'	=> $package['p_id'],
			'purchase'	=> $purchase['ps_id'],
			'time'		=> time()
		) );
    }

}

Then your code hook for IPS4 will look like this:

<?php

class hook
{
	/**
	 * On Purchase Generated
	 *
	 * @param	\IPS\nexus\Purchase	$purchase	The purchase
	 * @param	\IPS\nexus\Invoice	$invoice	The invoice
	 * @return	void
	 */
	public function onPurchaseGenerated( \IPS\nexus\Purchase $purchase, \IPS\nexus\Invoice $invoice )
    {
	    if ( in_array( $this->id, array( 1, 2, 3 ) ) )
	    {
			\IPS\Db::i()->insert( 'purchase_log', array(
				'member'	=> $purchase->member->member_id,
				'package'	=> $this->id,
				'purchase'	=> $purchase->id,
				'time'		=> time()
			) );
		}
		
		return parent::onPurchaseGenerated( $purchase, $invoice );
    }

}

 

Step 3: Download the plugin and install

Once you're ready, download your plugin from the Plugin Developer Center which will generate an xml file. Go to your live installation and install this plugin there.

 

Available methods

You are overloading \IPS\nexus\Package, so you can specify any methods which are part of that. The most common use-cases however are shown below.

void onPurchaseGenerated\IPS\nexus\Purchase $purchase, \IPS\nexus\Invoice $invoice )

Called when a purchase is generated; that is, when the invoice is marked as paid (either automatically, or by an admin).

  • $purchase
    Purchase object representing a line item on an invoice.
  • $invoice
    The invoice to which this purchase belongs

 

void onRenew( \IPS\nexus\Purchase $purchase, int $cycles )

Called when a renewal invoice for a purchase is paid.

  • $purchase
    Purchase object representing a line item on an invoice
  • $cycles
    The number of cycles this renewal covers. When a user renews their purchase, they sometimes have the ability to pay for future renewals in one go, and $cycles represents the number of renewal periods that have been paid for. 

 

void onExpirationDateChange\IPS\nexus\Purchase $purchase )

Called when the expiration date of a purchase changes, e.g. when the purchase is renewed.

  • $purchase
    Purchase object representing a line item on an invoice

 

void onExpire\IPS\nexus\Purchase $purchase )

Called when a purchase expires.

  • $purchase
    Purchase object representing a line item on an invoice

 

void onCancel\IPS\nexus\Purchase $purchase )

Called when a purchase is canceled.

  • $purchase
    Purchase object representing a line item on an invoice

 

void onDelete\IPS\nexus\Purchase $purchase )

Called when a purchase is deleted.

  • $purchase
    Purchase object representing a line item on an invoice

 

void onReactivate( \IPS\nexus\Purchase $purchase )

Called when a purchase is reactivated after having expired.

  • $purchase
    Purchase object representing a line item on an invoice

 

void onTransfer( \IPS\nexus\Purchase $purchase, \IPS\Member $newCustomer )

Called just before a purchase is transferred to another customer account.

  • $purchase
    Purchase object representing a line item on an invoice
  • $newCustomer
    Member object for the customer account to which this purchase is being transferred

 

void onChange( \IPS\nexus\Purchase $purchase, \IPS\nexus\Package $newPackage [, mixed $chosenRenewalOption = NULL] )

Called when a package in a purchase is upgraded or downgraded.

  • $purchase
    Purchase object representing a line item on an invoice
  • $newPackage
    The new package to which the purchase is being upgraded or downgraded
  • $chosenRenewalTerm
    The renewal term chosen for the new package; either an integer or a \IPS\nexus\RenewalTerm object (or NULL)

  Report Document


×
×
  • Create New...