diff --git a/.gitignore b/.gitignore index 7031cb6d..4a7d1dc0 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,14 @@ deploy-to-wp-org.sh # Test configs tests/e2e/config/local-* + +# Local config +local.env + +# Volumes mounted by Docker containers +docker/data +docker/wordpress +docker/logs + +# Tunneling scripts +/docker/bin/jt diff --git a/README.md b/README.md index 48159393..47e9e82a 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,10 @@ ## Getting Started +If Docker is your friend, please follow the instructions [here](https://github.com/Automattic/woocommerce-payments/blob/trunk/docker/README.md) to setup up the local Docker containers to manage all the services required to run this project. + +Otherwise, if you prefer doing things the hard way, please follow the instructions below. + 1. Clone this repository locally within the plugins directory of WordPress. 2. Go to your local installation, and activate this plugin. 3. Signup or sign into your [PayPal developer account]( https://developer.paypal.com) and [create buyer and seller accounts](https://developer.paypal.com/docs/payflow/express-checkout/testing/#create-paypal-sandbox-seller-and-buyer-accounts). diff --git a/bin/docker-setup.sh b/bin/docker-setup.sh new file mode 100755 index 00000000..7250730a --- /dev/null +++ b/bin/docker-setup.sh @@ -0,0 +1,135 @@ +#!/bin/bash + +# Exit if any command fails. +set -e + +WP_CONTAINER=${1-woocommerce_paypal_checkout_wordpress} +SITE_URL=${WP_URL-"localhost:8082"} + +redirect_output() { + if [[ -z "$DEBUG" ]]; then + "$@" > /dev/null + else + "$@" + fi +} + +# --user xfs forces the wordpress:cli container to use a user with the same ID as the main wordpress container. See: +# https://hub.docker.com/_/wordpress#running-as-an-arbitrary-user +cli() +{ + INTERACTIVE='' + if [ -t 1 ] ; then + INTERACTIVE='-it' + fi + redirect_output docker run $INTERACTIVE --env-file default.env --rm --user xfs --volumes-from $WP_CONTAINER --network container:$WP_CONTAINER wordpress:cli "$@" +} + +set +e +# Wait for containers to be started up before the setup. +# The db being accessible means that the db container started and the WP has been downloaded and the plugin linked +cli wp db check --path=/var/www/html --quiet > /dev/null +while [[ $? -ne 0 ]]; do + echo "Waiting until the service is ready..." + sleep 5s + cli wp db check --path=/var/www/html --quiet > /dev/null +done + +# If the plugin is already active then return early +cli wp plugin is-active woocommerce-gateway-paypal-express-checkout > /dev/null +if [[ $? -eq 0 ]]; then + set -e + echo + echo "PayPal Checkout is installed and active" + echo "SUCCESS! You should now be able to access http://${SITE_URL}/wp-admin/" + echo "You can login by using the username and password both as 'admin'" + exit 0 +fi + +set -e + +echo +echo "Setting up environment..." +echo + +echo "Pulling the WordPress CLI docker image..." +docker pull wordpress:cli > /dev/null + +echo "Setting up WordPress..." +cli wp core install \ + --path=/var/www/html \ + --url=$SITE_URL \ + --title=${SITE_TITLE-"WooCommerce PayPal Checkout Dev"} \ + --admin_name=${WP_ADMIN-admin} \ + --admin_password=${WP_ADMIN_PASSWORD-admin} \ + --admin_email=${WP_ADMIN_EMAIL-admin@example.com} \ + --skip-email + +echo "Updating WordPress to the latest version..." +cli wp core update --quiet + +echo "Updating the WordPress database..." +cli wp core update-db --quiet + +echo "Configuring WordPress to work with ngrok (in order to allow creating a Jetpack-WPCOM connection)"; +cli config set DOCKER_HOST "\$_SERVER['HTTP_X_ORIGINAL_HOST'] ?? \$_SERVER['HTTP_HOST'] ?? 'localhost'" --raw +cli config set DOCKER_REQUEST_URL "( ! empty( \$_SERVER['HTTPS'] ) ? 'https://' : 'http://' ) . DOCKER_HOST" --raw +cli config set WP_SITEURL DOCKER_REQUEST_URL --raw +cli config set WP_HOME DOCKER_REQUEST_URL --raw + +echo "Enabling WordPress debug flags" +cli config set WP_DEBUG true --raw +cli config set WP_DEBUG_DISPLAY true --raw +cli config set WP_DEBUG_LOG true --raw +cli config set SCRIPT_DEBUG true --raw + +echo "Enabling WordPress development environment (enforces Stripe testing mode)"; +cli config set WP_ENVIRONMENT_TYPE development + +echo "Updating permalink structure" +cli wp rewrite structure '/%postname%/' + +echo "Installing and activating WooCommerce..." +cli wp plugin install woocommerce --activate + +echo "Installing and activating Storefront theme..." +cli wp theme install storefront --activate + +echo "Adding basic WooCommerce settings..." +cli wp option set woocommerce_store_address "60 29th Street" +cli wp option set woocommerce_store_address_2 "#343" +cli wp option set woocommerce_store_city "San Francisco" +cli wp option set woocommerce_default_country "US:CA" +cli wp option set woocommerce_store_postcode "94110" +cli wp option set woocommerce_currency "USD" +cli wp option set woocommerce_product_type "both" +cli wp option set woocommerce_allow_tracking "no" + +echo "Importing WooCommerce shop pages..." +cli wp wc --user=admin tool run install_pages + +echo "Installing and activating the WordPress Importer plugin..." +cli wp plugin install wordpress-importer --activate + +echo "Importing some sample data..." +cli wp import wp-content/plugins/woocommerce/sample-data/sample_products.xml --authors=skip + +echo "Activating the WooCommerce PayPal Checkout plugin..." +cli wp plugin activate woocommerce-gateway-paypal-express-checkout + +if [ -f local.env ]; then + export $(grep -v '^#' local.env | xargs) + + if [[ "0" == "$(cli wp option list --search=woocommerce_ppec_paypal_settings --format=count)" ]]; then + echo "Creating WooCommerce PayPal Checkout settings" + cli wp option add woocommerce_ppec_paypal_settings --format=json "{\"enabled\":\"yes\",\"environment\":\"sandbox\",\"sandbox_api_username\":\"$PAYPAL_SANDBOX_API_USERNAME\",\"sandbox_api_password\":\"$PAYPAL_SANDBOX_API_PASSWORD\",\"sandbox_api_signature\":\"$PAYPAL_SANDBOX_API_SIGNATURE\"}" + else + echo "Updating WooCommerce PayPal Checkout settings" + cli wp option update woocommerce_ppec_paypal_settings --format=json "{\"enabled\":\"yes\",\"environment\":\"sandbox\",\"sandbox_api_username\":\"$PAYPAL_SANDBOX_API_USERNAME\",\"sandbox_api_password\":\"$PAYPAL_SANDBOX_API_PASSWORD\",\"sandbox_api_signature\":\"$PAYPAL_SANDBOX_API_SIGNATURE\"}" + fi +fi + +echo +echo "PayPal Checkout is installed and active" +echo "SUCCESS! You should now be able to access http://${SITE_URL}/wp-admin/" +echo "You can login by using the username and password both as 'admin'" diff --git a/bin/jurassic-tube-setup.sh b/bin/jurassic-tube-setup.sh new file mode 100755 index 00000000..1fa07db4 --- /dev/null +++ b/bin/jurassic-tube-setup.sh @@ -0,0 +1,48 @@ +#!/bin/bash + +# Exit if any command fails. +set -e + +echo "Checking if ${PWD}/docker/bin/jt directory exists..." + +if [ -d "${PWD}/docker/bin/jt" ]; then + echo "${PWD}/docker/bin/jt already exists." +else + echo "Creating ${PWD}/docker/bin/jt directory..." + mkdir "${PWD}/docker/bin/jt" +fi + +echo "Downloading the latest version of the installer script..." +echo + +# Download the installer (if it's not already present): +if [ ! -f "${PWD}/docker/bin/jt/installer.sh" ]; then + # Download the installer script: + curl "https://jurassic.tube/get-installer.php?env=wcpay" -o ${PWD}/docker/bin/jt/installer.sh && chmod +x ${PWD}/docker/bin/jt/installer.sh +fi + +echo "Running the installation script..." +echo + +# Run the installer script +source $PWD/docker/bin/jt/installer.sh + +echo +read -p "Go to https://jurassic.tube/ in a browser, paste your public key which was printed above into the box, and click 'Add Public Key'. Press enter to continue" +echo + +read -p "Go to https://jurassic.tube/ in a browser, add a subdomain using the desired name for your subdomain, and click 'Add Subdomain'. The subdomain name is what you will use to access WC PayPal Checkout in a browser. When this is done, type the subdomain name here and press enter. Please just type in the subdomain, not the full URL: " subdomain +echo + +# npm run wp option update home https://${subdomain}.jurassic.tube/ +# npm run wp option update siteurl https://${subdomain}.jurassic.tube/ + +read -p "Please enter your Automattic/WordPress.com username: " username +echo + +${PWD}/docker/bin/jt/config.sh username ${username} +${PWD}/docker/bin/jt/config.sh subdomain ${subdomain} + +echo "Setup complete!" +echo "Use the command: npm run tube:start from the root directory of your WC PayPal Checkouts project to start running Jurassic Tube." +echo diff --git a/default.env b/default.env new file mode 100644 index 00000000..0dbb6032 --- /dev/null +++ b/default.env @@ -0,0 +1,16 @@ +# WordPress +WORDPRESS_DB_HOST=db:3306 +WORDPRESS_DB_USER=wordpress +WORDPRESS_DB_PASSWORD=wordpress +WORDPRESS_DB_NAME=wordpress +WORDPRESS_DEBUG=1 +ABSPATH=/usr/src/wordpress/ + +# Database +MYSQL_ROOT_PASSWORD=wordpress +MYSQL_DATABASE=wordpress +MYSQL_USER=wordpress +MYSQL_PASSWORD=wordpress + +# WCPayPal +WCPAYPAL_DIR=/var/www/html/wp-content/plugins/woocommerce-gateway-paypal-express-checkout diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 00000000..3c4ce6d3 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,42 @@ +version: '3' + +volumes: + ## Kludge for not having the ./docker directory bound recursively + dockerdirectory: + +services: + wordpress: + build: ./docker/wordpress_xdebug + container_name: woocommerce_paypal_checkout_wordpress + image: woocommerce_paypal_checkout_wordpress + restart: always + depends_on: + - db + links: + - db:mysql + ports: + - "8082:80" + env_file: + - default.env + volumes: + - ./docker/wordpress:/var/www/html/ + - ./docker/logs/apache2/:/var/log/apache2 + - .:/var/www/html/wp-content/plugins/woocommerce-gateway-paypal-express-checkout + - dockerdirectory:/var/www/html/wp-content/plugins/woocommerce-gateway-paypal-express-checkout/docker + - ./docker/bin:/var/scripts + db: + container_name: woocommerce_paypal_checkout_mysql + image: mariadb:10.5.8 + ports: + - "5678:3306" + env_file: + - default.env + volumes: + - ./docker/data:/var/lib/mysql + phpMyAdmin: + container_name: woocommerce_paypal_checkout_phpmyadmin + image: phpmyadmin/phpmyadmin:latest + ports: + - "8083:80" + env_file: + - default.env diff --git a/docker/README.md b/docker/README.md new file mode 100644 index 00000000..52ed00f2 --- /dev/null +++ b/docker/README.md @@ -0,0 +1,61 @@ +# Setting up the Docker environment + +## Prerequisites + +### Docker +Obviously. + +### PayPal Developer Account +You will need to create or sign into a [PayPal developer account](https://developer.paypal.com/) and [create buyer and seller accounts](https://developer.paypal.com/docs/payflow/express-checkout/testing/#create-paypal-sandbox-seller-and-buyer-accounts) to use with this plugin. You will need to use the aforementioned PayPal Sandbox seller account credentials with your store in your site's settings for this plugin, while you will be able to use your buyer account credentials to make test purchases at checkout using this plugin. + +To load your Sandbox seller account credentials into the initialised Docker container for this site, create a `local.env` file in the route directory of this plugin including the following variables. + +``` +PAYPAL_SANDBOX_API_USERNAME=YOUR_SANDBOX_BUSINESS_ACCOUNT_API_USERNAME_HERE +PAYPAL_SANDBOX_API_PASSWORD=YOUR_SANDBOX_BUSINESS_ACCOUNT_API_PASSWORD_HERE +PAYPAL_SANDBOX_API_SIGNATURE=YOUR_SANDBOX_BUSINESS_ACCOUNT_API_SIGNATURE_HERE +``` + +## Getting started +To start up the local environment and create the containers for this plugin, run the following command. + +``` +npm run up +``` + +To bring the containers back down once you're finished, run this command. + +``` +npm run down +``` + +Your brand spanking new site should now be ready at http://localhost:8082 for you to use at your leisure. + +## Jurassic Tube (Optional) +If you would like to use or share a more stable URL as opposed to relying on `localhost`, there is an available [Jurassic Tube](https://fieldguide.automattic.com/jurassic-tube/) configuration for you to take advantage of. + +Once you have started the Docker containers for this plugin, simply run the below command to run the setup for Jurassic Tube and follow the instructions that will appear. + +``` +npm run tube:setup +``` + +Now to open up the tunnel to your Jurassic Tube URL, you can run `npm run tube:start` and you can close the tunnel as desired using `npm run tube:stop`. + +## Using the local environment + +### WordPress Admin +Open http://localhost:8082/wp-admin/ +``` +Username: admin +Password: admin +``` + +### Connecting to MySQL +Open phpMyAdmin at http://localhost:8083/, or connect using other MySQL clients with these credentials: +``` +Host: localhost +Port: 5678 +Username: wordpress +Password: wordpress +``` diff --git a/docker/wordpress_xdebug/Dockerfile b/docker/wordpress_xdebug/Dockerfile new file mode 100644 index 00000000..cc9a8dfd --- /dev/null +++ b/docker/wordpress_xdebug/Dockerfile @@ -0,0 +1,13 @@ +FROM wordpress:php7.3 +RUN pecl install xdebug-2.9.8 \ + && echo 'xdebug.remote_enable=1' >> $PHP_INI_DIR/php.ini \ + && echo 'xdebug.remote_port=9000' >> $PHP_INI_DIR/php.ini \ + && echo 'xdebug.remote_host=host.docker.internal' >> $PHP_INI_DIR/php.ini \ + && echo 'xdebug.remote_autostart=0' >> $PHP_INI_DIR/php.ini \ + && docker-php-ext-enable xdebug +RUN apt-get update \ + && apt-get install --assume-yes --quiet --no-install-recommends gnupg2 subversion mariadb-client less jq +RUN apt-get install -y openssh-client +RUN curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar \ + && chmod +x wp-cli.phar \ + && mv wp-cli.phar /usr/local/bin/wp diff --git a/package.json b/package.json index 83e1642e..3ec6771d 100644 --- a/package.json +++ b/package.json @@ -10,5 +10,12 @@ }, "config": { "wp_org_slug": "woocommerce-gateway-paypal-express-checkout" - } + }, + "scripts": { + "up": "docker-compose up --build --force-recreate -d && ./bin/docker-setup.sh", + "down": "docker-compose down", + "tube:setup": "./bin/jurassic-tube-setup.sh", + "tube:start": "./docker/bin/jt/tunnel.sh", + "tube:stop": "./docker/bin/jt/tunnel.sh break" + } }