Fastapi Alipay Pay Skill logo

Fastapi Alipay Pay Skill

Community
jiushiwon
fastapi-alipay-pay-skill

FastAPI 支付宝模块技能(独立版)。面向已有 FastAPI 项目,提供支付宝完整能力:当面付/扫码、手机网站支付(WAP)、PC 网站支付、App 支付、退款、回调验签、订单查询、关单。**不包含微信支付**,如需微信请引入 fastapi-wechat-pay-skill。触发词:"支付宝"、"当面付"、"扫码支付"、"手机网站支付"、"WAP支付"、"PC支付"、"APP支付"、"支付宝退款"、"支付宝回调"、"alipay"、"RSA2验签"。

Overview

Publisherjiushiwon
Repositorywg-skills
Skill namefastapi-alipay-pay-skill
Stars
123
Forks
38
Bundled files
3
LicenseApache-2.0
Links
  • Markdown instructions

    A SKILL.md file the model loads on demand, so it only costs tokens when a request actually matches.

  • Works with any LLM

    AI skills are plain Markdown, not provider-specific code, so this works with GPT, Claude, Gemini, Grok, or a local model.

  • 3 bundled files

    Scripts, templates, and references the model can read while it works. Files are read-only and never executed.

  • Open source

    Published by jiushiwon on GitHub. Read the source before you install it.

Installation

Install the Fastapi Alipay Pay Skill AI skill in TypingMind to use it with any LLM, or drop it into another agent that reads SKILL.md.

1

Install in TypingMind

TypingMind installs a skill straight from its GitHub folder — it reads SKILL.md, bundles the resource files, and stores the result locally.

  1. Open the app and go to Plugins → Skills.
  2. Choose "Install from GitHub".
  3. Paste the skill folder URL below and confirm.
    https://github.com/jiushiwon/wg-skills/tree/main/vibeCoding/backend/python/fastapi-module/fastapi-alipay-pay-skill
  4. Enable the skill in any chat where you want it available.
Plugins → Skills → Add skill → From GitHub URL, then paste the folder URL and press Continue.
2

Install in another agent

Any agent that reads the Agent Skills format can use this skill — copy the folder into that agent's skills directory.

Claude Code — .claude/skills
git clone --depth 1 https://github.com/jiushiwon/wg-skills.git /tmp/wg-skills
mkdir -p .claude/skills
cp -r /tmp/wg-skills/vibeCoding/backend/python/fastapi-module/fastapi-alipay-pay-skill .claude/skills/fastapi-alipay-pay-skill
Restart Claude Code after copying so it picks up the new skill.

Use it in TypingMind

Enable Fastapi Alipay Pay Skill in any TypingMind chat and the model takes it from there. Its name and description sit in the system prompt, and the moment a request matches, the model loads the full instructions itself — you never invoke it by hand, and it costs no tokens until it is actually used.

The model loads Fastapi Alipay Pay Skill on its own as soon as a request matches it.

Works with any AI model

AI skills are plain Markdown instructions rather than provider-specific code, so Fastapi Alipay Pay Skill is not tied to the model it was written for. Install it once in TypingMind and use it with GPT-5, Claude, Gemini, Grok, DeepSeek, Mistral, Llama, or a local model you run yourself — all on your own API keys.

  • Loaded only when it is needed

    The system prompt carries just the name and description. The instructions are fetched on the first matching request, so an idle skill costs nothing.

  • Switch models mid-chat

    Because the skill is instructions rather than code, changing model does not break it — the next model reads the same SKILL.md.

Skill instructions

This is the SKILL.md content the model loads. Read it before installing — a skill is instructions your model will follow.

FastAPI Alipay Module Skill

支付宝独立模块,只覆盖支付宝渠道。Python FastAPI 实现。

与合一版的关系

Skill范围推荐度
fastapi-alipay-pay-skill(本)仅支付宝⭐⭐⭐⭐⭐ 单一渠道
fastapi-wechat-pay-skill仅微信 V3⭐⭐⭐⭐⭐ 单一渠道
fastapi-payment-module-skill微信+支付宝合一旧版,已不推荐

能力清单

能力适用场景接口
当面付线下扫码 / 收银台POST /api/alipay/precreate
手机网站支付移动端浏览器 H5POST /api/alipay/wap
PC 网站支付PC 浏览器POST /api/alipay/page
App 支付iOS / Android AppPOST /api/alipay/app
退款全场景通用POST /api/alipay/refund
查询订单全场景通用GET /api/alipay/query/{order_no}
关单全场景通用POST /api/alipay/close/{order_no}
对账单下载财务对账GET /api/alipay/bill
回调验签全场景通用POST /api/alipay/notify

