Skip to content

Building and Testing Your System ๐Ÿ› ๏ธ

Now that you understand how the code works, let's get it running on your computer and test everything! We'll build and test locally first, then handle containerization and deployment in the next section.

Prerequisites โœ…

Before starting, make sure you have:

  • All accounts set up (AWS, Stripe, EMQX)
  • Python 3.9 or higher installed
  • Visual Studio Code (VSCode) installed
  • Git installed
  • Your code cloned from GitHub

Quick Check

Run these commands in your terminal to verify your setup:

python --version  # Should show 3.9 or higher
git --version    # Should show Git version

Part 1: Setting Up Your Development Environment ๐Ÿ’ป

1. Create Your Environment File

  1. In your project folder, create a new file called .env.local:

    # In VSCode, right-click in the explorer and select "New File"
    # Name it .env.local
    

  2. Add your configuration (replace with your actual values):

    STRIPE_API_KEY=sk_test_your_stripe_key
    STRIPE_WEBHOOK_SECRET=whsec_your_webhook_secret
    STRIPE_PRICE_ID=price_your_price_id
    MQTT_USERNAME=your_mqtt_username
    MQTT_PASSWORD=your_mqtt_password
    MQTT_BROKER=your.emqx.cloud.broker
    MQTT_PORT=8883
    

Keep It Secret!

Never commit your .env.local file to Git! It should already be in .gitignore.

2. Set Up Your Python Virtual Environment

Create and activate a virtual environment (open termninal in vs code):

python -m venv venv
.\venv\Scripts\activate
python3 -m venv venv
source venv/bin/activate

Install dependencies:

pip install -r requirements.txt

Part 2: Testing Your System ๐Ÿงช

Let's test each component to make sure everything works!

1. Test Local API

  1. Start the Flask application (you paste this into terminal and run the command):

    python app.py
    

  2. In a new terminal, test the status endpoint:

    curl http://localhost:5000/status
    

You should see:

{
  "status": "up",
  "message": "API is running"
}

2. Test MQTT Connection ๐Ÿ“ก

  1. Install the Mosquitto MQTT client:

Download from: https://mosquitto.org/download/

brew install mosquitto
sudo apt-get install mosquitto-clients
  1. Subscribe to test messages:

    mosquitto_sub -h your.emqx.cloud.broker -p 8883 \
      -t "arcade/machine/+/coinpulse" \
      -t "arcade/machine/+/gameover" \
      --cafile emqx.ca \
      -u "your_username" -P "your_password"
    

  2. Keep this terminal open to watch for messages!

3. Test Stripe Integration ๐Ÿ’ณ

  1. Install the Stripe CLI:

Download from: https://github.com/stripe/stripe-cli/releases/latest

brew install stripe/stripe-cli/stripe
# Download latest linux tar.gz from Stripe CLI releases
sudo tar -xvf stripe_X.X.X_linux_x86_64.tar.gz -C /usr/local/bin
  1. Login to Stripe CLI:

    stripe login
    

  2. Forward webhooks to your local app:

    stripe listen --forward-to localhost:5000/webhook
    

  3. Test payment flow:

    a. Create a payment link by running this command:

    curl -X POST http://localhost:5000/create-payment-link \
      -H "Content-Type: application/json" \
      -d '{"machine_id":"test123"}'
    

    You'll get back a response that looks like this:

    {
        "url": "https://checkout.stripe.com/c/pay/cs_test_..."
    }
    

    What is a Payment Link?

    A Stripe Payment Link is a hosted payment page that lets customers securely make payments. On your arcade cabinet, players will scan a QR code that leads to this page. Learn more about Stripe Payment Links.

    b. Copy the URL from the response and paste it into your web browser. You'll see Stripe's payment page, just like your arcade players will see when they scan the QR code!

    c. Complete the test payment using these card details:

    • Card number: 4242 4242 4242 4242
    • Expiry: Any future date
    • CVC: Any 3 digits
    • ZIP: Any 5 digits

    Test Cards

    The 4242 4242 4242 4242 card number is a special test card that Stripe provides. It will always work in test mode but won't work in production. Perfect for testing!

4. Verify Full Flow ๐Ÿ”„

Time to test everything together! You'll need three terminal windows:

python app.py
mosquitto_sub -h your.emqx.cloud.broker -p 8883 \
  -t "arcade/machine/+/coinpulse" \
  -t "arcade/machine/+/gameover" \
  --cafile emqx.ca \
  -u "your_username" -P "your_password"
stripe listen --forward-to localhost:5000/webhook

Now let's test the complete flow:

  1. Create and use a payment link:

    curl -X POST http://localhost:5000/create-payment-link \
      -H "Content-Type: application/json" \
      -d '{"machine_id":"test123"}'
    

  2. Complete the test payment in your browser

  3. Watch Terminal 2 for the MQTT coinpulse message

  4. Send a game over signal:

    curl -X POST http://localhost:5000/gameover \
      -H "Content-Type: application/json" \
      -d '{"machine_id":"test123"}'
    

  5. Watch Terminal 2 for the MQTT game over message

Troubleshooting Common Issues ๐Ÿ”ง

API Issues

  • Can't start Flask: Check if port 5000 is already in use
  • Environment variables: Make sure .env.local is in the right place
  • Import errors: Verify virtual environment is activated

MQTT Issues

  • Connection refused: Check broker address and port
  • Authentication failed: Verify username and password
  • SSL/TLS errors: Make sure you have the correct CA certificate

Stripe Issues

  • Invalid API key: Check your test API key in .env.local
  • Webhook errors: Ensure webhook forwarding is running
  • Payment fails: Verify you're using the correct test card

Success Criteria โœจ

Your local setup is working when you can:

  1. โœ… Get a successful response from the status endpoint
  2. โœ… Create a payment link
  3. โœ… Complete a test payment
  4. โœ… See the coinpulse MQTT message
  5. โœ… Send a game over signal
  6. โœ… See the game over MQTT message

Ready for Deployment!

Once you've verified all these steps, your system is working correctly and you're ready to move on to containerization and deployment!

Next Steps ๐Ÿ”œ

Once everything is tested and working:

  1. Save any test outputs you want to keep
  2. Stop all your test processes (Flask, Stripe CLI, MQTT subscriber)
  3. Make sure your changes are committed to Git
  4. Move on to the deployment section where we'll containerize and deploy your working code!

Continue to First Deployment โ†’ Need Help?