Project

General

Profile

Plugin system » History » Version 13

Max Milbers, 06/08/2011 10:52 PM

1 10 Oscar van Eijk
{{>toc}}
2
3 1 Oscar van Eijk
h1. Plugin system
4
5 5 Oscar van Eijk
Since VirtueMart v2, the Joomla! plugin system is used form payment and shipper plugins.
6 1 Oscar van Eijk
7
h2. Installing plugins
8
9
During development the the VM2 branch, the plugins are not available as Joomla install packages, so for test environments, they must be installed manually.
10
11
h3. Payment plugins
12
13
At the time of writing, only 2 of the former payment plugins have been converted to the new plugin system. Others should follow soon, all help is appreciated!!
14
15
Use the SQL query below to add the plugins to your database (assuming the table prefix is "jos_"):
16
17 12 Christopher Roussel
J1.5:
18 1 Oscar van Eijk
<pre>
19 4 Devendra Kumar Shukla
INSERT INTO `jos_plugins` (`id`, `name`, `element`, `folder`, `access`, `ordering`
20 1 Oscar van Eijk
  , `published`, `iscore`, `client_id`, `checked_out`, `checked_out_time`, `params`)
21
VALUES
22
 (NULL, 'VMPayment - Authorize', 'authorize', 'vmpayment', 0, 0, 1, 0, 0, 0, '0000-00-00 00:00:00', '')
23 12 Christopher Roussel
,(NULL, 'VMPayment - Cash on delivery', 'cashondel', 'vmpayment', 0, 0, 1, 0, 0, 0, '0000-00-00 00:00:00','');
24 1 Oscar van Eijk
</pre>
25 12 Christopher Roussel
J1.6:
26
<pre>
27
INSERT INTO `jos_extensions` (`extension_id`,  `type`, `name`, `element`, `folder`, `access`, `ordering`
28
  , `enabled`, `protected`, `client_id`, `checked_out`, `checked_out_time`, `params`)
29
VALUES
30
 (NULL, 'plugin', 'plg_vmpayment_authorize', 'authorize', 'vmpayment', 1, 0, 1, 0, 0, 0, '0000-00-00 00:00:00', '')
31
,(NULL, 'plugin', 'plg_vmpayment_cashondel', 'cashondel', 'vmpayment', 1, 0, 1, 0, 0, 0, '0000-00-00 00:00:00','');
32
</pre>
33 1 Oscar van Eijk
34 13 Max Milbers
Next, when you did not use the svn checkout, copy the plugin files (authorize.php, authorize.xml, cashondel.php and cashondel.xml), located in the folder /plugins/vmpayment, to the Joomla plugin directory. If that doesn't exist, it must be created first.
35 1 Oscar van Eijk
36
Now, in the store maintenance, you can add payment methods based on one of these plugins. Note at this moment it is required to select a vendor!
37
38
h3. Shipper plugins
39 3 Oscar van Eijk
40 1 Oscar van Eijk
This is very similar to the payment plugins. Only 1 plugin has been created (again: all help is appreciated!); 'standard', which provides a basic shipping option for postal services.
41
*Note:* Installing the sample data does _NOT_ provide shipping rates anymore!
42
43
Use the SQL query below to add the plugins to your database (assuming the table prefix is "jos_"):
44
45 12 Christopher Roussel
J1.5:
46 1 Oscar van Eijk
<pre>
47
INSERT INTO `jos_plugins` (`id`, `name`, `element`, `folder`, `access`, `ordering`
48
  , `published`, `iscore`, `client_id`, `checked_out`, `checked_out_time`, `params`)
49
VALUES
50 12 Christopher Roussel
 (NULL, 'VMShipper - Standard', 'standard', 'vmshipper', 0, 0, 1, 0, 0, 0, '0000-00-00 00:00:00', '');
51 1 Oscar van Eijk
</pre>
52
53 12 Christopher Roussel
J1.6: 
54
<pre>
55
INSERT INTO `jos_extensions` (`extension_id`, `type`, `name`, `element`, `folder`, `access`, `ordering`
56
  , `enabled`, `protected`, `client_id`, `checked_out`, `checked_out_time`, `params`)
57
VALUES
58
 (NULL, 'plugin', 'plg_vmshipper_standard', 'standard', 'vmshipper', 1, 0, 1, 0, 0, 0, '0000-00-00 00:00:00', '');