前置依赖

依赖说明
fastapi-init-skill项目骨架(必须)
fastapi-auth-module-skill当前用户获取(推荐)
python-redis-module-skill幂等防重 + 限流(推荐)

核心实现

1. 依赖

bash
# 支付宝官方 Python SDK
pip install python-alipay-sdk

# 或新版 pycryptodome(签名需要)
pip install pycryptodome

2. 配置(config.py)

python
from pydantic_settings import BaseSettings

class Settings(BaseSettings):
    # ====== 必填项:从开放平台获取 ======
    alipay_app_id: str = "2021000000000000"           # 16 位 APPID
    alipay_private_key: str = ""                       # 应用私钥(PEM PKCS8,含 BEGIN/END 头)
    alipay_public_key: str = ""                        # 支付宝公钥(**不是应用公钥**)

    # ====== 业务地址 ======
    alipay_gateway: str = "https://openapi.alipay.com/gateway.do"
    alipay_sandbox_gateway: str = "https://openapi.alipaydev.com/gateway.do"
    alipay_notify_url: str = "https://yourdomain.com/api/alipay/notify"
    alipay_return_url: str = "https://yourdomain.com/pay/return"

    # ====== 可选 ======
    alipay_sign_type: str = "RSA2"                     # 固定 RSA2
    alipay_charset: str = "UTF-8"
    alipay_format: str = "json"
    alipay_debug: bool = False

    class Config:
        env_file = ".env"

settings = Settings()

3. 数据模型(models.py)

python
import enum
from sqlalchemy import Column, BigInteger, String, DateTime, Enum as SQLEnum, Numeric, Text
from sqlalchemy.sql import func
from database import Base


class PayScene(str, enum.Enum):
    PRECREATE = "PRECREATE"   # 当面付
    WAP = "WAP"               # 手机网站
    PAGE = "PAGE"             # PC 网站
    APP = "APP"               # App 支付


class PayStatus(str, enum.Enum):
    PENDING = "PENDING"
    PAID = "PAID"
    CLOSED = "CLOSED"
    REFUNDED = "REFUNDED"


class RefundStatus(str, enum.Enum):
    PENDING = "PENDING"
    SUCCESS = "SUCCESS"
    FAILED = "FAILED"


class AlipayPayOrder(Base):
    __tablename__ = "wg_alipay_pay_order"
    id = Column(BigInteger, primary_key=True, autoincrement=True)
    order_no = Column(String(64), unique=True, nullable=False, index=True)
    trade_no = Column(String(64), comment="支付宝交易号")
    scene = Column(SQLEnum(PayScene))
    status = Column(SQLEnum(PayStatus), default=PayStatus.PENDING)
    # 金额单位:元(BigDecimal 对应 Python Decimal / Numeric)
    amount = Column(Numeric(10, 2), nullable=False)
    subject = Column(String(200))
    qr_code = Column(Text, comment="当面付二维码内容")
    paid_at = Column(DateTime)
    created_at = Column(DateTime, server_default=func.now())
    updated_at = Column(DateTime, server_default=func.now(), onupdate=func.now())


class AlipayRefundOrder(Base):
    __tablename__ = "wg_alipay_refund_order"
    id = Column(BigInteger, primary_key=True, autoincrement=True)
    refund_no = Column(String(64), unique=True, nullable=False)
    order_no = Column(String(64), nullable=False, index=True)
    refund_id = Column(String(64), comment="支付宝退款单号")
    refund_amount = Column(Numeric(10, 2), nullable=False)
    total_amount = Column(Numeric(10, 2), nullable=False)
    status = Column(SQLEnum(RefundStatus), default=RefundStatus.PENDING)
    reason = Column(String(200))
    refunded_at = Column(DateTime)
    created_at = Column(DateTime, server_default=func.now())

4. 服务层(核心)

python
# services/alipay_pay_service.py
from decimal import Decimal
from datetime import datetime
from typing import Optional

from sqlalchemy.orm import Session
from alipay import AliPay

from config import settings
from models import AlipayPayOrder, AlipayRefundOrder, PayStatus, RefundStatus, PayScene
from common.exceptions import BusinessException


