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