PluginProbe
VikBooking Hotel Booking Engine & PMS / 1.6.5
VikBooking Hotel Booking Engine & PMS v1.6.5
1.8.16 1.8.15 1.8.14 1.8.13 1.8.12 1.8.11 1.8.10 1.8.9 1.8.6 1.8.7 1.8.8 trunk 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 1.6.9 1.7.0 1.7.1 1.7.2 All 37 releases
vikbooking / libraries / adapter / payment / dispatcher.php

dispatcher.php in VikBooking Hotel Booking Engine & PMS 1.6.5, at libraries/adapter/payment/dispatcher.php

151 lines 4.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package VikWP - Libraries
4 * @subpackage adapter.payment
5 * @author E4J s.r.l.
6 * @copyright Copyright (C) 2023 E4J s.r.l. All Rights Reserved.
7 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
8 * @link https://vikwp.com
9 */
10
11 // No direct access
12 defined('ABSPATH') or die('No script kiddies please!');
13
14 JLoader::import('adapter.payment.payment');
15
16 /**
17 * Abstract factory used to instantiate different payment gateways.
18 * The example below describes how to hook the payments to this dispatcher.
19 *
20 * add_action('load_payment_gateway_myplugin', function(&$drivers, $payment)
21 * {
22 * if ($payment == 'paypal')
23 * {
24 * JLoader::import('admin.payments.paypal', MYPLUGIN_BASE);
25 * $drivers[] = 'MyPluginPayPalPayment';
26 * }
27 * }, 10, 2);
28 *
29 * It is mandatory to indicate also the number of accepted arguments.
30 *
31 * @since 10.1
32 */
33 class JPaymentDispatcher
34 {
35 /**
36 * A list containing all the payment instances.
37 *
38 * @var JPayment[]
39 */
40 protected static $instances = array();
41
42 /**
43 * Provides a new instance for the specified arguments.
44 *
45 * @param string $plugin The name of the plugin that requested the payment.
46 * @param string $payment The name of the payment that should be instantiated.
47 * @param mixed $order The details of the order that has to be paid.
48 * @param mixed $config The payment configuration array or a JSON string.
49 *
50 * @return JPayment The payment found.
51 *
52 * @throws RuntimeException In case the requested payment doesn't exist.
53 */
54 public static function getInstance($plugin, $payment, $order = array(), $config = array())
55 {
56 if (substr($payment, -4) == '.php')
57 {
58 // make sure the payment doesn't contain PHP extension
59 $payment = substr($payment, 0, -4);
60 }
61
62 // create unique identifier
63 $sign = $plugin . '.' . $payment;
64
65 // check if the payment was already instantiated
66 if (!isset(static::$instances[$sign]))
67 {
68 $classname = null;
69 $drivers = array();
70
71 /**
72 * Trigger action to obtain a list of classnames of the payment gateway.
73 * The action should autoload the file that contains the classname.
74 * In case the payment should be loaded, the classname MUST be
75 * pushed within the &$drivers array.
76 * Fires before the instantiation of the returned classname.
77 *
78 * @param array A reference to the list of available drivers.
79 * @param string The name of the gateway to load.
80 *
81 * @since 10.1.1
82 */
83 do_action_ref_array('load_payment_gateway_' . $plugin, array(&$drivers, $payment));
84
85 // use the last driver in the list
86 $classname = array_pop($drivers);
87
88 if (!$classname || !class_exists($classname))
89 {
90 // payment not found, raise an exception
91 throw new RuntimeException('The payment [' . $payment . '] for [' . $plugin . '] does not exist.', 404);
92 }
93
94 // instantiate the payment
95 $payment = new $classname($plugin, $order, $config);
96
97 if (!$payment instanceof JPayment)
98 {
99 // the class is not an instance of JPayment, raise an exception
100 throw new RuntimeException('The payment [' . $classname . '] is not a valid instance.', 500);
101 }
102
103 // cache the payment
104 static::$instances[$sign] = $payment;
105 }
106
107 return static::$instances[$sign];
108 }
109
110 /**
111 * Returns a list of all the drivers supported by the specified plugin.
112 * The payments will be returned in ascending order.
113 *
114 * @param string $plugin The name of the plugin.
115 *
116 * @return array A list of drivers.
117 *
118 * @since 10.1.35
119 */
120 public static function getSupportedDrivers($plugin)
121 {
122 // init drivers array
123 $drivers = array();
124
125 /**
126 * Hook used to filter the list of all the supported drivers.
127 * Every plugin attached to this filter will be able to push one
128 * or more gateways within the $drivers array.
129 *
130 * @param array An array containing the list of the supported payments.
131 *
132 * @since 10.1
133 */
134 $drivers = apply_filters('get_supported_payments_' . $plugin, $drivers);
135
136 // remove duplicated records
137 $drivers = array_values(array_unique(array_filter($drivers)));
138
139 // get rid of the file path and file extension
140 $drivers = array_map(function($driver)
141 {
142 return basename($driver, '.php');
143 }, $drivers);
144
145 // sort by ascending driver name
146 sort($drivers);
147
148 return $drivers;
149 }
150 }
151