59
</pre>
60 13 Max Milbers
Next, when you did not use the svn checkout, copy the plugin files (standard.php and standard.xml), located in the folder /plugins/vmshipper, to the Joomla plugin directory. If that doesn't exist, it must be created first.
61 1 Oscar van Eijk
62
Now use the 'Shipping' menu item in the backend to add 1 or more carriers based on this plugin. All you need to do here is give the shipper a name and select a vendor (this is optional; when no vendor is selected, this carrier is valid for all vendors).
63
Finally you can add the shipping rates for the created vendor. Make sure a there's always a valid shipping rate: customers select the carrier only and the plugin must be able to find a mathcing rate based on the total order weight and the shipto address!
64
65 5 Oscar van Eijk
h2. Plugin Development
66 1 Oscar van Eijk
67 5 Oscar van Eijk
All plugins for VirtueMart should be developed confirming the Joomla! plugin development methods. Refer to http://docs.joomla.org/Tutorial:Plugins for developers documentation for Joomla! plugins.
68
69
h3. Payment Plugins
70
71 7 Oscar van Eijk
Payment plugins are used both in the front-end and the backend. They must be created as classes deriving from the base class *vmPaymentPlugin*:
72 9 Oscar van Eijk
<pre><code class="php">
73 7 Oscar van Eijk
class plgVmPayment<myPlugin> extends vmPaymentPlugin {
74
	function plgVmPayment<myPlugin>(&$subject, $config) {
75
		$this->_pelement = basename(__FILE__, '.php');	// Required!
76
		$this->_createTable();				// Required, see below
77
		parent::__construct($subject, $config);
78
	}
79
}
80 9 Oscar van Eijk
</code></pre>
81 7 Oscar van Eijk
82
Below is the list with events and a description on what moment they are fired.
83 6 Oscar van Eijk
* *plgVmOnSelectPayment()* - This event is fired during the checkout process. It allows the shopper to select one of the available payment methods.
84 5 Oscar van Eijk
It should display a radio button (name: paym_id) to select the payment method. Other information (like credit card info) might be selected as well, depending on the plugin.
85
+Return:+
86
It must return the HTML code for displaying the form (radio button and optional extra selections).
87
+Parameters:+
88
# (VirtueMartCart object) The cart object 
89
# (integer, default 0) ID of an already selected payment method ID, if any
90
91 6 Oscar van Eijk
* *plgVmOnPaymentSelectCheck()* - This event is fired after the payment method has been selected. It can be used to store
92 5 Oscar van Eijk
additional payment info in the cart.
93
+Return:+
94
Plugins that were not selected must return null, otherwise True of False must be returned indicating Success or Failure.
95
+Parameters:+
96
# (VirtueMartCart object) The cart object 
97
98 10 Oscar van Eijk
99
100 6 Oscar van Eijk
* *plgVmOnCheckoutCheckPaymentData()* - This event is fired during the checkout process. It can be used to validate the payment data as entered by the user.
101 5 Oscar van Eijk
+Return:+
102
Plugins that were not selected must return null, otherwise True of False must be returned indicating Success or Failure.
103
+Parameters:+
104
None
105
106 6 Oscar van Eijk
* *plgVmOnConfirmedOrderStorePaymentData()* - This event is fired after the payment has been processed; it stores the payment method-specific data.
107 5 Oscar van Eijk
All plugins _must_ reimplement this method.
108
+Return:+
109
If the plugin is NOT actually executed (not the selected payment method), this method must return NULL
110
If this plugin IS executed, it MUST return the order status code that the order should get. This triggers the stock updates if required
111
+Parameters:+
112
# (integer) The ordernumber being processed
113
# (object) Data from the cart
114
# (array) Price information for this order
115
	
