| 1 |
<?php |
| 2 |
/** |
| 3 |
* Base Charitable_Square_Gateway_Processor_Interface instance. |
| 4 |
* |
| 5 |
* @package Charitable Square/Classes/Charitable_Square_Gateway_Processor |
| 6 |
* @author David Bisset |
| 7 |
* @copyright Copyright (c) 2021-2022, WP Charitable LLC |
| 8 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 9 |
* @since 1.8.7 |
| 10 |
*/ |
| 11 |
|
| 12 |
// Exit if accessed directly. |
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
if ( ! class_exists( 'Charitable_Square_Gateway_Processor' ) ) : |
| 18 |
|
| 19 |
/** |
| 20 |
* Charitable_Square_Gateway_Processor |
| 21 |
* |
| 22 |
* @since 1.8.0 |
| 23 |
*/ |
| 24 |
class Charitable_Square_Gateway_Processor { |
| 25 |
|
| 26 |
/** |
| 27 |
* The donation object. |
| 28 |
* |
| 29 |
* @since 1.8.0 |
| 30 |
* |
| 31 |
* @var Charitable_Donation |
| 32 |
*/ |
| 33 |
protected $donation; |
| 34 |
|
| 35 |
/** |
| 36 |
* Donation log instance for this donation. |
| 37 |
* |
| 38 |
* @since 1.8.0 |
| 39 |
* |
| 40 |
* @var Charitable_Donation_Log |
| 41 |
*/ |
| 42 |
protected $donation_log; |
| 43 |
|
| 44 |
/** |
| 45 |
* The donor object. |
| 46 |
* |
| 47 |
* @since 1.8.0 |
| 48 |
* |
| 49 |
* @var Charitable_Donor |
| 50 |
*/ |
| 51 |
protected $donor; |
| 52 |
|
| 53 |
/** |
| 54 |
* The donation processor object. |
| 55 |
* |
| 56 |
* @since 1.8.0 |
| 57 |
* |
| 58 |
* @var Charitable_Donation_Processor |
| 59 |
*/ |
| 60 |
protected $processor; |
| 61 |
|
| 62 |
/** |
| 63 |
* The Square gateway model. |
| 64 |
* |
| 65 |
* @since 1.8.0 |
| 66 |
* |
| 67 |
* @var Charitable_Gateway_Square_AM |
| 68 |
*/ |
| 69 |
protected $gateway; |
| 70 |
|
| 71 |
/** |
| 72 |
* Submitted donation values. |
| 73 |
* |
| 74 |
* @since 1.8.0 |
| 75 |
* |
| 76 |
* @var array |
| 77 |
*/ |
| 78 |
protected $donation_data; |
| 79 |
|
| 80 |
/** |
| 81 |
* Options passed to Square with certain API requests. |
| 82 |
* |
| 83 |
* @since 1.8.0 |
| 84 |
* |
| 85 |
* @var array |
| 86 |
*/ |
| 87 |
protected $options = array(); |
| 88 |
|
| 89 |
/** |
| 90 |
* Connect mode. |
| 91 |
* |
| 92 |
* This will remain unset if Square Connect is not active. If |
| 93 |
* it is active, this will either be `direct` or `charge_owner`. |
| 94 |
* |
| 95 |
* @since 1.8.0 |
| 96 |
* |
| 97 |
* @var string |
| 98 |
*/ |
| 99 |
protected $connect_mode; |
| 100 |
|
| 101 |
/** |
| 102 |
* Application fee. |
| 103 |
* |
| 104 |
* This will remain unset if Square Connect is not active. |
| 105 |
* |
| 106 |
* @since 1.8.0 |
| 107 |
* |
| 108 |
* @var string |
| 109 |
*/ |
| 110 |
protected $application_fee; |
| 111 |
|
| 112 |
/** |
| 113 |
* Destination account. |
| 114 |
* |
| 115 |
* This will remain unset if Square Connect is not active or |
| 116 |
* if Connect mode is set to `direct`. |
| 117 |
* |
| 118 |
* @since 1.8.0 |
| 119 |
* |
| 120 |
* @var string |
| 121 |
*/ |
| 122 |
protected $destination; |
| 123 |
|
| 124 |
/** |
| 125 |
* Customer object, always on the platform. |
| 126 |
* |
| 127 |
* @since 1.8.0 |
| 128 |
* |
| 129 |
* @var Charitable_Square_Customer |
| 130 |
*/ |
| 131 |
protected $customer; |
| 132 |
|
| 133 |
/** |
| 134 |
* Connected account customer object. |
| 135 |
* |
| 136 |
* @since 1.8.0 |
| 137 |
* |
| 138 |
* @var Charitable_Square_Connected_Customer |
| 139 |
*/ |
| 140 |
protected $connected_customer; |
| 141 |
|
| 142 |
/** |
| 143 |
* Set up class instance. |
| 144 |
* |
| 145 |
* @since 1.8.0 |
| 146 |
*/ |
| 147 |
public function __construct() {} |
| 148 |
|
| 149 |
/** |
| 150 |
* Get the donation ID for a given gateway transaction ID. |
| 151 |
* |
| 152 |
* @since 1.8.7 |
| 153 |
* |
| 154 |
* @param string $transaction_id The gateway transaction ID. |
| 155 |
* @return int|false The donation ID if found, false otherwise. |
| 156 |
*/ |
| 157 |
public static function get_donation_with_gateway_transaction_id( $transaction_id ) { |
| 158 |
|
| 159 |
// Make sure the transaction ID is a string and trimmed. |
| 160 |
$transaction_id = trim( $transaction_id ); |
| 161 |
|
| 162 |
// Direct SQL query since we know the data exists. |
| 163 |
global $wpdb; |
| 164 |
$donation_id = $wpdb->get_var( // phpcs:ignore |
| 165 |
$wpdb->prepare( |
| 166 |
"SELECT p.ID |
| 167 |
FROM {$wpdb->posts} p |
| 168 |
INNER JOIN {$wpdb->postmeta} pm ON p.ID = pm.post_id |
| 169 |
WHERE p.post_type = %s |
| 170 |
AND pm.meta_key = %s |
| 171 |
AND pm.meta_value = %s |
| 172 |
LIMIT 1", |
| 173 |
'donation', |
| 174 |
'_gateway_transaction_id', |
| 175 |
$transaction_id |
| 176 |
) |
| 177 |
); |
| 178 |
|
| 179 |
if ( charitable_is_debug( 'square' ) ) { |
| 180 |
// phpcs:disable |
| 181 |
error_log( 'Direct SQL Query Result: ' . ( $donation_id ? $donation_id : 'false' ) ); |
| 182 |
// phpcs:enable |
| 183 |
} |
| 184 |
|
| 185 |
return $donation_id ? (int) $donation_id : false; |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* Get the donation ID for a given gateway customer ID. |
| 190 |
* |
| 191 |
* @since 1.8.7 |
| 192 |
* |
| 193 |
* @param string $customer_id The gateway customer ID. |
| 194 |
* @return int|false The donation ID if found, false otherwise. |
| 195 |
*/ |
| 196 |
public static function get_donation_with_gateway_customer_id( $customer_id ) { |
| 197 |
global $wpdb; |
| 198 |
|
| 199 |
if ( charitable_is_debug( 'square' ) ) { |
| 200 |
// phpcs:disable |
| 201 |
error_log( 'get_donation_with_gateway_customer_id - starting' ); |
| 202 |
error_log( 'get_donation_with_gateway_customer_id - customer_id: ' . $customer_id ); |
| 203 |
// phpcs:enable |
| 204 |
} |
| 205 |
|
| 206 |
// the customer id is stored in donormeta table, either square_customer_id_test or square_customer_id_live depending on the test mode. |
| 207 |
$key = charitable_get_option( 'test_mode' ) ? 'square_customer_id_test' : 'square_customer_id_live'; |
| 208 |
|
| 209 |
if ( charitable_is_debug( 'square' ) ) { |
| 210 |
// phpcs:disable |
| 211 |
error_log( 'get_donation_with_gateway_customer_id - key: ' . $key ); |
| 212 |
// phpcs:enable |
| 213 |
} |
| 214 |
|
| 215 |
// get the donor id from the donormeta table that matches the customer id. |
| 216 |
$sql = $wpdb->prepare( |
| 217 |
"SELECT d.donor_id |
| 218 |
FROM {$wpdb->prefix}charitable_donormeta dm |
| 219 |
INNER JOIN {$wpdb->prefix}charitable_donors d ON dm.donor_id = d.donor_id |
| 220 |
WHERE dm.meta_key = %s |
| 221 |
AND dm.meta_value = %s |
| 222 |
LIMIT 1", |
| 223 |
$key, |
| 224 |
$customer_id |
| 225 |
); |
| 226 |
if ( charitable_is_debug( 'square' ) ) { |
| 227 |
// phpcs:disable |
| 228 |
error_log( 'get_donation_with_gateway_customer_id - donor sql: ' . $sql ); |
| 229 |
// phpcs:enable |
| 230 |
} |
| 231 |
$donor_id = $wpdb->get_var( $sql ); // phpcs:ignore |
| 232 |
|
| 233 |
if ( charitable_is_debug( 'square' ) ) { |
| 234 |
// phpcs:disable |
| 235 |
error_log( 'get_donation_with_gateway_customer_id - donor_id: ' . $donor_id ); |
| 236 |
// phpcs:enable |
| 237 |
} |
| 238 |
|
| 239 |
// now via the campaign_donations table find the latest donation for this donor id. |
| 240 |
$sql = $wpdb->prepare( |
| 241 |
"SELECT cd.donation_id |
| 242 |
FROM {$wpdb->prefix}charitable_campaign_donations cd |
| 243 |
WHERE cd.donor_id = %s |
| 244 |
ORDER BY cd.donation_id DESC |
| 245 |
LIMIT 1", |
| 246 |
$donor_id |
| 247 |
); |
| 248 |
if ( charitable_is_debug( 'square' ) ) { |
| 249 |
// phpcs:disable |
| 250 |
error_log( 'get_donation_with_gateway_customer_id - donation sql: ' . $sql ); |
| 251 |
// phpcs:enable |
| 252 |
} |
| 253 |
$donation_id = $wpdb->get_var( $sql ); // phpcs:ignore |
| 254 |
|
| 255 |
if ( charitable_is_debug( 'square' ) ) { |
| 256 |
// phpcs:disable |
| 257 |
error_log( 'get_donation_with_gateway_customer_id - donation_id: ' . $donation_id ); |
| 258 |
// phpcs:enable |
| 259 |
} |
| 260 |
|
| 261 |
return $donation_id ? (int) $donation_id : false; |
| 262 |
} |
| 263 |
|
| 264 |
/** |
| 265 |
* Returns whether the currency is a zero decimal currency. |
| 266 |
* |
| 267 |
* @since 1.8.0 |
| 268 |
* |
| 269 |
* @param string $currency The currency for the charge. If left blank, will check for the site currency. |
| 270 |
* @return boolean |
| 271 |
*/ |
| 272 |
public static function is_zero_decimal_currency( $currency = null ) { |
| 273 |
if ( is_null( $currency ) ) { |
| 274 |
$currency = charitable_get_currency(); |
| 275 |
} |
| 276 |
|
| 277 |
return in_array( strtoupper( $currency ), self::get_zero_decimal_currencies() ); // phpcs:ignore |
| 278 |
} |
| 279 |
|
| 280 |
/** |
| 281 |
* Return all zero-decimal currencies supported by Square. |
| 282 |
* |
| 283 |
* @since 1.8.0 |
| 284 |
* |
| 285 |
* @return array |
| 286 |
*/ |
| 287 |
public static function get_zero_decimal_currencies() { |
| 288 |
return array( |
| 289 |
'BIF', |
| 290 |
'CLP', |
| 291 |
'DJF', |
| 292 |
'GNF', |
| 293 |
'JPY', |
| 294 |
'KMF', |
| 295 |
'KRW', |
| 296 |
'MGA', |
| 297 |
'PYG', |
| 298 |
'RWF', |
| 299 |
'VND', |
| 300 |
'VUV', |
| 301 |
'XAF', |
| 302 |
'XOF', |
| 303 |
'XPF', |
| 304 |
); |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
endif; |
| 309 |
|