WC_Gateway_Paypal_PDT_Handler::check_response_for_order()publicWC 6.4

Check Response for PDT.

Method of the class: WC_Gateway_Paypal_PDT_Handler{}

No Hooks.

Return

null. Nothing (null).

Usage

$WC_Gateway_Paypal_PDT_Handler = new WC_Gateway_Paypal_PDT_Handler();
$WC_Gateway_Paypal_PDT_Handler->check_response_for_order( $wc_order_id );
$wc_order_id(mixed) (required)
The order id to check the response against.

Changelog

Since 6.4 Introduced.

WC_Gateway_Paypal_PDT_Handler::check_response_for_order() code WC 8.6.1

public function check_response_for_order( $wc_order_id ) {
	// phpcs:ignore WordPress.Security.NonceVerification.Recommended
	if ( empty( $_REQUEST['tx'] ) ) {
		return;
	}

	$wc_order = wc_get_order( $wc_order_id );
	if ( ! $wc_order->needs_payment() ) {
		return;
	}

	// phpcs:ignore WordPress.Security.NonceVerification.Recommended
	$transaction        = wc_clean( wp_unslash( $_REQUEST['tx'] ) );
	$transaction_result = $this->validate_transaction( $transaction );

	if ( $transaction_result ) {
		$status = strtolower( $transaction_result['payment_status'] );
		$amount = isset( $transaction_result['mc_gross'] ) ? $transaction_result['mc_gross'] : 0;
		$order  = $this->get_paypal_order( $transaction_result['custom'] );

		if ( ! $order ) {
			// No valid WC order found on tx data.
			return;
		}

		if ( $wc_order->get_id() !== $order->get_id() ) {
			/* translators: 1: order ID, 2: order ID. */
			WC_Gateway_Paypal::log( sprintf( __( 'Received PDT notification for order %1$d on endpoint for order %2$d.', 'woocommerce' ), $order->get_id(), $wc_order_id ), 'error' );
			return;
		}

		if ( 0 !== strcasecmp( trim( $transaction_result['receiver_email'] ), trim( $this->receiver_email ) ) ) {
			/* translators: 1: email address, 2: order ID . */
			WC_Gateway_Paypal::log( sprintf( __( 'Received PDT notification for another account: %1$s. Order ID: %2$d.', 'woocommerce' ), $transaction_result['receiver_email'], $order->get_id() ), 'error' );
			return;
		}

		// We have a valid response from PayPal.
		WC_Gateway_Paypal::log( 'PDT Transaction Status: ' . wc_print_r( $status, true ) );

		$order->add_meta_data( '_paypal_status', $status );
		$order->set_transaction_id( $transaction );

		if ( 'completed' === $status ) {
			if ( number_format( $order->get_total(), 2, '.', '' ) !== number_format( $amount, 2, '.', '' ) ) {
				WC_Gateway_Paypal::log( 'Payment error: Amounts do not match (amt ' . $amount . ')', 'error' );
				/* translators: 1: Payment amount */
				$this->payment_on_hold( $order, sprintf( __( 'Validation error: PayPal amounts do not match (amt %s).', 'woocommerce' ), $amount ) );
			} else {
				// Log paypal transaction fee and payment type.
				if ( ! empty( $transaction_result['mc_fee'] ) ) {
					$order->add_meta_data( 'PayPal Transaction Fee', wc_clean( $transaction_result['mc_fee'] ) );
				}
				if ( ! empty( $transaction_result['payment_type'] ) ) {
					$order->add_meta_data( 'Payment type', wc_clean( $transaction_result['payment_type'] ) );
				}

				$this->payment_complete( $order, $transaction, __( 'PDT payment completed', 'woocommerce' ) );
			}
		} else {
			if ( 'authorization' === $transaction_result['pending_reason'] ) {
				$this->payment_on_hold( $order, __( 'Payment authorized. Change payment status to processing or complete to capture funds.', 'woocommerce' ) );
			} else {
				/* translators: 1: Pending reason */
				$this->payment_on_hold( $order, sprintf( __( 'Payment pending (%s).', 'woocommerce' ), $transaction_result['pending_reason'] ) );
			}
		}
	} else {
		WC_Gateway_Paypal::log( 'Received invalid response from PayPal PDT' );
	}
}