BankTransfer
2 years ago
Stripe
1 month ago
AbstractPaymentMethod.php
1 month ago
PaymentMethodInterface.php
3 years ago
PaymentMethods.php
1 year ago
StoreGateway.php
3 years ago
WebhookHandlerInterface.php
3 years ago
index.php
3 years ago
AbstractPaymentMethod.php
493 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ProfilePress\Core\Membership\PaymentMethods; |
| 4 | |
| 5 | use ProfilePress\Core\Membership\Models\Order\OrderEntity; |
| 6 | use ProfilePress\Core\Membership\Models\Subscription\SubscriptionEntity; |
| 7 | use ProfilePress\Core\Membership\Models\Subscription\SubscriptionStatus; |
| 8 | use ProfilePress\Core\Membership\Services\TaxService; |
| 9 | |
| 10 | /** |
| 11 | * @property int $id |
| 12 | * @property string $title |
| 13 | * @property string $description |
| 14 | * @property string $method_title |
| 15 | * @property string $method_description |
| 16 | * @property string $order_button_text |
| 17 | * @property bool $has_fields |
| 18 | * @property string $icon |
| 19 | */ |
| 20 | abstract class AbstractPaymentMethod implements PaymentMethodInterface |
| 21 | { |
| 22 | const DEFAULT_CC_FORM = 'credit_card_form_support'; |
| 23 | |
| 24 | const REFUNDS = 'refunds_support'; |
| 25 | |
| 26 | const SUBSCRIPTIONS = 'subscriptions_support'; |
| 27 | |
| 28 | const SUBSCRIPTION_CANCELLATION = 'subscription_cancellation_support'; |
| 29 | |
| 30 | const TITLE_DB_OPTION_NAME = 'title'; |
| 31 | |
| 32 | const DESCRIPTION_DB_OPTION_NAME = 'description'; |
| 33 | |
| 34 | /** |
| 35 | * @var string Method Unique Identifier. |
| 36 | */ |
| 37 | protected $id; |
| 38 | |
| 39 | /** |
| 40 | * @var bool Useful if method should not show up on checkout page |
| 41 | */ |
| 42 | protected $backend_only = false; |
| 43 | |
| 44 | /** |
| 45 | * Gateway title for the frontend. |
| 46 | * |
| 47 | * @var string |
| 48 | */ |
| 49 | protected $title; |
| 50 | |
| 51 | /** |
| 52 | * Gateway description for the frontend. |
| 53 | * |
| 54 | * @var string |
| 55 | */ |
| 56 | protected $description; |
| 57 | |
| 58 | /** |
| 59 | * Gateway title. |
| 60 | * |
| 61 | * @var string |
| 62 | */ |
| 63 | protected $method_title = ''; |
| 64 | |
| 65 | /** |
| 66 | * Gateway description. |
| 67 | * |
| 68 | * @var string |
| 69 | */ |
| 70 | protected $method_description = ''; |
| 71 | |
| 72 | /** |
| 73 | * True if the gateway shows fields on the checkout. |
| 74 | * |
| 75 | * @var bool |
| 76 | */ |
| 77 | protected $has_fields = false; |
| 78 | |
| 79 | /** |
| 80 | * Icon for the gateway. |
| 81 | * |
| 82 | * @var string |
| 83 | */ |
| 84 | protected $icon; |
| 85 | |
| 86 | protected $supports = []; |
| 87 | |
| 88 | public function __construct() |
| 89 | { |
| 90 | add_action('init', [$this, 'webhook_callback'], 9); |
| 91 | |
| 92 | add_filter('ppress_subscription_can_cancel', [$this, 'can_cancel'], 10, 2); |
| 93 | |
| 94 | add_action('ppress_subscription_completed', [$this, 'cancel_sub_on_completion']); |
| 95 | |
| 96 | add_action('wp_enqueue_scripts', [$this, 'enqueue_frontend_assets']); |
| 97 | |
| 98 | add_filter('ppress_checkout_billing_validation', [$this, 'should_validate_billing_details']); |
| 99 | } |
| 100 | |
| 101 | public function __set($key, $value) |
| 102 | { |
| 103 | $this->$key = $value; |
| 104 | } |
| 105 | |
| 106 | public function __get($key) |
| 107 | { |
| 108 | $value = false; |
| 109 | |
| 110 | if (method_exists($this, "get_{$key}")) { |
| 111 | $value = call_user_func([$this, "get_{$key}"]); |
| 112 | } elseif (isset($this->$key)) { |
| 113 | $value = $this->$key; |
| 114 | } |
| 115 | |
| 116 | return $value; |
| 117 | } |
| 118 | |
| 119 | public function webhook_callback() |
| 120 | { |
| 121 | if ( ! isset($_GET['ppress-listener']) || $this->id !== $_GET['ppress-listener']) { |
| 122 | return; |
| 123 | } |
| 124 | |
| 125 | nocache_headers(); |
| 126 | |
| 127 | $this->process_webhook(); |
| 128 | exit; |
| 129 | } |
| 130 | |
| 131 | public function is_enabled() |
| 132 | { |
| 133 | return $this->get_value('enabled') == 'true'; |
| 134 | } |
| 135 | |
| 136 | public function is_backend_only() |
| 137 | { |
| 138 | return $this->backend_only === true; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Get Gateway Id. |
| 143 | * |
| 144 | * @return string The email id. |
| 145 | */ |
| 146 | public function get_id() |
| 147 | { |
| 148 | return $this->id; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Return the gateway's title. |
| 153 | * |
| 154 | * @return string |
| 155 | */ |
| 156 | public function get_title() |
| 157 | { |
| 158 | if (empty($this->title)) { |
| 159 | $this->title = $this->method_title; |
| 160 | } |
| 161 | |
| 162 | $db_title = $this->get_value(self::TITLE_DB_OPTION_NAME); |
| 163 | |
| 164 | if ( ! empty($db_title)) { |
| 165 | $this->title = $db_title; |
| 166 | } |
| 167 | |
| 168 | return apply_filters('ppress_gateway_title', $this->title, $this->id); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Return the gateway's description. |
| 173 | * |
| 174 | * @return string |
| 175 | */ |
| 176 | public function get_description() |
| 177 | { |
| 178 | $db_description = $this->get_value(self::DESCRIPTION_DB_OPTION_NAME); |
| 179 | |
| 180 | if ( ! empty($db_description)) { |
| 181 | $this->description = $db_description; |
| 182 | } |
| 183 | |
| 184 | return apply_filters('ppress_gateway_description', $this->description, $this->id, $this); |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Return the title for admin screens. |
| 189 | * |
| 190 | * @return string |
| 191 | */ |
| 192 | public function get_method_title() |
| 193 | { |
| 194 | return apply_filters('ppress_gateway_method_title', $this->method_title, $this->id, $this); |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Return the description for admin screens. |
| 199 | * |
| 200 | * @return string |
| 201 | */ |
| 202 | public function get_method_description() |
| 203 | { |
| 204 | return apply_filters('ppress_gateway_method_description', $this->method_description, $this->id, $this); |
| 205 | } |
| 206 | |
| 207 | public function admin_settings() |
| 208 | { |
| 209 | return [ |
| 210 | 'enabled' => [ |
| 211 | 'type' => 'checkbox', |
| 212 | 'label' => esc_html__('Enable / Disable', 'wp-user-avatar'), |
| 213 | /* translators: %s - Payment Gateway Title */ |
| 214 | 'checkbox_label' => __('Check to Enable', 'wp-user-avatar'), |
| 215 | ], |
| 216 | 'title' => [ |
| 217 | 'label' => __('Title', 'wp-user-avatar'), |
| 218 | 'type' => 'text', |
| 219 | 'value' => esc_html($this->get_title()), |
| 220 | ], |
| 221 | 'description' => [ |
| 222 | 'label' => esc_html__('Description', 'wp-user-avatar'), |
| 223 | 'type' => 'text', |
| 224 | 'value' => wp_kses_post($this->get_description()), |
| 225 | ], |
| 226 | ]; |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Check if the gateway has fields on the checkout. |
| 231 | * |
| 232 | * @return bool |
| 233 | */ |
| 234 | public function has_fields() |
| 235 | { |
| 236 | return (bool)$this->has_fields; |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Return the gateway's icon. |
| 241 | * |
| 242 | * @return string |
| 243 | */ |
| 244 | public function get_icon() |
| 245 | { |
| 246 | $icon = $this->icon ? '<img src="' . ppress_force_https_url($this->icon) . '" alt="' . esc_attr($this->get_title()) . '" />' : ''; |
| 247 | |
| 248 | return apply_filters('ppress_gateway_icon', $icon, $this->id, $this); |
| 249 | } |
| 250 | |
| 251 | public function get_admin_page_url() |
| 252 | { |
| 253 | return self::get_payment_method_admin_page_url($this->get_id()); |
| 254 | } |
| 255 | |
| 256 | public static function get_payment_method_admin_page_url($payment_method) |
| 257 | { |
| 258 | return add_query_arg([ |
| 259 | 'view' => 'payments', |
| 260 | 'section' => 'payment-methods', |
| 261 | 'method' => $payment_method |
| 262 | ], PPRESS_SETTINGS_SETTING_PAGE); |
| 263 | } |
| 264 | |
| 265 | public function get_webhook_url() |
| 266 | { |
| 267 | $domain = home_url('/'); |
| 268 | |
| 269 | if (defined('PPRESS_WEBHOOK_DOMAIN') && PPRESS_WEBHOOK_DOMAIN) { |
| 270 | $domain = PPRESS_WEBHOOK_DOMAIN; |
| 271 | } |
| 272 | |
| 273 | return add_query_arg(['ppress-listener' => $this->id], $domain); |
| 274 | } |
| 275 | |
| 276 | /** |
| 277 | * Get setting value. |
| 278 | * |
| 279 | * @param $setting |
| 280 | * @param bool $default |
| 281 | * |
| 282 | * @return mixed |
| 283 | */ |
| 284 | public function get_value($setting, $default = false) |
| 285 | { |
| 286 | $data = get_option(PPRESS_PAYMENT_METHODS_OPTION_NAME, []); |
| 287 | |
| 288 | $setting = str_replace($this->id . '_', '', $setting); |
| 289 | |
| 290 | return ppress_var($data, $this->id . '_' . $setting, $default); |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * If There are no payment fields show the description if set. |
| 295 | * Override this in your gateway if you have some. |
| 296 | */ |
| 297 | public function payment_fields() |
| 298 | { |
| 299 | $description = $this->get_description(); |
| 300 | |
| 301 | if ( ! empty($description)) { |
| 302 | echo wpautop(wptexturize($description)); |
| 303 | } |
| 304 | |
| 305 | if (TaxService::init()->is_tax_enabled()) { |
| 306 | |
| 307 | $this->billing_address_form(); |
| 308 | |
| 309 | $this->credit_card_form(); |
| 310 | |
| 311 | } else { |
| 312 | |
| 313 | $this->credit_card_form(); |
| 314 | |
| 315 | $this->billing_address_form(); |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | /** |
| 320 | * Useful for enqueuing frontend assets. |
| 321 | * |
| 322 | * @return void |
| 323 | */ |
| 324 | public function enqueue_frontend_assets() |
| 325 | { |
| 326 | |
| 327 | } |
| 328 | |
| 329 | public function should_validate_billing_details($val) |
| 330 | { |
| 331 | return $val; |
| 332 | } |
| 333 | |
| 334 | abstract function process_webhook(); |
| 335 | |
| 336 | /** |
| 337 | * Validate frontend fields. |
| 338 | * |
| 339 | * Validate payment fields on the frontend. |
| 340 | * |
| 341 | * @return bool|\WP_Error |
| 342 | */ |
| 343 | abstract function validate_fields(); |
| 344 | |
| 345 | /** |
| 346 | * Process Payment. |
| 347 | * |
| 348 | * Process the payment. Override this in your gateway. When implemented, this should. |
| 349 | * return the success and redirect in an array. e.g: |
| 350 | * |
| 351 | * return array( |
| 352 | * 'result' => 'success', |
| 353 | * 'redirect' => $this->get_return_url( $order ) |
| 354 | * ); |
| 355 | * |
| 356 | * @param int $order_id Order ID. |
| 357 | * |
| 358 | * @return mixed|void |
| 359 | */ |
| 360 | abstract function process_payment($order_id, $subscription_id, $customer_id); |
| 361 | |
| 362 | /** |
| 363 | * Process refund. |
| 364 | * |
| 365 | * If the payment gateway declares 'refunds' support, this will allow it to refund a passed in amount. |
| 366 | * |
| 367 | * @param int $order_id Order ID. |
| 368 | * @param string $amount Refund amount. |
| 369 | * @param string $reason Refund reason. |
| 370 | * |
| 371 | * @return boolean |
| 372 | */ |
| 373 | public function process_refund($order_id, $amount = null, $reason = '') |
| 374 | { |
| 375 | |
| 376 | } |
| 377 | |
| 378 | /** |
| 379 | * Get a link to the transaction on the 3rd party gateway site (if applicable). |
| 380 | * |
| 381 | * @param string $transaction_id |
| 382 | * @param OrderEntity $order |
| 383 | * |
| 384 | * @return string transaction URL, or empty string. |
| 385 | */ |
| 386 | public function link_transaction_id($transaction_id, $order) |
| 387 | { |
| 388 | return $transaction_id; |
| 389 | } |
| 390 | |
| 391 | /** |
| 392 | * Get subscription profile Link. |
| 393 | * |
| 394 | * @param string $profile_id The profile id. |
| 395 | * @param SubscriptionEntity $subscription |
| 396 | * |
| 397 | * @return string $profile_link The profile link link. |
| 398 | */ |
| 399 | public function link_profile_id($profile_id, $subscription) |
| 400 | { |
| 401 | return $profile_id; |
| 402 | } |
| 403 | |
| 404 | public function supports($feature) |
| 405 | { |
| 406 | return apply_filters('ppress_payment_gateway_supports', in_array($feature, $this->supports), $feature, $this); |
| 407 | } |
| 408 | |
| 409 | public function credit_card_form() |
| 410 | { |
| 411 | if ($this->supports(self::DEFAULT_CC_FORM)) { |
| 412 | ppress_render_view('checkout/credit-card-fields', [ |
| 413 | 'id' => $this->id |
| 414 | ]); |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | protected function billing_address_form() |
| 419 | { |
| 420 | ppress_render_view('checkout/form-billing-fields', ['payment_method' => $this->id]); |
| 421 | } |
| 422 | |
| 423 | /** |
| 424 | * Determines if a subscription can be cancelled through the gateway |
| 425 | */ |
| 426 | public function can_cancel($ret, $subscription) |
| 427 | { |
| 428 | return $ret; |
| 429 | } |
| 430 | |
| 431 | /** |
| 432 | * Returns an array of subscription statuses that can be cancelled |
| 433 | * |
| 434 | * @return array |
| 435 | */ |
| 436 | public function get_cancellable_statuses() |
| 437 | { |
| 438 | return apply_filters('ppress_subscription_cancellable_statuses', [SubscriptionStatus::ACTIVE, SubscriptionStatus::TRIALLING]); |
| 439 | } |
| 440 | |
| 441 | /** |
| 442 | * @param SubscriptionEntity $subscription |
| 443 | * |
| 444 | * @return bool|void |
| 445 | */ |
| 446 | public function cancel_sub_on_completion($subscription) |
| 447 | { |
| 448 | if ($subscription->get_payment_method() !== $this->id) { |
| 449 | return; |
| 450 | } |
| 451 | |
| 452 | return $this->cancel($subscription); |
| 453 | } |
| 454 | |
| 455 | /** |
| 456 | * Cancels a subscription. |
| 457 | * |
| 458 | * @param SubscriptionEntity $subscription |
| 459 | * |
| 460 | * @return bool |
| 461 | */ |
| 462 | public function cancel($subscription) |
| 463 | { |
| 464 | |
| 465 | } |
| 466 | |
| 467 | /** |
| 468 | * Get the return url (thank you page). |
| 469 | * |
| 470 | * @param $order_key |
| 471 | * |
| 472 | * @return string |
| 473 | */ |
| 474 | public function get_success_url($order_key = '') |
| 475 | { |
| 476 | return ppress_get_success_url($order_key); |
| 477 | } |
| 478 | |
| 479 | /** |
| 480 | * @param $order_key |
| 481 | * |
| 482 | * @return string |
| 483 | */ |
| 484 | public function get_cancel_url($order_key = '') |
| 485 | { |
| 486 | return ppress_get_cancel_url($order_key); |
| 487 | } |
| 488 | |
| 489 | public static function get_instance() |
| 490 | { |
| 491 | return new static(); |
| 492 | } |
| 493 | } |