wp-user-avatar
/
src
/
Membership
/
PaymentMethods
/
Stripe
/
WebhookHandlers
/
CustomerSubscriptionCreated.php
ChargeRefunded.php
11 months ago
CheckoutSessionAsyncPaymentFailed.php
4 months ago
CheckoutSessionAsyncPaymentSucceeded.php
4 months ago
CheckoutSessionCompleted.php
4 months ago
CustomerSubscriptionCreated.php
2 years ago
CustomerSubscriptionDeleted.php
3 years ago
CustomerSubscriptionUpdated.php
11 months ago
InvoicePaymentSucceeded.php
2 years ago
PaymentIntentSucceeded.php
3 years ago
index.php
3 years ago
CustomerSubscriptionCreated.php
54 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ProfilePress\Core\Membership\PaymentMethods\Stripe\WebhookHandlers; |
| 4 | |
| 5 | use ProfilePress\Core\Membership\Models\Subscription\SubscriptionStatus; |
| 6 | use ProfilePress\Core\Membership\PaymentMethods\WebhookHandlerInterface; |
| 7 | use ProfilePress\Core\Membership\Repositories\SubscriptionRepository; |
| 8 | use ProfilePressVendor\Carbon\CarbonImmutable; |
| 9 | |
| 10 | class CustomerSubscriptionCreated implements WebhookHandlerInterface |
| 11 | { |
| 12 | public function handle($event_data) |
| 13 | { |
| 14 | $subscription_profile_id = $event_data['id']; |
| 15 | |
| 16 | $subscription = SubscriptionRepository::init()->retrieveBy([ |
| 17 | 'profile_id' => $subscription_profile_id |
| 18 | ]); |
| 19 | |
| 20 | if (empty($subscription)) return; |
| 21 | |
| 22 | $subscription = $subscription[0]; |
| 23 | |
| 24 | switch ($event_data['status']) { |
| 25 | case 'active': |
| 26 | if ( ! $subscription->is_active()) { |
| 27 | $subscription->activate_subscription($subscription_profile_id); |
| 28 | } else { |
| 29 | $subscription->status = SubscriptionStatus::ACTIVE; |
| 30 | } |
| 31 | // ensures expiration date is in sync with Stripe |
| 32 | $subscription->expiration_date = CarbonImmutable::createFromTimestampUTC($event_data['current_period_end'])->toDateTimeString(); |
| 33 | break; |
| 34 | case 'trialing': |
| 35 | if ( ! $subscription->is_active()) { |
| 36 | $subscription->enable_subscription_trial($subscription_profile_id); |
| 37 | } else { |
| 38 | $subscription->status = SubscriptionStatus::TRIALLING; |
| 39 | } |
| 40 | // ensures expiration date is in sync with Stripe |
| 41 | $subscription->expiration_date = CarbonImmutable::createFromTimestampUTC($event_data['trial_end'])->toDateTimeString(); |
| 42 | break; |
| 43 | case 'unpaid': |
| 44 | $subscription->expire(); |
| 45 | break; |
| 46 | case 'canceled': |
| 47 | $subscription->status = SubscriptionStatus::CANCELLED; |
| 48 | break; |
| 49 | } |
| 50 | |
| 51 | $subscription->save(); |
| 52 | } |
| 53 | } |
| 54 |