116 6 Oscar van Eijk
* *plgVmOnShowOrderPaymentBE()* - This method is fired when showing the order details in the backend. It displays the the payment method-specific data.
117 5 Oscar van Eijk
All plugins _must_ reimplement this method.
118
+Return:+
119
Null when for payment methods that were not selected, text (HTML) otherwise
120
+Parameters:+
121
# (integer) The order ID
122
# (integer) Payment method used for this order
123
124 6 Oscar van Eijk
* *plgVmOnCancelPayment()* - This event is fired each time the status of an order is changed to Cancelled. It can be used to refund payments, void authorization etc.
125 5 Oscar van Eijk
When reimplementing this methis, the body _must_ start with this code:
126 9 Oscar van Eijk
<pre><code class="php">$_paymethodID = $this->getPaymentMethodForOrder($_orderID);
127 5 Oscar van Eijk
if (!$this->selectedThisMethod($this->_pelement, $_paymethodID)) {
128
	return;
129 9 Oscar van Eijk
}</code></pre>
130 5 Oscar van Eijk
+Parameters:+
131
 (integer The order ID
132
 (char) Previous order status
133
 (char) New order status
134
135 6 Oscar van Eijk
* *plgVmOnShipOrderPayment()* - This event is fired when the status of an order is changed to Shipped. It can be used to confirm or capture payments
136 5 Oscar van Eijk
When reimplementing this methis, the body _must_ start with this code:
137 9 Oscar van Eijk
<pre><code class="php">$_paymethodID = $this->getPaymentMethodForOrder($_orderID);
138 5 Oscar van Eijk
if (!$this->selectedThisMethod($this->_pelement, $_paymethodID)) {
139
	return;
140 9 Oscar van Eijk
}</code></pre>
141 5 Oscar van Eijk
+Return:+
142
True on success, False on failure, Null if this plugin was not activated
143
+Parameters:+
144 1 Oscar van Eijk
# (integer) Order ID
145 7 Oscar van Eijk
146
Other helper functions inherited from the base class:
147
148 9 Oscar van Eijk
* *_ createTable()* - This method is used to create and maintain the plugin specific database table(s).
149 7 Oscar van Eijk
It _must_ be reimplemented by all plugins.
150 1 Oscar van Eijk
+Example:+
151 8 Oscar van Eijk
<pre><code class="php">
152 7 Oscar van Eijk
$_scheme = DbScheme::get_instance();
153
$_scheme->create_scheme('#__vm_order_payment_'.$this->_pelement);
154
$_schemeCols = array(
155
	 'id' => array (
156
			 'type' => 'int'
157
			,'length' => 11
158
			,'auto_inc' => true
159
			,'null' => false
160
	)
161
	,'order_id' => array (
162
			 'type' => 'int'
163
			,'length' => 11
164
			,'null' => false
165
	)
166
	,'payment_method_id' => array (
167
			 'type' => 'text'
168
			,'null' => false
169
	)
170
);
171
$_schemeIdx = array(
172
	 'idx_order_payment' => array(
173
			 'columns' => array ('order_id')
174
			,'primary' => false
175
			,'unique' => false
176
			,'type' => null
177
	)
178
);
179
$_scheme->define_scheme($_schemeCols);
180
$_scheme->define_index($_schemeIdx);
181
if (!$_scheme->scheme()) {
182
	JError::raiseWarning(500, $_scheme->get_db_error());
183
}
184
$_scheme->reset();
185 8 Oscar van Eijk
</code></pre>
186 7 Oscar van Eijk
187 8 Oscar van Eijk
* *getPaymentMethodForOrder()* - Get the order payment ID for a given order number
188 7 Oscar van Eijk
+Return:+
189
The payment method ID, or -1 when not found 
190
+Parameters:+
191
# (integer) The order ID
192
193 8 Oscar van Eijk
* *getThisMethodName()* - Get the name of the payment method.
194 7 Oscar van Eijk
This method can _not_ be reimplemented
195
+Return:+
196
Paymenent method name
197
+Parameters:+
198
# (integer) The payment method ID
199
200 8 Oscar van Eijk
* *selectedThisMethod()* - This method checks if the selected payment method matches the current plugin
201 7 Oscar van Eijk
+Return:+
202
True if the calling plugin has the given payment ID, False otherwise.
203
+Parameters:+
204
# (string) Element name, taken from the plugin filename
205
# (integer) The payment method ID
206
207 8 Oscar van Eijk
* *writePaymentData()* - This method writes all payment plugin specific data to the plugin's table
208 7 Oscar van Eijk
+Return:+
209
True if the calling plugin has the given payment ID, False otherwise.
210
+Parameters:+
211
# (array) Indexed array in the format 'column_name' => 'value'
212
# (string) Table name
213 10 Oscar van Eijk
214
h3. Shipper Plugins
215
216
Shipper plugins are used both in the front-end and the backend. They must be created as classes deriving from the base class *vmShipperPlugin*:
217
<pre><code class="php">
218
class plgVmShipper<myPlugin> extends vmShipperPlugin {
219
	function plgVmPayment<myPlugin>(&$subject, $config) {
220
		$this->_pelement = basename(__FILE__, '.php');	// Required!
221
		$this->_createTable();				// Required, see below
222
		parent::__construct($subject, $config);
223
	}
224
}
225
</code></pre>
226
227
Here's a short overview of the events:
228
* When a shopper selects a shipper, *plgOnSelectShipper()* is fired. It displays the shipper and can be used for collecting extra - shipper specific - info.
229
* After selecting, *plgVmShipperSelected()* can be used to store extra shipper info in the cart. The selected shipper ID will be stored in the cart by the checkout process before this method is fired.
230
* *plgOnConfirmShipper()* is fired when the order is confirmed and stored to the database. It is called before the rest of the order or stored, when reimplemented, it _must_ include a call to parent::plgOnConfirmShipper() (or execute the same steps to put all data in the cart)
231
232
When a stored order is displayed in the backend, the following events are used:
233
* *plgVmOnShowOrderShipperBE()* displays specific data about (a) shipment(s) (NOTE: this plugin is _OUTSIDE_ any form!)
234
* *plgVmOnShowOrderLineShipperBE()*) can be used to show information about a single orderline, e.g. display a package code at line level when more packages are shipped.
235
* *plgVmOnEditOrderLineShipperBE() can be used add a package code for an order line when more packages are shipped.
236
* *plgVmOnUpdateOrderShipperBE()* is fired inside a form. It can be used to add shipper data, like package code.
237
* *plgVmOnSaveOrderShipperBE()* is fired from the backend after the order has been saved. If one of the show methods above have to option to add or edit info, this method must be used to save the data.
238
* *plgVmOnUpdateOrderLine()* is fired from the backend after an order line has been saved. This method must be reimplemented if plgVmOnEditOrderLineShipperBE() is used.
239
240
The frontend 1 show method:
241
* plgVmOnShowOrderShipperFE() collects and displays specific data about (a) shipment(s)
242
243
Below is the list with events and a description on what moment they are fired.
244
245
* *plgVmOnSelectShipper()* - This event is fired during the checkout process. It allows the shopper to select one of the available shippers.
246
It should display a radio button (name: shipper_id) to select the shipper. In the description, the shipping cost can also be displayed, based on the total order weight and the shipto country (this wil be calculated again during order confirmation)
247
+Return:+
248
HTML code to display the form
249
+Parameters:+
250
# (VirtueMartCart object)  Cart object
251
# (integer, default 0) ID of the currently shipper selected
252
253
* *plgVmOnShipperSelected()* - This event is fired after the shipping method has been selected. It can be used to store additional shipper info in the cart.
254
+Return:+
255
True on succes, false on failures, null when this plugin was not selected.
256
On errors, JError::raiseWarning (or JError::raiseError) must be used to set a message.
257
+Parameters:+
258
# (VirtueMartCart object)  Cart object
259
# (integer, default 0) ID of the shipper selected
260
261
* *plgVmOnConfirmShipper()* - This event is fired after the payment has been processed; it selects the actual shipping rate based on the shipto (country, zip) and/or order weight, and optionally writes extra info to the database (in which case this method must be reimplemented).
262
Reimplementation is not required, but when done, the following check MUST be made:
263
<pre><code class="php">
264
if (!$this->selectedThisShipper($this->_selement, $_cart->shipper_id)) {
265
	return null;
266
}</code></pre>
267
+Return:+
268
The shipping rate ID
269
Do _not_ return parent::plgVmOnConfirmShipper($_cart); it is valid but will produce extra overhead!
270
+Parameters:+
271
# (VirtueMartCart object) Cart object
272
273
* *plgVmOnShowOrderShipperBE()* - This method is fired when showing the order details in the backend. It displays the shipper-specific data.
274
NOTE, this plugin should NOT be used to display form fields, since it's called outside a form! Use *plgVmOnUpdateOrderBE()( instead!
275
+Return:+
276
Null for shippers that aren't active, text (HTML) otherwise
277
+Parameters:+
278
# (integer) The order ID
279
# (integer) Vendor ID
280
# (object) Object with 2 properties 'carrier' and 'name' 
281
282
* *plgVmOnEditOrderLineShipperBE()* - This method is fired when editing the order line details in the backend. It can be used to add line specific package codes
283
+Return:+
284
Null for shippers that aren't active, text (HTML) otherwise
285
+Parameters:+
286
# (integer) The order ID
287
# (integer) The order Line ID
288
289
* *plgVmOnUpdateOrderShipper()* - Save updated order data to the shipper specific table
290
+Return:+
291
True on success, false on failures (the rest of the save-process will be skipped!), or null when this shipper is not actived.
292
+Parameters:+
293
# (array) Form data
294
295
* *plgVmOnUpdateOrderLineShipper()* - Save updated orderline data to the shipper specific table
296
+Return:+
297
True on success, false on failures (the rest of the save-process will be skipped!), or null when this shipper is not actived.
298
+Parameters:+
299
# (array) Form data
300
301
* *plgVmOnShowOrderShipperFE()* - This method is fired when showing the order details in the frontend.  It displays the shipper-specific data.
302
+Return:+
303
Null for shippers that aren't active, text (HTML) otherwise
304
+Parameters:+
305
# (integer) The order ID
306
307
* *plgVmOnShowOrderLineShipperFE()* - This method is fired when showing the order details in the frontend, for every orderline. It can be used to display line specific package codes, e.g. with a link to external tracking and tracing systems
308
+Return:+
309
Null for shippers that aren't active, text (HTML) otherwise
310
+Parameters:+
311
# (integer) The order ID
312
# (integer) The order LineID
313
314
Other helper functions inherited from the base class:
315
316
* *_ createTable()* - This method is used to create and maintain the plugin specific database table(s).
317
It _must_ be reimplemented by all plugins.
318
+Example:+
319
<pre><code class="php">$_scheme = DbScheme::get_instance();
320
$_scheme->create_scheme('#__vm_order_shipper_'.$this->_selement);
321
$_schemeCols = array(
322
	 'id' => array (
323
			 'type' => 'int'
324
			,'length' => 11
325
			,'auto_inc' => true
326
			,'null' => false
327
	)
328
	,'order_id' => array (
329
			 'type' => 'int'
330
			,'length' => 11
331
			,'null' => false
332
	)
333
	,'shipper_id' => array (
334
			 'type' => 'text'
335
			,'null' => false
336
	)
337
);
338
$_schemeIdx = array(
339
	 'idx_order_payment' => array(
340
			 'columns' => array ('order_id')
341
			,'primary' => false
342
			,'unique' => false
343
			,'type' => null
344
	)
345
);
346
$_scheme->define_scheme($_schemeCols);
347
$_scheme->define_index($_schemeIdx);
348
if (!$_scheme->scheme()) {
349
	JError::raiseWarning(500, $_scheme->get_db_error());
350
}
351
$_scheme->reset();
352
</code></pre>
353
354
* *getThisShipperName()* -  Get the name of the shipper
355
+Return:+
356
Shipper name
357
+Parameters:+
358
# (integer) The Shipper ID
359
	final protected function ($_sid)
