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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
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