class AlipayPayService:
    def __init__(self, db: Session):
        self.db = db
        self._client: Optional[AliPay] = None

    def _get_client(self) -> AliPay:
        if self._client is None:
            self._client = AliPay(
                appid=settings.alipay_app_id,
                app_notify_url=settings.alipay_notify_url,
                app_private_key_string=settings.alipay_private_key,
                alipay_public_key_string=settings.alipay_public_key,
                sign_type=settings.alipay_sign_type,  # RSA2
                debug=settings.alipay_debug,
            )
        return self._client

    # ============== 统一下单 ==============

    def precreate(self, order_no: str, amount: Decimal, subject: str) -> str:
        """当面付(生成二维码)"""
        if self.db.query(AlipayPayOrder).filter(AlipayPayOrder.order_no == order_no).first():
            raise BusinessException(-3004, "订单号已存在")

        try:
            qr_code = self._get_client().api_alipay_trade_precreate(
                out_trade_no=order_no,
                total_amount=str(amount),
                subject=subject,
                notify_url=settings.alipay_notify_url,
            )
        except Exception as e:
            raise BusinessException(-3001, f"当面付预创建失败: {e}")

        order = AlipayPayOrder(
            order_no=order_no, scene=PayScene.PRECREATE,
            amount=amount, subject=subject,
            qr_code=qr_code, status=PayStatus.PENDING,
        )
        self.db.add(order)
        self.db.commit()
        return qr_code

    def wap_pay(self, order_no: str, amount: Decimal, subject: str) -> str:
        """手机网站支付(返回 form HTML)"""
        try:
            form_html = self._get_client().api_alipay_trade_wap_pay(
                out_trade_no=order_no,
                total_amount=str(amount),
                subject=subject,
                return_url=settings.alipay_return_url,
                notify_url=settings.alipay_notify_url,
                product_code="QUICK_WAP_WAY",
            )
        except Exception as e:
            raise BusinessException(-3001, f"WAP 支付失败: {e}")

        self._persist(order_no, PayScene.WAP, amount, subject)
        return form_html

    def page_pay(self, order_no: str, amount: Decimal, subject: str) -> str:
        """PC 网站支付(返回 form HTML,浏览器自动跳转)"""
        try:
            form_html = self._get_client().api_alipay_trade_page_pay(
                out_trade_no=order_no,
                total_amount=str(amount),
                subject=subject,
                return_url=settings.alipay_return_url,
                notify_url=settings.alipay_notify_url,
            )
        except Exception as e:
            raise BusinessException(-3001, f"PC 支付失败: {e}")

        self._persist(order_no, PayScene.PAGE, amount, subject)
        return form_html

    def app_pay(self, order_no: str, amount: Decimal, subject: str) -> str:
        """App 支付(返回 orderStr,App 端用 SDK 调起)"""
        try:
            order_str = self._get_client().api_alipay_trade_app_pay(
                out_trade_no=order_no,
                total_amount=str(amount),
                subject=subject,
                notify_url=settings.alipay_notify_url,
                product_code="QUICK_MSECURITY_PAY",
            )
        except Exception as e:
            raise BusinessException(-3001, f"App 支付失败: {e}")

        self._persist(order_no, PayScene.APP, amount, subject)
        return order_str

    def _persist(self, order_no: str, scene: PayScene, amount: Decimal, subject: str):
        if self.db.query(AlipayPayOrder).filter(AlipayPayOrder.order_no == order_no).first():
            raise BusinessException(-3004, "订单号已存在")
        order = AlipayPayOrder(
            order_no=order_no, scene=scene,
            amount=amount, subject=subject, status=PayStatus.PENDING,
        )
        self.db.add(order)
        self.db.commit()

    # ============== 退款 ==============

    def refund(self, order_no: str, refund_no: str, refund_amount: Decimal, reason: str = "") -> dict:
        order = self.db.query(AlipayPayOrder).filter(AlipayPayOrder.order_no == order_no).first()
        if not order:
            raise BusinessException(-3003, "原订单不存在")
        if order.status != PayStatus.PAID:
            raise BusinessException(-3005, "订单未支付")
        if refund_amount > order.amount:
            raise BusinessException(-3005, "退款金额超限")

        ro = AlipayRefundOrder(
            refund_no=refund_no, order_no=order_no,
            refund_amount=refund_amount, total_amount=order.amount,
            reason=reason, status=RefundStatus.PENDING,
        )
        self.db.add(ro)
        self.db.commit()

        try:
            result = self._get_client().api_alipay_trade_refund(
                out_trade_no=order_no,
                refund_amount=str(refund_amount),
                out_request_no=refund_no,
                refund_reason=reason,
            )
            if result.get("code") == "10000":
                return {"refundNo": refund_no, "status": "PROCESSING"}
            ro.status = RefundStatus.FAILED
            self.db.commit()
            raise BusinessException(-3005, f"退款失败: {result.get('sub_msg')}")
        except BusinessException:
            raise
        except Exception as e:
            ro.status = RefundStatus.FAILED
            self.db.commit()
            raise BusinessException(-3005, f"退款异常: {e}")

    # ============== 查询 / 关单 ==============

    def query_order(self, order_no: str) -> AlipayPayOrder:
        order = self.db.query(AlipayPayOrder).filter(AlipayPayOrder.order_no == order_no).first()
        if not order:
            raise BusinessException(-3003, "订单不存在")
        return order

    def close_order(self, order_no: str) -> None:
        order = self.db.query(AlipayPayOrder).filter(AlipayPayOrder.order_no == order_no).first()
        if not order:
            raise BusinessException(-3003, "订单不存在")

        try:
            result = self._get_client().api_alipay_trade_close(
                out_trade_no=order_no,
            )
            if result.get("code") == "10000":
                order.status = PayStatus.CLOSED
                self.db.commit()
            else:
                raise BusinessException(-3006, f"关单失败: {result.get('sub_msg')}")
        except BusinessException:
            raise
        except Exception as e:
            raise BusinessException(-3006, f"关单异常: {e}")

