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
ChargeRefunded.php
40 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ProfilePress\Core\Membership\PaymentMethods\Stripe\WebhookHandlers; |
| 4 | |
| 5 | use ProfilePress\Core\Membership\Models\Subscription\SubscriptionFactory; |
| 6 | use ProfilePress\Core\Membership\PaymentMethods\WebhookHandlerInterface; |
| 7 | use ProfilePress\Core\Membership\Repositories\OrderRepository; |
| 8 | |
| 9 | class ChargeRefunded implements WebhookHandlerInterface |
| 10 | { |
| 11 | public function handle($event_data) |
| 12 | { |
| 13 | // This is an uncaptured PaymentIntent, not a true refund. |
| 14 | if ( ! $event_data['captured']) return; |
| 15 | |
| 16 | $orders = OrderRepository::init()->retrieveBy(['transaction_id' => $event_data['payment_intent']]); |
| 17 | |
| 18 | if (empty($orders)) return; |
| 19 | |
| 20 | $order = $orders[0]; |
| 21 | |
| 22 | // If this was completely refunded, set the status to refunded. |
| 23 | if ($event_data['refunded'] === true) { |
| 24 | $order->refund_order(); |
| 25 | $subscription = SubscriptionFactory::fromId($order->subscription_id); |
| 26 | |
| 27 | if ( ! $subscription->is_recurring()) { |
| 28 | $subscription->cancel(); // cancelled one-time/lifetime sub are considered inactive if cancelled |
| 29 | } |
| 30 | |
| 31 | $last_order = $subscription->get_last_order(); |
| 32 | if ($last_order && $order->get_id() == $last_order->get_id()) $subscription->expire(); |
| 33 | |
| 34 | } else { |
| 35 | // If this was partially refunded, don't change the status. |
| 36 | $order->add_note(sprintf(__('Payment %s partially refunded in Stripe.', 'wp-user-avatar'), $event_data['payment_intent'])); |
| 37 | } |
| 38 | } |
| 39 | } |
| 40 |