WC_Gateway_Paypal::capture_payment()publicWC 1.0

Capture payment when the order is changed from on-hold to complete or processing

Method of the class: WC_Gateway_Paypal{}

No Hooks.

Return

null. Nothing (null).

Usage

$WC_Gateway_Paypal = new WC_Gateway_Paypal();
$WC_Gateway_Paypal->capture_payment( $order_id );
$order_id(int) (required)
Order ID.

WC_Gateway_Paypal::capture_payment() code WC 8.7.0

public function capture_payment( $order_id ) {
	$order = wc_get_order( $order_id );

	if ( 'paypal' === $order->get_payment_method() && 'pending' === $order->get_meta( '_paypal_status', true ) && $order->get_transaction_id() ) {
		$this->init_api();
		$result = WC_Gateway_Paypal_API_Handler::do_capture( $order );

		if ( is_wp_error( $result ) ) {
			static::log( 'Capture Failed: ' . $result->get_error_message(), 'error' );
			/* translators: %s: Paypal gateway error message */
			$order->add_order_note( sprintf( __( 'Payment could not be captured: %s', 'woocommerce' ), $result->get_error_message() ) );
			return;
		}

		static::log( 'Capture Result: ' . wc_print_r( $result, true ) );

		// phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
		if ( ! empty( $result->PAYMENTSTATUS ) ) {
			switch ( $result->PAYMENTSTATUS ) {
				case 'Completed':
					/* translators: 1: Amount, 2: Authorization ID, 3: Transaction ID */
					$order->add_order_note( sprintf( __( 'Payment of %1$s was captured - Auth ID: %2$s, Transaction ID: %3$s', 'woocommerce' ), $result->AMT, $result->AUTHORIZATIONID, $result->TRANSACTIONID ) );
					$order->update_meta_data( '_paypal_status', $result->PAYMENTSTATUS );
					$order->set_transaction_id( $result->TRANSACTIONID );
					$order->save();
					break;
				default:
					/* translators: 1: Authorization ID, 2: Payment status */
					$order->add_order_note( sprintf( __( 'Payment could not be captured - Auth ID: %1$s, Status: %2$s', 'woocommerce' ), $result->AUTHORIZATIONID, $result->PAYMENTSTATUS ) );
					break;
			}
		}
		// phpcs:enable
	}
}