Skip to content

iZND/Payment-for-Vending-Machine

Repository files navigation

Here’s a full version of the guide you can copy and paste directly into your README.md. This document is structured and formatted for easy inclusion in your repository.


Raspberry Pi Setup for Payment Gateway and Automatic Dispensing

This guide explains how to set up a Raspberry Pi to interface with a payment gateway and trigger the dispensing process upon successful payment notifications (e.g., via QR code).


Prerequisites

Hardware

  1. Raspberry Pi (e.g., Raspberry Pi 3/4).
  2. MicroSD card (16GB or larger).
  3. Power supply for the Raspberry Pi.
  4. Monitor, keyboard, and mouse (for initial setup).
  5. PLC (Programmable Logic Controller) to control the dispensing system.
  6. Required cables/connectors for communication between Raspberry Pi and PLC.

Software

  1. Raspberry Pi OS installed on the microSD card.
  2. Node.js and npm.
  3. Stripe account for payment processing (or equivalent payment gateway).
  4. Stripe CLI for testing (or equivalent testing tools).

Step 1: Prepare the Raspberry Pi

Install Raspberry Pi OS

  1. Download the Raspberry Pi Imager from raspberrypi.com.
  2. Flash the Raspberry Pi OS onto the microSD card.
  3. Insert the microSD card into the Raspberry Pi and boot it up.

Initial Configuration

  1. Open the Raspberry Pi Configuration tool:
    sudo raspi-config
  2. Perform the following configurations:
    • Enable SSH for remote access.
    • Configure Wi-Fi or Ethernet settings.
    • Set the hostname (e.g., payment-vending-pi).
    • Enable interfaces like GPIO, SPI, or Serial if needed for PLC communication.

Update the System

Run the following commands to ensure the system is updated:

sudo apt update && sudo apt upgrade -y

Step 2: Install Required Software

Install Node.js and npm

Install Node.js and npm:

curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt install -y nodejs

Install Git

Install Git for version control and repository management:

sudo apt install -y git

Clone the Repository

Navigate to the home directory and clone the repository:

git clone https://github.com/iZND/Payment-for-Vending-Machine.git
cd Payment-for-Vending-Machine

Install Project Dependencies

Install the required Node.js packages:

npm install

Step 3: Configure the Webhook Handler

Set Up Environment Variables

  1. Create a .env file in the project directory:
    nano .env
  2. Add the following configurations (replace placeholders with actual keys):
    STRIPE_SECRET_KEY=your_stripe_secret_key
    STRIPE_WEBHOOK_SECRET=your_webhook_secret
    PLC_PROTOCOL=modbus  # Example: Replace with your PLC protocol

Test the Webhook Server

  1. Start the webhook handler server:
    node Webhook_handler-Script1.js
  2. Use the Stripe CLI to forward test events to the server:
    stripe listen --forward-to localhost:4242/webhook

Step 4: Connect Raspberry Pi to the PLC

Determine the Communication Protocol

  • Identify the protocol supported by your PLC (e.g., Modbus RTU, Modbus TCP, GPIO, or Ethernet).

Physical Connection

  • Connect the Raspberry Pi to the PLC using the appropriate interface (e.g., GPIO pins, serial connection, or Ethernet cable).

Install Protocol Libraries

  • If using Modbus, install the modbus-serial library:
    npm install modbus-serial

Implement PLC Signal Logic

  1. Edit the sendSignalToPLC function in the code to send signals to the PLC:
    const ModbusRTU = require('modbus-serial');
    const client = new ModbusRTU();
    
    function sendSignalToPLC(value) {
        client.connectRTUBuffered("/dev/ttyUSB0", { baudRate: 9600 })
            .then(() => client.writeRegister(1, value))
            .then(() => console.log('Signal sent to PLC'))
            .catch(console.error)
            .finally(() => client.close());
    }

Step 5: Automate the System

