Automattic\WooCommerce\Gateways\PayPal
WebhookHandler::process_checkout_order_approved
Process the CHECKOUT.ORDER.APPROVED 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_checkout_order_approved( $event ): void;
- $event(array) (required)
- The webhook event data.
Changelog
| Since 10.5.0 | Introduced. |
WebhookHandler::process_checkout_order_approved() WebhookHandler::process checkout order approved code WC 10.7.0
private function process_checkout_order_approved( array $event ): void {
$custom_id = $event['resource']['purchase_units'][0]['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.
$paypal_status = $order->get_meta( PayPalConstants::PAYPAL_ORDER_META_STATUS, true );
if ( in_array( $paypal_status, array( PayPalConstants::STATUS_COMPLETED, PayPalConstants::STATUS_APPROVED ), true ) ) {
return;
}
$status = $event['resource']['status'] ?? null;
$paypal_order_id = $event['resource']['id'] ?? null;
if ( PayPalConstants::STATUS_APPROVED === $status ) {
\WC_Gateway_Paypal::log( 'PayPal payment approved. Order ID: ' . $order->get_id() );
$order->update_meta_data( PayPalConstants::PAYPAL_ORDER_META_STATUS, $status );
// Clear the shipping callback token by setting it to an empty string.
// This is done to prevent the token from being used again for the same order.
// We are not deleting the meta key as we use the existence of the meta key to determine if the token was ever generated for this order.
$order->update_meta_data( PayPalConstants::PAYPAL_ORDER_META_SHIPPING_CALLBACK_TOKEN, '' );
$order->save();
$order->add_order_note(
sprintf(
/* translators: %1$s: PayPal order ID */
__( 'PayPal payment approved. PayPal Order ID: %1$s', 'woocommerce' ),
$paypal_order_id
)
);
$order->save();
// Update the addresses in the order with the addresses from the PayPal order details.
PayPalHelper::update_addresses_in_order( $order, $event['resource'] );
// Authorize or capture the payment after approval.
$paypal_intent = $event['resource']['intent'] ?? null;
$action = PayPalConstants::INTENT_CAPTURE === $paypal_intent ? PayPalConstants::PAYMENT_ACTION_CAPTURE : PayPalConstants::PAYMENT_ACTION_AUTHORIZE;
$links = $event['resource']['links'] ?? array();
if ( ! is_array( $links ) ) {
$links = array();
}
$this->authorize_or_capture_payment( $order, $links, $action );
} else {
// This is unexpected for a CHECKOUT.ORDER.APPROVED event.
\WC_Gateway_Paypal::log( 'PayPal payment approval failed. Order ID: ' . $order->get_id() . ' Status: ' . $status );
$order->add_order_note(
sprintf(
/* translators: %1$s: PayPal order ID, %2$s: Status */
__( 'PayPal payment approval failed. PayPal Order ID: %1$s. Status: %2$s', 'woocommerce' ),
$paypal_order_id,
$status
)
);
}
}