Automattic\WooCommerce\Gateways\PayPal

Request::get_authorization_id_for_captureprivateWC 1.0

Get the authorization ID for the PayPal payment.

Method of the class: Request{}

No Hooks.

Returns

String|null.

Usage

// private - for code of main (parent) class only
$result = $this->get_authorization_id_for_capture( $order ): ?string;
$order(WC_Order) (required)
Order object.

Request::get_authorization_id_for_capture() code WC 10.8.1

private function get_authorization_id_for_capture( WC_Order $order ): ?string {
	$paypal_order_id  = $order->get_meta( PayPalConstants::PAYPAL_ORDER_META_ORDER_ID, true );
	$authorization_id = $order->get_meta( PayPalConstants::PAYPAL_ORDER_META_AUTHORIZATION_ID, true );
	$capture_id       = $order->get_meta( PayPalConstants::PAYPAL_ORDER_META_CAPTURE_ID, true );

	// If the PayPal order ID is not found or the capture ID is already set, return null.
	if ( ! $paypal_order_id || ! empty( $capture_id ) ) {
		return null;
	}

	// If '_paypal_authorization_checked' is set to 'yes', it means we've already made an API call to PayPal
	// and confirmed that no authorization object exists. This flag is set in two scenarios:
	// 1. Capture auth API call returned 404 (authorization object does not exist with the authorization ID).
	// 2. Order details API call returned empty authorization array (authorization object does not exist for this PayPal order).
	// Return null to avoid repeated API calls for orders that have no authorization data.
	if ( 'yes' === $order->get_meta( PayPalConstants::PAYPAL_ORDER_META_AUTHORIZATION_CHECKED, true ) ) {
		return null;
	}

	// If the authorization ID is not found, try to retrieve it from the PayPal order details.
	if ( empty( $authorization_id ) ) {
		\WC_Gateway_Paypal::log( 'Authorization ID not found, trying to retrieve from PayPal order details as a fallback for backwards compatibility. Order ID: ' . $order->get_id() );

		try {
			$order_data = $this->get_paypal_order_details( $paypal_order_id );
		} catch ( Exception $e ) {
			\WC_Gateway_Paypal::log( 'Error retrieving PayPal order details. Order ID: ' . $order->get_id() . '. Error: ' . $e->getMessage() );
			// On 404 (order not found), set flag to prevent repeated API calls.
			if ( false !== strpos( $e->getMessage(), 'HTTP 404' ) ) {
				$order->update_meta_data( PayPalConstants::PAYPAL_ORDER_META_AUTHORIZATION_CHECKED, 'yes' );
				$order->save();
			}
			return null;
		}

		try {
			$authorization_data = $this->get_latest_transaction_data(
				$order_data['purchase_units'][0]['payments']['authorizations'] ?? array()
			);

			$capture_data = $this->get_latest_transaction_data(
				$order_data['purchase_units'][0]['payments']['captures'] ?? array()
			);

			// If the payment is already captured, store the capture ID and status, and return null as there is no authorization ID that needs to be captured.
			if ( $capture_data && isset( $capture_data['id'] ) ) {
				$capture_id = $capture_data['id'];
				$order->update_meta_data( PayPalConstants::PAYPAL_ORDER_META_CAPTURE_ID, $capture_id );
				$order->update_meta_data( PayPalConstants::PAYPAL_ORDER_META_STATUS, $capture_data['status'] ?? PayPalConstants::STATUS_CAPTURED );
				$order->save();
				\WC_Gateway_Paypal::log( 'Storing capture ID from Paypal. Order ID: ' . $order->get_id() . '; capture ID: ' . $capture_id );
				return null;
			}

			if ( $authorization_data && isset( $authorization_data['id'], $authorization_data['status'] ) ) {
				// If the payment is already captured, return null as there is no authorization ID that needs to be captured.
				if ( PayPalConstants::STATUS_CAPTURED === $authorization_data['status'] ) {
					$order->update_meta_data( PayPalConstants::PAYPAL_ORDER_META_STATUS, PayPalConstants::STATUS_CAPTURED );
					$order->save();
					return null;
				}
				$authorization_id = $authorization_data['id'];
				$order->update_meta_data( PayPalConstants::PAYPAL_ORDER_META_AUTHORIZATION_ID, $authorization_id );
				$order->update_meta_data( PayPalConstants::PAYPAL_ORDER_META_STATUS, PayPalConstants::STATUS_AUTHORIZED );
				\WC_Gateway_Paypal::log( 'Storing authorization ID from Paypal. Order ID: ' . $order->get_id() . '; authorization ID: ' . $authorization_id );
				$order->save();
			} else {
				// Scenario 2: Order details API call returned empty authorization array (authorization object does not exist).
				// Store '_paypal_authorization_checked' flag to prevent repeated API calls.
				// This flag indicates that we've made an API call to get PayPal order details and confirmed no authorization object exists.
				\WC_Gateway_Paypal::log( 'Authorization ID not found in PayPal order details. Order ID: ' . $order->get_id() );
				$order->update_meta_data( PayPalConstants::PAYPAL_ORDER_META_AUTHORIZATION_CHECKED, 'yes' );
				$order->save();
				return null;
			}
		} catch ( Exception $e ) {
			\WC_Gateway_Paypal::log( 'Error retrieving authorization ID from PayPal order details. Order ID: ' . $order->get_id() . '. Error: ' . $e->getMessage() );
			return null;
		}
	}

	return $authorization_id;
}