mcpField
P
payment-gateway-integration

payment-gateway-integration

github.com/secondsky开发240463 次安装5 周前更新
代码仓库

关于 payment-gateway-integration

面向开发者的支付集成技能,提供使用 Stripe 或 PayPal 实现支付流程的参考。内容涵盖支付创建、Webhook、安全检查和失败处理。

主要功能

  1. 01Stripe Payment Intent 示例
  2. 02Webhook 签名验证
  3. 03PayPal 订单与退款说明
  4. 04支付安全检查清单
  5. 05错误处理与重试

适用场景

  1. 01为应用添加支付处理
  2. 02处理支付服务商的 Webhook
  3. 03检查支付代码的安全做法

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/secondsky/claude-skills --skill payment-gateway-integration

安装后在 Claude Code 中输入/payment-gateway-integration确认已加载。

附带文件references/paypal-integration.md

详细介绍

支付网关集成

集成安全的支付处理,并妥善处理错误和合规要求。

Stripe 集成(Node.js)

const stripe = require('stripe')(process.env.STRIPE_SECRET_KEY);

class PaymentService {
  async createPaymentIntent(amount, currency, customerId) {
    return stripe.paymentIntents.create({
      amount: Math.round(amount * 100), // Convert to cents
      currency,
      customer: customerId,
      automatic_payment_methods: { enabled: true }
    });
  }

  async createSubscription(customerId, priceId) {
    return stripe.subscriptions.create({
      customer: customerId,
      items: [{ price: priceId }],
      payment_behavior: 'default_incomplete',
      expand: ['latest_invoice.payment_intent']
    });
  }

  async refund(paymentIntentId, amount = null) {
    const params = { payment_intent: paymentIntentId };
    if (amount) params.amount = Math.round(amount * 100);
    return stripe.refunds.create(params);
  }
}

Webhook 处理

app.post('/webhooks/stripe', express.raw({ type: 'application/json' }), (req, res) => {
  const sig = req.headers['stripe-signature'];

  let event;
  try {
    event = stripe.webhooks.constructEvent(req.body, sig, process.env.STRIPE_WEBHOOK_SECRET);
  } catch (err) {
    return res.status(400).send(`Webhook Error: ${err.message}`);
  }

  switch (event.type) {
    case 'payment_intent.succeeded':
      await handlePaymentSuccess(event.data.object);
      break;
    case 'invoice.payment_failed':
      await handlePaymentFailed(event.data.object);
      break;
  }

  res.json({ received: true });
});

PayPal 集成

完整的 PayPal 实现请参阅 references/paypal-integration.md,包括:

  • 创建和捕获订单
  • 退款处理
  • Webhook 处理
  • 前端 SDK 集成
  • 成功/取消回调

安全检查清单

  • 仅使用官方 SDK
  • 验证 Webhook 签名
  • 切勿记录完整卡号
  • 仅存储最少的支付数据
  • 先在沙盒环境中测试
  • 所有支付路由均使用 HTTPS
  • 处理所有错误情况
  • 使用幂等键
  • 实现重试逻辑

最佳实践

应做:

  • 使用官方 SDK 库
  • 验证所有 Webhook 签名
  • 记录交易 ID(而非卡片数据)
  • 在沙盒环境中测试
  • 处理所有支付状态
  • 提供恰当的错误消息

不应做:

  • 直接处理原始卡片数据
  • 存储敏感支付信息
  • 将 API 密钥硬编码
  • 跳过 Webhook 签名验证
  • 忽略支付失败事件
  • 在生产环境中使用测试密钥

评论

支持 Markdown