diff --git a/modules/ppcp-api-client/src/Factory/PurchaseUnitFactory.php b/modules/ppcp-api-client/src/Factory/PurchaseUnitFactory.php index 9f8f12c5e..27e045297 100644 --- a/modules/ppcp-api-client/src/Factory/PurchaseUnitFactory.php +++ b/modules/ppcp-api-client/src/Factory/PurchaseUnitFactory.php @@ -9,10 +9,12 @@ declare(strict_types=1); namespace WooCommerce\PayPalCommerce\ApiClient\Factory; +use WC_Session_Handler; use WooCommerce\PayPalCommerce\ApiClient\Entity\Item; use WooCommerce\PayPalCommerce\ApiClient\Entity\PurchaseUnit; use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException; use WooCommerce\PayPalCommerce\ApiClient\Repository\PayeeRepository; +use WooCommerce\PayPalCommerce\Webhooks\CustomIds; /** * Class PurchaseUnitFactory @@ -187,7 +189,14 @@ class PurchaseUnitFactory { $payee = $this->payee_repository->payee(); - $custom_id = ''; + $custom_id = ''; + $session = WC()->session; + if ( $session instanceof WC_Session_Handler ) { + $session_id = $session->get_customer_unique_id(); + if ( $session_id ) { + $custom_id = CustomIds::CUSTOMER_ID_PREFIX . $session_id; + } + } $invoice_id = ''; $soft_descriptor = ''; $purchase_unit = new PurchaseUnit( diff --git a/modules/ppcp-webhooks/src/CustomIds.php b/modules/ppcp-webhooks/src/CustomIds.php new file mode 100644 index 000000000..97a78e43d --- /dev/null +++ b/modules/ppcp-webhooks/src/CustomIds.php @@ -0,0 +1,18 @@ +get_custom_ids_from_request( $request ); + + return array_values( + array_filter( + $ids, + function ( string $id ): bool { + return strpos( $id, CustomIds::CUSTOMER_ID_PREFIX ) === false; + } + ) + ); + } + + /** + * Get available WC customer ids from the given request. + * + * @param WP_REST_Request $request The request. + * @return string[] + */ + protected function get_wc_customer_ids_from_request( WP_REST_Request $request ): array { + $ids = $this->get_custom_ids_from_request( $request ); + + $customer_ids = array_values( + array_filter( + $ids, + function ( string $id ): bool { + return strpos( $id, CustomIds::CUSTOMER_ID_PREFIX ) === 0; + } + ) + ); + return array_map( + function ( string $str ): string { + return (string) substr( $str, strlen( CustomIds::CUSTOMER_ID_PREFIX ) ); + }, + $customer_ids ); }