| 1 |
<?php |
| 2 |
|
| 3 |
namespace Bookit\Gateways\Contracts; |
| 4 |
|
| 5 |
use Bookit\Vendor\StellarWP\Arrays\Arr; |
| 6 |
|
| 7 |
/** |
| 8 |
* Abstract Gateway Contract. |
| 9 |
* |
| 10 |
* @since 2.5.0 |
| 11 |
* |
| 12 |
* @package Bookit\Gateways\Contracts |
| 13 |
*/ |
| 14 |
abstract class Abstract_Gateway implements Gateway_Interface { |
| 15 |
|
| 16 |
/** |
| 17 |
* The Gateway key. |
| 18 |
* |
| 19 |
* @since 2.5.0 |
| 20 |
*/ |
| 21 |
protected static $key; |
| 22 |
|
| 23 |
/** |
| 24 |
* Supported currencies. |
| 25 |
* |
| 26 |
* @since 2.5.0 |
| 27 |
* |
| 28 |
* @var string[] |
| 29 |
*/ |
| 30 |
protected static $supported_currencies = []; |
| 31 |
|
| 32 |
/** |
| 33 |
* The option name prefix that configured whether a gateway is enabled. |
| 34 |
* It is followed by the gateway 'key' |
| 35 |
* |
| 36 |
* @since 2.5.0 |
| 37 |
* |
| 38 |
* @var string |
| 39 |
*/ |
| 40 |
public static $option_enabled_prefix = '_bookit_gateway_enabled_'; |
| 41 |
|
| 42 |
/** |
| 43 |
* @inheritDoc |
| 44 |
*/ |
| 45 |
public static function get_key() { |
| 46 |
return static::$key; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* @inheritDoc |
| 51 |
*/ |
| 52 |
public function register_gateway( array $gateways ) { |
| 53 |
$gateways[ static::get_key() ] = $this; |
| 54 |
|
| 55 |
return $gateways; |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* @inheritDoc |
| 60 |
*/ |
| 61 |
public static function should_show() { |
| 62 |
return true; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Generates a Tracking ID for this website. |
| 67 |
* |
| 68 |
* The Tracking ID is a site-specific identifier that links the client and platform accounts in the Payment Gateway |
| 69 |
* without exposing sensitive data. By default, the identifier generated is a URL in the format: |
| 70 |
* |
| 71 |
* {SITE_URL}?v={GATEWAY_VERSION}-{RANDOM_6_CHAR_HASH} |
| 72 |
* |
| 73 |
* @since 2.5.0 |
| 74 |
* |
| 75 |
* @return string |
| 76 |
*/ |
| 77 |
public function generate_unique_tracking_id() { |
| 78 |
$id = wp_generate_password( 6, false, false );; |
| 79 |
$url_frags = wp_parse_url( home_url() ); |
| 80 |
$url = Arr::get( $url_frags, 'host' ) . Arr::get( $url_frags, 'path' ); |
| 81 |
$url = add_query_arg( [ |
| 82 |
'v' => static::VERSION . '-' . $id, |
| 83 |
], $url ); |
| 84 |
|
| 85 |
// Always limit it to 127 chars. |
| 86 |
return substr( $url, 0, 127 ); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Get URL for the display logo. |
| 91 |
* |
| 92 |
* @since 2.5.0 |
| 93 |
* |
| 94 |
* @return string |
| 95 |
*/ |
| 96 |
public function get_logo_url(): string { |
| 97 |
return ''; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Get text to use a subtitle when listing gateways. |
| 102 |
* |
| 103 |
* @since 2.5.0 |
| 104 |
* |
| 105 |
* @return string |
| 106 |
*/ |
| 107 |
public function get_subtitle(): string { |
| 108 |
return ''; |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Returns the enabled option key. |
| 113 |
* |
| 114 |
* @since 2.5.0 |
| 115 |
* |
| 116 |
* @return string |
| 117 |
*/ |
| 118 |
public static function get_enabled_option_key(): string { |
| 119 |
return static::$option_enabled_prefix . self::get_key(); |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Returns if gateway is enabled. |
| 124 |
* |
| 125 |
* @since 2.5.0 |
| 126 |
* |
| 127 |
* @return boolean |
| 128 |
*/ |
| 129 |
public static function is_enabled(): bool { |
| 130 |
if ( ! static::should_show() ) { |
| 131 |
return false; |
| 132 |
} |
| 133 |
|
| 134 |
return bookit_is_truthy( get_option( static::get_enabled_option_key() ) ); |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Disable the gateway toggle. |
| 139 |
* |
| 140 |
* @since 2.5.0 |
| 141 |
* |
| 142 |
* @return bool |
| 143 |
*/ |
| 144 |
public static function disable() { |
| 145 |
if ( ! static::is_enabled() ) { |
| 146 |
return true; |
| 147 |
} |
| 148 |
|
| 149 |
return delete_option( static::get_enabled_option_key() ); |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Get supported currencies. |
| 154 |
* |
| 155 |
* @since 2.5.0 |
| 156 |
* |
| 157 |
* @return string[] |
| 158 |
*/ |
| 159 |
public static function get_supported_currencies() { |
| 160 |
/** |
| 161 |
* Filter to modify supported currencies for this gateway. |
| 162 |
* |
| 163 |
* @since 2.5.0 |
| 164 |
* |
| 165 |
* @param string[] $supported_currencies Array of three-letter, supported currency codes. |
| 166 |
*/ |
| 167 |
return apply_filters( 'bookit_gateway_supported_currencies_' . static::$key, static::$supported_currencies ); |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Is currency supported. |
| 172 |
* |
| 173 |
* @since 2.5.0 |
| 174 |
* |
| 175 |
* @param string $currency_code Currency code. |
| 176 |
* |
| 177 |
* @return bool |
| 178 |
*/ |
| 179 |
public static function is_currency_supported( $currency_code ) { |
| 180 |
if ( empty( $currency_code ) ) { |
| 181 |
return false; |
| 182 |
} |
| 183 |
|
| 184 |
$supported_currencies = static::get_supported_currencies(); |
| 185 |
|
| 186 |
// If supported currencies aren't set, assume it's supported. |
| 187 |
if ( empty( $supported_currencies ) ) { |
| 188 |
return true; |
| 189 |
} |
| 190 |
|
| 191 |
return in_array( $currency_code, $supported_currencies, true ); |
| 192 |
} |
| 193 |
} |
| 194 |
|