| 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) 2022, 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 |
* @var string The gateway's unique identifier. |
| 29 |
*/ |
| 30 |
const ID = ''; |
| 31 |
|
| 32 |
/** |
| 33 |
* @var string Name of the payment gateway. |
| 34 |
* @since 1.0.0 |
| 35 |
*/ |
| 36 |
protected $name; |
| 37 |
|
| 38 |
|
| 39 |
/** |
| 40 |
* @var string Name of the payment gateway. |
| 41 |
* @since 1.0.0 |
| 42 |
*/ |
| 43 |
protected $badge; |
| 44 |
|
| 45 |
/** |
| 46 |
* @var array The default values for all settings added by the gateway. |
| 47 |
* @since 1.0.0 |
| 48 |
*/ |
| 49 |
protected $defaults; |
| 50 |
|
| 51 |
/** |
| 52 |
* Supported features such as 'credit-card', and 'recurring' donations |
| 53 |
* |
| 54 |
* @var string[] |
| 55 |
* @since 1.3.0 |
| 56 |
*/ |
| 57 |
protected $supports = array(); |
| 58 |
|
| 59 |
/** |
| 60 |
* Return the gateway name. |
| 61 |
* |
| 62 |
* @since 1.0.0 |
| 63 |
* |
| 64 |
* @return string |
| 65 |
*/ |
| 66 |
public function get_name() { |
| 67 |
return $this->name; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Return the gateway name. |
| 72 |
* |
| 73 |
* @since 1.0.0 |
| 74 |
* |
| 75 |
* @return string |
| 76 |
*/ |
| 77 |
public function get_badge() { |
| 78 |
return $this->badge; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Returns the default gateway label to be displayed to donors. |
| 83 |
* |
| 84 |
* @since 1.0.0 |
| 85 |
* |
| 86 |
* @return string |
| 87 |
*/ |
| 88 |
public function get_default_label() { |
| 89 |
return isset( $this->defaults['label'] ) ? $this->defaults['label'] : $this->get_name(); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Provide default gateway settings fields. |
| 94 |
* |
| 95 |
* @since 1.0.0 |
| 96 |
* |
| 97 |
* @return array |
| 98 |
*/ |
| 99 |
public function default_gateway_settings() { |
| 100 |
return array( |
| 101 |
'section_gateway' => array( |
| 102 |
'type' => 'heading', |
| 103 |
'title' => $this->get_name(), |
| 104 |
'badge' => $this->get_badge(), |
| 105 |
'priority' => 2, |
| 106 |
), |
| 107 |
'label' => array( |
| 108 |
'type' => 'text', |
| 109 |
'title' => __( 'Gateway Label', 'charitable' ), |
| 110 |
'help' => __( 'The label that will be shown to donors on the donation form.', 'charitable' ), |
| 111 |
'priority' => 4, |
| 112 |
'default' => $this->get_default_label(), |
| 113 |
), |
| 114 |
); |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Return the settings for this gateway. |
| 119 |
* |
| 120 |
* @since 1.0.0 |
| 121 |
* |
| 122 |
* @return array |
| 123 |
*/ |
| 124 |
public function get_settings() { |
| 125 |
return charitable_get_option( 'gateways_' . $this->get_gateway_id(), array() ); |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Retrieve the gateway label. |
| 130 |
* |
| 131 |
* @since 1.0.0 |
| 132 |
* |
| 133 |
* @return string |
| 134 |
*/ |
| 135 |
public function get_label() { |
| 136 |
return charitable_get_option( 'label', $this->get_default_label(), $this->get_settings() ); |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Return the value for a particular gateway setting. |
| 141 |
* |
| 142 |
* @since 1.0.0 |
| 143 |
* |
| 144 |
* @param string $setting |
| 145 |
* @return mixed |
| 146 |
*/ |
| 147 |
public function get_value( $setting ) { |
| 148 |
$default = isset( $this->defaults[ $setting ] ) ? $this->defaults[ $setting ] : ''; |
| 149 |
return charitable_get_option( $setting, $default, $this->get_settings() ); |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Check if a gateway supports a given feature. |
| 154 |
* |
| 155 |
* Gateways should override this to declare support (or lack of support) for a feature. |
| 156 |
* |
| 157 |
* @since 1.3.0 |
| 158 |
* |
| 159 |
* @param string $feature string The name of a feature to test support for. |
| 160 |
* @return bool True if the gateway supports the feature, false otherwise. |
| 161 |
*/ |
| 162 |
public function supports( $feature ) { |
| 163 |
$supported = in_array( $feature, $this->supports ) ? true : false; |
| 164 |
|
| 165 |
/* Provide backwards compatibility for gateways that have not been updated. */ |
| 166 |
if ( ! $supported && 'credit-card' == $feature && isset( $this->credit_card_form ) ) { |
| 167 |
$supported = $this->credit_card_form; |
| 168 |
} |
| 169 |
|
| 170 |
return apply_filters( 'charitable_payment_gateway_supports', $supported, $feature, $this ); |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* Checks whether a particular donation is refundable. |
| 175 |
* |
| 176 |
* @since 1.6.0 |
| 177 |
* |
| 178 |
* @param Charitable_Donation $donation The donation object. |
| 179 |
* @return boolean |
| 180 |
*/ |
| 181 |
public function is_donation_refundable( Charitable_Donation $donation ) { |
| 182 |
return $this->supports( 'refunds' ); |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* Returns an array of credit card fields. |
| 187 |
* |
| 188 |
* If the gateway requires different fields, this can simply be redefined |
| 189 |
* in the child class. |
| 190 |
* |
| 191 |
* @since 1.0.0 |
| 192 |
* |
| 193 |
* @return array[] |
| 194 |
*/ |
| 195 |
public function get_credit_card_fields() { |
| 196 |
/** |
| 197 |
* Filter the credit card fields. |
| 198 |
* |
| 199 |
* @since 1.0.0 |
| 200 |
* |
| 201 |
* @param array $fields The credit card fields. |
| 202 |
*/ |
| 203 |
return apply_filters( |
| 204 |
'charitable_credit_card_fields', |
| 205 |
array( |
| 206 |
'cc_name' => array( |
| 207 |
'label' => __( 'Name on Card', 'charitable' ), |
| 208 |
'type' => 'text', |
| 209 |
'required' => true, |
| 210 |
'priority' => 2, |
| 211 |
'data_type' => 'gateway', |
| 212 |
), |
| 213 |
'cc_number' => array( |
| 214 |
'label' => __( 'Card Number', 'charitable' ), |
| 215 |
'type' => 'text', |
| 216 |
'required' => true, |
| 217 |
'priority' => 4, |
| 218 |
'data_type' => 'gateway', |
| 219 |
), |
| 220 |
'cc_cvc' => array( |
| 221 |
'label' => __( 'CVV Number', 'charitable' ), |
| 222 |
'type' => 'text', |
| 223 |
'required' => true, |
| 224 |
'priority' => 6, |
| 225 |
'data_type' => 'gateway', |
| 226 |
), |
| 227 |
'cc_expiration' => array( |
| 228 |
'label' => __( 'Expiration', 'charitable' ), |
| 229 |
'type' => 'cc-expiration', |
| 230 |
'required' => true, |
| 231 |
'priority' => 8, |
| 232 |
'data_type' => 'gateway', |
| 233 |
), |
| 234 |
), |
| 235 |
$this |
| 236 |
); |
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
* Redirect the donation to the processing page. |
| 241 |
* |
| 242 |
* @since 1.0.0 |
| 243 |
* |
| 244 |
* @param mixed $result The result to be returned to the AJAX process. |
| 245 |
* @param int $donation_id The donation ID. |
| 246 |
* @return array |
| 247 |
*/ |
| 248 |
public static function redirect_to_processing( $result, $donation_id ) { |
| 249 |
return array( |
| 250 |
'safe' => true, |
| 251 |
'redirect' => charitable_get_permalink( |
| 252 |
'donation_processing_page', |
| 253 |
array( |
| 254 |
'donation_id' => $donation_id, |
| 255 |
) |
| 256 |
), |
| 257 |
); |
| 258 |
} |
| 259 |
|
| 260 |
/** |
| 261 |
* Returns whether a credit card form is required for this gateway. |
| 262 |
* |
| 263 |
* @since 1.0.0 |
| 264 |
* |
| 265 |
* @return boolean |
| 266 |
* |
| 267 |
* @deprecated |
| 268 |
*/ |
| 269 |
public function requires_credit_card_form() { |
| 270 |
charitable_get_deprecated()->deprecated_function( __METHOD__, '1.3.0', 'Charitable_Gateway::supports( \'credit-card\' )' ); |
| 271 |
return $this->supports( 'credit-card' ); |
| 272 |
} |
| 273 |
|
| 274 |
/** |
| 275 |
* Register gateway settings. |
| 276 |
* |
| 277 |
* @since 1.0.0 |
| 278 |
* |
| 279 |
* @param array $settings |
| 280 |
* @return array |
| 281 |
*/ |
| 282 |
abstract public function gateway_settings( $settings ); |
| 283 |
} |
| 284 |
|
| 285 |
endif; |
| 286 |
|