| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Modules\Payments\PaymentMethods\Stripe; |
| 4 |
|
| 5 |
use FluentForm\App\Models\Transaction; |
| 6 |
use FluentForm\App\Modules\Payments\PaymentHelper; |
| 7 |
use FluentForm\App\Modules\Payments\PaymentMethods\Stripe\API\ApiRequest; |
| 8 |
|
| 9 |
if (!defined('ABSPATH')) { |
| 10 |
exit; // Exit if accessed directly. |
| 11 |
} |
| 12 |
|
| 13 |
class PaymentManager |
| 14 |
{ |
| 15 |
public function cancelSubscription($subscription, $scope = 'admin', $submission = false) |
| 16 |
{ |
| 17 |
if(!$submission) { |
| 18 |
$submission = fluentFormApi('submissions')->find($subscription->submission_id); |
| 19 |
} |
| 20 |
|
| 21 |
if(!$submission || $submission->payment_method != 'stripe') { |
| 22 |
return new \WP_Error('method_mismatch', __('Failed to cancel this subscription', 'fluentform')); |
| 23 |
} |
| 24 |
|
| 25 |
// Get the first transaction to determine the payment mode |
| 26 |
$lastTransaction = Transaction::where('subscription_id', $subscription->id) |
| 27 |
->where('payment_method', 'stripe') |
| 28 |
->orderBy('id', 'DESC') |
| 29 |
->first(); |
| 30 |
|
| 31 |
if(!$lastTransaction) { |
| 32 |
return new \WP_Error('transaction_mismatch', __('Failed to cancel this subscription', 'fluentform')); |
| 33 |
} |
| 34 |
|
| 35 |
$vendorSubscriptionId = $subscription->vendor_subscription_id; |
| 36 |
|
| 37 |
if(!$vendorSubscriptionId) { |
| 38 |
return new \WP_Error('no_vendor_subscription_found', __('Failed to cancel this subscription', 'fluentform')); |
| 39 |
} |
| 40 |
|
| 41 |
$secretKey = StripeSettings::getSecretKey($submission->form_id); |
| 42 |
ApiRequest::set_secret_key($secretKey); |
| 43 |
$response = ApiRequest::request([], 'subscriptions/'.$vendorSubscriptionId, 'DELETE'); |
| 44 |
|
| 45 |
if(is_wp_error($response)) { |
| 46 |
return $response; |
| 47 |
} |
| 48 |
|
| 49 |
PaymentHelper::recordSubscriptionCancelled($subscription, $response, [ |
| 50 |
'parent_source_id' => $submission->form_id, |
| 51 |
'source_type' => 'submission_item', |
| 52 |
'source_id' => $submission->id, |
| 53 |
'component' => 'General', |
| 54 |
'status' => 'info', |
| 55 |
'title' => __('Subscription has been cancelled by ', 'fluentform') . $scope, |
| 56 |
'description' => __('Subscription has been cancelled from ', 'fluentform') . $submission->payment_method |
| 57 |
]); |
| 58 |
|
| 59 |
// It's a success so let's send a valid response |
| 60 |
return $response; |
| 61 |
} |
| 62 |
|
| 63 |
public function refundTransaction($transaction, $scope = 'admin') |
| 64 |
{ |
| 65 |
|
| 66 |
} |
| 67 |
|
| 68 |
private function retrieveVendorSubscription($subscription) |
| 69 |
{ |
| 70 |
|
| 71 |
} |
| 72 |
|
| 73 |
private function retrieveVendorTransaction($transaction) |
| 74 |
{ |
| 75 |
|
| 76 |
} |
| 77 |
} |
| 78 |
|