GithubHelp home page GithubHelp logo

abraxas-365 / user_oauth_stripe_skeleton Goto Github PK

View Code? Open in Web Editor NEW
2.0 1.0 0.0 139 KB

UserOAuthStripeSkeleton is a basic template that provides a foundational setup for implementing OAuth authentication and Stripe integration in web applications. This skeleton is ideal for developers looking to streamline the development of projects requiring user authentication and payment processing capabilities.

Dockerfile 1.29% Rust 98.71%
outh2 rust stipe-payment stripe

user_oauth_stripe_skeleton's Introduction

Rust Skeleton Service for OAuth2 Authentication and Stripe Payments

Overview

This Rust-based service is designed to handle user authentication using OAuth2 and manage payment processes through Stripe. It provides essential endpoints for user sign-up and login using OAuth2, as well as endpoints for creating and updating payment statuses via Stripe.

Features

  • OAuth2 Authentication: Integration with Google's OAuth2 for user authentication.
  • User Management: Database management for storing user information securely.
  • Stripe Payment Integration: APIs to create payments and update their status.

Pre-requisites

Ensure you have Rust and Cargo installed on your machine. You can follow the installation guide here: Rust Programming Language. Additionally, you'll need:

  • PostgreSQL server
  • Access to a Stripe account for API keys
  • Access to Google Cloud for OAuth2 credentials

Configuration Before starting the service, configure the environmental variables by creating a .env file in the root directory with the following variables:

  • GOOGLE_CLIENT_ID: Your Google app client ID

  • GOOGLE_CLIENT_SECRET: Your Google app client secret

  • GOOGLE_REDIRECT_URI: The redirect URI set in your Google app

  • DATABASE_URL: Your PostgreSQL database URL

  • STRIPE_SECRET: 1234

  • STRIPE_CHECKOUT_CANCEL_URL: https://1234.com

  • STRIPE_CHECKOUT_SUCCESS_URL: https://1234.com

  • STRIPE_WEBHOOK_SECRET: 1234

  • JWT_SECRET: your-secret-key

Database Setup

This service uses PostgreSQL. Run the SQL commands below to set up the required tables:

-- Create the 'users' table
-- If you need to add more information you should create another table
-- This is just the core for the skeleton to work
CREATE TABLE users (
    id SERIAL PRIMARY KEY,
    email VARCHAR(255) UNIQUE NOT NULL,
    name VARCHAR(255) NOT NULL,
    image_url TEXT,
    oauth_provider VARCHAR(255) NOT NULL,
    oauth_id VARCHAR(255) NOT NULL,
    stripe_customer_id VARCHAR(255),
    oauth_refresh_token TEXT NOT NULL,
    created_at TIMESTAMPTZ DEFAULT (NOW() AT TIME ZONE 'utc')
);


-- Creating a junction table for users and stripe products
CREATE TABLE user_subscription (
    user_id INTEGER NOT NULL,
    stripe_product_id VARCHAR(255) NOT NULL,
    stripe_payment_id VARCHAR(255) NOT NULL,
    subscription_date TIMESTAMPTZ DEFAULT (NOW() AT TIME ZONE 'utc'),
    is_active BOOLEAN DEFAULT TRUE,
    PRIMARY KEY (user_id, stripe_product_id),
    FOREIGN KEY (user_id) REFERENCES users(id),
    FOREIGN KEY (stripe_product_id) REFERENCES products(stripe_product_id) -- Assumes a table 'products' exists
);

-- Define enum type for payment status
CREATE TYPE payment_status AS ENUM ('pending', 'successful', 'failed', 'denied');

CREATE TABLE payments (
    stripe_payment_id VARCHAR(255) NOT NULL PRIMARY KEY,
    user_id INTEGER NOT NULL,
    stripe_product_id VARCHAR(255) NOT NULL,
    payment_date TIMESTAMPTZ DEFAULT (NOW() AT TIME ZONE 'utc'),
    payment_status payment_status DEFAULT 'pending', -- using the enum type for payment status
    FOREIGN KEY (user_id) REFERENCES users(id)
);

Running the Service

To run the service, perform the following commands in the terminal:

cargo build --release
cargo run

or whith Docker

docker compose build
docker compose up

API Endpoints

Authentication

  • GET /auth/redirect: Redirects to Google OAuth.
  • GET /auth/callback: Callback endpoint for Google OAuth.

Stripe

  • POST /stripe/checkout: Endpoint to create a checkout.
  • GET /stripe/products: Get stripe products
  • POST /stripe/webhook: The weebhook stripe uses

Subscription

  • GET /subscription/{user-id}: Get the subscription of a user

User

  • GET /user: Get User information

cURL Requests

Below are the cURL commands to interact with the API endpoints defined in the application. These serve as examples for testing or initial integration checks.

OAuth2 Authentication

Redirect to OAuth Provider:

# This endpoint is typically accessed directly via a browser to handle redirects properly.
curl -X GET "http://localhost:80/auth/redirect"

Stripe Payments

Create Chekout:

curl -X POST "http://localhost:80/stripe/checkout?product_id={product_id}" \
     -H "Content-Type: application/json" \
     -H "Authorization: Bearer <token>"

Get Products:

 curl -X GET http://localhost:80/stripe/products \
     -H "Content-Type: application/json"

Get User Subscription:

curl -X GET http://localhost:80/subscription/{user_id} \
     -H "Content-Type: application/json"

Get User

curl -X GET http://localhost:80/user \
     -H "Content-Type: application/json" \
     -H "Authorization: Bearer <token>"

Middleware

The service is equipped with JWT validation middleware for secure API calls.

Conclusion

This service is a basic skeleton for integrating OAuth2 and Stripe payments in a single Rust application. It can be expanded with further OAuth providers and more complex payment handling mechanisms as needed.

user_oauth_stripe_skeleton's People

Contributors

abraxas-365 avatar

Stargazers

Judd Misael R. Baguio avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.