| 1 |
<?php |
| 2 |
/** |
| 3 |
* Gateway abstract model |
| 4 |
* |
| 5 |
* @version 1.0.0 |
| 6 |
* @package Charitable/Classes/Charitable_Gateway |
| 7 |
* @author David Bisset |
| 8 |
* @copyright Copyright (c) 2023, WP Charitable LLC |
| 9 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 10 |
*/ |
| 11 |
|
| 12 |
// Exit if accessed directly. |
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
if ( ! class_exists( 'Charitable_Gateway' ) ) : |
| 18 |
|
| 19 |
/** |
| 20 |
* Charitable_Gateway |
| 21 |
* |
| 22 |
* @abstract |
| 23 |
* @since 1.0.0 |
| 24 |
*/ |
| 25 |
abstract class Charitable_Gateway implements Charitable_Gateway_Interface { |
| 26 |
|
| 27 |
/** |
| 28 |
* The gateway's unique identifier. |
| 29 |
* |
| 30 |
* @var string The gateway's unique identifier. |
| 31 |
*/ |
| 32 |
const ID = ''; |
| 33 |
|
| 34 |
/** |
| 35 |
* The gateway's name. |
| 36 |
* |
| 37 |
* @var string Name of the payment gateway. |
| 38 |
* @since 1.0.0 |
| 39 |
*/ |
| 40 |
protected $name; |
| 41 |
|
| 42 |
/** |
| 43 |
* Credit card form. |
| 44 |
* |
| 45 |
* @var string Credit card form. |
| 46 |
* @since 1.8.0 |
| 47 |
*/ |
| 48 |
protected $credit_card_form; |
| 49 |
|
| 50 |
/** |
| 51 |
* The gateway's badge. |
| 52 |
* |
| 53 |
* @var string Name of the payment gateway. |
| 54 |
* @since 1.0.0 |
| 55 |
*/ |
| 56 |
protected $badge; |
| 57 |
|
| 58 |
/** |
| 59 |
* The recommended status. |
| 60 |
* |
| 61 |
* @var string Recommended status. |
| 62 |
* @since 1.8.1.10 |
| 63 |
*/ |
| 64 |
protected $recommended; |
| 65 |
|
| 66 |
/** |
| 67 |
* The gateway's default settings. |
| 68 |
* |
| 69 |
* @var array The default values for all settings added by the gateway. |
| 70 |
* @since 1.0.0 |
| 71 |
*/ |
| 72 |
protected $defaults; |
| 73 |
|
| 74 |
/** |
| 75 |
* Supported features such as 'credit-card', and 'recurring' donations |
| 76 |
* |
| 77 |
* @var string[] |
| 78 |
* @since 1.3.0 |
| 79 |
*/ |
| 80 |
protected $supports = array(); |
| 81 |
|
| 82 |
/** |
| 83 |
* Return the gateway name. |
| 84 |
* |
| 85 |
* @since 1.0.0 |
| 86 |
* |
| 87 |
* @return string |
| 88 |
*/ |
| 89 |
public function get_name() { |
| 90 |
return $this->name; |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Return the gateway name. |
| 95 |
* |
| 96 |
* @since 1.0.0 |
| 97 |
* |
| 98 |
* @return string |
| 99 |
*/ |
| 100 |
public function get_badge() { |
| 101 |
return $this->badge; |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Return the recommended status. |
| 106 |
* |
| 107 |
* @since 1.8.1.10 |
| 108 |
* |
| 109 |
* @return string |
| 110 |
*/ |
| 111 |
public function get_recommended() { |
| 112 |
return $this->recommended; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Returns the default gateway label to be displayed to donors. |
| 117 |
* |
| 118 |
* @since 1.0.0 |
| 119 |
* |
| 120 |
* @return string |
| 121 |
*/ |
| 122 |
public function get_default_label() { |
| 123 |
return isset( $this->defaults['label'] ) ? $this->defaults['label'] : $this->get_name(); |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Returns the default gateway label to be displayed to donors. |
| 128 |
* |
| 129 |
* @since 1.0.0 |
| 130 |
* |
| 131 |
* @return string |
| 132 |
*/ |
| 133 |
public function get_default_recommended() { |
| 134 |
return isset( $this->defaults['label'] ) ? $this->defaults['label'] : $this->get_name(); |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Provide default gateway settings fields. |
| 139 |
* |
| 140 |
* @since 1.0.0 |
| 141 |
* @since 1.8.1.10 Added recommended key to settings. |
| 142 |
* |
| 143 |
* @return array |
| 144 |
*/ |
| 145 |
public function default_gateway_settings() { |
| 146 |
return array( |
| 147 |
'section_gateway' => array( |
| 148 |
'type' => 'heading', |
| 149 |
'title' => $this->get_name(), |
| 150 |
'badge' => $this->get_badge(), |
| 151 |
'priority' => 2, |
| 152 |
'recommended' => false, |
| 153 |
), |
| 154 |
'label' => array( |
| 155 |
'type' => 'text', |
| 156 |
'title' => __( 'Gateway Label', 'charitable' ), |
| 157 |
'help' => __( 'The label that will be shown to donors on the donation form.', 'charitable' ), |
| 158 |
'priority' => 3, |
| 159 |
'default' => $this->get_default_label(), |
| 160 |
'recommended' => false, |
| 161 |
), |
| 162 |
); |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Return the settings for this gateway. |
| 167 |
* |
| 168 |
* @since 1.0.0 |
| 169 |
* |
| 170 |
* @return array |
| 171 |
*/ |
| 172 |
public function get_settings() { |
| 173 |
return charitable_get_option( 'gateways_' . $this->get_gateway_id(), array() ); |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Retrieve the gateway label. |
| 178 |
* |
| 179 |
* @since 1.0.0 |
| 180 |
* |
| 181 |
* @return string |
| 182 |
*/ |
| 183 |
public function get_label() { |
| 184 |
return charitable_get_option( 'label', $this->get_default_label(), $this->get_settings() ); |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Return the value for a particular gateway setting. |
| 189 |
* |
| 190 |
* @since 1.0.0 |
| 191 |
* |
| 192 |
* @param string $setting The setting key. |
| 193 |
* @return mixed |
| 194 |
*/ |
| 195 |
public function get_value( $setting ) { |
| 196 |
$default = isset( $this->defaults[ $setting ] ) ? $this->defaults[ $setting ] : ''; |
| 197 |
return charitable_get_option( $setting, $default, $this->get_settings() ); |
| 198 |
} |
| 199 |
|
| 200 |
/** |
| 201 |
* Check if a gateway supports a given feature. |
| 202 |
* |
| 203 |
* Gateways should override this to declare support (or lack of support) for a feature. |
| 204 |
* |
| 205 |
* @since 1.3.0 |
| 206 |
* |
| 207 |
* @param string $feature string The name of a feature to test support for. |
| 208 |
* @return bool True if the gateway supports the feature, false otherwise. |
| 209 |
*/ |
| 210 |
public function supports( $feature ) { |
| 211 |
$supported = in_array( $feature, $this->supports ) ? true : false; |
| 212 |
|
| 213 |
/* Provide backwards compatibility for gateways that have not been updated. */ |
| 214 |
if ( ! $supported && 'credit-card' == $feature && isset( $this->credit_card_form ) ) { |
| 215 |
$supported = $this->credit_card_form; |
| 216 |
} |
| 217 |
|
| 218 |
return apply_filters( 'charitable_payment_gateway_supports', $supported, $feature, $this ); |
| 219 |
} |
| 220 |
|
| 221 |
/** |
| 222 |
* Checks whether a particular donation is refundable. |
| 223 |
* |
| 224 |
* @since 1.6.0 |
| 225 |
* |
| 226 |
* @param Charitable_Donation $donation The donation object. |
| 227 |
* @return boolean |
| 228 |
*/ |
| 229 |
public function is_donation_refundable( Charitable_Donation $donation ) { |
| 230 |
return $this->supports( 'refunds' ); |
| 231 |
} |
| 232 |
|
| 233 |
/** |
| 234 |
* Returns an array of credit card fields. |
| 235 |
* |
| 236 |
* If the gateway requires different fields, this can simply be redefined |
| 237 |
* in the child class. |
| 238 |
* |
| 239 |
* @since 1.0.0 |
| 240 |
* |
| 241 |
* @return array[] |
| 242 |
*/ |
| 243 |
public function get_credit_card_fields() { |
| 244 |
/** |
| 245 |
* Filter the credit card fields. |
| 246 |
* |
| 247 |
* @since 1.0.0 |
| 248 |
* |
| 249 |
* @param array $fields The credit card fields. |
| 250 |
*/ |
| 251 |
return apply_filters( |
| 252 |
'charitable_credit_card_fields', |
| 253 |
array( |
| 254 |
'cc_name' => array( |
| 255 |
'label' => __( 'Name on Card', 'charitable' ), |
| 256 |
'type' => 'text', |
| 257 |
'required' => true, |
| 258 |
'priority' => 2, |
| 259 |
'data_type' => 'gateway', |
| 260 |
), |
| 261 |
'cc_number' => array( |
| 262 |
'label' => __( 'Card Number', 'charitable' ), |
| 263 |
'type' => 'text', |
| 264 |
'required' => true, |
| 265 |
'priority' => 4, |
| 266 |
'data_type' => 'gateway', |
| 267 |
), |
| 268 |
'cc_cvc' => array( |
| 269 |
'label' => __( 'CVV Number', 'charitable' ), |
| 270 |
'type' => 'text', |
| 271 |
'required' => true, |
| 272 |
'priority' => 6, |
| 273 |
'data_type' => 'gateway', |
| 274 |
), |
| 275 |
'cc_expiration' => array( |
| 276 |
'label' => __( 'Expiration', 'charitable' ), |
| 277 |
'type' => 'cc-expiration', |
| 278 |
'required' => true, |
| 279 |
'priority' => 8, |
| 280 |
'data_type' => 'gateway', |
| 281 |
), |
| 282 |
), |
| 283 |
$this |
| 284 |
); |
| 285 |
} |
| 286 |
|
| 287 |
/** |
| 288 |
* Redirect the donation to the processing page. |
| 289 |
* |
| 290 |
* @since 1.0.0 |
| 291 |
* |
| 292 |
* @param mixed $result The result to be returned to the AJAX process. |
| 293 |
* @param int $donation_id The donation ID. |
| 294 |
* @return array |
| 295 |
*/ |
| 296 |
public static function redirect_to_processing( $result, $donation_id ) { |
| 297 |
return array( |
| 298 |
'safe' => true, |
| 299 |
'redirect' => charitable_get_permalink( |
| 300 |
'donation_processing_page', |
| 301 |
array( |
| 302 |
'donation_id' => $donation_id, |
| 303 |
) |
| 304 |
), |
| 305 |
); |
| 306 |
} |
| 307 |
|
| 308 |
/** |
| 309 |
* Returns whether a credit card form is required for this gateway. |
| 310 |
* |
| 311 |
* @since 1.0.0 |
| 312 |
* |
| 313 |
* @return boolean |
| 314 |
* |
| 315 |
* @deprecated |
| 316 |
*/ |
| 317 |
public function requires_credit_card_form() { |
| 318 |
charitable_get_deprecated()->deprecated_function( __METHOD__, '1.3.0', 'Charitable_Gateway::supports( \'credit-card\' )' ); |
| 319 |
return $this->supports( 'credit-card' ); |
| 320 |
} |
| 321 |
|
| 322 |
/** |
| 323 |
* Register gateway settings. |
| 324 |
* |
| 325 |
* @since 1.0.0 |
| 326 |
* |
| 327 |
* @param array $settings The existing gateway settings. |
| 328 |
* @return array |
| 329 |
*/ |
| 330 |
abstract public function gateway_settings( $settings ); |
| 331 |
} |
| 332 |
|
| 333 |
endif; |
| 334 |
|