Repository navigation
WPS Migration: address settings parity - #60017
Conversation
Testing GuidelinesHi @annemirasol , Apart from reviewing the code changes, please make sure to review the testing instructions (Guide) and verify that relevant tests (E2E, Unit, Integration, etc.) have been added or updated as needed. Reminder: PR reviewers are required to document testing performed. This includes:
|
There was a problem hiding this comment.
Pull Request Overview
This PR implements address settings parity between the legacy PayPal gateway and the new PayPal Orders v2 API migration, ensuring consistent behavior for shipping details and address override options.
- Adds Orders v2 API support with address handling that matches legacy behavior
- Introduces webhook handling for PayPal payment events
- Conditionally hides API credential settings when using Orders v2
Reviewed Changes
Copilot reviewed 10 out of 10 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
class-wc-gateway-paypal.php |
Modified payment processing to support Orders v2 API and conditionally route between legacy and new flows |
class-wc-gateway-paypal-request.php |
Added Orders v2 API methods for creating orders and capturing payments with proper address handling |
class-wc-gateway-paypal-helper.php |
Added helper class to determine whether to use Orders v2 API |
settings-paypal.php |
Modified settings to conditionally hide API credentials when using Orders v2 |
class-wc-gateway-paypal-webhook-handler.php |
Added webhook event handling for PayPal payment events |
| REST API controllers | Added proxy and webhook controllers for testing PayPal integration |
| Test file | Added unit tests for PayPal request functionality |
📝 WalkthroughWalkthroughThis update introduces PayPal Orders v2 API support to WooCommerce's PayPal gateway. It adds new REST API controllers and webhook handlers for PayPal event processing, implements a helper class for API selection logic, and extends the gateway's payment processing flow. Corresponding unit tests and settings logic are also updated, with several new files and significant method additions. Changes
Sequence Diagram(s)sequenceDiagram
participant Customer
participant WooCommerce
participant PayPal
participant WebhookHandler
Customer->>WooCommerce: Initiate payment
WooCommerce->>PayPal: Create PayPal Order (Orders v2 API)
PayPal-->>WooCommerce: Order ID & Approval URL
WooCommerce->>Customer: Redirect to Approval URL
Customer->>PayPal: Approve payment
PayPal->>WebhookHandler: Send CHECKOUT.ORDER.APPROVED webhook
WebhookHandler->>WooCommerce: Capture Payment via PayPal API
PayPal-->>WebhookHandler: PAYMENT.CAPTURE.COMPLETED webhook
WebhookHandler->>WooCommerce: Mark order as paid
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Note ⚡️ Unit Test Generation is now available in beta!Learn more here, or try it out under "Finishing Touches" below. ✨ Finishing Touches
🧪 Generate unit tests
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
Documentation and Community
|
There was a problem hiding this comment.
Actionable comments posted: 20
🧹 Nitpick comments (9)
plugins/woocommerce/includes/gateways/paypal/includes/class-wc-gateway-paypal-helper.php (1)
25-28: TODO: Implement merchant eligibility checksThe TODO comment indicates that merchant eligibility logic (checking if merchant needs API keys and ToS acceptance) is not yet implemented. This is important for the migration flow.
Would you like me to help implement the merchant eligibility checks or create an issue to track this requirement?
plugins/woocommerce/includes/rest-api/Controllers/Version3/class-wc-rest-paypal-proxy-controller.php (1)
116-116: Security: Validate PayPal-Request-Id formatThe PayPal-Request-Id uses uniqid() which may not provide sufficient entropy for idempotency keys.
-'PayPal-Request-Id' => uniqid(), // A unique ID for idempotency (recommended by PayPal). +'PayPal-Request-Id' => wp_generate_uuid4(), // A unique ID for idempotency (recommended by PayPal).plugins/woocommerce/tests/php/includes/gateways/paypal/class-wc-gateway-paypal-request-test.php (3)
13-15: Fix class documentation mismatchThe class documentation refers to
WC_Gateway_Paypal_Testbut the actual class name isWC_Gateway_Paypal_Request_Test./** - * Class WC_Gateway_Paypal_Test. + * Class WC_Gateway_Paypal_Request_Test. */
70-70: Remove or use the unused $result variableThe static analysis correctly identifies that
$resultis assigned but never used in the test.-$result = $request->create_paypal_order( $order ); +$request->create_paypal_order( $order );
1-157: Consider adding more test coverageThe tests cover basic success/error cases and parameter validation, but could benefit from additional scenarios such as:
- Testing with different payment actions (authorize vs capture)
- Testing with different shipping scenarios
- Testing edge cases like very long order descriptions
Would you like me to generate additional test cases to improve coverage?
plugins/woocommerce/includes/gateways/paypal/includes/class-wc-gateway-paypal-request.php (4)
109-110: Address TODO comments about wpcom endpointsMultiple TODO comments indicate these endpoints should be replaced with wpcom endpoints. This needs to be completed before production deployment.
Would you like assistance in implementing the wpcom endpoint integration?
Also applies to: 174-175, 242-243, 252-253
360-360: Remove unused parameters in method callStatic analysis correctly identifies unused parameters in the get_item_subtotal call.
-'value' => $order->get_item_subtotal( $item, $include_tax = false, $rounding_enabled = false ), +'value' => $order->get_item_subtotal( $item, false, false ),
403-403: Fix missing space in function parameterMissing space after parameter name in function declaration.
-private function get_paypal_shipping_preference( $order) { +private function get_paypal_shipping_preference( $order ) {
142-142: Avoid null return in error casesReturning null on error makes it harder for calling code to distinguish between different error conditions.
Consider throwing specific exceptions or returning a WP_Error object instead:
-return null; +return new WP_Error( 'paypal_order_creation_failed', $e->getMessage() );
📜 Review details
Configuration used: .coderabbit.yml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (10)
plugins/woocommerce/includes/gateways/paypal/class-wc-gateway-paypal.php(4 hunks)plugins/woocommerce/includes/gateways/paypal/includes/class-wc-gateway-paypal-helper.php(1 hunks)plugins/woocommerce/includes/gateways/paypal/includes/class-wc-gateway-paypal-request.php(3 hunks)plugins/woocommerce/includes/gateways/paypal/includes/class-wc-gateway-paypal-webhook-handler.php(1 hunks)plugins/woocommerce/includes/gateways/paypal/includes/settings-paypal.php(3 hunks)plugins/woocommerce/includes/rest-api/Controllers/Version3/class-wc-rest-paypal-proxy-controller.php(1 hunks)plugins/woocommerce/includes/rest-api/Controllers/Version3/class-wc-rest-paypal-webhooks-controller.php(1 hunks)plugins/woocommerce/includes/rest-api/Controllers/Version3/class-wc-rest-paypal-webhooks-proxy-controller.php(1 hunks)plugins/woocommerce/includes/rest-api/Server.php(1 hunks)plugins/woocommerce/tests/php/includes/gateways/paypal/class-wc-gateway-paypal-request-test.php(1 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
**/*.{php,js,jsx,ts,tsx}
📄 CodeRabbit Inference Engine (.cursor/rules/code-quality.mdc)
**/*.{php,js,jsx,ts,tsx}: Guard against unexpected inputs
Sanitize and validate any potentially dangerous inputs
Ensure code is backwards compatible
Write code that is readable and intuitive
Ensure code has unit or E2E tests where applicable
Files:
plugins/woocommerce/includes/rest-api/Server.phpplugins/woocommerce/includes/gateways/paypal/includes/class-wc-gateway-paypal-helper.phpplugins/woocommerce/includes/gateways/paypal/includes/settings-paypal.phpplugins/woocommerce/includes/rest-api/Controllers/Version3/class-wc-rest-paypal-webhooks-proxy-controller.phpplugins/woocommerce/includes/rest-api/Controllers/Version3/class-wc-rest-paypal-webhooks-controller.phpplugins/woocommerce/includes/rest-api/Controllers/Version3/class-wc-rest-paypal-proxy-controller.phpplugins/woocommerce/includes/gateways/paypal/class-wc-gateway-paypal.phpplugins/woocommerce/tests/php/includes/gateways/paypal/class-wc-gateway-paypal-request-test.phpplugins/woocommerce/includes/gateways/paypal/includes/class-wc-gateway-paypal-webhook-handler.phpplugins/woocommerce/includes/gateways/paypal/includes/class-wc-gateway-paypal-request.php
**/*.{php,js,ts,jsx,tsx}
⚙️ CodeRabbit Configuration File
**/*.{php,js,ts,jsx,tsx}: Don't trust that extension developers will follow the best practices, make sure the code:
- Guards against unexpected inputs.
- Sanitizes and validates any potentially dangerous inputs.
- Is backwards compatible.
- Is readable and intuitive.
- Has unit or E2E tests where applicable.
Files:
plugins/woocommerce/includes/rest-api/Server.phpplugins/woocommerce/includes/gateways/paypal/includes/class-wc-gateway-paypal-helper.phpplugins/woocommerce/includes/gateways/paypal/includes/settings-paypal.phpplugins/woocommerce/includes/rest-api/Controllers/Version3/class-wc-rest-paypal-webhooks-proxy-controller.phpplugins/woocommerce/includes/rest-api/Controllers/Version3/class-wc-rest-paypal-webhooks-controller.phpplugins/woocommerce/includes/rest-api/Controllers/Version3/class-wc-rest-paypal-proxy-controller.phpplugins/woocommerce/includes/gateways/paypal/class-wc-gateway-paypal.phpplugins/woocommerce/tests/php/includes/gateways/paypal/class-wc-gateway-paypal-request-test.phpplugins/woocommerce/includes/gateways/paypal/includes/class-wc-gateway-paypal-webhook-handler.phpplugins/woocommerce/includes/gateways/paypal/includes/class-wc-gateway-paypal-request.php
🪛 PHPMD (2.15.0)
plugins/woocommerce/includes/rest-api/Controllers/Version3/class-wc-rest-paypal-webhooks-controller.php
81-81: Avoid unused local variables such as '$data'. (Unused Code Rules)
(UnusedLocalVariable)
plugins/woocommerce/tests/php/includes/gateways/paypal/class-wc-gateway-paypal-request-test.php
70-70: Avoid unused local variables such as '$result'. (Unused Code Rules)
(UnusedLocalVariable)
124-124: Avoid unused parameters such as '$value'. (Unused Code Rules)
(UnusedFormalParameter)
124-124: Avoid unused parameters such as '$parsed_url'. (Unused Code Rules)
(UnusedFormalParameter)
152-152: Avoid unused parameters such as '$value'. (Unused Code Rules)
(UnusedFormalParameter)
152-152: Avoid unused parameters such as '$parsed_url'. (Unused Code Rules)
(UnusedFormalParameter)
plugins/woocommerce/includes/gateways/paypal/includes/class-wc-gateway-paypal-request.php
360-360: Avoid unused local variables such as '$include_tax'. (Unused Code Rules)
(UnusedLocalVariable)
360-360: Avoid unused local variables such as '$rounding_enabled'. (Unused Code Rules)
(UnusedLocalVariable)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (24)
- GitHub Check: Blocks e2e tests 8/10 - @woocommerce/plugin-woocommerce [e2e]
- GitHub Check: Blocks e2e tests 10/10 - @woocommerce/plugin-woocommerce [e2e]
- GitHub Check: Blocks e2e tests 7/10 - @woocommerce/plugin-woocommerce [e2e]
- GitHub Check: Blocks e2e tests 9/10 - @woocommerce/plugin-woocommerce [e2e]
- GitHub Check: Blocks e2e tests 2/10 - @woocommerce/plugin-woocommerce [e2e]
- GitHub Check: Blocks e2e tests 5/10 - @woocommerce/plugin-woocommerce [e2e]
- GitHub Check: Blocks e2e tests 6/10 - @woocommerce/plugin-woocommerce [e2e]
- GitHub Check: Blocks e2e tests 4/10 - @woocommerce/plugin-woocommerce [e2e]
- GitHub Check: Blocks e2e tests 1/10 - @woocommerce/plugin-woocommerce [e2e]
- GitHub Check: Metrics - @woocommerce/plugin-woocommerce [performance]
- GitHub Check: Blocks e2e tests 3/10 - @woocommerce/plugin-woocommerce [e2e]
- GitHub Check: Core API tests - @woocommerce/plugin-woocommerce [api]
- GitHub Check: Core e2e tests 6/6 - @woocommerce/plugin-woocommerce [e2e]
- GitHub Check: PHP: 7.4 WP: latest - 1 [WP 6.7.2] 2/2 - @woocommerce/plugin-woocommerce [unit:php]
- GitHub Check: Core e2e tests 5/6 - @woocommerce/plugin-woocommerce [e2e]
- GitHub Check: PHP: 8.4 WP: latest [WP latest] 2/2 - @woocommerce/plugin-woocommerce [unit:php]
- GitHub Check: Core e2e tests 4/6 - @woocommerce/plugin-woocommerce [e2e]
- GitHub Check: Core e2e tests 2/6 - @woocommerce/plugin-woocommerce [e2e]
- GitHub Check: Core e2e tests 3/6 - @woocommerce/plugin-woocommerce [e2e]
- GitHub Check: Core e2e tests 1/6 - @woocommerce/plugin-woocommerce [e2e]
- GitHub Check: PHP: 8.4 WP: latest [WP latest] 1/2 - @woocommerce/plugin-woocommerce [unit:php]
- GitHub Check: PHP: 7.4 WP: latest - 1 [WP 6.7.2] 1/2 - @woocommerce/plugin-woocommerce [unit:php]
- GitHub Check: Lint - @woocommerce/plugin-woocommerce
- GitHub Check: build
🔇 Additional comments (5)
plugins/woocommerce/includes/gateways/paypal/includes/settings-paypal.php (1)
139-197: Good implementation of conditional API settingsThe conditional inclusion of API credential settings based on Orders v2 usage is well implemented. This ensures merchants using the new API don't see unnecessary legacy settings.
plugins/woocommerce/includes/gateways/paypal/class-wc-gateway-paypal.php (2)
37-37: Good implementation of lazy logging initializationThe change from
falsetonullfor$log_enabledand the lazy initialization in thelog()method is a good optimization that avoids loading settings unnecessarily.Also applies to: 162-165
379-394: Well-implemented Orders v2 API integrationThe integration properly:
- Uses the helper class for consistency
- Validates the PayPal order response
- Saves the PayPal order ID as metadata
- Provides user-friendly error messages
- Falls back to legacy flow when Orders v2 is disabled
plugins/woocommerce/includes/gateways/paypal/includes/class-wc-gateway-paypal-webhook-handler.php (1)
142-142: Validate filter_var return value
filter_varwith FILTER_VALIDATE_URL can return false, which should be checked before using the URL.-if ( 'capture' === $link['rel'] && 'POST' === $link['method'] && filter_var( $link['href'], FILTER_VALIDATE_URL ) ) { +$validated_url = filter_var( $link['href'], FILTER_VALIDATE_URL ); +if ( 'capture' === $link['rel'] && 'POST' === $link['method'] && $validated_url !== false ) { - $capture_url = esc_url_raw( $link['href'] ); + $capture_url = esc_url_raw( $validated_url );Likely an incorrect or invalid review comment.
plugins/woocommerce/includes/gateways/paypal/includes/class-wc-gateway-paypal-request.php (1)
327-341: Good implementation of custom ID validationThe code properly validates the custom ID length to ensure it doesn't exceed PayPal's 255 character limit.
Test using WordPress PlaygroundThe changes in this pull request can be previewed and tested using a WordPress Playground instance. Test this pull request with WordPress Playground. Note that this URL is valid for 30 days from when this comment was last updated. You can update it by closing/reopening the PR or pushing a new commit. |
|
@annemirasol you can ignore all the AI comments. I mistakenly requested for a review before changing the base branch. So all the comments (except one) are irrelevant to this PR. |
annemirasol
left a comment
There was a problem hiding this comment.
Works as described, LGTM!
Note: address_override was confusing. Maybe later we should consider rephrasing the label, or adding a better explanation.
Note: There's one more linting error.
That's true. I also had a hard time understanding it. The opposite logic seems more intuitive. |
* Add proxy layer for testing * WPS migration: implement Orders v2 approval phase (#59860) * WPS migration: specify user_action field for order creation (#59942) Specify user action as PAY_NOW * Update paypal settings (#59943) * WPS Migration: Capture payment (#59945) * WPS Migration: Set invoice id for transaction details (#59996) * WPS Migration: Add timeout and capture condtion (#60027) * WPS Migration: address settings parity (#60017) * WPS migration: include testmode flag in requests (#60041) * WPS migration: site registration with WPCOM (#59950) * WPS Migration: Settings refactor (#60082) * WPS Migration: Authorize payment (#60044) * WPS Migration: Fix capture failure (#60136) * WPS Migration: Hide 'Pay' button on order received page (#60114) * WPS Migration: Check PayPal payment status before sending capture request (#60242) check status before sending capture request * WPS Migration: Update PayPal `custom_id` (#60308) * WPS Migration: validate webhooks (#60322) * add blog_id in custom data * update proxy webhook to generate endpoint from site url * jp check * remove test changes * validate webhook * validate test webhook * remove backslash * check if class & method exists * fix return and log * WPS migration: Transact onboarding (#60238) * Add Transact merchant onboarding * Add Transact provider onboarding * Handle onboarding responses and set local flag * Include PayPal account email in onboarding request * Update is_transact_onboarded to use yes/no values * Update should_use_orders_v2() check * Move stuff to manager class, add caching * Feedback: remove unnecessary store_url and statement_descriptor * Remove error logs and fix phpcs errors * Wrap API call in helper method; make class final for security * Add unit tests * Remove changes unrelated to branch * Remove email from merchant onboarding request and response * Fix onboarding logic, limit trigger * Check use_orders_v2 filter before onboarding * Feedback: fix GET request body * Remove unused libraries Co-authored-by: Mayisha <33387139+Mayisha@users.noreply.github.com> * Fix class docblock typo Co-authored-by: Mayisha <33387139+Mayisha@users.noreply.github.com> * Add gateway type in constructor param Co-authored-by: Mayisha <33387139+Mayisha@users.noreply.github.com> * Revert changes to API loaders * Use already available response data Co-authored-by: Mayisha <33387139+Mayisha@users.noreply.github.com> * Remove unused libraries in test Co-authored-by: Mayisha <33387139+Mayisha@users.noreply.github.com> * Move onboarding trigger to plugin update and settings save * Cache merchant and provider data on onboarding complete * Fix onboarding status checking, caching --------- Co-authored-by: Mayisha <33387139+Mayisha@users.noreply.github.com> * WPS migration: PayPal gateway singleton (#60440) * Implement singleton for WC_Gateway_Paypal * Fix unit tests * Remove unnecessary changes to unit test * WPS migration: authenticated requests to proxy (#60065) * Handle 201 in capture/authorize payment * Use Jetpack for authenticated requests to proxy * Update proxy endpoints * Fix unit tests * WPS MIgration: Use `experiece_context` instead of `application_context` (#60478) * Delete temp proxy * Remove is_tos_accepted() checks * Remove unused methods * Fix unit test pre_http_request filter arg count * Use wc_format_decimal() and wc_get_price_decimals() * Fix WC_Gateway_Paypal_Request_Test unit tests * Remove webhooks controller test route and method * Check for json_decode() failure * Also accept 201 for capture authorized payment * Use wc_print_r when logging response array * Fix gateway check * Remove old TODO re webhook validation * Add check for invalid webhook payload * WPS migration: fix onboarding plugin update trigger (#60617) * Use woocommerce_updated action for onboarding trigger * Fix is enabled check * WPS Migration: Note PayPal debug ID (#60558) * WPS migration: fix loading and hiding of legacy settings (#60682) * Make merchant and provider cache getters consistent with network getters * Remove invalid HTML in order note * Add check for missing approval link * Perform URL validation on the request method side * Sanitize payee email first * Limit order item name, format decimals for item price * Avoid unnecessary option write for transact_onboarding_complete flag * Include 201 in possible PAYER_ACTION_REQUIRED status * Tweak some comments * Move item name max length to a constant * Add default off feature flag * Add changelog * Fix unit tests * WPS Migration: Add PayPal constants (#60895) * add safety checks for keys * extract get_action_url method * consolidate get account from cache and update account in cache methods * create a common get_transact_account_data method * get cache key if param is empty * Unify cache key logic and update unit tests --------- Co-authored-by: Mayisha <mayisha.alam@automattic.com>
Submission Review Guidelines:
Changes proposed in this Pull Request:
This PR achieves feature parity for
Shipping DetailsandAddress Overrideoptions in the PayPal settings page.Shipping Details: If this box is checked (yes) we send the shipping address to PayPal, otherwise we send the billing addressAddress override: If this box is checked (yes), address sent from the merchant site is used as the shipping address. Shopper can not select another address from the PP modal.Towards PAYPAL-46
How to test the changes in this Pull Request:
Basic setup
Testing the changes
Shipping detailsandAddress override. Following is a table of expected behavior based on the current behavior in WPS.Note If you change and select a different shipping address on the PP modal, it does not reflect on the store order. The WC order still shows the shipping address from the checkout page. Though this is also the current behavior in legacy, we should fix it at some point.
Testing that has already taken place:
Changelog entry
Changelog Entry Details
Significance
Type
Message
Changelog Entry Comment
Comment