PluginProbe ʕ •ᴥ•ʔ
OttoKit: All-in-One Automation Platform / 1.1.32
OttoKit: All-in-One Automation Platform v1.1.32
1.1.32 1.1.31 1.1.30 1.1.29 1.1.28 1.1.27 1.1.9 trunk 1.0.10 1.0.11 1.0.12 1.0.13 1.0.14 1.0.15 1.0.16 1.0.17 1.0.18 1.0.19 1.0.20 1.0.21 1.0.22 1.0.23 1.0.24 1.0.25 1.0.26 1.0.27 1.0.28 1.0.29 1.0.30 1.0.31 1.0.32 1.0.33 1.0.34 1.0.35 1.0.36 1.0.37 1.0.38 1.0.39 1.0.40 1.0.41 1.0.42 1.0.43 1.0.44 1.0.45 1.0.46 1.0.47 1.0.48 1.0.49 1.0.50 1.0.51 1.0.52 1.0.53 1.0.54 1.0.55 1.0.56 1.0.57 1.0.58 1.0.59 1.0.60 1.0.61 1.0.62 1.0.63 1.0.64 1.0.65 1.0.66 1.0.67 1.0.68 1.0.69 1.0.7 1.0.70 1.0.71 1.0.72 1.0.73 1.0.74 1.0.75 1.0.76 1.0.77 1.0.78 1.0.79 1.0.8 1.0.80 1.0.81 1.0.82 1.0.83 1.0.84 1.0.85 1.0.86 1.0.87 1.0.88 1.0.89 1.0.9 1.0.90 1.1.0 1.1.1 1.1.10 1.1.11 1.1.12 1.1.13 1.1.14 1.1.15 1.1.16 1.1.17 1.1.18 1.1.19 1.1.2 1.1.20 1.1.21 1.1.22 1.1.23 1.1.24 1.1.25 1.1.26 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8
suretriggers / src / Integrations / edd / actions / create-purchase.php
suretriggers / src / Integrations / edd / actions Last commit date
create-discount.php 10 months ago create-purchase.php 8 months ago delete-discount.php 10 months ago edd-create-discount.php 10 months ago fetch-discount-details.php 10 months ago find-user-have-active-inactive-license-for-specific-download.php 2 years ago find-user-have-subscription-for-specific-download.php 2 years ago find-user-purchased-download.php 10 months ago revoke-purchase.php 8 months ago update-license-status.php 8 months ago update-purchase-meta.php 8 months ago update-purchase.php 8 months ago
create-purchase.php
496 lines
1 <?php
2 /**
3 * CreatePurchase.
4 * php version 5.6
5 *
6 * @category CreatePurchase
7 * @package SureTriggers
8 * @author BSF <username@example.com>
9 * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3
10 * @link https://www.brainstormforce.com/
11 * @since 1.0.0
12 */
13
14 namespace SureTriggers\Integrations\EDD\Actions;
15
16 use SureTriggers\Integrations\AutomateAction;
17 use SureTriggers\Traits\SingletonLoader;
18 use Exception;
19
20 /**
21 * CreatePurchase
22 *
23 * @category CreatePurchase
24 * @package SureTriggers
25 * @author BSF <username@example.com>
26 * @license https://www.gnu.org/licenses/gpl-3.0.html GPLv3
27 * @link https://www.brainstormforce.com/
28 * @since 1.0.0
29 */
30 class CreatePurchase extends AutomateAction {
31
32 /**
33 * Integration type.
34 *
35 * @var string
36 */
37 public $integration = 'EDD';
38
39 /**
40 * Action name.
41 *
42 * @var string
43 */
44 public $action = 'edd_create_purchase';
45
46 use SingletonLoader;
47
48 /**
49 * Register action.
50 *
51 * @param array $actions action data.
52 * @return array
53 */
54 public function register( $actions ) {
55 $actions[ $this->integration ][ $this->action ] = [
56 'label' => __( 'Create Purchase/Order', 'suretriggers' ),
57 'action' => $this->action,
58 'function' => [ $this, 'action_listener' ],
59 ];
60
61 return $actions;
62 }
63
64 /**
65 * Action listener.
66 *
67 * @param int $user_id user_id.
68 * @param int $automation_id automation_id.
69 * @param array $fields fields.
70 * @param array $selected_options selected_options.
71 * @return array|bool
72 * @throws Exception Exception.
73 */
74 public function _action_listener( $user_id, $automation_id, $fields, $selected_options ) {
75 if ( ! function_exists( 'edd_insert_payment' ) || ! class_exists( 'EDD_Payment' ) ) {
76 return [
77 'status' => 'error',
78 'message' => 'EDD plugin is not active.',
79 ];
80 }
81
82 if ( empty( $selected_options['customer_email'] ) || empty( $selected_options['download_id'] ) ) {
83 return [
84 'status' => 'error',
85 'message' => 'Missing required parameters (customer_email, download_id)',
86 ];
87 }
88
89 $customer_email = sanitize_email( $selected_options['customer_email'] );
90 if ( ! is_email( $customer_email ) ) {
91 return [
92 'status' => 'error',
93 'message' => 'Invalid email address provided',
94 ];
95 }
96
97 $download_id = intval( $selected_options['download_id'] );
98 $quantity = 1;
99 $price_id = isset( $selected_options['price_id'] ) ? intval( $selected_options['price_id'] ) : null;
100
101 if ( $download_id <= 0 ) {
102 return [
103 'status' => 'error',
104 'message' => 'Invalid download ID provided',
105 ];
106 }
107
108 $download_post = get_post( $download_id );
109 if ( ! $download_post || 'download' !== $download_post->post_type ) {
110 return [
111 'status' => 'error',
112 'message' => 'Download not found with ID: ' . $download_id,
113 ];
114 }
115
116 if ( ! function_exists( 'edd_get_download_price' ) ) {
117 return [
118 'status' => 'error',
119 'message' => 'EDD function edd_get_download_price not found. Please check EDD plugin installation.',
120 ];
121 }
122
123
124 if ( null !== $price_id ) {
125 // Get variable prices and find correct price by ID.
126 if ( function_exists( 'edd_get_variable_prices' ) && function_exists( 'edd_has_variable_prices' ) && edd_has_variable_prices( $download_id ) ) {
127 $variable_prices = edd_get_variable_prices( $download_id );
128 if ( isset( $variable_prices[ $price_id ] ) ) {
129 $item_price = floatval( $variable_prices[ $price_id ]['amount'] );
130 } else {
131 $item_price = edd_get_download_price( $download_id );
132 }
133 } else {
134 $item_price = edd_get_download_price( $download_id, $price_id );
135 }
136 } else {
137 $item_price = edd_get_download_price( $download_id );
138 }
139 $subtotal = $item_price * $quantity;
140
141 $discount_code = isset( $selected_options['discount_code'] ) ? sanitize_text_field( $selected_options['discount_code'] ) : '';
142 $discount_amount = 0;
143 $total = $subtotal;
144
145 if ( ! empty( $discount_code ) && function_exists( 'edd_get_discount_by_code' ) ) {
146 $discount = edd_get_discount_by_code( $discount_code );
147 if ( $discount && 'active' === $discount->status ) {
148 $product_reqs = $discount->get_product_reqs();
149 $excluded_products = $discount->get_excluded_products();
150
151 $discount_valid = true;
152
153 if ( ! empty( $product_reqs ) && ! in_array( $download_id, $product_reqs ) ) {
154 $discount_valid = false;
155 }
156
157 if ( ! empty( $excluded_products ) && in_array( $download_id, $excluded_products ) ) {
158 $discount_valid = false;
159 }
160
161 $min_amount = $discount->get_min_charge_amount();
162 if ( $min_amount > 0 && $subtotal < $min_amount ) {
163 $discount_valid = false;
164 }
165
166 if ( $discount_valid ) {
167 $discount_type = $discount->get_amount_type();
168 $discount_value = $discount->get_amount();
169
170 if ( 'percent' === $discount_type ) {
171 $discount_amount = ( $subtotal * $discount_value ) / 100;
172 } else {
173 $discount_amount = $discount_value;
174 }
175
176 if ( $discount_amount > $subtotal ) {
177 $discount_amount = $subtotal;
178 }
179
180 $total = $subtotal - $discount_amount;
181 }
182 }
183 }
184
185 $download_options = [
186 'quantity' => $quantity,
187 ];
188
189 if ( null !== $price_id ) {
190 $download_options['price_id'] = $price_id;
191 }
192
193 $downloads = [
194 [
195 'id' => $download_id,
196 'options' => $download_options,
197 ],
198 ];
199
200 $user_info = [
201 'id' => $user_id,
202 'email' => $customer_email,
203 'first_name' => isset( $selected_options['first_name'] ) ? sanitize_text_field( $selected_options['first_name'] ) : '',
204 'last_name' => isset( $selected_options['last_name'] ) ? sanitize_text_field( $selected_options['last_name'] ) : '',
205 'discount' => isset( $selected_options['discount_code'] ) ? sanitize_text_field( $selected_options['discount_code'] ) : 'none',
206 'address' => [
207 'line1' => isset( $selected_options['address_line1'] ) ? sanitize_text_field( $selected_options['address_line1'] ) : '',
208 'line2' => isset( $selected_options['address_line2'] ) ? sanitize_text_field( $selected_options['address_line2'] ) : '',
209 'city' => isset( $selected_options['city'] ) ? sanitize_text_field( $selected_options['city'] ) : '',
210 'state' => isset( $selected_options['state'] ) ? sanitize_text_field( $selected_options['state'] ) : '',
211 'zip' => isset( $selected_options['zip'] ) ? sanitize_text_field( $selected_options['zip'] ) : '',
212 'country' => isset( $selected_options['country'] ) ? sanitize_text_field( $selected_options['country'] ) : '',
213 ],
214 ];
215
216 $payment_data = [
217 'price' => isset( $selected_options['total_amount'] ) ? floatval( $selected_options['total_amount'] ) : $total,
218 'subtotal' => $subtotal,
219 'discount' => $discount_amount,
220 'date' => isset( $selected_options['date'] ) ? sanitize_text_field( $selected_options['date'] ) : gmdate( 'Y-m-d H:i:s' ),
221 'user_email' => $customer_email,
222 'purchase_key' => strtolower( md5( uniqid() ) ),
223 'currency' => isset( $selected_options['currency'] ) ? sanitize_text_field( $selected_options['currency'] ) : ( function_exists( 'edd_get_currency' ) ? edd_get_currency() : 'USD' ),
224 'downloads' => $downloads,
225 'user_info' => $user_info,
226 'cart_details' => [],
227 'status' => isset( $selected_options['status'] ) ? sanitize_text_field( $selected_options['status'] ) : 'pending',
228 'gateway' => isset( $selected_options['payment_method'] ) ? sanitize_text_field( $selected_options['payment_method'] ) : 'manual',
229 'fees' => [],
230 ];
231
232 if ( function_exists( 'edd_use_taxes' ) && function_exists( 'edd_get_tax_rate' ) && edd_use_taxes() ) {
233 $tax_rate = edd_get_tax_rate( $user_info['address']['country'], $user_info['address']['state'] );
234 $tax = ( $payment_data['price'] * $tax_rate );
235 $payment_data['tax'] = $tax;
236 }
237
238 $item_options = [];
239 if ( null !== $price_id ) {
240 $item_options['price_id'] = $price_id;
241 }
242
243 $payment_data['cart_details'][] = [
244 'name' => get_the_title( $download_id ),
245 'id' => $download_id,
246 'item_number' => [
247 'id' => $download_id,
248 'options' => $item_options,
249 ],
250 'price' => $item_price,
251 'item_price' => $item_price,
252 'quantity' => $quantity,
253 'discount' => $discount_amount,
254 'subtotal' => $subtotal,
255 'tax' => 0,
256 'fees' => [],
257 ];
258
259 $payment_id = edd_insert_payment( $payment_data );
260
261 if ( ! $payment_id ) {
262 return [
263 'status' => 'error',
264 'message' => 'Failed to create purchase. Please check your parameters and try again.',
265 ];
266 }
267
268 $payment = new \EDD_Payment( $payment_id );
269
270 // Auto-generate license keys for EDD Software Licensing.
271 $this->auto_generate_license_keys( $payment_id, $payment_data );
272
273 // Setup license status management for refunds/revokes.
274 $this->setup_license_status_management( $payment_id );
275
276 // Get generated license keys.
277 $license_keys = $this->get_license_keys_for_payment( $payment_id );
278
279 $purchase_data = [
280 'purchase_id' => $payment_id,
281 'purchase_key' => $payment->key,
282 'customer_email' => $payment->email,
283 'customer_id' => $payment->customer_id,
284 'user_id' => $payment->user_id,
285 'first_name' => $payment->first_name,
286 'last_name' => $payment->last_name,
287 'total_amount' => $payment->total,
288 'subtotal' => $payment->subtotal,
289 'tax' => $payment->tax,
290 'currency' => $payment->currency,
291 'status' => $payment->status,
292 'payment_method' => $payment->gateway,
293 'date' => $payment->date,
294 'downloads' => array_map(
295 function( $item ) {
296 return [
297 'download_id' => $item['id'],
298 'name' => $item['name'],
299 'price' => $item['price'],
300 'quantity' => isset( $item['quantity'] ) ? $item['quantity'] : 1,
301 ];
302 },
303 $payment->cart_details
304 ),
305 'license_keys' => $license_keys,
306 ];
307
308 return [
309 'status' => 'success',
310 'message' => 'Purchase created successfully',
311 'purchase' => $purchase_data,
312 ];
313 }
314
315 /**
316 * Auto-generate license keys for purchased products
317 *
318 * @param int $payment_id The payment ID.
319 * @param array $payment_data The payment data.
320 * @since 1.0.0
321 *
322 * @return void
323 */
324 private function auto_generate_license_keys( $payment_id, $payment_data ) {
325 // Check if EDD Software Licensing is available.
326 if ( ! function_exists( 'edd_software_licensing' ) || ! class_exists( 'EDD_Software_Licensing' ) || ! class_exists( 'EDD_SL_License' ) ) {
327 return;
328 }
329
330 if ( empty( $payment_data['cart_details'] ) ) {
331 return;
332 }
333
334 foreach ( $payment_data['cart_details'] as $cart_index => $cart_item ) {
335 $download_id = isset( $cart_item['id'] ) ? intval( $cart_item['id'] ) : 0;
336
337 if ( empty( $download_id ) ) {
338 continue;
339 }
340
341 // Get price ID if available.
342 $price_id = null;
343 if ( isset( $cart_item['item_number']['options']['price_id'] ) ) {
344 $price_id = $cart_item['item_number']['options']['price_id'];
345 }
346
347 // Check if license already exists.
348 $existing_license = edd_software_licensing()->get_license_by_purchase( $payment_id, $download_id, $cart_index );
349
350 if ( ! empty( $existing_license ) ) {
351 continue;
352 }
353
354 // Create license.
355 /**
356 * EDD Software License instance.
357 *
358 * @var \EDD_SL_License $license
359 */
360 $license = new \EDD_SL_License();
361 $license->create( $download_id, $payment_id, $price_id, $cart_index );
362 }
363 }
364
365 /**
366 * Setup license status management for refunds and revokes
367 *
368 * @param int $payment_id The payment ID.
369 * @since 1.0.0
370 *
371 * @return void
372 */
373 private function setup_license_status_management( $payment_id ) {
374 // Check if EDD Software Licensing is available.
375 if ( ! function_exists( 'edd_software_licensing' ) || ! class_exists( 'EDD_Software_Licensing' ) ) {
376 return;
377 }
378
379 // Hook into payment status changes to handle license status updates.
380 add_action( 'edd_update_payment_status', [ $this, 'handle_license_status_on_payment_change' ], 10, 3 );
381 }
382
383 /**
384 * Handle license status changes when payment status changes
385 *
386 * @param int $payment_id The payment ID.
387 * @param string $new_status The new payment status.
388 * @param string $old_status The old payment status.
389 * @since 1.0.0
390 *
391 * @return void
392 */
393 public function handle_license_status_on_payment_change( $payment_id, $new_status, $old_status ) {
394 // Only process if EDD Software Licensing is available.
395 if ( ! function_exists( 'edd_software_licensing' ) || ! class_exists( 'EDD_SL_License' ) ) {
396 return;
397 }
398
399 // Handle revoked and refunded statuses - both should disable licenses.
400 if ( 'revoked' === $new_status || 'refunded' === $new_status ) {
401 $this->disable_licenses_for_payment( $payment_id );
402 }
403 }
404
405 /**
406 * Disable all licenses associated with a payment
407 *
408 * @param int $payment_id The payment ID.
409 * @since 1.0.0
410 *
411 * @return void
412 */
413 private function disable_licenses_for_payment( $payment_id ) {
414 // Check if EDD Software Licensing functions are available.
415 if ( ! function_exists( 'edd_software_licensing' ) || ! class_exists( 'EDD_SL_License' ) ) {
416 return;
417 }
418
419 // Get all licenses for this payment.
420 $licenses = edd_software_licensing()->get_licenses_of_purchase( $payment_id );
421
422 if ( empty( $licenses ) ) {
423 return;
424 }
425
426 foreach ( $licenses as $license_data ) {
427 if ( ! isset( $license_data->ID ) ) {
428 continue;
429 }
430
431 /**
432 * EDD Software License instance.
433 *
434 * @var \EDD_SL_License $license
435 */
436 $license = new \EDD_SL_License( $license_data->ID );
437
438 // Only update if license is not already disabled.
439 if ( 'disabled' !== $license->status ) {
440 $license->update_meta( 'status', 'disabled' );
441 $license->status = 'disabled';
442
443 // Trigger action for other plugins to hook into.
444 do_action( 'edd_sl_license_disabled', $license->ID, $payment_id );
445 }
446 }
447 }
448
449 /**
450 * Get license keys for a payment
451 *
452 * @param int $payment_id The payment ID.
453 * @since 1.0.0
454 *
455 * @return array
456 */
457 private function get_license_keys_for_payment( $payment_id ) {
458 // Check if EDD Software Licensing is available.
459 if ( ! function_exists( 'edd_software_licensing' ) || ! class_exists( 'EDD_SL_License' ) ) {
460 return [];
461 }
462
463 // Get all licenses for this payment.
464 $licenses = edd_software_licensing()->get_licenses_of_purchase( $payment_id );
465
466 if ( empty( $licenses ) ) {
467 return [];
468 }
469
470 $license_data = [];
471 foreach ( $licenses as $license ) {
472 if ( ! isset( $license->ID ) ) {
473 continue;
474 }
475
476 /**
477 * EDD Software License instance.
478 *
479 * @var \EDD_SL_License $license_obj
480 */
481 $license_obj = new \EDD_SL_License( $license->ID );
482
483 $license_data[] = [
484 'license_id' => $license_obj->ID,
485 'license_key' => $license_obj->key,
486 'status' => $license_obj->status,
487 'download_id' => $license_obj->download_id,
488 ];
489 }
490
491 return $license_data;
492 }
493 }
494
495 CreatePurchase::get_instance();
496