wp-user-avatar
/
src
/
Membership
/
PaymentMethods
/
Stripe
/
WebhookHandlers
/
CheckoutSessionCompleted.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
CheckoutSessionCompleted.php
77 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ProfilePress\Core\Membership\PaymentMethods\Stripe\WebhookHandlers; |
| 4 | |
| 5 | use ProfilePress\Core\Membership\Models\Order\OrderFactory; |
| 6 | use ProfilePress\Core\Membership\Models\Subscription\SubscriptionFactory; |
| 7 | use ProfilePress\Core\Membership\PaymentMethods\Stripe\APIClass; |
| 8 | use ProfilePress\Core\Membership\PaymentMethods\Stripe\PaymentHelpers; |
| 9 | use ProfilePress\Core\Membership\PaymentMethods\WebhookHandlerInterface; |
| 10 | use ProfilePress\Core\Membership\Services\Calculator; |
| 11 | |
| 12 | class CheckoutSessionCompleted implements WebhookHandlerInterface |
| 13 | { |
| 14 | public function handle($event_data) |
| 15 | { |
| 16 | if ( ! in_array($event_data['mode'], ['subscription', 'payment'], true)) return; |
| 17 | |
| 18 | /** ensures checkout is paid for because of delayed payment methods |
| 19 | * @see https://stripe.com/docs/payments/checkout/fulfill-orders#delayed-notification |
| 20 | */ |
| 21 | if ($event_data['payment_status'] != 'paid') return; |
| 22 | |
| 23 | $order = OrderFactory::fromOrderKey($event_data['client_reference_id']); |
| 24 | |
| 25 | $subscription = SubscriptionFactory::fromId($order->subscription_id); |
| 26 | |
| 27 | if ($event_data['mode'] == 'subscription') { |
| 28 | |
| 29 | $stripe_subscription = APIClass::stripeClient()->subscriptions->retrieve($event_data['subscription'], [ |
| 30 | 'expand' => ['latest_invoice'] |
| 31 | ])->toArray(); |
| 32 | |
| 33 | $transaction_id = $stripe_subscription['latest_invoice']['payment_intent']; |
| 34 | |
| 35 | } else { |
| 36 | |
| 37 | $transaction_id = $event_data['payment_intent']; |
| 38 | } |
| 39 | |
| 40 | if ($order->exists() && ! $order->is_completed()) { |
| 41 | |
| 42 | if (isset($event_data['total_details']['amount_tax'])) { |
| 43 | |
| 44 | if (Calculator::init($event_data['total_details']['amount_tax'])->isGreaterThanZero()) { |
| 45 | |
| 46 | $order->tax = PaymentHelpers::stripe_amount_to_ppress_amount($event_data['total_details']['amount_tax']); |
| 47 | |
| 48 | $order->subtotal = PaymentHelpers::stripe_amount_to_ppress_amount($event_data['amount_subtotal']); |
| 49 | |
| 50 | $order->total = PaymentHelpers::stripe_amount_to_ppress_amount($event_data['amount_total']); |
| 51 | |
| 52 | $order->save(); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | $order->complete_order($transaction_id); |
| 57 | } |
| 58 | |
| 59 | if ( $subscription->exists() && ! $subscription->is_active()) { |
| 60 | |
| 61 | if ($event_data['mode'] == 'subscription') { |
| 62 | |
| 63 | $subscription->profile_id = $event_data['subscription']; |
| 64 | |
| 65 | if ($subscription->has_trial()) { |
| 66 | $subscription->enable_subscription_trial(); |
| 67 | } else { |
| 68 | $subscription->activate_subscription(); |
| 69 | } |
| 70 | |
| 71 | } else { |
| 72 | $subscription->activate_subscription(); |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 |