plugin.php
215 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @package VikAppointments |
| 4 | * @subpackage core |
| 5 | * @author E4J s.r.l. |
| 6 | * @copyright Copyright (C) 2021 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 | /** |
| 15 | * Payment adpater used to allow third-party plugins to perform the payment process. |
| 16 | * |
| 17 | * The I/O of this class MUST be the same for all the E4J programs that support |
| 18 | * extendable payment methods. |
| 19 | * |
| 20 | * @note The class prefix is equals to the 3-letter name of the program, |
| 21 | * "VAP" in this case. |
| 22 | * |
| 23 | * @since 1.7.1 |
| 24 | */ |
| 25 | final class VAPPaymentPlugin |
| 26 | { |
| 27 | /** |
| 28 | * The payment driver name. |
| 29 | * |
| 30 | * @var string |
| 31 | */ |
| 32 | private $driver; |
| 33 | |
| 34 | /** |
| 35 | * The order information needed to complete the payment process. |
| 36 | * |
| 37 | * @var JRegistry |
| 38 | */ |
| 39 | private $order; |
| 40 | |
| 41 | /** |
| 42 | * The payment configuration. |
| 43 | * |
| 44 | * @var JRegistry |
| 45 | */ |
| 46 | private $params; |
| 47 | |
| 48 | /** |
| 49 | * The event dispatcher instance. |
| 50 | * |
| 51 | * @var mixed |
| 52 | */ |
| 53 | private $dispatcher; |
| 54 | |
| 55 | /** |
| 56 | * Class constructor. |
| 57 | * |
| 58 | * @param string $driver The driver name. |
| 59 | * @param array $order The order info array. |
| 60 | * @param array $params The payment configuration saved through the fields |
| 61 | * generated by onLoadPaymentMethodConfigurationForm. |
| 62 | */ |
| 63 | public function __construct($driver, $order, $params = array()) |
| 64 | { |
| 65 | $this->driver = $driver; |
| 66 | $this->order = new JRegistry($order); |
| 67 | $this->params = new JRegistry($params); |
| 68 | |
| 69 | /** |
| 70 | * Create the event dispatcher. |
| 71 | * |
| 72 | * @note Handle the dispatcher according to the platform version. |
| 73 | */ |
| 74 | $this->dispatcher = VAPFactory::getEventDispatcher(); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * This method is invoked every time a user visits the page of a |
| 79 | * reservation with PENDING Status. |
| 80 | * |
| 81 | * @return void |
| 82 | */ |
| 83 | public function showPayment() |
| 84 | { |
| 85 | /** |
| 86 | * Trigger hook to allow third-party plugins to start a payment transaction. |
| 87 | * Here it is possible to do a redirect to the bank website or to display |
| 88 | * a seamless form. |
| 89 | * |
| 90 | * @note The caller (1st argument) must be equals to the program in use. |
| 91 | * |
| 92 | * @param string $element The component that triggered this event. |
| 93 | * @param string $payment The ID of the payment driver. |
| 94 | * @param JRegistry $order The order info registry. |
| 95 | * @param JRegistry $params The payment configuration saved through the fields |
| 96 | * generated by onLoadPaymentMethodConfigurationForm. |
| 97 | * |
| 98 | * @return string The HTML to display. |
| 99 | * |
| 100 | * @since 1.7.1 |
| 101 | */ |
| 102 | $results = $this->dispatcher->trigger('onStartPaymentTransaction', array('vikappointments', $this->driver, $this->order, $this->params)); |
| 103 | |
| 104 | // auto-display all the returned strings |
| 105 | echo implode("\n", array_filter($results)); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Validates the transaction details sent from the bank. |
| 110 | * This method is invoked by the system every time the Notify URL |
| 111 | * is visited (the one used in the showPayment() method). |
| 112 | * |
| 113 | * @return array The array result, which MUST contain the "verified" key (1 or 0). |
| 114 | */ |
| 115 | public function validatePayment() |
| 116 | { |
| 117 | /** |
| 118 | * Create payment status. |
| 119 | * |
| 120 | * @note The status class must be loaded and renamed |
| 121 | * according to the program requirements. |
| 122 | */ |
| 123 | VAPLoader::import('libraries.payment.status'); |
| 124 | $status = new VAPPaymentStatus(); |
| 125 | |
| 126 | /** |
| 127 | * Trigger hook to allow third-party plugins to validate the transaction started |
| 128 | * through the showPayment method. |
| 129 | * |
| 130 | * @note The caller (1st argument) must be equals to the program in use. |
| 131 | * |
| 132 | * @param string $element The component that triggered this event. |
| 133 | * @param string $payment The ID of the payment driver. |
| 134 | * @param VAPPaymentStatus $status The object holding the payment status. |
| 135 | * @param JRegistry $order The order info registry. |
| 136 | * @param JRegistry $params The payment configuration saved through the fields |
| 137 | * generated by onLoadPaymentMethodConfigurationForm. |
| 138 | * |
| 139 | * @return void |
| 140 | * |
| 141 | * @since 1.7.1 |
| 142 | */ |
| 143 | $this->dispatcher->trigger('onValidatePaymentTransaction', array('vikappointments', $this->driver, $status, $this->order, $this->params)); |
| 144 | |
| 145 | return $status; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * This function is called after the payment has been validated for redirect actions. |
| 150 | * When this method is called, the class is invoked after the validatePayment() function. |
| 151 | * |
| 152 | * @param boolean $result The result of the transaction. |
| 153 | * |
| 154 | * @return void |
| 155 | */ |
| 156 | public function afterValidation($result = false) |
| 157 | { |
| 158 | /** |
| 159 | * Trigger hook to allow third-party plugins to do something after validating |
| 160 | * a payment transaction, in example to do a redirect or to terminate the session. |
| 161 | * |
| 162 | * @note The caller (1st argument) must be equals to the program in use. |
| 163 | * |
| 164 | * @param string $element The component that triggered this event. |
| 165 | * @param string $payment The ID of the payment driver. |
| 166 | * @param boolean $status True in case of success, false otherwise. |
| 167 | * @param JRegistry $order The order info registry. |
| 168 | * @param JRegistry $params The payment configuration saved through the fields |
| 169 | * generated by onLoadPaymentMethodConfigurationForm. |
| 170 | * |
| 171 | * @return void |
| 172 | * |
| 173 | * @since 1.7.1 |
| 174 | */ |
| 175 | $this->dispatcher->trigger('onCompletePaymentTransaction', array('vikappointments', $this->driver, $result, $this->order, $this->params)); |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Performs the refund request of a payment. |
| 180 | * |
| 181 | * @return array The array result, which MUST contain the "verified" key (1 or 0). |
| 182 | */ |
| 183 | public function refund() |
| 184 | { |
| 185 | /** |
| 186 | * Create payment status. |
| 187 | * |
| 188 | * @note The status class must be loaded and renamed |
| 189 | * according to the program requirements. |
| 190 | */ |
| 191 | VAPLoader::import('libraries.payment.status'); |
| 192 | $status = new VAPPaymentStatus(); |
| 193 | |
| 194 | /** |
| 195 | * Trigger hook to allow third-party plugins to perform a refund of a transaction. |
| 196 | * |
| 197 | * @note The caller (1st argument) must be equals to the program in use. |
| 198 | * |
| 199 | * @param string $element The component that triggered this event. |
| 200 | * @param string $payment The ID of the payment driver. |
| 201 | * @param VAPPaymentStatus $status The object holding the payment status. |
| 202 | * @param JRegistry $order The order info registry. |
| 203 | * @param JRegistry $params The payment configuration saved through the fields |
| 204 | * generated by onLoadPaymentMethodConfigurationForm. |
| 205 | * |
| 206 | * @return void |
| 207 | * |
| 208 | * @since 1.7.1 |
| 209 | */ |
| 210 | $this->dispatcher->trigger('onRefundPaymentTransaction', array('vikappointments', $this->driver, $status, $this->order, $this->params)); |
| 211 | |
| 212 | return $status; |
| 213 | } |
| 214 | } |
| 215 |