5. 路由

python
# routers/alipay_pay.py
from decimal import Decimal
from fastapi import APIRouter, Depends, Request, Form
from sqlalchemy.orm import Session

from database import get_db
from services.alipay_pay_service import AlipayPayService
from common.response import ApiResponse

router = APIRouter(prefix="/api/alipay", tags=["支付宝"])


@router.post("/precreate")
def precreate(order_no: str, amount: Decimal, subject: str, db: Session = Depends(get_db)):
    qr_code = AlipayPayService(db).precreate(order_no, amount, subject)
    return ApiResponse.ok({"qrCode": qr_code})


@router.post("/wap")
def wap(order_no: str, amount: Decimal, subject: str, db: Session = Depends(get_db)):
    html = AlipayPayService(db).wap_pay(order_no, amount, subject)
    return ApiResponse.ok({"formHtml": html})


@router.post("/page")
def page(order_no: str, amount: Decimal, subject: str, db: Session = Depends(get_db)):
    html = AlipayPayService(db).page_pay(order_no, amount, subject)
    return ApiResponse.ok({"formHtml": html})


@router.post("/app")
def app(order_no: str, amount: Decimal, subject: str, db: Session = Depends(get_db)):
    order_str = AlipayPayService(db).app_pay(order_no, amount, subject)
    return ApiResponse.ok({"orderStr": order_str})


@router.post("/refund")
def refund(order_no: str, refund_no: str, refund_amount: Decimal, reason: str = "", db: Session = Depends(get_db)):
    return ApiResponse.ok(AlipayPayService(db).refund(order_no, refund_no, refund_amount, reason))


@router.get("/query/{order_no}")
def query(order_no: str, db: Session = Depends(get_db)):
    return ApiResponse.ok(AlipayPayService(db).query_order(order_no))


@router.post("/close/{order_no}")
def close(order_no: str, db: Session = Depends(get_db)):
    AlipayPayService(db).close_order(order_no)
    return ApiResponse.ok()


@router.post("/notify")
async def notify(request: Request, db: Session = Depends(get_db)):
    """支付宝回调(form 表单 POST)"""
    from config import settings
    form = await request.form()
    data = dict(form)

    # 验签(SDK 内部完成)
    from alipay import AliPay
    alipay = AliPay(
        appid=settings.alipay_app_id,
        app_notify_url=None,
        app_private_key_string=settings.alipay_private_key,
        alipay_public_key_string=settings.alipay_public_key,
        sign_type=settings.alipay_sign_type,
    )

    if not alipay.verify(data, data.pop("sign")):
        # 验签失败返回 fail
        from fastapi.responses import PlainTextResponse
        return PlainTextResponse("fail", status_code=400)

    order_no = data.get("out_trade_no")
    trade_no = data.get("trade_no")
    trade_status = data.get("trade_status")

    order = db.query(AlipayPayOrder).filter(AlipayPayOrder.order_no == order_no).first()
    if order:
        if trade_status in ("TRADE_SUCCESS", "TRADE_FINISHED"):
            order.status = PayStatus.PAID
            order.trade_no = trade_no
            order.paid_at = datetime.now()
        elif trade_status == "TRADE_CLOSED":
            order.status = PayStatus.CLOSED
        db.commit()

    # 必须返回纯文本 "success"(小写无空格)
    from fastapi.responses import PlainTextResponse
    return PlainTextResponse("success")

