| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Modules\Payments\Classes; |
| 4 |
|
| 5 |
use FluentForm\App\Modules\Payments\PaymentHelper; |
| 6 |
|
| 7 |
if (!defined('ABSPATH')) { |
| 8 |
exit; // Exit if accessed directly. |
| 9 |
} |
| 10 |
|
| 11 |
class PaymentManagement |
| 12 |
{ |
| 13 |
public function cancelSubscription($subscription) |
| 14 |
{ |
| 15 |
$validStatuses = [ |
| 16 |
'active', |
| 17 |
'trialling', |
| 18 |
'failing' |
| 19 |
]; |
| 20 |
|
| 21 |
if (!in_array($subscription->status, $validStatuses)) { |
| 22 |
return new \WP_Error('wrong_status', 'Sorry, You can not cancel this subscription'); |
| 23 |
} |
| 24 |
$oldStatus = $subscription->status; |
| 25 |
$newStatus = 'cancelled'; |
| 26 |
$submission = fluentFormApi('submissions')->find($subscription->submission_id); |
| 27 |
|
| 28 |
// Now let's try to cancel this subscription |
| 29 |
$handler = apply_filters_deprecated( |
| 30 |
'fluentform_payment_manager_class_' . $submission->payment_method, |
| 31 |
[ |
| 32 |
false |
| 33 |
], |
| 34 |
FLUENTFORM_FRAMEWORK_UPGRADE, |
| 35 |
'fluentform/payment_manager_class_' . $submission->payment_method, |
| 36 |
'Use fluentform/payment_manager_class_' . $submission->payment_method . ' instead of fluentform_payment_manager_class_' . $submission->payment_method |
| 37 |
); |
| 38 |
$handler = apply_filters('fluentform/payment_manager_class_' . $submission->payment_method, $handler); |
| 39 |
|
| 40 |
$message = 'Subscription has been marked as cancelled'; |
| 41 |
|
| 42 |
if($handler) { // we have handler so the subscription cancellation will be managed by them |
| 43 |
$response = $handler->cancelSubscription($subscription, 'admin', $submission); |
| 44 |
if(is_wp_error($response)) { |
| 45 |
return $response; |
| 46 |
} |
| 47 |
} else { |
| 48 |
PaymentHelper::recordSubscriptionCancelled($subscription, false, [ |
| 49 |
'parent_source_id' => $submission->form_id, |
| 50 |
'source_type' => 'submission_item', |
| 51 |
'source_id' => $submission->id, |
| 52 |
'component' => 'General', |
| 53 |
'status' => 'info', |
| 54 |
'title' => __('Subscription has been cancelled by admin', 'fluentform'), |
| 55 |
'description' => __('Subscription has been cancelled locally. Subscription may not cancelled at ', 'fluentform') . $submission->payment_method |
| 56 |
]); |
| 57 |
} |
| 58 |
|
| 59 |
do_action_deprecated( |
| 60 |
'fluentform_payment_subscription_status_to_cancelled', |
| 61 |
[ |
| 62 |
$subscription, |
| 63 |
$submission, |
| 64 |
$oldStatus |
| 65 |
], |
| 66 |
FLUENTFORM_FRAMEWORK_UPGRADE, |
| 67 |
'fluentform/payment_subscription_status_to_cancelled', |
| 68 |
'Use fluentform/payment_subscription_status_to_cancelled instead of fluentform_payment_subscription_status_to_cancelled.' |
| 69 |
); |
| 70 |
|
| 71 |
do_action('fluentform/payment_subscription_status_to_cancelled', $subscription, $submission, $oldStatus); |
| 72 |
|
| 73 |
do_action_deprecated( |
| 74 |
'fluentform_payment_subscription_status_' . $submission->payment_method . '_to_' . $newStatus, |
| 75 |
[ |
| 76 |
$subscription, |
| 77 |
$submission, |
| 78 |
$oldStatus |
| 79 |
], |
| 80 |
FLUENTFORM_FRAMEWORK_UPGRADE, |
| 81 |
'fluentform/payment_subscription_status_' . $submission->payment_method . '_to_' . $newStatus, |
| 82 |
'Use fluentform/payment_subscription_status_' . $submission->payment_method . '_to_' . $newStatus . ' instead of fluentform_payment_subscription_status_' . $submission->payment_method . '_to_' . $newStatus |
| 83 |
); |
| 84 |
|
| 85 |
do_action('fluentform/payment_subscription_status_' . $submission->payment_method . '_to_' . $newStatus, $subscription, $submission, $oldStatus); |
| 86 |
|
| 87 |
|
| 88 |
return $message; |
| 89 |
} |
| 90 |
} |
| 91 |
|