eZ Community » Learn » eZ Publish » Selling Pay-Per-Download Products

Selling Pay-Per-Download Products

Monday 31 August 2009 12:00:00 am

  • Currently 5 out of 5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

With the permissions structure in place, we need to create the workflow event that will assign buyers the correct role with the correct limitation when they have checked out. We need to create a workflow extension that will be triggered after the buyer has paid.

The framework for a workflow extension is quite straightforward, including the following extension file and folder structure (beneath eZ Publish's "extension" folder):

payperdownload
-- eventtypes
---- event
------ payperdownload
-------- payperdownloadtype.php
-- settings
---- workflow.ini.append.php
---- payperdownload.ini

workflow.ini

In workflow.ini.append.php within your extension, you need to refer to the extension directory itself as containing (a) workflow event(s), and specify the name of the event, prefixed by "event_":

<?php /* #?ini charset="utf-8"?
 
[EventSettings]
ExtensionDirectories[]=payperdownload
AvailableEventTypes[]=event_payperdownload
 
*/ ?>

payperdownloadtype.php

payperdownloadtype.php will contain all of the code you want to execute when the workflow is triggered. For now, we will just set up the framework for the class using some boilerplate code:

<?php
 
class PayPerDownloadType extends eZWorkflowEventType
{
    const WORKFLOW_TYPE_STRING = "payperdownload";
 
    public function __construct()
    {
        parent::__construct( self::WORKFLOW_TYPE_STRING, ezi18n( 'kernel/workflow/event', 'Pay Per Download' ) );
        $this->setTriggerTypes( array( 'shop' => array( 'checkout' => array( 'before' ) ) ) );
    }
 
    public function execute( $process, $event )
    {
        // Important code will go here
 
        return eZWorkflowType::STATUS_ACCEPTED;
    }
}
 
eZWorkflowEventType::registerEventType( PayPerDownloadType::WORKFLOW_TYPE_STRING, 'PayPerDownloadType' );
?>

The "PayPerDownloadType" function defines the workflow event and specifies the trigger(s) for which it is meant. The "execute" function will contain all of our important code to assign the role based on the product(s) purchased. We will write this code shortly. The last line in the file registers our event with eZ Publish.

For now our code does nothing, of course, but we should first make sure that our framework works before we proceed.

Activate the extension

The extension is activated as is the normal process in site.ini.append.php, and we will do this in that file in settings/override:

[ExtensionSettings]
ActiveExtensions[]
# ... other extensions are also activated in this list
ActiveExtensions[]=payperdownload

You should also clear the cache and regenerate the autoloads file.

Tell eZ Publish when to use the workflow

We want our event to directly follow the PayPal payment event, and will actually use the same workflow. Go to "Setup > Workflows" in the Administration Interface click the "Payment gateways" workflow group, then edit the "Payment and Fulfillment" workflow. Add an event of the "Event / Pay per download" type in this workflow.

As our workflow event doesn't really do anything yet, you shouldn't notice any difference when you use your "Test User" account to purchase a product. However, if you were able to add an event of the "Pay Per Download" type in the Administration Interface, that means that eZ Publish has recognized your workflow extension.

36 542 Users on board!

Tutorial menu

Printable

Printer Friendly version of the full article on one page with plain styles

Author(s)

Proudly Developed with from