WC_Gateway_Paypal::capture_payment
Capture payment when the order is changed from on-hold to complete or processing
Method of the class: WC_Gateway_Paypal{}
No Hooks.
Returns
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() WC Gateway Paypal::capture payment code WC 10.7.0
public function capture_payment( $order_id ) {
$order = wc_get_order( $order_id );
if ( ! $order || ! $order instanceof WC_Order ) {
return;
}
// Bail if the order is not a PayPal order.
if ( self::ID !== $order->get_payment_method() ) {
return;
}
// If the order is authorized via legacy API, the '_paypal_status' meta will be 'pending'.
$is_authorized_via_legacy_api = 'pending' === $order->get_meta( PayPalConstants::PAYPAL_ORDER_META_STATUS, true );
if ( $this->should_use_orders_v2() && ! $is_authorized_via_legacy_api ) {
$paypal_request = new PayPalRequest( $this );
$paypal_request->capture_authorized_payment( $order );
return;
}
if ( 'pending' === $order->get_meta( PayPalConstants::PAYPAL_ORDER_META_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( PayPalConstants::PAYPAL_ORDER_META_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
}
}