PaymentGateways
1 year ago
AdminMenu.php
1 year ago
BillingController.php
1 year ago
CartController.php
1 year ago
CheckoutController.php
1 year ago
CouponController.php
1 year ago
Ecommerce.php
1 year ago
EmailController.php
1 year ago
HooksHandler.php
1 year ago
OptionKeys.php
1 year ago
OrderActivitiesController.php
1 year ago
OrderController.php
1 year ago
PaymentHandler.php
1 year ago
Settings.php
1 year ago
Tax.php
1 year ago
currency.php
1 year ago
Ecommerce.php
225 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Main class to handle tutor native e-commerce. |
| 4 | * |
| 5 | * @package Tutor\Ecommerce |
| 6 | * @author Themeum <support@themeum.com> |
| 7 | * @link https://themeum.com |
| 8 | * @since 3.0.0 |
| 9 | */ |
| 10 | |
| 11 | namespace Tutor\Ecommerce; |
| 12 | |
| 13 | use TUTOR\Course; |
| 14 | use Tutor\Ecommerce\PaymentHandler; |
| 15 | use TUTOR\Input; |
| 16 | use Tutor\PaymentGateways\Configs\PaypalConfig; |
| 17 | use Tutor\PaymentGateways\GatewayFactory; |
| 18 | use Tutor\PaymentGateways\PaypalGateway; |
| 19 | |
| 20 | if ( ! defined( 'ABSPATH' ) ) { |
| 21 | exit; |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * Class Ecommerce |
| 26 | * |
| 27 | * @since 3.0.0 |
| 28 | */ |
| 29 | class Ecommerce { |
| 30 | |
| 31 | /** |
| 32 | * Native ecommerce engin |
| 33 | * |
| 34 | * @since 3.0.0 |
| 35 | * |
| 36 | * @var string |
| 37 | */ |
| 38 | const MONETIZE_BY = 'tutor'; |
| 39 | |
| 40 | /** |
| 41 | * Construct function to initialize e-commerce classes |
| 42 | * |
| 43 | * @since 3.0.0 |
| 44 | * |
| 45 | * @param bool $register_hooks register hooks. |
| 46 | */ |
| 47 | public function __construct( $register_hooks = true ) { |
| 48 | |
| 49 | if ( ! $register_hooks ) { |
| 50 | return; |
| 51 | } |
| 52 | |
| 53 | add_filter( 'tutor_monetization_options', array( $this, 'add_monetization_option' ) ); |
| 54 | |
| 55 | new Settings(); |
| 56 | new Tax(); |
| 57 | // Include currency file. |
| 58 | require_once tutor()->path . 'ecommerce/currency.php'; |
| 59 | |
| 60 | if ( ! tutor_utils()->is_monetize_by_tutor() ) { |
| 61 | return; |
| 62 | } |
| 63 | |
| 64 | add_action( 'save_post_' . tutor()->course_post_type, array( $this, 'save_price' ), 10, 2 ); |
| 65 | |
| 66 | new AdminMenu(); |
| 67 | new CartController(); |
| 68 | new CheckoutController(); |
| 69 | new OrderController(); |
| 70 | new OrderActivitiesController(); |
| 71 | new CouponController(); |
| 72 | new BillingController(); |
| 73 | new HooksHandler(); |
| 74 | new EmailController(); |
| 75 | new PaymentHandler(); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Save course price and course sale price. |
| 80 | * |
| 81 | * @since 3.0.0 |
| 82 | * |
| 83 | * @param int $post_ID course ID. |
| 84 | * @param mixed $post course details. |
| 85 | * |
| 86 | * @return void |
| 87 | */ |
| 88 | public function save_price( $post_ID, $post ) { |
| 89 | if ( ! tutor_utils()->is_monetize_by_tutor() ) { |
| 90 | return; |
| 91 | } |
| 92 | |
| 93 | $course_price = Input::post( 'course_price', 0, Input::TYPE_NUMERIC ); |
| 94 | $sale_price = Input::post( 'course_sale_price', 0, Input::TYPE_NUMERIC ); |
| 95 | |
| 96 | if ( $course_price ) { |
| 97 | update_post_meta( $post_ID, Course::COURSE_PRICE_META, $course_price ); |
| 98 | } |
| 99 | |
| 100 | if ( Input::has( 'course_sale_price' ) ) { |
| 101 | update_post_meta( $post_ID, Course::COURSE_SALE_PRICE_META, $sale_price ); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Add monetization option. |
| 107 | * |
| 108 | * @since 3.0.0 |
| 109 | * |
| 110 | * @param array $arr options. |
| 111 | * |
| 112 | * @return array |
| 113 | */ |
| 114 | public function add_monetization_option( $arr ) { |
| 115 | $arr[ self::MONETIZE_BY ] = __( 'Native', 'tutor' ); |
| 116 | return array_reverse( $arr ); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Create & return payment gateway object |
| 121 | * |
| 122 | * Return instance expose setup_payment_and_redirect & get_webhook_data |
| 123 | * methods for creating payments. |
| 124 | * |
| 125 | * @since 3.0.0 |
| 126 | * |
| 127 | * @param string $gateway Reference class of gateway. |
| 128 | * |
| 129 | * @throws \Exception Throw exception if error occur. |
| 130 | * |
| 131 | * @return GatewayBase |
| 132 | */ |
| 133 | public static function get_payment_gateway_object( string $gateway ) { |
| 134 | try { |
| 135 | return GatewayFactory::create( $gateway ); |
| 136 | } catch ( \Throwable $th ) { |
| 137 | throw new \Exception( $th->getMessage() ); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Get payment gateways with reference class |
| 143 | * |
| 144 | * @since 3.0.0 |
| 145 | * |
| 146 | * @param string $gateway gateway name. |
| 147 | * |
| 148 | * @return array|null |
| 149 | */ |
| 150 | public static function payment_gateways_with_ref( $gateway = null ) { |
| 151 | $arr = array( |
| 152 | 'paypal' => array( |
| 153 | 'gateway_class' => PaypalGateway::class, |
| 154 | 'config_class' => PaypalConfig::class, |
| 155 | ), |
| 156 | ); |
| 157 | |
| 158 | $arr = apply_filters( 'tutor_payment_gateways_with_class', $arr ); |
| 159 | |
| 160 | return is_null( $gateway ) ? $arr : $arr[ $gateway ] ?? null; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Check if a payment gateway configured |
| 165 | * |
| 166 | * @since 1.0.0 |
| 167 | * |
| 168 | * @param string $gateway_slug Payment gateway name. |
| 169 | * |
| 170 | * @return bool |
| 171 | */ |
| 172 | public static function is_payment_gateway_configured( $gateway_slug ) { |
| 173 | $payment_settings = Settings::get_payment_gateway_settings( $gateway_slug ); |
| 174 | if ( isset( $payment_settings['is_manual'] ) && $payment_settings['is_manual'] ) { |
| 175 | return true; |
| 176 | } |
| 177 | |
| 178 | $gateway_ref = self::payment_gateways_with_ref( $gateway_slug ); |
| 179 | if ( $gateway_ref ) { |
| 180 | try { |
| 181 | $gateway_obj = self::get_payment_gateway_object( $gateway_ref['gateway_class'] ); |
| 182 | $config_class = $gateway_obj->get_config_class(); |
| 183 | return ( new $config_class() )->is_configured(); |
| 184 | } catch ( \Throwable $th ) { |
| 185 | return false; |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | return false; |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Get error message for unfinished payment method |
| 194 | * |
| 195 | * @since 3.0.0 |
| 196 | * |
| 197 | * @param string $payment_method payment method name. |
| 198 | * |
| 199 | * @return string error message for incomplete payment method setup. |
| 200 | */ |
| 201 | public static function get_incomplete_payment_setup_error_message( $payment_method ) { |
| 202 | /* translators: %s: payment gateway */ |
| 203 | return sprintf( __( '%s payment method is not configured properly. Please contact with site administrator!', 'tutor' ), ucfirst( $payment_method ) ); |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Get payment method label. |
| 208 | * |
| 209 | * @since 3.0.0 |
| 210 | * |
| 211 | * @param string $payment_method payment method name. |
| 212 | * |
| 213 | * @return string |
| 214 | */ |
| 215 | public static function get_payment_method_label( $payment_method ) { |
| 216 | $payment_method_labels = array( |
| 217 | 'paypal' => __( 'PayPal', 'tutor' ), |
| 218 | ); |
| 219 | |
| 220 | $payment_method_labels = apply_filters( 'tutor_payment_method_labels', $payment_method_labels ); |
| 221 | |
| 222 | return $payment_method_labels[ $payment_method ] ?? ucfirst( $payment_method ); |
| 223 | } |
| 224 | } |
| 225 |