关键踩坑

坑现象解决
验签失败回调报 INVALID_SIGN用的是应用公钥,换成支付宝公钥
App 支付无反应报 INVALID_PARAMETERproduct_code 必须填 QUICK_MSECURITY_PAY
私钥格式错ValueError: Could not deserialize必须 PKCS8(PEM 头是 -----BEGIN PRIVATE KEY-----)
当面付返回的 code_url 不到账没有扫码买家账号必须用沙箱 App登录
沙箱 404网关用错沙箱用 openapi.alipaydev.com
Decimal 精度0.1 + 0.2 = 0.30000000000004用 Decimal(str(amount)) 不用 float

不做

  • ❌ 商户号开通(参考 merchant-onboarding.md)
  • ❌ 密钥生成(参考同上)
  • ❌ UI 组件
  • ❌ 微信支付(独立 skill)

触发场景

  • 「帮我加支付宝(Python 版)」
  • 「FastAPI 集成当面付 / WAP / App 支付」
  • 「做支付宝退款」
  • 「配置支付宝 RSA2 签名」

Bundled files

The model reads these on demand while the skill is loaded. They are exposed as readable files and are never executed.

Frequently asked questions

What does the Fastapi Alipay Pay Skill AI skill do?

FastAPI 支付宝模块技能(独立版)。面向已有 FastAPI 项目,提供支付宝完整能力:当面付/扫码、手机网站支付(WAP)、PC 网站支付、App 支付、退款、回调验签、订单查询、关单。**不包含微信支付**,如需微信请引入 fastapi-wechat-pay-skill。触发词:"支付宝"、"当面付"、"扫码支付"、"手机网站支付"、"WAP支付"、"PC支付"、"APP支付"、"支付宝退款"、"支付宝回调"、"alipay"、"RSA2验签"。

Why use Fastapi Alipay Pay Skill on TypingMind?

Because you install it once and use it with any model. Fastapi Alipay Pay Skill is plain Markdown rather than provider-specific code, so the same skill runs on GPT-5, Claude, Gemini, Grok, or a local model — and you can switch model mid-chat without it breaking. TypingMind runs on your own API keys, so you pay providers directly instead of a per-seat subscription, and your skills and chats stay in your own storage.

How do I install Fastapi Alipay Pay Skill in TypingMind?

Open Plugins → Skills → Install from GitHub in TypingMind and paste https://github.com/jiushiwon/wg-skills/tree/main/vibeCoding/backend/python/fastapi-module/fastapi-alipay-pay-skill. TypingMind reads its SKILL.md and bundles its files and installs it as a skill you can enable per chat.

Which AI models can use Fastapi Alipay Pay Skill?

Any model you connect in TypingMind. AI skills are plain Markdown instructions rather than provider-specific code, so GPT, Claude, Gemini, Grok, and local models can all load this skill when a request matches it.

How many AI models can I use with Fastapi Alipay Pay Skill?

As many as you like. As long as a model supports skills, you can use Fastapi Alipay Pay Skill with it — GPT, Claude, Gemini, Grok, DeepSeek, Mistral, Llama and more — all on TypingMind with your own API keys.

Is the Fastapi Alipay Pay Skill AI skill free?

Yes. It is published on GitHub by jiushiwon under the Apache-2.0 license. You only pay your own AI provider for the tokens you use.

What are AI skills?

An AI skill is a reusable instruction bundle that teaches an AI model how to do one specific task. It follows the open Agent Skills format: a SKILL.md file with a name and description, plus any scripts, templates or reference files the model may need. The model reads the instructions only when your request matches the skill, so an installed skill costs nothing until it is used.

How are AI skills different from plugins or MCP servers?

A plugin or MCP server gives a model new tools to call — code that runs somewhere and returns a result. An AI skill gives the model knowledge and process instead: how to approach a task, which steps to follow, what good output looks like. Skills are plain Markdown, so they need no server, no API key and no runtime, and they work with any model.

View all

Set up your own AI workspace now

Get notified about new features and future giveaways by subscribing to our newsletter 👇