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.1 3.6.2 3.6.3 3.6.4 3.7.0 3.7.1 3.7.2 3.7.3 3.7.4 3.8.0 3.8.1 3.8.2 3.8.3 3.9.0 3.9.1 3.9.10 3.9.2 3.9.3 3.9.4 3.9.5 3.9.6 3.9.7 3.9.8 3.9.9
tutor / ecommerce / Ecommerce.php
tutor / ecommerce Last commit date
Cart 10 months ago PaymentGateways 8 months ago AdminMenu.php 9 months ago BillingController.php 1 year ago CartController.php 1 year ago CheckoutController.php 7 months ago CouponController.php 11 months ago Ecommerce.php 1 year ago EmailController.php 11 months ago HooksHandler.php 7 months ago OptionKeys.php 1 year ago OrderActivitiesController.php 1 year ago OrderController.php 7 months ago PaymentHandler.php 9 months ago Settings.php 9 months ago Tax.php 9 months ago currency.php 1 year 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