360
361
* *writeShipperData()* - This method writes all shipper plugin specific data to the plugin's table
362
+Parameters:+
363
# (array) Indexed array in the format 'column_name' => 'value'
364
# (string) Table name
365
366
* *getShippingRateIDForOrder()* - Get the shipping rate ID for a given order number
367
+Return:+
368
The shipping rate ID, or -1 when not found 
369
+Parameters:+
370
# (integer) The order ID
371
372
* *getShippingRate()* -  Get the total price for a shipping rate
373
+Return:+
374
Price in display format
375
+Parameters:+
376
# (integer) Shipping rate ID
377
	
378
* *getShipperIDForOrder()* - Get the shipper ID for a given order number
379
+Return:+
380
The shipper ID, or -1 when not found 
381
+Parameters:+
382
# (integer) The order ID
383
384
* *selectShippingRate()* - Select the shipping rate ID, based on the selected shipper in combination with the shipto address (country and zipcode) and the total order weight.
385
+Return:+
386
Shipping rate ID, -1 when no match is found. Only 1 selected ID will be returned; if more ID's match, the cheapest will be selected.
387
+Parameters:+
388
# (VirtueMartCart object) Cart object
389
# (integer, default 0) Shipper ID, by default taken from the cart
390
391
* *selectedThisShipper()* - This method checks if the selected shipper matches the current plugin
392
+Return:+
393
True if the calling plugin has the given payment ID
394
+Parameters:+
395
# (string) Element name, taken from the plugin filename
396
# (integer) The shipper ID
397
398
* *getOrderWeight()* - Get the total weight for the order, based on which the proper shipping rate can be selected.
399
+Return:+
400
Total weight for the order
401
+Parameters:+
402
# (VirtueMartCart object) Cart object
403
404
* *getCarriers()* - Fill an array with all carriers found with this plugin for the current vendor
405
+Return:+
406
True when carrier(s) was (were) found for this vendor, false otherwise
407
+Parameters:+
408
# (integer) The vendor ID taken from the cart.
409
410
* *validateVendor()* - Check if this shipper has carriers for the current vendor.
411
+Return:+
412
True when a shipper_id was found for this vendor, false otherwise
413
+Parameters:+
414
# (integer) The vendor ID taken from the cart.