AddressRequirements.php
8 months ago
Buttons.php
5 months ago
Constants.php
3 months ago
Helper.php
3 months ago
Notices.php
5 months ago
Request.php
2 months ago
TransactAccountManager.php
5 months ago
WebhookHandler.php
3 months ago
WebhookHandler.php
277 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Automattic\WooCommerce\Gateways\PayPal; |
| 6 | |
| 7 | use Automattic\WooCommerce\Enums\OrderStatus; |
| 8 | use Automattic\WooCommerce\Gateways\PayPal\Constants as PayPalConstants; |
| 9 | use Automattic\WooCommerce\Gateways\PayPal\Helper as PayPalHelper; |
| 10 | use Automattic\WooCommerce\Gateways\PayPal\Request as PayPalRequest; |
| 11 | |
| 12 | if ( ! defined( 'ABSPATH' ) ) { |
| 13 | exit; |
| 14 | } |
| 15 | |
| 16 | /** |
| 17 | * Class WebhookHandler file. |
| 18 | * |
| 19 | * Handles webhook events. |
| 20 | * |
| 21 | * @since 10.5.0 |
| 22 | */ |
| 23 | class WebhookHandler { |
| 24 | |
| 25 | /** |
| 26 | * Process the webhook event. |
| 27 | * |
| 28 | * @since 10.5.0 |
| 29 | * |
| 30 | * @param \WP_REST_Request $request The request object. |
| 31 | * @return void |
| 32 | */ |
| 33 | public function process_webhook( \WP_REST_Request $request ): void { |
| 34 | $data = $request->get_json_params(); |
| 35 | if ( ! is_array( $data ) || empty( $data['event_type'] ) || empty( $data['resource'] ) ) { |
| 36 | \WC_Gateway_Paypal::log( 'Invalid PayPal webhook payload: ' . wc_print_r( $data, true ) ); |
| 37 | return; |
| 38 | } |
| 39 | |
| 40 | \WC_Gateway_Paypal::log( 'Webhook received: ' . wc_print_r( PayPalHelper::redact_data( $data ), true ) ); |
| 41 | |
| 42 | switch ( $data['event_type'] ) { |
| 43 | case 'CHECKOUT.ORDER.APPROVED': |
| 44 | $this->process_checkout_order_approved( $data ); |
| 45 | break; |
| 46 | case 'PAYMENT.CAPTURE.PENDING': |
| 47 | $this->process_payment_capture_pending( $data ); |
| 48 | break; |
| 49 | case 'PAYMENT.CAPTURE.COMPLETED': |
| 50 | $this->process_payment_capture_completed( $data ); |
| 51 | break; |
| 52 | case 'PAYMENT.AUTHORIZATION.CREATED': |
| 53 | $this->process_payment_authorization_created( $data ); |
| 54 | break; |
| 55 | default: |
| 56 | \WC_Gateway_Paypal::log( 'Unhandled PayPal webhook event: ' . wc_print_r( PayPalHelper::redact_data( $data ), true ) ); |
| 57 | break; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Process the CHECKOUT.ORDER.APPROVED webhook event. |
| 63 | * |
| 64 | * @since 10.5.0 |
| 65 | * |
| 66 | * @param array $event The webhook event data. |
| 67 | * @return void |
| 68 | */ |
| 69 | private function process_checkout_order_approved( array $event ): void { |
| 70 | $custom_id = $event['resource']['purchase_units'][0]['custom_id'] ?? ''; |
| 71 | $order = PayPalHelper::get_wc_order_from_paypal_custom_id( $custom_id ); |
| 72 | if ( ! $order ) { |
| 73 | \WC_Gateway_Paypal::log( 'Invalid order. Custom ID: ' . wc_print_r( $custom_id, true ) ); |
| 74 | return; |
| 75 | } |
| 76 | |
| 77 | // Skip if the payment is already processed. |
| 78 | $paypal_status = $order->get_meta( PayPalConstants::PAYPAL_ORDER_META_STATUS, true ); |
| 79 | if ( in_array( $paypal_status, array( PayPalConstants::STATUS_COMPLETED, PayPalConstants::STATUS_APPROVED ), true ) ) { |
| 80 | return; |
| 81 | } |
| 82 | |
| 83 | $status = $event['resource']['status'] ?? null; |
| 84 | $paypal_order_id = $event['resource']['id'] ?? null; |
| 85 | if ( PayPalConstants::STATUS_APPROVED === $status ) { |
| 86 | \WC_Gateway_Paypal::log( 'PayPal payment approved. Order ID: ' . $order->get_id() ); |
| 87 | $order->update_meta_data( PayPalConstants::PAYPAL_ORDER_META_STATUS, $status ); |
| 88 | // Clear the shipping callback token by setting it to an empty string. |
| 89 | // This is done to prevent the token from being used again for the same order. |
| 90 | // We are not deleting the meta key as we use the existence of the meta key to determine if the token was ever generated for this order. |
| 91 | $order->update_meta_data( PayPalConstants::PAYPAL_ORDER_META_SHIPPING_CALLBACK_TOKEN, '' ); |
| 92 | $order->save(); |
| 93 | $order->add_order_note( |
| 94 | sprintf( |
| 95 | /* translators: %1$s: PayPal order ID */ |
| 96 | __( 'PayPal payment approved. PayPal Order ID: %1$s', 'woocommerce' ), |
| 97 | $paypal_order_id |
| 98 | ) |
| 99 | ); |
| 100 | $order->save(); |
| 101 | |
| 102 | // Update the addresses in the order with the addresses from the PayPal order details. |
| 103 | PayPalHelper::update_addresses_in_order( $order, $event['resource'] ); |
| 104 | |
| 105 | // Authorize or capture the payment after approval. |
| 106 | $paypal_intent = $event['resource']['intent'] ?? null; |
| 107 | $action = PayPalConstants::INTENT_CAPTURE === $paypal_intent ? PayPalConstants::PAYMENT_ACTION_CAPTURE : PayPalConstants::PAYMENT_ACTION_AUTHORIZE; |
| 108 | |
| 109 | $links = $event['resource']['links'] ?? array(); |
| 110 | if ( ! is_array( $links ) ) { |
| 111 | $links = array(); |
| 112 | } |
| 113 | |
| 114 | $this->authorize_or_capture_payment( $order, $links, $action ); |
| 115 | } else { |
| 116 | // This is unexpected for a CHECKOUT.ORDER.APPROVED event. |
| 117 | \WC_Gateway_Paypal::log( 'PayPal payment approval failed. Order ID: ' . $order->get_id() . ' Status: ' . $status ); |
| 118 | $order->add_order_note( |
| 119 | sprintf( |
| 120 | /* translators: %1$s: PayPal order ID, %2$s: Status */ |
| 121 | __( 'PayPal payment approval failed. PayPal Order ID: %1$s. Status: %2$s', 'woocommerce' ), |
| 122 | $paypal_order_id, |
| 123 | $status |
| 124 | ) |
| 125 | ); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Process the PAYMENT.CAPTURE.COMPLETED webhook event. |
| 131 | * |
| 132 | * @since 10.5.0 |
| 133 | * |
| 134 | * @param array $event The webhook event data. |
| 135 | * @return void |
| 136 | */ |
| 137 | private function process_payment_capture_completed( array $event ): void { |
| 138 | $custom_id = $event['resource']['custom_id'] ?? ''; |
| 139 | $order = PayPalHelper::get_wc_order_from_paypal_custom_id( $custom_id ); |
| 140 | if ( ! $order ) { |
| 141 | \WC_Gateway_Paypal::log( 'Invalid order. Custom ID: ' . wc_print_r( $custom_id, true ) ); |
| 142 | return; |
| 143 | } |
| 144 | |
| 145 | // Skip if the payment is already processed. |
| 146 | if ( PayPalConstants::STATUS_COMPLETED === $order->get_meta( PayPalConstants::PAYPAL_ORDER_META_STATUS, true ) ) { |
| 147 | return; |
| 148 | } |
| 149 | |
| 150 | $transaction_id = $event['resource']['id'] ?? null; |
| 151 | $status = $event['resource']['status'] ?? null; |
| 152 | $order->set_transaction_id( $transaction_id ); |
| 153 | $order->update_meta_data( PayPalConstants::PAYPAL_ORDER_META_CAPTURE_ID, $transaction_id ); |
| 154 | $order->update_meta_data( PayPalConstants::PAYPAL_ORDER_META_STATUS, $status ); |
| 155 | $order->payment_complete(); |
| 156 | $order->add_order_note( |
| 157 | sprintf( |
| 158 | /* translators: %1$s: Transaction ID */ |
| 159 | __( 'PayPal payment captured. Transaction ID: %1$s.', 'woocommerce' ), |
| 160 | $transaction_id |
| 161 | ) |
| 162 | ); |
| 163 | $order->save(); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Process the PAYMENT.CAPTURE.PENDING webhook event. |
| 168 | * |
| 169 | * @since 10.5.0 |
| 170 | * |
| 171 | * @param array $event The webhook event data. |
| 172 | * @return void |
| 173 | */ |
| 174 | private function process_payment_capture_pending( array $event ): void { |
| 175 | $custom_id = $event['resource']['custom_id'] ?? ''; |
| 176 | $order = PayPalHelper::get_wc_order_from_paypal_custom_id( $custom_id ); |
| 177 | if ( ! $order ) { |
| 178 | \WC_Gateway_Paypal::log( 'Invalid order. Custom ID: ' . wc_print_r( $custom_id, true ) ); |
| 179 | return; |
| 180 | } |
| 181 | |
| 182 | // Skip if the payment is already processed. |
| 183 | if ( PayPalConstants::STATUS_COMPLETED === $order->get_meta( PayPalConstants::PAYPAL_ORDER_META_STATUS, true ) ) { |
| 184 | return; |
| 185 | } |
| 186 | |
| 187 | $transaction_id = $event['resource']['id'] ?? null; |
| 188 | $status = $event['resource']['status'] ?? null; |
| 189 | $reason = $event['resource']['status_details']['reason'] ?? 'Unknown'; |
| 190 | $order->set_transaction_id( $transaction_id ); |
| 191 | $order->update_meta_data( PayPalConstants::PAYPAL_ORDER_META_CAPTURE_ID, $transaction_id ); |
| 192 | $order->update_meta_data( PayPalConstants::PAYPAL_ORDER_META_STATUS, $status ); |
| 193 | /* translators: %s: reason */ |
| 194 | $order->update_status( OrderStatus::ON_HOLD, sprintf( __( 'Payment pending (reason: %s).', 'woocommerce' ), $reason ) ); |
| 195 | $order->save(); |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Process the PAYMENT.AUTHORIZATION.CREATED webhook event. |
| 200 | * |
| 201 | * @since 10.5.0 |
| 202 | * |
| 203 | * @param array $event The webhook event data. |
| 204 | * @return void |
| 205 | */ |
| 206 | private function process_payment_authorization_created( array $event ): void { |
| 207 | $custom_id = $event['resource']['custom_id'] ?? ''; |
| 208 | $order = PayPalHelper::get_wc_order_from_paypal_custom_id( $custom_id ); |
| 209 | if ( ! $order ) { |
| 210 | \WC_Gateway_Paypal::log( 'Invalid order. Custom ID: ' . wc_print_r( $custom_id, true ) ); |
| 211 | return; |
| 212 | } |
| 213 | |
| 214 | // Skip if the payment is already processed. |
| 215 | if ( PayPalConstants::STATUS_COMPLETED === $order->get_meta( PayPalConstants::PAYPAL_ORDER_META_STATUS, true ) ) { |
| 216 | return; |
| 217 | } |
| 218 | |
| 219 | $transaction_id = $event['resource']['id'] ?? null; |
| 220 | $order->set_transaction_id( $transaction_id ); |
| 221 | $order->update_meta_data( PayPalConstants::PAYPAL_ORDER_META_AUTHORIZATION_ID, $transaction_id ); |
| 222 | $order->update_meta_data( PayPalConstants::PAYPAL_ORDER_META_STATUS, PayPalConstants::STATUS_AUTHORIZED ); |
| 223 | $order->add_order_note( |
| 224 | sprintf( |
| 225 | /* translators: %1$s: Transaction ID */ |
| 226 | __( 'PayPal payment authorized. Transaction ID: %1$s. Change payment status to processing or complete to capture funds.', 'woocommerce' ), |
| 227 | $transaction_id |
| 228 | ) |
| 229 | ); |
| 230 | $order->update_status( OrderStatus::ON_HOLD ); |
| 231 | $order->save(); |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Capture the payment. |
| 236 | * |
| 237 | * @since 10.5.0 |
| 238 | * |
| 239 | * @param \WC_Order $order The order object. |
| 240 | * @param array $links The links from the webhook event. |
| 241 | * @param string $action The action to perform (capture or authorize). |
| 242 | * @return void |
| 243 | */ |
| 244 | private function authorize_or_capture_payment( \WC_Order $order, array $links, string $action ): void { |
| 245 | $action_url = $this->get_action_url( $links, $action ); |
| 246 | |
| 247 | $payment_gateways = WC()->payment_gateways()->payment_gateways(); |
| 248 | if ( ! isset( $payment_gateways['paypal'] ) ) { |
| 249 | \WC_Gateway_Paypal::log( 'PayPal gateway is not available.' ); |
| 250 | return; |
| 251 | } |
| 252 | $gateway = $payment_gateways['paypal']; |
| 253 | $paypal_request = new PayPalRequest( $gateway ); |
| 254 | $paypal_request->authorize_or_capture_payment( $order, $action_url, $action ); |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Get the action URL from the links. |
| 259 | * |
| 260 | * @since 10.5.0 |
| 261 | * |
| 262 | * @param array $links The links from the webhook event. |
| 263 | * @param string $action The action to perform (capture or authorize). |
| 264 | * @return string|null |
| 265 | */ |
| 266 | private function get_action_url( array $links, string $action ): ?string { |
| 267 | $action_url = null; |
| 268 | foreach ( $links as $link ) { |
| 269 | if ( $action === $link['rel'] && 'POST' === $link['method'] && filter_var( $link['href'], FILTER_VALIDATE_URL ) ) { |
| 270 | $action_url = esc_url_raw( $link['href'] ); |
| 271 | break; |
| 272 | } |
| 273 | } |
| 274 | return $action_url; |
| 275 | } |
| 276 | } |
| 277 |