Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework onboarding code and add REST controller for integration with the OBW #121

Merged
merged 5 commits into from
Feb 16, 2021

Conversation

jorgeatorres
Copy link
Member

@jorgeatorres jorgeatorres commented Feb 4, 2021

Issue: woocommerce/woocommerce-admin#5788


Description

This PR refactors some of the code in the on-boarding module in order to expose certain internal details that are needed by WooCommerce's onboarding wizard. Also, in order to workaround several limitations in PayPal's partner.js (such as the inability to trigger the popup for dynamically loaded buttons, certain issues with Firefox, etc.), onboarding.js was heavily modified too.

To permit integration with WooCommerce's onboarding wizard, a REST controller was also added. In particular, this controller exposes the following routes:

  • /wc-paypal/v1/onboarding/get-params [POST]
    Returns information useful for presenting the user with a sign-up link/button.

    • Parameters (JSON):
      • environment: either 'sandbox' or 'production'. Optional. Defaults to 'sandbox'.
    • Response (JSON):
      • environment: Environment for which the parameters apply.
      • scriptURL: URL to the onboarding.js script.
      • scriptData: Data required by the onboarding.js script. Should be added to the PayPalCommerceGatewayOnboarding variable in the global context.
      • onboardCompleteCallback: Value to be used for the data-paypal-onboard-complete attribute in the sign-up link or button.
      • signupLink: Generated sign-up link. Final redirect URL defaults to the settings page.
  • /wc-paypal/v1/onboarding/get-status [GET]
    Returns information about the current onboarding status.

    • Parameters (JSON): N/A.
    • Response (JSON):
      • environment: Current environment.
      • onboarded: true if onboarded was completed for the current environment.
      • state: Textual representation of the current onboarding status ("start", "progressive", "onboarded").
      • sandbox.onboarded: Same as onboarded but for the "sandbox" environment in particular.
      • sandbox.state: Same as state but for the "sandbox" environment in particular.
      • production.onboarded: Same as onboarded but for the "production" environment in particular.
      • production.state: Same as state but for the "production" environment in particular.
  • /wc-paypal/v1/onboarding/set-credentials [POST]
    Allows setting credentials for a given environment and also enables the gateway.

    • Parameters (JSON):
      • environment: Environment the credentials apply to. Required.
      • merchant_id: Merchant ID. Required.
      • merchant_email: Merchant e-mail. Required.
      • client_id: API Client ID. Required.
      • client_secret: API Client Secret. Required.
    • Response (JSON): Empty or error in case of error (missing fields etc.).

Steps to test:

Make sure the usual onboarding flow works correctly

  1. Go to WC > Settings > Payments > PayPal.
  2. Disconnect (if connected).
  3. Click "Connect to PayPal" or "Connect to PayPal Sandbox" (depending on whether you have sandbox enabled or not).
  4. A popup should appear and you should be able to complete the onboarding flow.
  5. After clicking to return to the site at the final step of the process, you should be back at the settings screen, with all credential fields correctly filled out.
  6. Perform a test purchase from the frontend to confirm.
  7. (Optional) Repeat the above but for the other mode (sandbox/production).

Test the REST endpoints

  1. Go to WC > Settings > Advanced > REST API and create an API key for testing. It should be for a user with sufficient access (i.e. "install_plugins" capability).

  2. Keep key and secret handy.

  3. Make sure the endpoints work correctly. Test with different inputs. Some example requests:

    cURL command Expected output
    curl -X GET https://SITE/wp-json/wc-paypal/v1/onboarding/get-status -u API_KEY:API_SECRET Onboarding status as described above
    curl -X POST https://SITE/wp-json/wc-paypal/v1/onboarding/get-params -u API_KEY:API_SECRET Onboarding parameters (signup link, environment, etc.) as describe above
    curl -X POST https://SITE/wp-json/wc-paypal/v1/onboarding/set-credentials -u API_KEY:API_SECRET -H 'content-type: application/json' -d '{"environment":"whatever"}' Error due to invalid environment.
    curl -X POST https://SITE/wp-json/wc-paypal/v1/onboarding/set-credentials -u API_KEY:API_SECRET -H 'content-type: application/json' -d '{"environment":"sandbox","merchant_id":"MERCHANTID","client_id":"CLIENTID"}' Error due to missing fields "merchant_email" and "client_secret".
    curl -X POST https://SITE/wp-json/wc-paypal/v1/onboarding/set-credentials -u API_KEY:API_SECRET -H 'content-type: application/json' -d '{"environment":"sandbox","merchant_id":"MERCHANTID","merchant_email":"MERCHANTEMAIL","client_id":"CLIENTID","client_secret":"CLIENTSECRET"}' No error. Confirm credentials were set on the settings screen.

…he onboarding flow

- Adds action `woocommerce_paypal_payments_onboarding_before_redirect`
- Adds filter `woocommerce_paypal_payments_onboarding_redirect_url`
- Add `ppcp_onboarding` object and separate sandbox/production callbacks.
- Prevent errors when loading `onboarding.js` outside of the settings screen.
- Workaround PayPal’s partner.js shortcomings.
@jorgeatorres jorgeatorres force-pushed the obw-work branch 2 times, most recently from 78a61eb to 57fdd97 Compare February 4, 2021 20:45
Adds various routes under `/wc-paypal/v1/onboarding` including `/status` (onboarding status), `/set-credentials` (manually set merchant ID/email and keys) and `/get-params` (information useful for presenting the user with an onboarding button).
@chickenn00dle
Copy link
Contributor

Nice work @jorgeatorres!

I tested the onboarding flow for sandbox and production and there were no issues. I also tested each endpoint individually and everything checks out.

Since @mattallan mentioned he'd also review this, I won't merge so he can also have a look.

@mattallan
Copy link
Contributor

Nice one @jorgeatorres! I've tested all those API endpoints as well as the normal onboarding flow and it's all working perfectly from my end.

This is good to merge ! 💯

Copy link
Contributor

@strangerkir strangerkir left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

'woocommerce-paypal-payments'
'return_url_description' => apply_filters(
'woocommerce_paypal_payments_partner_config_override_return_url_description',
__( 'Return to your shop.', 'woocommerce-paypal-payments' )
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably, it makes sense to use esc_html__ instead of just __.

@danielhuesken danielhuesken removed the request for review from Dinamiko February 11, 2021 09:50
@jorgeatorres
Copy link
Member Author

Thank you all for the reviews! Merging now.

@jorgeatorres jorgeatorres merged commit 2d15078 into trunk Feb 16, 2021
@jorgeatorres jorgeatorres deleted the obw-work branch February 16, 2021 13:53
@Dinamiko Dinamiko mentioned this pull request Mar 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants