| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Modules\Payments\PaymentMethods\Stripe\API; |
| 4 |
|
| 5 |
if (!defined('ABSPATH')) { |
| 6 |
exit; |
| 7 |
} |
| 8 |
|
| 9 |
use FluentForm\App\Modules\Payments\PaymentHelper; |
| 10 |
use FluentForm\App\Modules\Payments\PaymentMethods\Stripe\StripeSettings; |
| 11 |
|
| 12 |
/** |
| 13 |
* Handle Payment Charge Via Stripe |
| 14 |
* @since 1.2.0 |
| 15 |
*/ |
| 16 |
class Plan |
| 17 |
{ |
| 18 |
use RequestProcessor; |
| 19 |
|
| 20 |
public static function retrieve($planId, $formId) |
| 21 |
{ |
| 22 |
try { |
| 23 |
$secretKey = apply_filters_deprecated( |
| 24 |
'fluentform-payment_stripe_secret_key', |
| 25 |
[ |
| 26 |
StripeSettings::getSecretKey($formId), |
| 27 |
$formId |
| 28 |
], |
| 29 |
FLUENTFORM_FRAMEWORK_UPGRADE, |
| 30 |
'fluentform/payment_stripe_secret_key', |
| 31 |
'Use fluentform/payment_stripe_secret_key instead of fluentform-payment_stripe_secret_key.' |
| 32 |
); |
| 33 |
|
| 34 |
$secretKey = apply_filters('fluentform/payment_stripe_secret_key', $secretKey, $formId); |
| 35 |
|
| 36 |
ApiRequest::set_secret_key($secretKey); |
| 37 |
|
| 38 |
$response = ApiRequest::request([], 'plans/' . $planId, 'GET'); |
| 39 |
|
| 40 |
return static::processResponse($response); |
| 41 |
} catch (\Exception $e) { |
| 42 |
// Something else happened, completely unrelated to Stripe |
| 43 |
return static::errorHandler('non_stripe', esc_html__('General Error', 'fluentform') . ': ' . $e->getMessage()); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
public static function create($plan, $formId) |
| 48 |
{ |
| 49 |
$secretKey = apply_filters_deprecated( |
| 50 |
'fluentform-payment_stripe_secret_key', |
| 51 |
[ |
| 52 |
StripeSettings::getSecretKey($formId), |
| 53 |
$formId |
| 54 |
], |
| 55 |
FLUENTFORM_FRAMEWORK_UPGRADE, |
| 56 |
'fluentform/payment_stripe_secret_key', |
| 57 |
'Use fluentform/payment_stripe_secret_key instead of fluentform-payment_stripe_secret_key.' |
| 58 |
); |
| 59 |
|
| 60 |
$secretKey = apply_filters('fluentform/payment_stripe_secret_key', $secretKey, $formId); |
| 61 |
|
| 62 |
ApiRequest::set_secret_key($secretKey); |
| 63 |
|
| 64 |
$response = ApiRequest::request($plan, 'plans', 'POST'); |
| 65 |
|
| 66 |
return static::processResponse($response); |
| 67 |
} |
| 68 |
|
| 69 |
public static function getPriceIdsFromSubscriptionTransaction($subscription, $subscriptionTransaction) |
| 70 |
{ |
| 71 |
if ($subscriptionTransaction->transaction_type != 'subscription') { |
| 72 |
return false; |
| 73 |
} |
| 74 |
|
| 75 |
$planItems = []; |
| 76 |
$subscriptionPlan = self::getSubscriptionPlanBySubscription($subscription, $subscriptionTransaction->currency); |
| 77 |
|
| 78 |
if(is_wp_error($subscriptionPlan)) { |
| 79 |
return $subscriptionPlan; |
| 80 |
} |
| 81 |
|
| 82 |
$planItems[] = [ |
| 83 |
'price' => $subscriptionPlan->id, |
| 84 |
'quantity' => $subscription->quantity ? $subscription->quantity : 1 |
| 85 |
]; |
| 86 |
|
| 87 |
// Maybe we have signup fee and other single payment amount |
| 88 |
$transactionTotal = $subscriptionTransaction->payment_total; |
| 89 |
|
| 90 |
$subscriptionFirstTotal = 0; |
| 91 |
|
| 92 |
if (!$subscription->trial_days) { |
| 93 |
$subscriptionFirstTotal += $subscription->recurring_amount; |
| 94 |
} |
| 95 |
|
| 96 |
$signupFee = 0; |
| 97 |
if ($transactionTotal > $subscriptionFirstTotal) { |
| 98 |
$signupFee = $transactionTotal - $subscriptionFirstTotal; |
| 99 |
} |
| 100 |
|
| 101 |
return [ |
| 102 |
'items' => $planItems, |
| 103 |
'signup_fee' => $signupFee |
| 104 |
]; |
| 105 |
} |
| 106 |
|
| 107 |
public static function getOrCreate($subscription, $submission) |
| 108 |
{ |
| 109 |
if (PaymentHelper::isZeroDecimal($submission->currency)) { |
| 110 |
$subscription->recurring_amount = intval($subscription->recurring_amount / 100); |
| 111 |
} |
| 112 |
|
| 113 |
// Generate The subscription ID Here |
| 114 |
$subscriptionId = static::getGeneratedSubscriptionId($subscription, $submission->currency); |
| 115 |
|
| 116 |
$stripePlan = static::retrieve($subscriptionId, $subscription->form_id); |
| 117 |
|
| 118 |
if ($stripePlan && !is_wp_error($stripePlan)) { |
| 119 |
return $stripePlan; |
| 120 |
} |
| 121 |
|
| 122 |
// We don't have this plan yet. Now we have to create the plan from subscription |
| 123 |
$plan = array( |
| 124 |
'id' => $subscriptionId, |
| 125 |
'currency' => $submission->currency, |
| 126 |
'interval' => $subscription->billing_interval, |
| 127 |
'amount' => $subscription->recurring_amount, |
| 128 |
'trial_period_days' => $subscription->trial_days, |
| 129 |
'product' => [ |
| 130 |
'id' => $subscriptionId, |
| 131 |
'name' => $subscription->item_name . ' (' . $subscription->plan_name . ')', |
| 132 |
'type' => 'service' |
| 133 |
], |
| 134 |
'metadata' => [ |
| 135 |
'form_id' => $subscription->form_id, |
| 136 |
'element_id' => $subscription->element_id, |
| 137 |
'wp_plugin' => 'Fluent Forms Pro' |
| 138 |
] |
| 139 |
); |
| 140 |
|
| 141 |
return static::create($plan, $subscription->form_id); |
| 142 |
} |
| 143 |
|
| 144 |
public static function getGeneratedSubscriptionId($subscription, $currency = 'USD') |
| 145 |
{ |
| 146 |
$subscriptionId = 'fluentform_' . $subscription->element_id . '_' . $subscription->recurring_amount . '_' . $subscription->billing_interval . '_' . $subscription->trial_days . '_' . $currency;; |
| 147 |
|
| 148 |
$subscriptionId = apply_filters_deprecated( |
| 149 |
'fluentform_stripe_plan_name_generated', |
| 150 |
[ |
| 151 |
$subscriptionId, |
| 152 |
$subscription, |
| 153 |
$currency |
| 154 |
], |
| 155 |
FLUENTFORM_FRAMEWORK_UPGRADE, |
| 156 |
'fluentform/stripe_plan_name_generated', |
| 157 |
'Use fluentform/stripe_plan_name_generated instead of fluentform_stripe_plan_name_generated.' |
| 158 |
); |
| 159 |
|
| 160 |
return apply_filters('fluentform/stripe_plan_name_generated', $subscriptionId, $subscription, $currency); |
| 161 |
} |
| 162 |
|
| 163 |
public static function subscribe($subscriptionArgs, $formId) |
| 164 |
{ |
| 165 |
$secretKey = apply_filters_deprecated( |
| 166 |
'fluentform-payment_stripe_secret_key', |
| 167 |
[ |
| 168 |
StripeSettings::getSecretKey($formId), |
| 169 |
$formId |
| 170 |
], |
| 171 |
FLUENTFORM_FRAMEWORK_UPGRADE, |
| 172 |
'fluentform/payment_stripe_secret_key', |
| 173 |
'Use fluentform/payment_stripe_secret_key instead of fluentform-payment_stripe_secret_key.' |
| 174 |
); |
| 175 |
|
| 176 |
$secretKey = apply_filters('fluentform/payment_stripe_secret_key', $secretKey, $formId); |
| 177 |
|
| 178 |
ApiRequest::set_secret_key($secretKey); |
| 179 |
|
| 180 |
return ApiRequest::request($subscriptionArgs, 'subscriptions', 'POST'); |
| 181 |
} |
| 182 |
|
| 183 |
public static function getCancelledAtTimestamp($subscription) |
| 184 |
{ |
| 185 |
if(!$subscription->bill_times) { |
| 186 |
return false; // bill times is unlimited |
| 187 |
} |
| 188 |
|
| 189 |
$billTimes = $subscription->bill_times; |
| 190 |
$billPeriod = $subscription->billing_interval; |
| 191 |
|
| 192 |
$dateString = $billTimes.' '.$billPeriod; |
| 193 |
|
| 194 |
if($subscription->expiration_at) { |
| 195 |
return strtotime($subscription->expiration_at) - time() + strtotime($dateString); // after 6 hours |
| 196 |
} |
| 197 |
|
| 198 |
return strtotime($dateString); |
| 199 |
|
| 200 |
} |
| 201 |
|
| 202 |
public static function getSubscriptionPlanBySubscription($subscription, $currency) |
| 203 |
{ |
| 204 |
$recurringAmount = $subscription->recurring_amount; |
| 205 |
if (PaymentHelper::isZeroDecimal($currency)) { |
| 206 |
$recurringAmount = intval($subscription->recurring_amount / 100); |
| 207 |
} |
| 208 |
|
| 209 |
$planNameArgs = [ |
| 210 |
'fluentform', |
| 211 |
$subscription->form_id, |
| 212 |
$recurringAmount, |
| 213 |
$subscription->billing_interval, |
| 214 |
$currency |
| 215 |
]; |
| 216 |
|
| 217 |
$planName = apply_filters_deprecated( |
| 218 |
'fluentform_stripe_plan_name', |
| 219 |
[ |
| 220 |
implode('_', $planNameArgs), |
| 221 |
$subscription |
| 222 |
], |
| 223 |
FLUENTFORM_FRAMEWORK_UPGRADE, |
| 224 |
'fluentform/stripe_plan_name', |
| 225 |
'Use fluentform/stripe_plan_name instead of fluentform_stripe_plan_name.' |
| 226 |
); |
| 227 |
|
| 228 |
$planName = apply_filters('fluentform/stripe_plan_name', $planName, $subscription); |
| 229 |
|
| 230 |
$subscriptionPlan = static::retrieve($planName, $subscription->form_id); |
| 231 |
|
| 232 |
if (!is_wp_error($subscriptionPlan) && $subscriptionPlan) { |
| 233 |
return $subscriptionPlan; |
| 234 |
} |
| 235 |
|
| 236 |
$plan = array( |
| 237 |
'id' => $planName, |
| 238 |
'currency' => $currency, |
| 239 |
'interval' => $subscription->billing_interval, |
| 240 |
'amount' => $recurringAmount, |
| 241 |
'trial_period_days' => $subscription->trial_days, |
| 242 |
'product' => [ |
| 243 |
'id' => $subscription->id, |
| 244 |
'name' => $subscription->plan_name, |
| 245 |
'type' => 'service' |
| 246 |
], |
| 247 |
'metadata' => [ |
| 248 |
'form_id' => $subscription->form_id, |
| 249 |
'element_id' => $subscription->element_id, |
| 250 |
'wp_plugin' => 'Fluent Forms Pro' |
| 251 |
], |
| 252 |
'nickname' => $subscription->item_name . ' - ' . $subscription->plan_name |
| 253 |
); |
| 254 |
|
| 255 |
return static::create($plan, $subscription->form_id); |
| 256 |
} |
| 257 |
|
| 258 |
public static function maybeSetCancelAt($subscription, $stripeSub) |
| 259 |
{ |
| 260 |
if($stripeSub->cancel_at) { |
| 261 |
return; |
| 262 |
} |
| 263 |
|
| 264 |
if(!$subscription->bill_times) { |
| 265 |
return; |
| 266 |
} |
| 267 |
|
| 268 |
$trialOffset = 0; |
| 269 |
|
| 270 |
$dateStr = '+'.$subscription->bill_times.' '.$subscription->billing_interval; |
| 271 |
|
| 272 |
if($subscription->trial_days) { |
| 273 |
$trialOffset = $subscription->trial_days * 86400; |
| 274 |
} |
| 275 |
|
| 276 |
$startingTimestamp = $stripeSub->created; |
| 277 |
|
| 278 |
$cancelAt = $startingTimestamp + $trialOffset + (strtotime($dateStr) - time()); |
| 279 |
|
| 280 |
$secretKey = apply_filters_deprecated( |
| 281 |
'fluentform-payment_stripe_secret_key', |
| 282 |
[ |
| 283 |
StripeSettings::getSecretKey($subscription->form_id), |
| 284 |
$subscription->form_id |
| 285 |
], |
| 286 |
FLUENTFORM_FRAMEWORK_UPGRADE, |
| 287 |
'fluentform/payment_stripe_secret_key', |
| 288 |
'Use fluentform/payment_stripe_secret_key instead of fluentform-payment_stripe_secret_key.' |
| 289 |
); |
| 290 |
$secretKey = apply_filters('fluentform/payment_stripe_secret_key', $secretKey, $subscription->form_id); |
| 291 |
|
| 292 |
ApiRequest::set_secret_key($secretKey); |
| 293 |
|
| 294 |
return ApiRequest::request([ |
| 295 |
'cancel_at' => $cancelAt |
| 296 |
], 'subscriptions/' . $stripeSub->id, 'POST'); |
| 297 |
|
| 298 |
} |
| 299 |
|
| 300 |
} |
| 301 |
|