Run the Webhook Server on Boot

  1. Create a systemd service to run the webhook handler at startup:
    sudo nano /etc/systemd/system/payment-vending.service
  2. Add the following content:
    [Unit]
    Description=Payment Vending Webhook Service
    After=network.target
    
    [Service]
    ExecStart=/usr/bin/node /home/pi/Payment-for-Vending-Machine/Webhook_handler-Script1.js
    Restart=always
    User=pi
    Group=pi
    Environment=PATH=/usr/bin:/usr/local/bin
    Environment=NODE_ENV=production
    WorkingDirectory=/home/pi/Payment-for-Vending-Machine
    
    [Install]
    WantedBy=multi-user.target
  3. Enable and start the service:
    sudo systemctl enable payment-vending
    sudo systemctl start payment-vending

Monitor the Service

Check the status of the service:

sudo systemctl status payment-vending

Step 6: Test the Entire System

Simulate a Payment

Use the Stripe CLI to simulate a successful payment:

stripe trigger checkout.session.completed

Verify Dispensing

Confirm that the Raspberry Pi sends a signal to the PLC and the dispensing mechanism is triggered.


Troubleshooting

Common Issues

  1. Webhook Server Not Running:

    • Check the status of the systemd service:
      sudo systemctl status payment-vending
    • Restart the service if necessary:
      sudo systemctl restart payment-vending
  2. PLC Communication Issues:

    • Verify the physical connection between the Raspberry Pi and the PLC.
    • Check the protocol configuration in the sendSignalToPLC function.
  3. Stripe Webhook Signature Error:

    • Ensure the STRIPE_WEBHOOK_SECRET in the .env file matches your Stripe settings.

Contributing

Feel free to contribute to this project by submitting issues or pull requests. Follow the standard GitHub workflow for contributions.


License

This project is licensed under the MIT License. See the LICENSE file for more information.


This guide ensures a smooth setup process for the Raspberry Pi and payment gateway integration. Let me know if you'd like to refine or customize it further!

Webhook Handler for Stripe Payments

This project implements a webhook handler using Node.js to process Stripe payment events. It listens for Stripe webhook events, validates them, and processes successful payment events to send signals to a PLC (Programmable Logic Controller).

Features

  • Listens for Stripe webhook events on a specified endpoint.
  • Validates incoming webhook events using Stripe's signature.
  • Processes checkout.session.completed events to handle successful payments.
  • Sends a signal to a PLC with the payment amount.

Prerequisites

  1. Node.js: Ensure you have Node.js installed. You can download it from Node.js official website.
  2. Stripe Account: You need a Stripe account to obtain your API keys and webhook secret.

Setup Instructions

  1. Clone or download this repository.
  2. Navigate to the project directory:
  3. Install the required dependencies:
  4. Replace the placeholders in the script with your actual Stripe keys:
    • your_stripe_secret_key: Your Stripe secret key.
    • your_webhook_secret: Your Stripe webhook secret.

Running the Script

  1. Start the server:
  2. The server will start listening on port 4242.

Testing the Webhook

  1. Use the Stripe CLI to forward events to your local server:
  2. The server will start listening on port 4242.

Testing the Webhook

  1. Use the Stripe CLI to forward events to your local server:
  2. Trigger a test event using the Stripe CLI:

How It Works

  1. The script listens for POST requests on the /webhook endpoint.
  2. It validates the incoming request using the stripe-signature header and the webhook secret.
  3. If the event type is checkout.session.completed, it extracts the payment amount and sends a signal to the PLC using the sendSignalToPLC function.

Code Overview

  • Dependencies:
    • express: For creating the server and handling HTTP requests.
    • body-parser: For parsing raw JSON payloads.
    • stripe: For validating and processing Stripe webhook events.
  • Key Functions:
    • sendSignalToPLC(tokens): Sends a signal to the PLC with the payment amount.

Notes

  • Ensure your firewall or antivirus software does not block port 4242.
  • This script assumes the payment amount is in cents and converts it to dollars before sending it to the PLC.

License

This project is licensed under the MIT License.

About

Accepts QRCode Payment for Vending Machine

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors