Automattic\WooCommerce\Gateways\PayPal

WebhookHandler::process_payment_capture_completedprivateWC 10.5.0

Process the PAYMENT.CAPTURE.COMPLETED webhook event.

Method of the class: WebhookHandler{}

No Hooks.

Returns

null. Nothing (null).

Usage

// private - for code of main (parent) class only
$result = $this->process_payment_capture_completed( $event ): void;
$event(array) (required)
The webhook event data.

Changelog

Since 10.5.0 Introduced.

WebhookHandler::process_payment_capture_completed() code WC 10.7.0

private function process_payment_capture_completed( array $event ): void {
	$custom_id = $event['resource']['custom_id'] ?? '';
	$order     = PayPalHelper::get_wc_order_from_paypal_custom_id( $custom_id );
	if ( ! $order ) {
		\WC_Gateway_Paypal::log( 'Invalid order. Custom ID: ' . wc_print_r( $custom_id, true ) );
		return;
	}

	// Skip if the payment is already processed.
	if ( PayPalConstants::STATUS_COMPLETED === $order->get_meta( PayPalConstants::PAYPAL_ORDER_META_STATUS, true ) ) {
		return;
	}

	$transaction_id = $event['resource']['id'] ?? null;
	$status         = $event['resource']['status'] ?? null;
	$order->set_transaction_id( $transaction_id );
	$order->update_meta_data( PayPalConstants::PAYPAL_ORDER_META_CAPTURE_ID, $transaction_id );
	$order->update_meta_data( PayPalConstants::PAYPAL_ORDER_META_STATUS, $status );
	$order->payment_complete();
	$order->add_order_note(
		sprintf(
			/* translators: %1$s: Transaction ID */
			__( 'PayPal payment captured. Transaction ID: %1$s.', 'woocommerce' ),
			$transaction_id
		)
	);
	$order->save();
}